27
27
import java .util .concurrent .ExecutorService ;
28
28
import java .util .concurrent .Executors ;
29
29
import java .util .concurrent .Future ;
30
+ import java .util .concurrent .TimeUnit ;
30
31
import java .util .concurrent .atomic .AtomicBoolean ;
31
32
import java .util .concurrent .atomic .AtomicInteger ;
32
33
@@ -313,6 +314,8 @@ private static class TestException extends RuntimeException {
313
314
@ Test
314
315
public void testNoBufferingOrBlockingOfSequence () throws Throwable {
315
316
final CountDownLatch finished = new CountDownLatch (1 );
317
+ final int COUNT = 30 ;
318
+ final CountDownLatch timeHasPassed = new CountDownLatch (COUNT );
316
319
final AtomicBoolean running = new AtomicBoolean (true );
317
320
final AtomicInteger count = new AtomicInteger (0 );
318
321
final Observable <Integer > obs = Observable .create (new Func1 <Observer <Integer >, Subscription >() {
@@ -326,7 +329,7 @@ public void run() {
326
329
try {
327
330
while (running .get ()) {
328
331
o .onNext (count .incrementAndGet ());
329
- Thread . sleep ( 0 , 100 );
332
+ timeHasPassed . countDown ( );
330
333
}
331
334
o .onCompleted ();
332
335
} catch (Throwable e ) {
@@ -350,19 +353,14 @@ public void run() {
350
353
// we should have a different value
351
354
assertTrue ("a and b should be different" , a != b );
352
355
353
- // wait for some time
354
- Thread .sleep (100 );
355
- // make sure the counter in the observable has increased beyond b
356
- while (count .get () <= (b + 10 )) {
357
- Thread .sleep (100 );
358
- }
356
+ // wait for some time (if times out we are blocked somewhere so fail ... set very high for very slow, constrained machines)
357
+ timeHasPassed .await (8000 , TimeUnit .MILLISECONDS );
359
358
360
359
assertTrue (it .hasNext ());
361
- int expectedHigherThan = count .get ();
362
360
int c = it .next ();
363
361
364
362
assertTrue ("c should not just be the next in sequence" , c != (b + 1 ));
365
- assertTrue ("expected that c [" + c + "] is higher than " + expectedHigherThan , c > expectedHigherThan );
363
+ assertTrue ("expected that c [" + c + "] is higher than or equal to " + COUNT , c >= COUNT );
366
364
367
365
assertTrue (it .hasNext ());
368
366
0 commit comments