@@ -52,23 +52,37 @@ public void testSimpleDelayingQueue() throws Exception {
52
52
53
53
@ Test
54
54
public void testDeduping () throws Exception {
55
+ final Instant staticTime = Instant .now ();
55
56
DefaultDelayingQueue <String > queue = new DefaultDelayingQueue <>();
56
57
String item = "foo" ;
57
58
59
+ // Hold time still
60
+ queue .injectTimeSource (
61
+ () -> {
62
+ return staticTime ;
63
+ });
64
+
58
65
queue .addAfter (item , Duration .ofMillis (50 ));
59
66
assertTrue (waitForWaitingQueueToFill (queue ));
60
67
queue .addAfter (item , Duration .ofMillis (70 ));
61
68
assertTrue (waitForWaitingQueueToFill (queue ));
62
69
assertTrue ("should not have added" , queue .length () == 0 );
63
70
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
+ });
66
76
assertTrue (waitForAdded (queue , 1 ));
67
77
item = queue .get ();
68
78
queue .done (item );
69
79
70
80
// step past the second add
71
- Thread .sleep (20L );
81
+ // Advance time
82
+ queue .injectTimeSource (
83
+ () -> {
84
+ return staticTime .plusMillis (90 );
85
+ });
72
86
assertTrue ("should not have added" , queue .length () == 0 );
73
87
74
88
// test again, but this time the earlier should override
@@ -77,19 +91,34 @@ public void testDeduping() throws Exception {
77
91
assertTrue (waitForWaitingQueueToFill (queue ));
78
92
assertTrue ("should not have added" , queue .length () == 0 );
79
93
80
- Thread .sleep (40L );
94
+ // Advance time
95
+ queue .injectTimeSource (
96
+ () -> {
97
+ return staticTime .plusMillis (150 );
98
+ });
81
99
assertTrue (waitForAdded (queue , 1 ));
82
100
item = queue .get ();
83
101
queue .done (item );
84
102
103
+
85
104
// step past the second add
86
- Thread .sleep (1L );
105
+ // Advance time
106
+ queue .injectTimeSource (
107
+ () -> {
108
+ return staticTime .plusMillis (190 );
109
+ });
87
110
assertTrue ("should not have added" , queue .length () == 0 );
88
111
}
89
112
90
113
@ Test
91
114
public void testCopyShifting () throws Exception {
115
+ final Instant staticTime = Instant .now ();
92
116
DefaultDelayingQueue <String > queue = new DefaultDelayingQueue <>();
117
+ queue .injectTimeSource (
118
+ () -> {
119
+ return staticTime ;
120
+ });
121
+
93
122
final String first = "foo" ;
94
123
final String second = "bar" ;
95
124
final String third = "baz" ;
@@ -100,7 +129,10 @@ public void testCopyShifting() throws Exception {
100
129
assertTrue (waitForWaitingQueueToFill (queue ));
101
130
assertTrue ("should not have added" , queue .length () == 0 );
102
131
103
- Thread .sleep (2000L );
132
+ queue .injectTimeSource (
133
+ () -> {
134
+ return staticTime .plusMillis (2000 );
135
+ });
104
136
assertTrue (waitForAdded (queue , 3 ));
105
137
String actualFirst = queue .get ();
106
138
assertEquals (actualFirst , third );
0 commit comments