1515 */
1616package rx .operators ;
1717
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 ;
2026
2127import java .util .Arrays ;
2228import java .util .concurrent .CountDownLatch ;
29+ import java .util .concurrent .TimeUnit ;
2330import java .util .concurrent .TimeoutException ;
31+ import java .util .concurrent .atomic .AtomicBoolean ;
2432
2533import org .junit .Test ;
2634import org .mockito .InOrder ;
3038import rx .Observable ;
3139import rx .Observable .OnSubscribe ;
3240import rx .Observer ;
41+ import rx .Scheduler ;
3342import rx .Subscriber ;
3443import rx .observers .TestSubscriber ;
3544import rx .schedulers .Schedulers ;
@@ -329,6 +338,8 @@ public void testTimeoutSelectorWithTimeoutAndOnNextRaceCondition() throws Interr
329338 final CountDownLatch observerReceivedTwo = new CountDownLatch (1 );
330339 final CountDownLatch timeoutEmittedOne = new CountDownLatch (1 );
331340 final CountDownLatch observerCompleted = new CountDownLatch (1 );
341+ final CountDownLatch enteredTimeoutOne = new CountDownLatch (1 );
342+ final AtomicBoolean latchTimeout = new AtomicBoolean (false );
332343
333344 final Func1 <Integer , Observable <Integer >> timeoutFunc = new Func1 <Integer , Observable <Integer >>() {
334345 @ Override
@@ -338,31 +349,23 @@ public Observable<Integer> call(Integer t1) {
338349 return Observable .create (new OnSubscribe <Integer >() {
339350 @ Override
340351 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 );
351360 }
361+ break ;
362+ } catch (InterruptedException e ) {
363+ // Since we just want to emulate a busy method,
364+ // we ignore the interrupt signal from Scheduler.
352365 }
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 ();
365366 }
367+ subscriber .onNext (1 );
368+ timeoutEmittedOne .countDown ();
366369 }
367370 }).subscribeOn (Schedulers .newThread ());
368371 } else {
@@ -401,9 +404,18 @@ public void run() {
401404 PublishSubject <Integer > source = PublishSubject .create ();
402405 source .timeout (timeoutFunc , Observable .from (3 )).subscribe (ts );
403406 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+ }
404414 source .onNext (2 ); // disable timeout
405415 try {
406- timeoutEmittedOne .await ();
416+ if (!timeoutEmittedOne .await (30 , TimeUnit .SECONDS )) {
417+ latchTimeout .set (true );
418+ }
407419 } catch (InterruptedException e ) {
408420 e .printStackTrace ();
409421 }
@@ -412,7 +424,11 @@ public void run() {
412424
413425 }).start ();
414426
415- observerCompleted .await ();
427+ if (!observerCompleted .await (30 , TimeUnit .SECONDS )) {
428+ latchTimeout .set (true );
429+ }
430+
431+ assertFalse ("CoundDownLatch timeout" , latchTimeout .get ());
416432
417433 InOrder inOrder = inOrder (o );
418434 inOrder .verify (o ).onNext (1 );
0 commit comments