|
| 1 | +package rx.internal.operators; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | +import rx.Single; |
| 6 | +import rx.exceptions.TestException; |
| 7 | +import rx.functions.Action0; |
| 8 | +import rx.observers.TestSubscriber; |
| 9 | +import rx.subjects.PublishSubject; |
| 10 | + |
| 11 | +import java.util.concurrent.atomic.AtomicInteger; |
| 12 | + |
| 13 | +public class SingleOnSubscribeDelaySubscriptionOtherTest { |
| 14 | + @Test |
| 15 | + public void noPrematureSubscription() { |
| 16 | + PublishSubject<Object> other = PublishSubject.create(); |
| 17 | + |
| 18 | + TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 19 | + |
| 20 | + final AtomicInteger subscribed = new AtomicInteger(); |
| 21 | + |
| 22 | + Single.just(1) |
| 23 | + .doOnSubscribe(new Action0() { |
| 24 | + @Override |
| 25 | + public void call() { |
| 26 | + subscribed.getAndIncrement(); |
| 27 | + } |
| 28 | + }) |
| 29 | + .delaySubscription(other) |
| 30 | + .subscribe(ts); |
| 31 | + |
| 32 | + ts.assertNotCompleted(); |
| 33 | + ts.assertNoErrors(); |
| 34 | + ts.assertNoValues(); |
| 35 | + |
| 36 | + Assert.assertEquals("Premature subscription", 0, subscribed.get()); |
| 37 | + |
| 38 | + other.onNext(1); |
| 39 | + |
| 40 | + Assert.assertEquals("No subscription", 1, subscribed.get()); |
| 41 | + |
| 42 | + ts.assertValue(1); |
| 43 | + ts.assertNoErrors(); |
| 44 | + ts.assertCompleted(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void noPrematureSubscriptionToError() { |
| 49 | + PublishSubject<Object> other = PublishSubject.create(); |
| 50 | + |
| 51 | + TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 52 | + |
| 53 | + final AtomicInteger subscribed = new AtomicInteger(); |
| 54 | + |
| 55 | + Single.<Integer>error(new TestException()) |
| 56 | + .doOnSubscribe(new Action0() { |
| 57 | + @Override |
| 58 | + public void call() { |
| 59 | + subscribed.getAndIncrement(); |
| 60 | + } |
| 61 | + }) |
| 62 | + .delaySubscription(other) |
| 63 | + .subscribe(ts); |
| 64 | + |
| 65 | + ts.assertNotCompleted(); |
| 66 | + ts.assertNoErrors(); |
| 67 | + ts.assertNoValues(); |
| 68 | + |
| 69 | + Assert.assertEquals("Premature subscription", 0, subscribed.get()); |
| 70 | + |
| 71 | + other.onNext(1); |
| 72 | + |
| 73 | + Assert.assertEquals("No subscription", 1, subscribed.get()); |
| 74 | + |
| 75 | + ts.assertNoValues(); |
| 76 | + ts.assertNotCompleted(); |
| 77 | + ts.assertError(TestException.class); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void noSubscriptionIfOtherErrors() { |
| 82 | + PublishSubject<Object> other = PublishSubject.create(); |
| 83 | + |
| 84 | + TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 85 | + |
| 86 | + final AtomicInteger subscribed = new AtomicInteger(); |
| 87 | + |
| 88 | + Single.<Integer>error(new TestException()) |
| 89 | + .doOnSubscribe(new Action0() { |
| 90 | + @Override |
| 91 | + public void call() { |
| 92 | + subscribed.getAndIncrement(); |
| 93 | + } |
| 94 | + }) |
| 95 | + .delaySubscription(other) |
| 96 | + .subscribe(ts); |
| 97 | + |
| 98 | + ts.assertNotCompleted(); |
| 99 | + ts.assertNoErrors(); |
| 100 | + ts.assertNoValues(); |
| 101 | + |
| 102 | + Assert.assertEquals("Premature subscription", 0, subscribed.get()); |
| 103 | + |
| 104 | + other.onError(new TestException()); |
| 105 | + |
| 106 | + Assert.assertEquals("Premature subscription", 0, subscribed.get()); |
| 107 | + |
| 108 | + ts.assertNoValues(); |
| 109 | + ts.assertNotCompleted(); |
| 110 | + ts.assertError(TestException.class); |
| 111 | + } |
| 112 | +} |
0 commit comments