Skip to content

Commit 74bc15d

Browse files
committed
Extend and clean up ObjectSleeper test. (#559)
1 parent 8081fb5 commit 74bc15d

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

metafacture-flowcontrol/src/test/java/org/metafacture/flowcontrol/ObjectSleeperTest.java

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,28 @@
1616

1717
package org.metafacture.flowcontrol;
1818

19-
import static org.junit.Assert.assertTrue;
20-
2119
import org.metafacture.framework.ObjectReceiver;
2220

21+
import org.junit.Assert;
2322
import org.junit.Before;
2423
import org.junit.Test;
2524
import org.mockito.Mock;
2625
import org.mockito.MockitoAnnotations;
2726

28-
import java.time.Duration;
29-
import java.time.Instant;
27+
import java.util.function.Consumer;
3028

3129
/**
3230
* Tests for class {@link ObjectSleeper}.
33-
*
34-
* @author Tobias Bülte
35-
*
36-
*/
31+
*
32+
* @author Tobias Bülte
33+
*/
3734
public final class ObjectSleeperTest {
3835

36+
private static final int PROCESS_OVERHEAD_MILLISECONDS = 100;
37+
38+
private static final int MILLISECONDS_PER_SECOND = 1_000;
39+
private static final int NANOSECONDS_PER_MILLISECOND = 1_000_000;
40+
3941
@Mock
4042
private ObjectReceiver<String> receiver;
4143

@@ -46,20 +48,39 @@ public void setup() {
4648

4749
@Test
4850
public void shouldTestIfClockedTimeExceedsDuration() {
49-
long sleepTime = 100;
51+
final int sleepTime = 1234;
52+
assertSleep(sleepTime, s -> s.setSleepTime(sleepTime));
53+
}
5054

51-
ObjectSleeper<String> objectSleeper = new ObjectSleeper<>();
52-
objectSleeper.setReceiver(receiver);
53-
objectSleeper.setSleepTime(sleepTime);
54-
Instant start = Instant.now();
55-
objectSleeper.process(null);
56-
Instant end = Instant.now();
55+
@Test
56+
public void shouldTestIfClockedTimeExceedsDurationInMilliseconds() {
57+
final int sleepTime = 567;
58+
assertSleep(sleepTime, s -> {
59+
s.setSleepTime(sleepTime);
60+
s.setTimeUnit("MILLISECONDS");
61+
});
62+
}
63+
64+
@Test
65+
public void shouldTestIfClockedTimeExceedsDurationInSeconds() {
66+
final int sleepTime = 1;
67+
assertSleep(sleepTime * MILLISECONDS_PER_SECOND, s -> {
68+
s.setSleepTime(sleepTime);
69+
s.setTimeUnit("SECOND");
70+
});
71+
}
5772

58-
Duration timeElapsed = Duration.between(start, end);
73+
private void assertSleep(final long expectedMillis, final Consumer<ObjectSleeper> consumer) {
74+
final ObjectSleeper<String> objectSleeper = new ObjectSleeper<>();
75+
objectSleeper.setReceiver(receiver);
76+
consumer.accept(objectSleeper);
5977

60-
assertTrue(timeElapsed.toMillis() >= sleepTime);
78+
final long startTime = System.nanoTime();
79+
objectSleeper.process(null);
80+
final long actualMillis = (System.nanoTime() - startTime) / NANOSECONDS_PER_MILLISECOND;
6181

82+
Assert.assertTrue("sleep time too short: " + actualMillis, actualMillis >= expectedMillis);
83+
Assert.assertTrue("sleep time too long: " + actualMillis, actualMillis < expectedMillis + PROCESS_OVERHEAD_MILLISECONDS);
6284
}
6385

64-
6586
}

0 commit comments

Comments
 (0)