|
16 | 16 | package rx.internal.operators;
|
17 | 17 |
|
18 | 18 | import static org.junit.Assert.assertEquals;
|
| 19 | +import static org.junit.Assert.assertTrue; |
19 | 20 | import static org.mockito.Mockito.inOrder;
|
20 | 21 | import static org.mockito.Mockito.mock;
|
21 | 22 | import static org.mockito.Mockito.times;
|
22 | 23 | import static rx.internal.operators.OnSubscribeAmb.amb;
|
23 | 24 |
|
24 | 25 | import java.io.IOException;
|
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.concurrent.CountDownLatch; |
25 | 28 | import java.util.concurrent.TimeUnit;
|
26 | 29 | import java.util.concurrent.atomic.AtomicLong;
|
27 | 30 |
|
@@ -219,4 +222,49 @@ public void testBackpressure() {
|
219 | 222 | ts.assertNoErrors();
|
220 | 223 | assertEquals(RxRingBuffer.SIZE * 2, ts.getOnNextEvents().size());
|
221 | 224 | }
|
| 225 | + |
| 226 | + |
| 227 | + @Test |
| 228 | + public void testSubscriptionOnlyHappensOnce() throws InterruptedException { |
| 229 | + final AtomicLong count = new AtomicLong(); |
| 230 | + Action0 incrementer = new Action0() { |
| 231 | + @Override |
| 232 | + public void call() { |
| 233 | + count.incrementAndGet(); |
| 234 | + } |
| 235 | + }; |
| 236 | + //this aync stream should emit first |
| 237 | + Observable<Integer> o1 = Observable.just(1).doOnSubscribe(incrementer) |
| 238 | + .delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation()); |
| 239 | + //this stream emits second |
| 240 | + Observable<Integer> o2 = Observable.just(1).doOnSubscribe(incrementer) |
| 241 | + .delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation()); |
| 242 | + TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); |
| 243 | + Observable.amb(o1, o2).subscribe(ts); |
| 244 | + ts.requestMore(1); |
| 245 | + ts.awaitTerminalEvent(5, TimeUnit.SECONDS); |
| 246 | + ts.assertNoErrors(); |
| 247 | + assertEquals(2, count.get()); |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + public void testRequestsPropagatedToChildren() throws InterruptedException { |
| 252 | + //this aync stream should emit first |
| 253 | + Observable<Integer> o1 = Observable.from(Arrays.asList(1, 2, 3)) |
| 254 | + .delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation()); |
| 255 | + //this stream emits second |
| 256 | + Observable<Integer> o2 = Observable.from(Arrays.asList(4, 5, 6)) |
| 257 | + .delay(200, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation()); |
| 258 | + TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); |
| 259 | + //before subscription request 1 |
| 260 | + ts.requestMore(1); |
| 261 | + Observable.amb(o1, o2).subscribe(ts); |
| 262 | + // before first emission request 20 more |
| 263 | + // this request should suffice to emit all |
| 264 | + ts.requestMore(20); |
| 265 | + //ensure stream does not hang |
| 266 | + ts.awaitTerminalEvent(5, TimeUnit.SECONDS); |
| 267 | + ts.assertNoErrors(); |
| 268 | + } |
| 269 | + |
222 | 270 | }
|
0 commit comments