15
15
*/
16
16
package rx .operators ;
17
17
18
- import static org .mockito .Matchers .*;
19
- import static org .mockito .Mockito .*;
18
+ import static org .junit .Assert .assertFalse ;
19
+ import static org .mockito .Matchers .any ;
20
+ import static org .mockito .Matchers .isA ;
21
+ import static org .mockito .Mockito .doAnswer ;
22
+ import static org .mockito .Mockito .inOrder ;
23
+ import static org .mockito .Mockito .mock ;
24
+ import static org .mockito .Mockito .never ;
25
+ import static org .mockito .Mockito .verify ;
20
26
21
27
import java .util .Arrays ;
22
28
import java .util .concurrent .CountDownLatch ;
29
+ import java .util .concurrent .TimeUnit ;
23
30
import java .util .concurrent .TimeoutException ;
31
+ import java .util .concurrent .atomic .AtomicBoolean ;
24
32
25
33
import org .junit .Test ;
26
34
import org .mockito .InOrder ;
30
38
import rx .Observable ;
31
39
import rx .Observable .OnSubscribe ;
32
40
import rx .Observer ;
41
+ import rx .Scheduler ;
33
42
import rx .Subscriber ;
34
43
import rx .observers .TestSubscriber ;
35
44
import rx .schedulers .Schedulers ;
@@ -329,6 +338,8 @@ public void testTimeoutSelectorWithTimeoutAndOnNextRaceCondition() throws Interr
329
338
final CountDownLatch observerReceivedTwo = new CountDownLatch (1 );
330
339
final CountDownLatch timeoutEmittedOne = new CountDownLatch (1 );
331
340
final CountDownLatch observerCompleted = new CountDownLatch (1 );
341
+ final CountDownLatch enteredTimeoutOne = new CountDownLatch (1 );
342
+ final AtomicBoolean latchTimeout = new AtomicBoolean (false );
332
343
333
344
final Func1 <Integer , Observable <Integer >> timeoutFunc = new Func1 <Integer , Observable <Integer >>() {
334
345
@ Override
@@ -338,31 +349,23 @@ public Observable<Integer> call(Integer t1) {
338
349
return Observable .create (new OnSubscribe <Integer >() {
339
350
@ Override
340
351
public void call (Subscriber <? super Integer > subscriber ) {
341
- subscriber .add (Subscriptions .create (new Action0 () {
342
- @ Override
343
- public void call () {
344
- try {
345
- // emulate "unsubscribe" is busy and finishes after timeout.onNext(1)
346
- timeoutEmittedOne .await ();
347
- } catch (InterruptedException e ) {
348
- // if we are interrupted then we complete (as this can happen when unsubscribed)
349
- observerCompleted .countDown ();
350
- e .printStackTrace ();
352
+ enteredTimeoutOne .countDown ();
353
+ // force the timeout message be sent after observer.onNext(2)
354
+ while (true ) {
355
+ try {
356
+ if (!observerReceivedTwo .await (30 , TimeUnit .SECONDS )) {
357
+ // CountDownLatch timeout
358
+ // There should be something wrong
359
+ latchTimeout .set (true );
351
360
}
361
+ break ;
362
+ } catch (InterruptedException e ) {
363
+ // Since we just want to emulate a busy method,
364
+ // we ignore the interrupt signal from Scheduler.
352
365
}
353
- }));
354
- // force the timeout message be sent after observer.onNext(2)
355
- try {
356
- observerReceivedTwo .await ();
357
- } catch (InterruptedException e ) {
358
- // if we are interrupted then we complete (as this can happen when unsubscribed)
359
- observerCompleted .countDown ();
360
- e .printStackTrace ();
361
- }
362
- if (!subscriber .isUnsubscribed ()) {
363
- subscriber .onNext (1 );
364
- timeoutEmittedOne .countDown ();
365
366
}
367
+ subscriber .onNext (1 );
368
+ timeoutEmittedOne .countDown ();
366
369
}
367
370
}).subscribeOn (Schedulers .newThread ());
368
371
} else {
@@ -401,9 +404,18 @@ public void run() {
401
404
PublishSubject <Integer > source = PublishSubject .create ();
402
405
source .timeout (timeoutFunc , Observable .from (3 )).subscribe (ts );
403
406
source .onNext (1 ); // start timeout
407
+ try {
408
+ if (!enteredTimeoutOne .await (30 , TimeUnit .SECONDS )) {
409
+ latchTimeout .set (true );
410
+ }
411
+ } catch (InterruptedException e ) {
412
+ e .printStackTrace ();
413
+ }
404
414
source .onNext (2 ); // disable timeout
405
415
try {
406
- timeoutEmittedOne .await ();
416
+ if (!timeoutEmittedOne .await (30 , TimeUnit .SECONDS )) {
417
+ latchTimeout .set (true );
418
+ }
407
419
} catch (InterruptedException e ) {
408
420
e .printStackTrace ();
409
421
}
@@ -412,7 +424,11 @@ public void run() {
412
424
413
425
}).start ();
414
426
415
- observerCompleted .await ();
427
+ if (!observerCompleted .await (30 , TimeUnit .SECONDS )) {
428
+ latchTimeout .set (true );
429
+ }
430
+
431
+ assertFalse ("CoundDownLatch timeout" , latchTimeout .get ());
416
432
417
433
InOrder inOrder = inOrder (o );
418
434
inOrder .verify (o ).onNext (1 );
0 commit comments