@@ -52,23 +52,37 @@ public void testSimpleDelayingQueue() throws Exception {
5252
5353 @ Test
5454 public void testDeduping () throws Exception {
55+ final Instant staticTime = Instant .now ();
5556 DefaultDelayingQueue <String > queue = new DefaultDelayingQueue <>();
5657 String item = "foo" ;
5758
59+ // Hold time still
60+ queue .injectTimeSource (
61+ () -> {
62+ return staticTime ;
63+ });
64+
5865 queue .addAfter (item , Duration .ofMillis (50 ));
5966 assertTrue (waitForWaitingQueueToFill (queue ));
6067 queue .addAfter (item , Duration .ofMillis (70 ));
6168 assertTrue (waitForWaitingQueueToFill (queue ));
6269 assertTrue ("should not have added" , queue .length () == 0 );
6370
64- // step past the first block, we should receive now
65- Thread .sleep (60L );
71+ // Advance time
72+ queue .injectTimeSource (
73+ () -> {
74+ return staticTime .plusMillis (60 );
75+ });
6676 assertTrue (waitForAdded (queue , 1 ));
6777 item = queue .get ();
6878 queue .done (item );
6979
7080 // step past the second add
71- Thread .sleep (20L );
81+ // Advance time
82+ queue .injectTimeSource (
83+ () -> {
84+ return staticTime .plusMillis (90 );
85+ });
7286 assertTrue ("should not have added" , queue .length () == 0 );
7387
7488 // test again, but this time the earlier should override
@@ -77,19 +91,33 @@ public void testDeduping() throws Exception {
7791 assertTrue (waitForWaitingQueueToFill (queue ));
7892 assertTrue ("should not have added" , queue .length () == 0 );
7993
80- Thread .sleep (40L );
94+ // Advance time
95+ queue .injectTimeSource (
96+ () -> {
97+ return staticTime .plusMillis (150 );
98+ });
8199 assertTrue (waitForAdded (queue , 1 ));
82100 item = queue .get ();
83101 queue .done (item );
84102
85103 // step past the second add
86- Thread .sleep (1L );
104+ // Advance time
105+ queue .injectTimeSource (
106+ () -> {
107+ return staticTime .plusMillis (190 );
108+ });
87109 assertTrue ("should not have added" , queue .length () == 0 );
88110 }
89111
90112 @ Test
91113 public void testCopyShifting () throws Exception {
114+ final Instant staticTime = Instant .now ();
92115 DefaultDelayingQueue <String > queue = new DefaultDelayingQueue <>();
116+ queue .injectTimeSource (
117+ () -> {
118+ return staticTime ;
119+ });
120+
93121 final String first = "foo" ;
94122 final String second = "bar" ;
95123 final String third = "baz" ;
@@ -100,7 +128,10 @@ public void testCopyShifting() throws Exception {
100128 assertTrue (waitForWaitingQueueToFill (queue ));
101129 assertTrue ("should not have added" , queue .length () == 0 );
102130
103- Thread .sleep (2000L );
131+ queue .injectTimeSource (
132+ () -> {
133+ return staticTime .plusMillis (2000 );
134+ });
104135 assertTrue (waitForAdded (queue , 3 ));
105136 String actualFirst = queue .get ();
106137 assertEquals (actualFirst , third );
0 commit comments