From 0adb7719529a222a665eff33159e2b9920f78e99 Mon Sep 17 00:00:00 2001 From: mrizzi Date: Mon, 28 Sep 2020 15:35:06 +0200 Subject: [PATCH 1/4] WINDUPRULE-665 Implemented multiple attempts approach --- .../rules/tests/WindupRulesLinksTest.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/jboss/windup/rules/tests/WindupRulesLinksTest.java b/src/test/java/org/jboss/windup/rules/tests/WindupRulesLinksTest.java index ee4031f21..809a6907a 100644 --- a/src/test/java/org/jboss/windup/rules/tests/WindupRulesLinksTest.java +++ b/src/test/java/org/jboss/windup/rules/tests/WindupRulesLinksTest.java @@ -50,6 +50,8 @@ public class WindupRulesLinksTest { private static final Logger LOG = LoggerFactory.getLogger(WindupRulesLinksTest.class); private static final String RUN_TEST_MATCHING = "runTestsMatching"; + private static final String RETRY_ATTEMPTS = "retryAttempts"; + private static final int RETRY_ATTEMPTS_NUM = Integer.parseInt(System.getProperty(RETRY_ATTEMPTS, "3")); private static final List ACCEPTED_RESPONSE_CODE = Arrays.asList( HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_MOVED_PERM, @@ -152,8 +154,20 @@ private boolean isValidLink(final String link) final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); // property name from https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html urlConn.setConnectTimeout(Integer.getInteger("sun.net.client.defaultConnectTimeout", 5000)); - urlConn.connect(); - CACHE_ANALYZED_LINKS.put(link, urlConn.getResponseCode()); + int attempt = 0; + while (true) + { + try { + urlConn.connect(); + CACHE_ANALYZED_LINKS.put(link, urlConn.getResponseCode()); + break; + } catch (IOException e) { + LOG.warn(String.format("Attempt [%s/%s]: '%s' exception connecting to %s", ++attempt, RETRY_ATTEMPTS_NUM, e.getMessage(), link)); + if (attempt == RETRY_ATTEMPTS_NUM) throw e; + } finally { + urlConn.disconnect(); + } + } } final boolean validLink = ACCEPTED_RESPONSE_CODE.contains(CACHE_ANALYZED_LINKS.get(link)); if (validLink) LOG.debug(String.format("Response code %d for %s [%dms]", CACHE_ANALYZED_LINKS.get(link), link, System.currentTimeMillis() - starTime)); From dc34e6aa5b583f629742e82174e0d17a2a4583f0 Mon Sep 17 00:00:00 2001 From: mrizzi Date: Wed, 30 Sep 2020 18:28:05 +0200 Subject: [PATCH 2/4] WINDUPRULE-665 Adopting Awaitility --- pom.xml | 6 +++ .../rules/tests/WindupRulesLinksTest.java | 50 ++++++++++++------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index 6c28da2b3..5e7fbf0f7 100644 --- a/pom.xml +++ b/pom.xml @@ -196,6 +196,12 @@ 1.7.30 test + + org.awaitility + awaitility + 4.0.3 + test + diff --git a/src/test/java/org/jboss/windup/rules/tests/WindupRulesLinksTest.java b/src/test/java/org/jboss/windup/rules/tests/WindupRulesLinksTest.java index 809a6907a..0ba3a5d53 100644 --- a/src/test/java/org/jboss/windup/rules/tests/WindupRulesLinksTest.java +++ b/src/test/java/org/jboss/windup/rules/tests/WindupRulesLinksTest.java @@ -1,6 +1,7 @@ package org.jboss.windup.rules.tests; import org.apache.commons.lang3.StringUtils; +import org.awaitility.core.ConditionTimeoutException; import org.jboss.windup.util.file.FileSuffixPredicate; import org.jboss.windup.util.file.FileVisit; import org.junit.AfterClass; @@ -22,6 +23,8 @@ import java.net.HttpURLConnection; import java.net.URL; import java.nio.file.Path; +import java.time.Duration; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -33,6 +36,8 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; +import static org.awaitility.Awaitility.with; + /** * This tests all the href attribute in ALL the rules every time the tests are executed (PR and nightly builds) * Helpful to ensure we have always links that points to something so that links are really helpful for the user @@ -43,6 +48,12 @@ * To change the connection timeout use the standard "-Dsun.net.client.defaultConnectTimeout=" property * from https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html * + * The default poll interval for the same link in case of connection timeout is 200 milliseconds. + * To change it use the property "-DpollInterval=" with specified in milliseconds. + * + * The default poll duration for the same link in case of connection timeout is 2000 milliseconds. + * To change it use the property "-DpollAtMost=" with specified in milliseconds. + * * To quickly see the debug log, execute the test with the property "-Dorg.slf4j.simpleLogger.log.org.jboss.windup=debug" */ @RunWith(Parameterized.class) @@ -50,8 +61,10 @@ public class WindupRulesLinksTest { private static final Logger LOG = LoggerFactory.getLogger(WindupRulesLinksTest.class); private static final String RUN_TEST_MATCHING = "runTestsMatching"; - private static final String RETRY_ATTEMPTS = "retryAttempts"; - private static final int RETRY_ATTEMPTS_NUM = Integer.parseInt(System.getProperty(RETRY_ATTEMPTS, "3")); + private static final String LINK_POLL_INTERVAL = "pollInterval"; + private static final Duration LINK_POLL_INTERVAL_DURATION = Duration.of(Long.getLong(LINK_POLL_INTERVAL, 200L), ChronoUnit.MILLIS); + private static final String LINK_POLL_AT_MOST = "pollAtMost"; + private static final Duration LINK_POLL_AT_MOST_DURATION = Duration.of(Long.getLong(LINK_POLL_AT_MOST, 2000L), ChronoUnit.MILLIS); private static final List ACCEPTED_RESPONSE_CODE = Arrays.asList( HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_MOVED_PERM, @@ -154,26 +167,29 @@ private boolean isValidLink(final String link) final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); // property name from https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html urlConn.setConnectTimeout(Integer.getInteger("sun.net.client.defaultConnectTimeout", 5000)); - int attempt = 0; - while (true) - { - try { - urlConn.connect(); - CACHE_ANALYZED_LINKS.put(link, urlConn.getResponseCode()); - break; - } catch (IOException e) { - LOG.warn(String.format("Attempt [%s/%s]: '%s' exception connecting to %s", ++attempt, RETRY_ATTEMPTS_NUM, e.getMessage(), link)); - if (attempt == RETRY_ATTEMPTS_NUM) throw e; - } finally { - urlConn.disconnect(); - } - } + with() + .pollDelay(Duration.ZERO) + .pollInterval(LINK_POLL_INTERVAL_DURATION) + .await() + .atMost(LINK_POLL_AT_MOST_DURATION) + .until(() -> { + try { + urlConn.connect(); + return true; + } catch (IOException e) { + LOG.warn(String.format("'%s' exception connecting to %s", e.getMessage(), link)); + return false; + } finally { + urlConn.disconnect(); + } + }); + CACHE_ANALYZED_LINKS.put(link, urlConn.getResponseCode()); } final boolean validLink = ACCEPTED_RESPONSE_CODE.contains(CACHE_ANALYZED_LINKS.get(link)); if (validLink) LOG.debug(String.format("Response code %d for %s [%dms]", CACHE_ANALYZED_LINKS.get(link), link, System.currentTimeMillis() - starTime)); else LOG.error(String.format("Response code %d for %s [%dms]", CACHE_ANALYZED_LINKS.get(link), link, System.currentTimeMillis() - starTime)); return validLink; - } catch (IOException e) { + } catch (IOException | ConditionTimeoutException e) { LOG.error(String.format("'%s' exception connecting to %s", e.getMessage(), link)); return false; } From b46c49e56c569c286e6a9ad61f7b77fa5861b133 Mon Sep 17 00:00:00 2001 From: mrizzi Date: Thu, 1 Oct 2020 11:07:58 +0200 Subject: [PATCH 3/4] WINDUPRULE-665 Changed 'atMost' duration --- .../org/jboss/windup/rules/tests/WindupRulesLinksTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/jboss/windup/rules/tests/WindupRulesLinksTest.java b/src/test/java/org/jboss/windup/rules/tests/WindupRulesLinksTest.java index 0ba3a5d53..c9d7206d3 100644 --- a/src/test/java/org/jboss/windup/rules/tests/WindupRulesLinksTest.java +++ b/src/test/java/org/jboss/windup/rules/tests/WindupRulesLinksTest.java @@ -51,7 +51,7 @@ * The default poll interval for the same link in case of connection timeout is 200 milliseconds. * To change it use the property "-DpollInterval=" with specified in milliseconds. * - * The default poll duration for the same link in case of connection timeout is 2000 milliseconds. + * The default poll duration for the same link in case of connection timeout is 30000 milliseconds. * To change it use the property "-DpollAtMost=" with specified in milliseconds. * * To quickly see the debug log, execute the test with the property "-Dorg.slf4j.simpleLogger.log.org.jboss.windup=debug" @@ -64,7 +64,7 @@ public class WindupRulesLinksTest { private static final String LINK_POLL_INTERVAL = "pollInterval"; private static final Duration LINK_POLL_INTERVAL_DURATION = Duration.of(Long.getLong(LINK_POLL_INTERVAL, 200L), ChronoUnit.MILLIS); private static final String LINK_POLL_AT_MOST = "pollAtMost"; - private static final Duration LINK_POLL_AT_MOST_DURATION = Duration.of(Long.getLong(LINK_POLL_AT_MOST, 2000L), ChronoUnit.MILLIS); + private static final Duration LINK_POLL_AT_MOST_DURATION = Duration.of(Long.getLong(LINK_POLL_AT_MOST, 30000L), ChronoUnit.MILLIS); private static final List ACCEPTED_RESPONSE_CODE = Arrays.asList( HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_MOVED_PERM, From ca9953d0319a1471fde609f8cd1f36383b14bada Mon Sep 17 00:00:00 2001 From: mrizzi Date: Tue, 6 Oct 2020 09:15:43 +0200 Subject: [PATCH 4/4] WINDUPRULE-665 Restored WindupRulesLinksTest execution --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index 33edb692c..5e7fbf0f7 100644 --- a/pom.xml +++ b/pom.xml @@ -312,7 +312,6 @@ ${arguments} **/WindupRulesMultipleTests* - **/WindupRulesLinksTest*