Skip to content

Commit c9c816e

Browse files
Refactor test to use CountDownLatch instead of Thread.sleep
1 parent af3720e commit c9c816e

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

rxjava-core/src/main/java/rx/operators/OperationObserveOn.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@
1515
*/
1616
package rx.operators;
1717

18-
import static org.mockito.Matchers.*;
18+
import static org.junit.Assert.*;
1919
import static org.mockito.Mockito.*;
2020

21-
import org.junit.Test;
21+
import java.util.concurrent.CountDownLatch;
22+
import java.util.concurrent.TimeUnit;
2223

24+
import org.junit.Test;
2325
import org.mockito.InOrder;
26+
import org.mockito.invocation.InvocationOnMock;
27+
import org.mockito.stubbing.Answer;
28+
2429
import rx.Observable;
2530
import rx.Observer;
2631
import rx.Scheduler;
@@ -66,7 +71,6 @@ public void testObserveOn() {
6671
verify(observer, times(1)).onCompleted();
6772
}
6873

69-
7074
@Test
7175
@SuppressWarnings("unchecked")
7276
public void testOrdering() throws InterruptedException {
@@ -76,9 +80,21 @@ public void testOrdering() throws InterruptedException {
7680

7781
InOrder inOrder = inOrder(observer);
7882

83+
final CountDownLatch completedLatch = new CountDownLatch(1);
84+
doAnswer(new Answer<Void>() {
85+
86+
@Override
87+
public Void answer(InvocationOnMock invocation) throws Throwable {
88+
completedLatch.countDown();
89+
return null;
90+
}
91+
}).when(observer).onCompleted();
92+
7993
obs.observeOn(Schedulers.threadPoolForComputation()).subscribe(observer);
8094

81-
Thread.sleep(500); // !!! not a true unit test
95+
if (!completedLatch.await(1000, TimeUnit.MILLISECONDS)) {
96+
fail("timed out waiting");
97+
}
8298

8399
inOrder.verify(observer, times(1)).onNext("one");
84100
inOrder.verify(observer, times(1)).onNext(null);

0 commit comments

Comments
 (0)