|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.observables; |
| 17 | + |
| 18 | +import java.util.concurrent.atomic.*; |
| 19 | + |
| 20 | +import org.junit.*; |
| 21 | + |
| 22 | +import rx.*; |
| 23 | +import rx.functions.*; |
| 24 | +import rx.observers.TestSubscriber; |
| 25 | + |
| 26 | +public class ConnectableObservableTest { |
| 27 | + @Test |
| 28 | + public void testAutoConnect() { |
| 29 | + final AtomicInteger run = new AtomicInteger(); |
| 30 | + |
| 31 | + ConnectableObservable<Integer> co = Observable.defer(new Func0<Observable<Integer>>() { |
| 32 | + @Override |
| 33 | + public Observable<Integer> call() { |
| 34 | + return Observable.just(run.incrementAndGet()); |
| 35 | + } |
| 36 | + }).publish(); |
| 37 | + |
| 38 | + Observable<Integer> source = co.autoConnect(); |
| 39 | + |
| 40 | + Assert.assertEquals(0, run.get()); |
| 41 | + |
| 42 | + TestSubscriber<Integer> ts1 = TestSubscriber.create(); |
| 43 | + source.subscribe(ts1); |
| 44 | + |
| 45 | + ts1.assertCompleted(); |
| 46 | + ts1.assertNoErrors(); |
| 47 | + ts1.assertValue(1); |
| 48 | + |
| 49 | + Assert.assertEquals(1, run.get()); |
| 50 | + |
| 51 | + TestSubscriber<Integer> ts2 = TestSubscriber.create(); |
| 52 | + source.subscribe(ts2); |
| 53 | + |
| 54 | + ts2.assertNotCompleted(); |
| 55 | + ts2.assertNoErrors(); |
| 56 | + ts2.assertNoValues(); |
| 57 | + |
| 58 | + Assert.assertEquals(1, run.get()); |
| 59 | + } |
| 60 | + @Test |
| 61 | + public void testAutoConnect0() { |
| 62 | + final AtomicInteger run = new AtomicInteger(); |
| 63 | + |
| 64 | + ConnectableObservable<Integer> co = Observable.defer(new Func0<Observable<Integer>>() { |
| 65 | + @Override |
| 66 | + public Observable<Integer> call() { |
| 67 | + return Observable.just(run.incrementAndGet()); |
| 68 | + } |
| 69 | + }).publish(); |
| 70 | + |
| 71 | + Observable<Integer> source = co.autoConnect(0); |
| 72 | + |
| 73 | + Assert.assertEquals(1, run.get()); |
| 74 | + |
| 75 | + TestSubscriber<Integer> ts1 = TestSubscriber.create(); |
| 76 | + source.subscribe(ts1); |
| 77 | + |
| 78 | + ts1.assertNotCompleted(); |
| 79 | + ts1.assertNoErrors(); |
| 80 | + ts1.assertNoValues(); |
| 81 | + |
| 82 | + Assert.assertEquals(1, run.get()); |
| 83 | + |
| 84 | + TestSubscriber<Integer> ts2 = TestSubscriber.create(); |
| 85 | + source.subscribe(ts2); |
| 86 | + |
| 87 | + ts2.assertNotCompleted(); |
| 88 | + ts2.assertNoErrors(); |
| 89 | + ts2.assertNoValues(); |
| 90 | + |
| 91 | + Assert.assertEquals(1, run.get()); |
| 92 | + } |
| 93 | + @Test |
| 94 | + public void testAutoConnect2() { |
| 95 | + final AtomicInteger run = new AtomicInteger(); |
| 96 | + |
| 97 | + ConnectableObservable<Integer> co = Observable.defer(new Func0<Observable<Integer>>() { |
| 98 | + @Override |
| 99 | + public Observable<Integer> call() { |
| 100 | + return Observable.just(run.incrementAndGet()); |
| 101 | + } |
| 102 | + }).publish(); |
| 103 | + |
| 104 | + Observable<Integer> source = co.autoConnect(2); |
| 105 | + |
| 106 | + Assert.assertEquals(0, run.get()); |
| 107 | + |
| 108 | + TestSubscriber<Integer> ts1 = TestSubscriber.create(); |
| 109 | + source.subscribe(ts1); |
| 110 | + |
| 111 | + ts1.assertNotCompleted(); |
| 112 | + ts1.assertNoErrors(); |
| 113 | + ts1.assertNoValues(); |
| 114 | + |
| 115 | + Assert.assertEquals(0, run.get()); |
| 116 | + |
| 117 | + TestSubscriber<Integer> ts2 = TestSubscriber.create(); |
| 118 | + source.subscribe(ts2); |
| 119 | + |
| 120 | + Assert.assertEquals(1, run.get()); |
| 121 | + |
| 122 | + ts1.assertCompleted(); |
| 123 | + ts1.assertNoErrors(); |
| 124 | + ts1.assertValue(1); |
| 125 | + |
| 126 | + ts2.assertCompleted(); |
| 127 | + ts2.assertNoErrors(); |
| 128 | + ts2.assertValue(1); |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testAutoConnectUnsubscribe() { |
| 134 | + final AtomicInteger run = new AtomicInteger(); |
| 135 | + |
| 136 | + ConnectableObservable<Integer> co = Observable.defer(new Func0<Observable<Integer>>() { |
| 137 | + @Override |
| 138 | + public Observable<Integer> call() { |
| 139 | + return Observable.range(run.incrementAndGet(), 10); |
| 140 | + } |
| 141 | + }).publish(); |
| 142 | + |
| 143 | + final AtomicReference<Subscription> conn = new AtomicReference<Subscription>(); |
| 144 | + |
| 145 | + Observable<Integer> source = co.autoConnect(1, new Action1<Subscription>() { |
| 146 | + @Override |
| 147 | + public void call(Subscription t) { |
| 148 | + conn.set(t); |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + Assert.assertEquals(0, run.get()); |
| 153 | + |
| 154 | + TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 155 | + @Override |
| 156 | + public void onNext(Integer t) { |
| 157 | + super.onNext(t); |
| 158 | + Subscription s = conn.get(); |
| 159 | + if (s != null) { |
| 160 | + s.unsubscribe(); |
| 161 | + } else { |
| 162 | + onError(new NullPointerException("No connection reference")); |
| 163 | + } |
| 164 | + } |
| 165 | + }; |
| 166 | + |
| 167 | + source.subscribe(ts); |
| 168 | + |
| 169 | + ts.assertNotCompleted(); |
| 170 | + ts.assertNoErrors(); |
| 171 | + ts.assertValue(1); |
| 172 | + |
| 173 | + Assert.assertTrue("Connection not unsubscribed?", conn.get().isUnsubscribed()); |
| 174 | + } |
| 175 | +} |
0 commit comments