Skip to content

Commit 5550079

Browse files
committed
Using a more descriptive error message when using VERY long wait conditions or poll durations (issue 290)
1 parent 2a9814b commit 5550079

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

awaitility/src/main/java/org/awaitility/core/DurationFactory.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ public static Duration of(long amount, TimeUnit timeUnit) {
4949
default:
5050
throw new IllegalStateException("Cannot convert " + TimeUnit.class.getSimpleName() + " to a " + ChronoUnit.class.getSimpleName());
5151
}
52-
return Duration.of(amount, chronoUnit);
52+
53+
Duration duration = Duration.of(amount, chronoUnit);
54+
checkThatDurationCanBeConvertedToNanos(duration, amount, timeUnit);
55+
return duration;
56+
}
57+
58+
@SuppressWarnings("ResultOfMethodCallIgnored")
59+
private static void checkThatDurationCanBeConvertedToNanos(Duration timeout, long amount, TimeUnit timeUnit) {
60+
try {
61+
timeout.toNanos();
62+
} catch (ArithmeticException e) {
63+
throw new IllegalArgumentException(String.format("Cannot convert %s %s to nanoseconds, as required by Awaitility, because this value is too large", amount, timeUnit), e);
64+
}
5365
}
5466
}

awaitility/src/test/java/org/awaitility/AwaitilityTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,15 @@ public void awaitDuringTimeOnConditionMessingAtLeastAtMost() throws Exception {
503503
assertThat(duration.toMillis(), greaterThan(1000L));
504504
}
505505

506+
@Test
507+
public void throwsIAEWhenTimeoutIsTooLargeForTheUnit() {
508+
int timeoutMinutes = Integer.MAX_VALUE;
509+
exception.expect(IllegalArgumentException.class);
510+
exception.expectMessage("Cannot convert " + timeoutMinutes + " MINUTES to nanoseconds, as required by Awaitility, because this value is too large");
511+
512+
Awaitility.await().atMost(timeoutMinutes, TimeUnit.MINUTES).until(() -> true);
513+
}
514+
506515
private Callable<Boolean> fakeRepositoryValueEqualsOne() {
507516
return new FakeRepositoryEqualsOne(fakeRepository);
508517
}

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Changelog next version
22
----------------------
33
* Support for kotlin.time.Duration in Kotlin DSL (thanks to Ivo Šmíd for PR)
44
* Upgraded kotlin version in the awaitility-kotlin module to 2.1.10
5+
* Using a more descriptive error message when using VERY long wait conditions or poll durations (issue 290)
56

67
Changelog 4.2.2 (2024-08-07)
78
----------------------------

0 commit comments

Comments
 (0)