File tree Expand file tree Collapse file tree 2 files changed +19
-9
lines changed Expand file tree Collapse file tree 2 files changed +19
-9
lines changed Original file line number Diff line number Diff line change @@ -48,7 +48,8 @@ public Iterator<T> iterator() {
48
48
49
49
}
50
50
51
- private static class NextIterator <T > implements Iterator <T > {
51
+ // test needs to access the observer.waiting flag non-blockingly.
52
+ /* private */ static final class NextIterator <T > implements Iterator <T > {
52
53
53
54
private final NextObserver <? extends T > observer ;
54
55
private T next ;
@@ -60,6 +61,12 @@ private NextIterator(NextObserver<? extends T> observer) {
60
61
this .observer = observer ;
61
62
}
62
63
64
+ // in tests, set the waiting flag without blocking for the next value to
65
+ // allow lockstepping instead of multi-threading
66
+ void setWaiting (boolean value ) {
67
+ observer .waiting .set (value );
68
+ }
69
+
63
70
@ Override
64
71
public boolean hasNext () {
65
72
if (error != null ) {
Original file line number Diff line number Diff line change @@ -296,24 +296,27 @@ public void run() {
296
296
System .out .println ("a: " + a + " b: " + b + " c: " + c );
297
297
}
298
298
299
- @ Test (timeout = 8000 )
299
+ @ Test /* (timeout = 8000) */
300
300
public void testSingleSourceManyIterators () throws InterruptedException {
301
- BlockingObservable <Long > source = Observable .interval (200 , TimeUnit .MILLISECONDS ).take (10 ).toBlockingObservable ();
301
+ PublishSubject <Long > ps = PublishSubject .create ();
302
+ BlockingObservable <Long > source = ps .take (10 ).toBlockingObservable ();
302
303
303
304
Iterable <Long > iter = source .next ();
304
305
305
306
for (int j = 0 ; j < 3 ; j ++) {
306
- Iterator <Long > it = iter .iterator ();
307
+ OperationNext . NextIterator <Long > it = ( OperationNext . NextIterator < Long >) iter .iterator ();
307
308
308
- for (int i = 0 ; i < 9 ; i ++) {
309
+ for (long i = 0 ; i < 9 ; i ++) {
309
310
// hasNext has to set the waiting to true, otherwise, all onNext will be skipped
311
+ it .setWaiting (true );
312
+ ps .onNext (i );
310
313
Assert .assertEquals (true , it .hasNext ());
311
- Assert .assertEquals (Long .valueOf (i ), it .next ());
314
+ Assert .assertEquals (j + "th iteration" , Long .valueOf (i ), it .next ());
312
315
}
316
+ it .setWaiting (true );
317
+ ps .onNext (9L );
313
318
314
- Thread .sleep (400 );
315
-
316
- Assert .assertEquals (false , it .hasNext ());
319
+ Assert .assertEquals (j + "th iteration" , false , it .hasNext ());
317
320
}
318
321
319
322
}
You can’t perform that action at this time.
0 commit comments