@@ -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,34 @@ 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
103+
85104 // step past the second add
86- Thread .sleep (1L );
105+ // Advance time
106+ queue .injectTimeSource (
107+ () -> {
108+ return staticTime .plusMillis (190 );
109+ });
87110 assertTrue ("should not have added" , queue .length () == 0 );
88111 }
89112
90113 @ Test
91114 public void testCopyShifting () throws Exception {
115+ final Instant staticTime = Instant .now ();
92116 DefaultDelayingQueue <String > queue = new DefaultDelayingQueue <>();
117+ queue .injectTimeSource (
118+ () -> {
119+ return staticTime ;
120+ });
121+
93122 final String first = "foo" ;
94123 final String second = "bar" ;
95124 final String third = "baz" ;
@@ -100,7 +129,10 @@ public void testCopyShifting() throws Exception {
100129 assertTrue (waitForWaitingQueueToFill (queue ));
101130 assertTrue ("should not have added" , queue .length () == 0 );
102131
103- Thread .sleep (2000L );
132+ queue .injectTimeSource (
133+ () -> {
134+ return staticTime .plusMillis (2000 );
135+ });
104136 assertTrue (waitForAdded (queue , 3 ));
105137 String actualFirst = queue .get ();
106138 assertEquals (actualFirst , third );
0 commit comments