|
16 | 16 |
|
17 | 17 | package rx.internal.operators;
|
18 | 18 |
|
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | +import static org.mockito.Matchers.any; |
19 | 22 | import static org.mockito.Mockito.*;
|
20 | 23 |
|
21 | 24 | import java.util.Arrays;
|
22 |
| - |
23 |
| -import static org.junit.Assert.*; |
| 25 | +import java.util.Collections; |
24 | 26 |
|
25 | 27 | import org.junit.Test;
|
26 | 28 |
|
|
34 | 36 | import rx.observers.TestObserver;
|
35 | 37 | import rx.observers.TestSubscriber;
|
36 | 38 | import rx.schedulers.Schedulers;
|
| 39 | +import rx.subjects.PublishSubject; |
37 | 40 |
|
38 | 41 | /**
|
39 | 42 | * Test the onBackpressureBlock() behavior.
|
@@ -161,13 +164,15 @@ public void onStart() {
|
161 | 164 | Thread.sleep(WAIT);
|
162 | 165 |
|
163 | 166 | o.assertReceivedOnNext(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
|
164 |
| - o.assertNoErrors(); |
165 |
| - assertTrue(o.getOnCompletedEvents().isEmpty()); |
| 167 | + o.assertTerminalEvent(); |
| 168 | + assertEquals(1, o.getOnErrorEvents().size()); |
| 169 | + assertTrue(o.getOnErrorEvents().get(0) instanceof TestException); |
166 | 170 |
|
167 | 171 | o.requestMore(10);
|
168 | 172 |
|
169 | 173 | Thread.sleep(WAIT);
|
170 | 174 |
|
| 175 | + o.assertReceivedOnNext(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); |
171 | 176 | o.assertTerminalEvent();
|
172 | 177 | assertEquals(1, o.getOnErrorEvents().size());
|
173 | 178 | assertTrue(o.getOnErrorEvents().get(0) instanceof TestException);
|
@@ -259,4 +264,84 @@ public void testTakeWorksSubscriberRequestUnlimitedBufferedException() {
|
259 | 264 | o.assertNoErrors();
|
260 | 265 | o.assertTerminalEvent();
|
261 | 266 | }
|
| 267 | + @Test(timeout = 10000) |
| 268 | + public void testOnCompletedDoesntWaitIfNoEvents() { |
| 269 | + |
| 270 | + TestSubscriber<Integer> o = new TestSubscriber<Integer>() { |
| 271 | + @Override |
| 272 | + public void onStart() { |
| 273 | + request(0); // make sure it doesn't start in unlimited mode |
| 274 | + } |
| 275 | + }; |
| 276 | + Observable.<Integer>empty().onBackpressureBlock(2).subscribe(o); |
| 277 | + |
| 278 | + o.assertNoErrors(); |
| 279 | + o.assertTerminalEvent(); |
| 280 | + o.assertReceivedOnNext(Collections.<Integer>emptyList()); |
| 281 | + } |
| 282 | + @Test(timeout = 10000) |
| 283 | + public void testOnCompletedDoesWaitIfEvents() { |
| 284 | + |
| 285 | + TestSubscriber<Integer> o = new TestSubscriber<Integer>() { |
| 286 | + @Override |
| 287 | + public void onStart() { |
| 288 | + request(0); // make sure it doesn't start in unlimited mode |
| 289 | + } |
| 290 | + }; |
| 291 | + Observable.just(1).onBackpressureBlock(2).subscribe(o); |
| 292 | + |
| 293 | + o.assertReceivedOnNext(Collections.<Integer>emptyList()); |
| 294 | + assertTrue(o.getOnErrorEvents().isEmpty()); |
| 295 | + assertTrue(o.getOnCompletedEvents().isEmpty()); |
| 296 | + } |
| 297 | + @Test(timeout = 10000) |
| 298 | + public void testOnCompletedDoesntWaitIfNoEvents2() { |
| 299 | + final PublishSubject<Integer> ps = PublishSubject.create(); |
| 300 | + TestSubscriber<Integer> o = new TestSubscriber<Integer>() { |
| 301 | + @Override |
| 302 | + public void onStart() { |
| 303 | + request(0); // make sure it doesn't start in unlimited mode |
| 304 | + } |
| 305 | + @Override |
| 306 | + public void onNext(Integer t) { |
| 307 | + super.onNext(t); |
| 308 | + ps.onCompleted(); // as if an async completion arrived while in the loop |
| 309 | + } |
| 310 | + }; |
| 311 | + ps.onBackpressureBlock(2).unsafeSubscribe(o); |
| 312 | + ps.onNext(1); |
| 313 | + o.requestMore(1); |
| 314 | + |
| 315 | + o.assertNoErrors(); |
| 316 | + o.assertTerminalEvent(); |
| 317 | + o.assertReceivedOnNext(Arrays.asList(1)); |
| 318 | + } |
| 319 | + @Test(timeout = 10000) |
| 320 | + public void testOnCompletedDoesntWaitIfNoEvents3() { |
| 321 | + final PublishSubject<Integer> ps = PublishSubject.create(); |
| 322 | + TestSubscriber<Integer> o = new TestSubscriber<Integer>() { |
| 323 | + boolean once = true; |
| 324 | + @Override |
| 325 | + public void onStart() { |
| 326 | + request(0); // make sure it doesn't start in unlimited mode |
| 327 | + } |
| 328 | + @Override |
| 329 | + public void onNext(Integer t) { |
| 330 | + super.onNext(t); |
| 331 | + if (once) { |
| 332 | + once = false; |
| 333 | + ps.onNext(2); |
| 334 | + ps.onCompleted(); // as if an async completion arrived while in the loop |
| 335 | + requestMore(1); |
| 336 | + } |
| 337 | + } |
| 338 | + }; |
| 339 | + ps.onBackpressureBlock(3).unsafeSubscribe(o); |
| 340 | + ps.onNext(1); |
| 341 | + o.requestMore(1); |
| 342 | + |
| 343 | + o.assertNoErrors(); |
| 344 | + o.assertTerminalEvent(); |
| 345 | + o.assertReceivedOnNext(Arrays.asList(1, 2)); |
| 346 | + } |
262 | 347 | }
|
0 commit comments