19
19
20
20
import java .util .PriorityQueue ;
21
21
import java .util .concurrent .TimeUnit ;
22
+ import java .util .concurrent .atomic .AtomicInteger ;
22
23
23
24
import org .junit .Test ;
24
25
import org .mockito .InOrder ;
@@ -43,6 +44,8 @@ public static CurrentThreadScheduler getInstance() {
43
44
private CurrentThreadScheduler () {
44
45
}
45
46
47
+ private final AtomicInteger counter = new AtomicInteger (0 );
48
+
46
49
@ Override
47
50
public <T > Subscription schedule (T state , Func2 <Scheduler , T , Subscription > action ) {
48
51
DiscardableAction <T > discardableAction = new DiscardableAction <T >(state , action );
@@ -68,7 +71,7 @@ private void enqueue(DiscardableAction<?> action, long execTime) {
68
71
QUEUE .set (queue );
69
72
}
70
73
71
- queue .add (new TimedAction (action , execTime ));
74
+ queue .add (new TimedAction (action , execTime , counter . incrementAndGet () ));
72
75
73
76
if (exec ) {
74
77
while (!queue .isEmpty ()) {
@@ -82,15 +85,21 @@ private void enqueue(DiscardableAction<?> action, long execTime) {
82
85
private static class TimedAction implements Comparable <TimedAction > {
83
86
final DiscardableAction <?> action ;
84
87
final Long execTime ;
88
+ final Integer count ; // In case if time between enqueueing took less than 1ms
85
89
86
- private TimedAction (DiscardableAction <?> action , Long execTime ) {
90
+ private TimedAction (DiscardableAction <?> action , Long execTime , Integer count ) {
87
91
this .action = action ;
88
92
this .execTime = execTime ;
93
+ this .count = count ;
89
94
}
90
95
91
96
@ Override
92
- public int compareTo (TimedAction timedAction ) {
93
- return execTime .compareTo (timedAction .execTime );
97
+ public int compareTo (TimedAction that ) {
98
+ int result = execTime .compareTo (that .execTime );
99
+ if (result == 0 ) {
100
+ return count .compareTo (that .count );
101
+ }
102
+ return result ;
94
103
}
95
104
}
96
105
@@ -184,6 +193,35 @@ public void call() {
184
193
185
194
}
186
195
196
+ @ Test
197
+ public void testMixOfDelayedAndNonDelayedActions () {
198
+ final CurrentThreadScheduler scheduler = new CurrentThreadScheduler ();
199
+
200
+ final Action0 first = mock (Action0 .class );
201
+ final Action0 second = mock (Action0 .class );
202
+ final Action0 third = mock (Action0 .class );
203
+ final Action0 fourth = mock (Action0 .class );
204
+
205
+ scheduler .schedule (new Action0 () {
206
+ @ Override
207
+ public void call () {
208
+ scheduler .schedule (first );
209
+ scheduler .schedule (second , 300 , TimeUnit .MILLISECONDS );
210
+ scheduler .schedule (third , 100 , TimeUnit .MILLISECONDS );
211
+ scheduler .schedule (fourth );
212
+ }
213
+ });
214
+
215
+ InOrder inOrder = inOrder (first , second , third , fourth );
216
+
217
+ inOrder .verify (first , times (1 )).call ();
218
+ inOrder .verify (fourth , times (1 )).call ();
219
+ inOrder .verify (third , times (1 )).call ();
220
+ inOrder .verify (second , times (1 )).call ();
221
+
222
+
223
+ }
224
+
187
225
}
188
226
189
227
}
0 commit comments