Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableDelaySubscriptionOtherTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.observable;
15
16 import java.util.concurrent.*;
17 import java.util.concurrent.atomic.AtomicInteger;
18
19 import org.junit.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.disposables.Disposable;
23 import io.reactivex.rxjava3.exceptions.TestException;
24 import io.reactivex.rxjava3.functions.*;
25 import io.reactivex.rxjava3.observers.TestObserver;
26 import io.reactivex.rxjava3.schedulers.Schedulers;
27 import io.reactivex.rxjava3.subjects.PublishSubject;
28 import io.reactivex.rxjava3.testsupport.TestHelper;
29
30 public class ObservableDelaySubscriptionOtherTest extends RxJavaTest {
31 @Test
32 public void noPrematureSubscription() {
33 PublishSubject<Object> other = PublishSubject.create();
34
35 TestObserver<Integer> to = new TestObserver<>();
36
37 final AtomicInteger subscribed = new AtomicInteger();
38
39 Observable.just(1)
40 .doOnSubscribe(new Consumer<Disposable>() {
41 @Override
42 public void accept(Disposable d) {
43 subscribed.getAndIncrement();
44 }
45 })
46 .delaySubscription(other)
47 .subscribe(to);
48
49 to.assertNotComplete();
50 to.assertNoErrors();
51 to.assertNoValues();
52
53 Assert.assertEquals("Premature subscription", 0, subscribed.get());
54
55 other.onNext(1);
56
57 Assert.assertEquals("No subscription", 1, subscribed.get());
58
59 to.assertValue(1);
60 to.assertNoErrors();
61 to.assertComplete();
62 }
63
64 @Test
65 public void noMultipleSubscriptions() {
66 PublishSubject<Object> other = PublishSubject.create();
67
68 TestObserver<Integer> to = new TestObserver<>();
69
70 final AtomicInteger subscribed = new AtomicInteger();
71
72 Observable.just(1)
73 .doOnSubscribe(new Consumer<Disposable>() {
74 @Override
75 public void accept(Disposable d) {
76 subscribed.getAndIncrement();
77 }
78 })
79 .delaySubscription(other)
80 .subscribe(to);
81
82 to.assertNotComplete();
83 to.assertNoErrors();
84 to.assertNoValues();
85
86 Assert.assertEquals("Premature subscription", 0, subscribed.get());
87
88 other.onNext(1);
89 other.onNext(2);
90
91 Assert.assertEquals("No subscription", 1, subscribed.get());
92
93 to.assertValue(1);
94 to.assertNoErrors();
95 to.assertComplete();
96 }
97
98 @Test
99 public void completeTriggersSubscription() {
100 PublishSubject<Object> other = PublishSubject.create();
101
102 TestObserver<Integer> to = new TestObserver<>();
103
104 final AtomicInteger subscribed = new AtomicInteger();
105
106 Observable.just(1)
107 .doOnSubscribe(new Consumer<Disposable>() {
108 @Override
109 public void accept(Disposable d) {
110 subscribed.getAndIncrement();
111 }
112 })
113 .delaySubscription(other)
114 .subscribe(to);
115
116 to.assertNotComplete();
117 to.assertNoErrors();
118 to.assertNoValues();
119
120 Assert.assertEquals("Premature subscription", 0, subscribed.get());
121
122 other.onComplete();
123
124 Assert.assertEquals("No subscription", 1, subscribed.get());
125
126 to.assertValue(1);
127 to.assertNoErrors();
128 to.assertComplete();
129 }
130
131 @Test
132 public void noPrematureSubscriptionToError() {
133 PublishSubject<Object> other = PublishSubject.create();
134
135 TestObserver<Integer> to = new TestObserver<>();
136
137 final AtomicInteger subscribed = new AtomicInteger();
138
139 Observable.<Integer>error(new TestException())
140 .doOnSubscribe(new Consumer<Disposable>() {
141 @Override
142 public void accept(Disposable d) {
143 subscribed.getAndIncrement();
144 }
145 })
146 .delaySubscription(other)
147 .subscribe(to);
148
149 to.assertNotComplete();
150 to.assertNoErrors();
151 to.assertNoValues();
152
153 Assert.assertEquals("Premature subscription", 0, subscribed.get());
154
155 other.onComplete();
156
157 Assert.assertEquals("No subscription", 1, subscribed.get());
158
159 to.assertNoValues();
160 to.assertNotComplete();
161 to.assertError(TestException.class);
162 }
163
164 @Test
165 public void noSubscriptionIfOtherErrors() {
166 PublishSubject<Object> other = PublishSubject.create();
167
168 TestObserver<Integer> to = new TestObserver<>();
169
170 final AtomicInteger subscribed = new AtomicInteger();
171
172 Observable.<Integer>error(new TestException())
173 .doOnSubscribe(new Consumer<Disposable>() {
174 @Override
175 public void accept(Disposable d) {
176 subscribed.getAndIncrement();
177 }
178 })
179 .delaySubscription(other)
180 .subscribe(to);
181
182 to.assertNotComplete();
183 to.assertNoErrors();
184 to.assertNoValues();
185
186 Assert.assertEquals("Premature subscription", 0, subscribed.get());
187
188 other.onError(new TestException());
189
190 Assert.assertEquals("Premature subscription", 0, subscribed.get());
191
192 to.assertNoValues();
193 to.assertNotComplete();
194 to.assertError(TestException.class);
195 }
196
197 @Test
198 public void badSourceOther() {
199 TestHelper.checkBadSourceObservable(new Function<Observable<Integer>, Object>() {
200 @Override
201 public Object apply(Observable<Integer> o) throws Exception {
202 return Observable.just(1).delaySubscription(o);
203 }
204 }, false, 1, 1, 1);
205 }
206
207 @Test
208 public void afterDelayNoInterrupt() {
209 ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
210 try {
211 for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec) }) {
212 final TestObserver<Boolean> observer = TestObserver.create();
213 observer.withTag(s.getClass().getSimpleName());
214
215 Observable.<Boolean>create(new ObservableOnSubscribe<Boolean>() {
216 @Override
217 public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
218 emitter.onNext(Thread.interrupted());
219 emitter.onComplete();
220 }
221 })
222 .delaySubscription(100, TimeUnit.MILLISECONDS, s)
223 .subscribe(observer);
224
225 observer.awaitDone(5, TimeUnit.SECONDS);
226 observer.assertValue(false);
227 }
228 } finally {
229 exec.shutdown();
230 }
231 }
232
233 }