Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleUsingTest.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.single;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.disposables.*;
24 import io.reactivex.rxjava3.exceptions.*;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.internal.functions.Functions;
27 import io.reactivex.rxjava3.observers.TestObserver;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.processors.PublishProcessor;
30 import io.reactivex.rxjava3.testsupport.*;
31
32 public class SingleUsingTest extends RxJavaTest {
33
34 Function<Disposable, Single<Integer>> mapper = new Function<Disposable, Single<Integer>>() {
35 @Override
36 public Single<Integer> apply(Disposable d) throws Exception {
37 return Single.just(1);
38 }
39 };
40
41 Function<Disposable, Single<Integer>> mapperThrows = new Function<Disposable, Single<Integer>>() {
42 @Override
43 public Single<Integer> apply(Disposable d) throws Exception {
44 throw new TestException("Mapper");
45 }
46 };
47
48 Consumer<Disposable> disposer = new Consumer<Disposable>() {
49 @Override
50 public void accept(Disposable d) throws Exception {
51 d.dispose();
52 }
53 };
54
55 Consumer<Disposable> disposerThrows = new Consumer<Disposable>() {
56 @Override
57 public void accept(Disposable d) throws Exception {
58 throw new TestException("Disposer");
59 }
60 };
61
62 @Test
63 public void resourceSupplierThrows() {
64 Single.using(new Supplier<Integer>() {
65 @Override
66 public Integer get() throws Exception {
67 throw new TestException();
68 }
69 }, Functions.justFunction(Single.just(1)), Functions.emptyConsumer())
70 .test()
71 .assertFailure(TestException.class);
72 }
73
74 @Test
75 public void normalEager() {
76 Single.using(Functions.justSupplier(1), Functions.justFunction(Single.just(1)), Functions.emptyConsumer())
77 .test()
78 .assertResult(1);
79 }
80
81 @Test
82 public void normalNonEager() {
83 Single.using(Functions.justSupplier(1), Functions.justFunction(Single.just(1)), Functions.emptyConsumer(), false)
84 .test()
85 .assertResult(1);
86 }
87
88 @Test
89 public void errorEager() {
90 Single.using(Functions.justSupplier(1), Functions.justFunction(Single.error(new TestException())), Functions.emptyConsumer())
91 .test()
92 .assertFailure(TestException.class);
93 }
94
95 @Test
96 public void errorNonEager() {
97 Single.using(Functions.justSupplier(1), Functions.justFunction(Single.error(new TestException())), Functions.emptyConsumer(), false)
98 .test()
99 .assertFailure(TestException.class);
100 }
101
102 @Test
103 public void eagerMapperThrowsDisposerThrows() {
104 TestObserverEx<Integer> to = Single.using(Functions.justSupplier(Disposable.empty()), mapperThrows, disposerThrows)
105 .to(TestHelper.<Integer>testConsumer())
106 .assertFailure(CompositeException.class);
107
108 List<Throwable> ce = TestHelper.compositeList(to.errors().get(0));
109 TestHelper.assertError(ce, 0, TestException.class, "Mapper");
110 TestHelper.assertError(ce, 1, TestException.class, "Disposer");
111 }
112
113 @Test
114 public void noneagerMapperThrowsDisposerThrows() {
115
116 List<Throwable> errors = TestHelper.trackPluginErrors();
117
118 try {
119 Single.using(Functions.justSupplier(Disposable.empty()), mapperThrows, disposerThrows, false)
120 .to(TestHelper.<Integer>testConsumer())
121 .assertFailureAndMessage(TestException.class, "Mapper");
122
123 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Disposer");
124 } finally {
125 RxJavaPlugins.reset();
126 }
127 }
128
129 @Test
130 public void resourceDisposedIfMapperCrashes() {
131 Disposable d = Disposable.empty();
132
133 Single.using(Functions.justSupplier(d), mapperThrows, disposer)
134 .test()
135 .assertFailure(TestException.class);
136
137 assertTrue(d.isDisposed());
138 }
139
140 @Test
141 public void resourceDisposedIfMapperCrashesNonEager() {
142 Disposable d = Disposable.empty();
143
144 Single.using(Functions.justSupplier(d), mapperThrows, disposer, false)
145 .test()
146 .assertFailure(TestException.class);
147
148 assertTrue(d.isDisposed());
149 }
150
151 @Test
152 public void dispose() {
153 Disposable d = Disposable.empty();
154
155 Single.using(Functions.justSupplier(d), mapper, disposer, false)
156 .test(true);
157
158 assertTrue(d.isDisposed());
159 }
160
161 @Test
162 public void disposerThrowsEager() {
163 Single.using(Functions.justSupplier(Disposable.empty()), mapper, disposerThrows)
164 .test()
165 .assertFailure(TestException.class);
166 }
167
168 @Test
169 public void disposerThrowsNonEager() {
170
171 List<Throwable> errors = TestHelper.trackPluginErrors();
172
173 try {
174 Single.using(Functions.justSupplier(Disposable.empty()), mapper, disposerThrows, false)
175 .test()
176 .assertResult(1);
177 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Disposer");
178 } finally {
179 RxJavaPlugins.reset();
180 }
181 }
182
183 @Test
184 public void errorAndDisposerThrowsEager() {
185 TestObserverEx<Integer> to = Single.using(Functions.justSupplier(Disposable.empty()),
186 new Function<Disposable, SingleSource<Integer>>() {
187 @Override
188 public SingleSource<Integer> apply(Disposable v) throws Exception {
189 return Single.<Integer>error(new TestException("Mapper-run"));
190 }
191 }, disposerThrows)
192 .to(TestHelper.<Integer>testConsumer())
193 .assertFailure(CompositeException.class);
194
195 List<Throwable> ce = TestHelper.compositeList(to.errors().get(0));
196 TestHelper.assertError(ce, 0, TestException.class, "Mapper-run");
197 TestHelper.assertError(ce, 1, TestException.class, "Disposer");
198 }
199
200 @Test
201 public void errorAndDisposerThrowsNonEager() {
202 List<Throwable> errors = TestHelper.trackPluginErrors();
203
204 try {
205 Single.using(Functions.justSupplier(Disposable.empty()),
206 new Function<Disposable, SingleSource<Integer>>() {
207 @Override
208 public SingleSource<Integer> apply(Disposable v) throws Exception {
209 return Single.<Integer>error(new TestException("Mapper-run"));
210 }
211 }, disposerThrows, false)
212 .test()
213 .assertFailure(TestException.class);
214 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Disposer");
215 } finally {
216 RxJavaPlugins.reset();
217 }
218 }
219
220 @Test
221 public void successDisposeRace() {
222 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
223 final PublishProcessor<Integer> pp = PublishProcessor.create();
224
225 Disposable d = Disposable.empty();
226
227 final TestObserver<Integer> to = Single.using(Functions.justSupplier(d), new Function<Disposable, SingleSource<Integer>>() {
228 @Override
229 public SingleSource<Integer> apply(Disposable v) throws Exception {
230 return pp.single(-99);
231 }
232 }, disposer)
233 .test();
234
235 pp.onNext(1);
236
237 Runnable r1 = new Runnable() {
238 @Override
239 public void run() {
240 pp.onComplete();
241 }
242 };
243 Runnable r2 = new Runnable() {
244 @Override
245 public void run() {
246 to.dispose();
247 }
248 };
249
250 TestHelper.race(r1, r2);
251
252 assertTrue(d.isDisposed());
253 }
254 }
255
256 @Test
257 public void doubleOnSubscribe() {
258 List<Throwable> errors = TestHelper.trackPluginErrors();
259
260 try {
261 Single.using(Functions.justSupplier(1), new Function<Integer, SingleSource<Integer>>() {
262 @Override
263 public SingleSource<Integer> apply(Integer v) throws Exception {
264 return new Single<Integer>() {
265 @Override
266 protected void subscribeActual(SingleObserver<? super Integer> observer) {
267 observer.onSubscribe(Disposable.empty());
268
269 assertFalse(((Disposable)observer).isDisposed());
270
271 Disposable d = Disposable.empty();
272 observer.onSubscribe(d);
273
274 assertTrue(d.isDisposed());
275
276 assertFalse(((Disposable)observer).isDisposed());
277
278 observer.onSuccess(1);
279
280 assertTrue(((Disposable)observer).isDisposed());
281 }
282 };
283 }
284 }, Functions.emptyConsumer())
285 .test()
286 .assertResult(1)
287 ;
288
289 TestHelper.assertError(errors, 0, IllegalStateException.class, "Disposable already set!");
290 } finally {
291 RxJavaPlugins.reset();
292 }
293 }
294
295 @Test
296 @SuppressUndeliverable
297 public void errorDisposeRace() {
298 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
299 final PublishProcessor<Integer> pp = PublishProcessor.create();
300
301 Disposable d = Disposable.empty();
302
303 final TestObserver<Integer> to = Single.using(Functions.justSupplier(d), new Function<Disposable, SingleSource<Integer>>() {
304 @Override
305 public SingleSource<Integer> apply(Disposable v) throws Exception {
306 return pp.single(-99);
307 }
308 }, disposer)
309 .test();
310
311 final TestException ex = new TestException();
312
313 Runnable r1 = new Runnable() {
314 @Override
315 public void run() {
316 pp.onError(ex);
317 }
318 };
319 Runnable r2 = new Runnable() {
320 @Override
321 public void run() {
322 to.dispose();
323 }
324 };
325
326 TestHelper.race(r1, r2);
327
328 assertTrue(d.isDisposed());
329 }
330 }
331
332 @Test
333 public void eagerDisposeResourceThenDisposeUpstream() {
334 final StringBuilder sb = new StringBuilder();
335
336 TestObserver<Integer> to = Single.using(Functions.justSupplier(1),
337 new Function<Integer, Single<Integer>>() {
338 @Override
339 public Single<Integer> apply(Integer t) throws Throwable {
340 return Single.<Integer>never()
341 .doOnDispose(new Action() {
342 @Override
343 public void run() throws Throwable {
344 sb.append("Dispose");
345 }
346 })
347 ;
348 }
349 }, new Consumer<Integer>() {
350 @Override
351 public void accept(Integer t) throws Throwable {
352 sb.append("Resource");
353 }
354 }, true)
355 .test()
356 ;
357 to.assertEmpty();
358
359 to.dispose();
360
361 assertEquals("ResourceDispose", sb.toString());
362 }
363
364 @Test
365 public void nonEagerDisposeUpstreamThenDisposeResource() {
366 final StringBuilder sb = new StringBuilder();
367
368 TestObserver<Integer> to = Single.using(Functions.justSupplier(1),
369 new Function<Integer, Single<Integer>>() {
370 @Override
371 public Single<Integer> apply(Integer t) throws Throwable {
372 return Single.<Integer>never()
373 .doOnDispose(new Action() {
374 @Override
375 public void run() throws Throwable {
376 sb.append("Dispose");
377 }
378 })
379 ;
380 }
381 }, new Consumer<Integer>() {
382 @Override
383 public void accept(Integer t) throws Throwable {
384 sb.append("Resource");
385 }
386 }, false)
387 .test()
388 ;
389 to.assertEmpty();
390
391 to.dispose();
392
393 assertEquals("DisposeResource", sb.toString());
394 }
395 }