From 067ebfb421d06a3f58f700439e151b2fd5d6e20f Mon Sep 17 00:00:00 2001 From: Richard North Date: Sat, 30 Apr 2016 16:01:40 +0100 Subject: [PATCH] Fix a bug whereby inconsistents utility would incorrectly convert any interval expressed in a non-milliseconds unit. --- pom.xml | 2 +- .../ducttape/inconsistents/Inconsistents.java | 2 +- .../ducttape/inconsistents/InconsistentsTest.java | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index f0b7944..20b0e19 100644 --- a/pom.xml +++ b/pom.xml @@ -55,7 +55,7 @@ org.rnorth.visible-assertions visible-assertions - 1.0.3 + 1.0.5 test diff --git a/src/main/java/org/rnorth/ducttape/inconsistents/Inconsistents.java b/src/main/java/org/rnorth/ducttape/inconsistents/Inconsistents.java index d733e41..c8695c7 100644 --- a/src/main/java/org/rnorth/ducttape/inconsistents/Inconsistents.java +++ b/src/main/java/org/rnorth/ducttape/inconsistents/Inconsistents.java @@ -38,7 +38,7 @@ public static T retryUntilConsistent(final int consistentTime, final int tot long[] bestRun = {0}; Object[] bestRunValue = {null}; - long consistentTimeInMillis = timeUnit.convert(consistentTime, TimeUnit.MILLISECONDS); + long consistentTimeInMillis = TimeUnit.MILLISECONDS.convert(consistentTime, timeUnit); return Unreliables.retryUntilSuccess(totalTimeout, timeUnit, () -> { T value = lambda.call(); diff --git a/src/test/java/org/rnorth/ducttape/inconsistents/InconsistentsTest.java b/src/test/java/org/rnorth/ducttape/inconsistents/InconsistentsTest.java index e9d6248..ec0c418 100644 --- a/src/test/java/org/rnorth/ducttape/inconsistents/InconsistentsTest.java +++ b/src/test/java/org/rnorth/ducttape/inconsistents/InconsistentsTest.java @@ -5,6 +5,7 @@ import java.util.concurrent.TimeUnit; +import static org.junit.Assert.assertTrue; import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; @@ -49,4 +50,18 @@ public void testNotConsistentLongEnough() { assertEquals("An exception is thrown if the result is never consistent", InconsistentResultsException.class, cause.getClass()); } } + + @Test + public void testUnitConversion() { + long start = System.currentTimeMillis(); + + Inconsistents.retryUntilConsistent(1, 5, TimeUnit.SECONDS, () -> { + Thread.sleep(10L); + // this result won't be consistent until after 1.5s + long now = System.currentTimeMillis(); + return (now - start) / 1500; + }); + + assertTrue("At least one second elapsed", System.currentTimeMillis() - start > 1000); + } } \ No newline at end of file