Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableAmbTest.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.completable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19 import java.util.concurrent.*;
20 import java.util.concurrent.atomic.AtomicBoolean;
21
22 import org.junit.Test;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.disposables.*;
26 import io.reactivex.rxjava3.exceptions.TestException;
27 import io.reactivex.rxjava3.functions.*;
28 import io.reactivex.rxjava3.internal.functions.Functions;
29 import io.reactivex.rxjava3.internal.operators.completable.CompletableAmb.Amb;
30 import io.reactivex.rxjava3.observers.TestObserver;
31 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
32 import io.reactivex.rxjava3.processors.PublishProcessor;
33 import io.reactivex.rxjava3.schedulers.Schedulers;
34 import io.reactivex.rxjava3.subjects.*;
35 import io.reactivex.rxjava3.testsupport.TestHelper;
36
37 public class CompletableAmbTest extends RxJavaTest {
38
39 @Test
40 public void ambLots() {
41 List<Completable> ms = new ArrayList<>();
42
43 for (int i = 0; i < 32; i++) {
44 ms.add(Completable.never());
45 }
46
47 ms.add(Completable.complete());
48
49 Completable.amb(ms)
50 .test()
51 .assertResult();
52 }
53
54 @Test
55 public void ambFirstDone() {
56 Completable.amb(Arrays.asList(Completable.complete(), Completable.complete()))
57 .test()
58 .assertResult();
59 }
60
61 @Test
62 public void dispose() {
63 PublishProcessor<Integer> pp1 = PublishProcessor.create();
64 PublishProcessor<Integer> pp2 = PublishProcessor.create();
65
66 TestObserver<Void> to = Completable.amb(Arrays.asList(pp1.ignoreElements(), pp2.ignoreElements()))
67 .test();
68
69 assertTrue(pp1.hasSubscribers());
70 assertTrue(pp2.hasSubscribers());
71
72 to.dispose();
73
74 assertFalse(pp1.hasSubscribers());
75 assertFalse(pp2.hasSubscribers());
76 }
77
78 @Test
79 public void innerErrorRace() {
80 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
81 List<Throwable> errors = TestHelper.trackPluginErrors();
82 try {
83 final PublishProcessor<Integer> pp0 = PublishProcessor.create();
84 final PublishProcessor<Integer> pp1 = PublishProcessor.create();
85
86 final TestObserver<Void> to = Completable.amb(Arrays.asList(pp0.ignoreElements(), pp1.ignoreElements()))
87 .test();
88
89 final TestException ex = new TestException();
90
91 Runnable r1 = new Runnable() {
92 @Override
93 public void run() {
94 pp0.onError(ex);
95 }
96 };
97
98 Runnable r2 = new Runnable() {
99 @Override
100 public void run() {
101 pp1.onError(ex);
102 }
103 };
104
105 TestHelper.race(r1, r2);
106
107 to.assertFailure(TestException.class);
108
109 if (!errors.isEmpty()) {
110 TestHelper.assertUndeliverable(errors, 0, TestException.class);
111 }
112 } finally {
113 RxJavaPlugins.reset();
114 }
115 }
116 }
117
118 @Test
119 public void nullSourceSuccessRace() {
120 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
121 List<Throwable> errors = TestHelper.trackPluginErrors();
122
123 try {
124
125 final Subject<Integer> ps = ReplaySubject.create();
126 ps.onNext(1);
127
128 final Completable source = Completable.ambArray(ps.ignoreElements(), Completable.never(), Completable.never(), null);
129
130 Runnable r1 = new Runnable() {
131 @Override
132 public void run() {
133 source.test();
134 }
135 };
136
137 Runnable r2 = new Runnable() {
138 @Override
139 public void run() {
140 ps.onComplete();
141 }
142 };
143
144 TestHelper.race(r1, r2);
145
146 if (!errors.isEmpty()) {
147 TestHelper.assertError(errors, 0, NullPointerException.class);
148 }
149 } finally {
150 RxJavaPlugins.reset();
151 }
152 }
153 }
154
155 @Test
156 public void ambWithOrder() {
157 Completable error = Completable.error(new RuntimeException());
158 Completable.complete().ambWith(error).test().assertComplete();
159 }
160
161 @Test
162 public void ambIterableOrder() {
163 Completable error = Completable.error(new RuntimeException());
164 Completable.amb(Arrays.asList(Completable.complete(), error)).test().assertComplete();
165 }
166
167 @Test
168 public void ambArrayOrder() {
169 Completable error = Completable.error(new RuntimeException());
170 Completable.ambArray(Completable.complete(), error).test().assertComplete();
171 }
172
173 @Test
174 public void ambRace() {
175 TestObserver<Void> to = new TestObserver<>();
176 to.onSubscribe(Disposable.empty());
177
178 CompositeDisposable cd = new CompositeDisposable();
179 AtomicBoolean once = new AtomicBoolean();
180 Amb a = new Amb(once, cd, to);
181 a.onSubscribe(Disposable.empty());
182
183 a.onComplete();
184 a.onComplete();
185
186 List<Throwable> errors = TestHelper.trackPluginErrors();
187 try {
188 a.onError(new TestException());
189
190 TestHelper.assertUndeliverable(errors, 0, TestException.class);
191 } finally {
192 RxJavaPlugins.reset();
193 }
194 }
195
196 @Test
197 public void untilCompletableMainComplete() {
198 CompletableSubject main = CompletableSubject.create();
199 CompletableSubject other = CompletableSubject.create();
200
201 TestObserver<Void> to = main.ambWith(other).test();
202
203 assertTrue("Main no observers?", main.hasObservers());
204 assertTrue("Other no observers?", other.hasObservers());
205
206 main.onComplete();
207
208 assertFalse("Main has observers?", main.hasObservers());
209 assertFalse("Other has observers?", other.hasObservers());
210
211 to.assertResult();
212 }
213
214 @Test
215 public void untilCompletableMainError() {
216 CompletableSubject main = CompletableSubject.create();
217 CompletableSubject other = CompletableSubject.create();
218
219 TestObserver<Void> to = main.ambWith(other).test();
220
221 assertTrue("Main no observers?", main.hasObservers());
222 assertTrue("Other no observers?", other.hasObservers());
223
224 main.onError(new TestException());
225
226 assertFalse("Main has observers?", main.hasObservers());
227 assertFalse("Other has observers?", other.hasObservers());
228
229 to.assertFailure(TestException.class);
230 }
231
232 @Test
233 public void untilCompletableOtherOnComplete() {
234 CompletableSubject main = CompletableSubject.create();
235 CompletableSubject other = CompletableSubject.create();
236
237 TestObserver<Void> to = main.ambWith(other).test();
238
239 assertTrue("Main no observers?", main.hasObservers());
240 assertTrue("Other no observers?", other.hasObservers());
241
242 other.onComplete();
243
244 assertFalse("Main has observers?", main.hasObservers());
245 assertFalse("Other has observers?", other.hasObservers());
246
247 to.assertResult();
248 }
249
250 @Test
251 public void untilCompletableOtherError() {
252 CompletableSubject main = CompletableSubject.create();
253 CompletableSubject other = CompletableSubject.create();
254
255 TestObserver<Void> to = main.ambWith(other).test();
256
257 assertTrue("Main no observers?", main.hasObservers());
258 assertTrue("Other no observers?", other.hasObservers());
259
260 other.onError(new TestException());
261
262 assertFalse("Main has observers?", main.hasObservers());
263 assertFalse("Other has observers?", other.hasObservers());
264
265 to.assertFailure(TestException.class);
266 }
267
268 @Test
269 public void noWinnerErrorDispose() throws Exception {
270 final TestException ex = new TestException();
271 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
272 final AtomicBoolean interrupted = new AtomicBoolean();
273 final CountDownLatch cdl = new CountDownLatch(1);
274
275 Completable.ambArray(
276 Completable.error(ex)
277 .subscribeOn(Schedulers.single())
278 .observeOn(Schedulers.computation()),
279 Completable.never()
280 )
281 .subscribe(Functions.EMPTY_ACTION, new Consumer<Throwable>() {
282 @Override
283 public void accept(Throwable e) throws Exception {
284 interrupted.set(Thread.currentThread().isInterrupted());
285 cdl.countDown();
286 }
287 });
288
289 assertTrue(cdl.await(500, TimeUnit.SECONDS));
290 assertFalse("Interrupted!", interrupted.get());
291 }
292 }
293
294 @Test
295 public void noWinnerCompleteDispose() throws Exception {
296 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
297 final AtomicBoolean interrupted = new AtomicBoolean();
298 final CountDownLatch cdl = new CountDownLatch(1);
299
300 Completable.ambArray(
301 Completable.complete()
302 .subscribeOn(Schedulers.single())
303 .observeOn(Schedulers.computation()),
304 Completable.never()
305 )
306 .subscribe(new Action() {
307 @Override
308 public void run() throws Exception {
309 interrupted.set(Thread.currentThread().isInterrupted());
310 cdl.countDown();
311 }
312 });
313
314 assertTrue(cdl.await(500, TimeUnit.SECONDS));
315 assertFalse("Interrupted!", interrupted.get());
316 }
317 }
318
319 @Test
320 public void completableSourcesInIterable() {
321 CompletableSource source = new CompletableSource() {
322 @Override
323 public void subscribe(CompletableObserver observer) {
324 Completable.complete().subscribe(observer);
325 }
326 };
327
328 Completable.amb(Arrays.asList(source, source))
329 .test()
330 .assertResult();
331 }
332 }