|
24 | 24 | import io.reactivex.*; |
25 | 25 | import io.reactivex.Observable; |
26 | 26 | import io.reactivex.Observer; |
27 | | -import io.reactivex.disposables.Disposables; |
| 27 | +import io.reactivex.disposables.*; |
28 | 28 | import io.reactivex.exceptions.TestException; |
29 | 29 | import io.reactivex.internal.operators.observable.BlockingObservableNext.NextObserver; |
30 | 30 | import io.reactivex.plugins.RxJavaPlugins; |
@@ -234,67 +234,81 @@ public void testNextWithCallingHasNextMultipleTimes() { |
234 | 234 | */ |
235 | 235 | @Test |
236 | 236 | public void testNoBufferingOrBlockingOfSequence() throws Throwable { |
237 | | - final CountDownLatch finished = new CountDownLatch(1); |
238 | | - final int COUNT = 30; |
239 | | - final CountDownLatch timeHasPassed = new CountDownLatch(COUNT); |
240 | | - final AtomicBoolean running = new AtomicBoolean(true); |
241 | | - final AtomicInteger count = new AtomicInteger(0); |
242 | | - final Observable<Integer> obs = Observable.unsafeCreate(new ObservableSource<Integer>() { |
243 | | - |
244 | | - @Override |
245 | | - public void subscribe(final Observer<? super Integer> o) { |
246 | | - o.onSubscribe(Disposables.empty()); |
247 | | - new Thread(new Runnable() { |
| 237 | + int repeat = 0; |
| 238 | + for (;;) { |
| 239 | + final SerialDisposable task = new SerialDisposable(); |
| 240 | + try { |
| 241 | + final CountDownLatch finished = new CountDownLatch(1); |
| 242 | + final int COUNT = 30; |
| 243 | + final CountDownLatch timeHasPassed = new CountDownLatch(COUNT); |
| 244 | + final AtomicBoolean running = new AtomicBoolean(true); |
| 245 | + final AtomicInteger count = new AtomicInteger(0); |
| 246 | + final Observable<Integer> obs = Observable.unsafeCreate(new ObservableSource<Integer>() { |
248 | 247 |
|
249 | 248 | @Override |
250 | | - public void run() { |
251 | | - try { |
252 | | - while (running.get()) { |
253 | | - o.onNext(count.incrementAndGet()); |
254 | | - timeHasPassed.countDown(); |
| 249 | + public void subscribe(final Observer<? super Integer> o) { |
| 250 | + o.onSubscribe(Disposables.empty()); |
| 251 | + task.replace(Schedulers.single().scheduleDirect(new Runnable() { |
| 252 | + |
| 253 | + @Override |
| 254 | + public void run() { |
| 255 | + try { |
| 256 | + while (running.get() && !task.isDisposed()) { |
| 257 | + o.onNext(count.incrementAndGet()); |
| 258 | + timeHasPassed.countDown(); |
| 259 | + } |
| 260 | + o.onComplete(); |
| 261 | + } catch (Throwable e) { |
| 262 | + o.onError(e); |
| 263 | + } finally { |
| 264 | + finished.countDown(); |
| 265 | + } |
255 | 266 | } |
256 | | - o.onComplete(); |
257 | | - } catch (Throwable e) { |
258 | | - o.onError(e); |
259 | | - } finally { |
260 | | - finished.countDown(); |
261 | | - } |
| 267 | + })); |
262 | 268 | } |
263 | | - }).start(); |
264 | | - } |
265 | 269 |
|
266 | | - }); |
| 270 | + }); |
267 | 271 |
|
268 | | - Iterator<Integer> it = next(obs).iterator(); |
| 272 | + Iterator<Integer> it = next(obs).iterator(); |
269 | 273 |
|
270 | | - assertTrue(it.hasNext()); |
271 | | - int a = it.next(); |
272 | | - assertTrue(it.hasNext()); |
273 | | - int b = it.next(); |
274 | | - // we should have a different value |
275 | | - assertTrue("a and b should be different", a != b); |
| 274 | + assertTrue(it.hasNext()); |
| 275 | + int a = it.next(); |
| 276 | + assertTrue(it.hasNext()); |
| 277 | + int b = it.next(); |
| 278 | + // we should have a different value |
| 279 | + assertTrue("a and b should be different", a != b); |
276 | 280 |
|
277 | | - // wait for some time (if times out we are blocked somewhere so fail ... set very high for very slow, constrained machines) |
278 | | - timeHasPassed.await(8000, TimeUnit.MILLISECONDS); |
| 281 | + // wait for some time (if times out we are blocked somewhere so fail ... set very high for very slow, constrained machines) |
| 282 | + timeHasPassed.await(8000, TimeUnit.MILLISECONDS); |
279 | 283 |
|
280 | | - assertTrue(it.hasNext()); |
281 | | - int c = it.next(); |
| 284 | + assertTrue(it.hasNext()); |
| 285 | + int c = it.next(); |
282 | 286 |
|
283 | | - assertTrue("c should not just be the next in sequence", c != (b + 1)); |
284 | | - assertTrue("expected that c [" + c + "] is higher than or equal to " + COUNT, c >= COUNT); |
| 287 | + assertTrue("c should not just be the next in sequence", c != (b + 1)); |
| 288 | + assertTrue("expected that c [" + c + "] is higher than or equal to " + COUNT, c >= COUNT); |
285 | 289 |
|
286 | | - assertTrue(it.hasNext()); |
287 | | - int d = it.next(); |
288 | | - assertTrue(d > c); |
| 290 | + assertTrue(it.hasNext()); |
| 291 | + int d = it.next(); |
| 292 | + assertTrue(d > c); |
289 | 293 |
|
290 | | - // shut down the thread |
291 | | - running.set(false); |
| 294 | + // shut down the thread |
| 295 | + running.set(false); |
292 | 296 |
|
293 | | - finished.await(); |
| 297 | + finished.await(); |
294 | 298 |
|
295 | | - assertFalse(it.hasNext()); |
| 299 | + assertFalse(it.hasNext()); |
296 | 300 |
|
297 | | - System.out.println("a: " + a + " b: " + b + " c: " + c); |
| 301 | + System.out.println("a: " + a + " b: " + b + " c: " + c); |
| 302 | + break; |
| 303 | + } catch (AssertionError ex) { |
| 304 | + if (++repeat == 3) { |
| 305 | + throw ex; |
| 306 | + } |
| 307 | + Thread.sleep((int)(1000 * Math.pow(2, repeat - 1))); |
| 308 | + } finally { |
| 309 | + task.dispose(); |
| 310 | + } |
| 311 | + } |
298 | 312 | } |
299 | 313 |
|
300 | 314 | @Test /* (timeout = 8000) */ |
|
0 commit comments