Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/observers/DeferredScalarObserverTest.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.observers;
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.observers.TestObserver;
26 import io.reactivex.rxjava3.operators.QueueDisposable;
27 import io.reactivex.rxjava3.operators.QueueFuseable;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.testsupport.*;
30
31 public class DeferredScalarObserverTest extends RxJavaTest {
32
33 static final class TakeFirst extends DeferredScalarObserver<Integer, Integer> {
34
35 private static final long serialVersionUID = -2793723002312330530L;
36
37 TakeFirst(Observer<? super Integer> downstream) {
38 super(downstream);
39 }
40
41 @Override
42 public void onNext(Integer value) {
43 upstream.dispose();
44 complete(value);
45 complete(value);
46 }
47
48 }
49
50 @Test
51 public void normal() {
52 List<Throwable> errors = TestHelper.trackPluginErrors();
53 try {
54 TestObserver<Integer> to = new TestObserver<>();
55
56 TakeFirst source = new TakeFirst(to);
57
58 source.onSubscribe(Disposable.empty());
59
60 Disposable d = Disposable.empty();
61 source.onSubscribe(d);
62
63 assertTrue(d.isDisposed());
64
65 source.onNext(1);
66
67 to.assertResult(1);
68
69 TestHelper.assertError(errors, 0, ProtocolViolationException.class);
70 } finally {
71 RxJavaPlugins.reset();
72 }
73 }
74
75 @Test
76 public void error() {
77 TestObserver<Integer> to = new TestObserver<>();
78
79 TakeFirst source = new TakeFirst(to);
80
81 source.onSubscribe(Disposable.empty());
82 source.onError(new TestException());
83
84 to.assertFailure(TestException.class);
85 }
86
87 @Test
88 public void complete() {
89 TestObserver<Integer> to = new TestObserver<>();
90
91 TakeFirst source = new TakeFirst(to);
92
93 source.onSubscribe(Disposable.empty());
94 source.onComplete();
95
96 to.assertResult();
97 }
98
99 @Test
100 public void dispose() {
101 TestObserver<Integer> to = new TestObserver<>();
102
103 TakeFirst source = new TakeFirst(to);
104
105 Disposable d = Disposable.empty();
106
107 source.onSubscribe(d);
108
109 assertFalse(d.isDisposed());
110
111 to.dispose();
112
113 assertTrue(d.isDisposed());
114
115 assertTrue(source.isDisposed());
116 }
117
118 @Test
119 public void fused() {
120 List<Throwable> errors = TestHelper.trackPluginErrors();
121 try {
122 TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ANY);
123
124 TakeFirst source = new TakeFirst(to);
125
126 Disposable d = Disposable.empty();
127
128 source.onSubscribe(d);
129
130 to.assertFuseable();
131 to.assertFusionMode(QueueFuseable.ASYNC);
132
133 source.onNext(1);
134 source.onNext(1);
135 source.onError(new TestException());
136 source.onComplete();
137
138 assertTrue(d.isDisposed());
139
140 to.assertResult(1);
141
142 TestHelper.assertUndeliverable(errors, 0, TestException.class);
143 } finally {
144 RxJavaPlugins.reset();
145 }
146 }
147
148 @Test
149 public void fusedReject() {
150 List<Throwable> errors = TestHelper.trackPluginErrors();
151 try {
152 TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.SYNC);
153
154 TakeFirst source = new TakeFirst(to);
155
156 Disposable d = Disposable.empty();
157
158 source.onSubscribe(d);
159
160 to.assertFuseable();
161 to.assertFusionMode(QueueFuseable.NONE);
162
163 source.onNext(1);
164 source.onNext(1);
165 source.onError(new TestException());
166 source.onComplete();
167
168 assertTrue(d.isDisposed());
169
170 to.assertResult(1);
171
172 TestHelper.assertUndeliverable(errors, 0, TestException.class);
173 } finally {
174 RxJavaPlugins.reset();
175 }
176 }
177
178 static final class TakeLast extends DeferredScalarObserver<Integer, Integer> {
179
180 private static final long serialVersionUID = -2793723002312330530L;
181
182 TakeLast(Observer<? super Integer> downstream) {
183 super(downstream);
184 }
185
186 @Override
187 public void onNext(Integer value) {
188 this.value = value;
189 }
190
191 }
192
193 @Test
194 public void nonfusedTerminateMore() {
195 List<Throwable> errors = TestHelper.trackPluginErrors();
196 try {
197 TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.NONE);
198
199 TakeLast source = new TakeLast(to);
200
201 Disposable d = Disposable.empty();
202
203 source.onSubscribe(d);
204
205 source.onNext(1);
206 source.onComplete();
207 source.onComplete();
208 source.onError(new TestException());
209
210 to.assertResult(1);
211
212 TestHelper.assertUndeliverable(errors, 0, TestException.class);
213 } finally {
214 RxJavaPlugins.reset();
215 }
216 }
217
218 @Test
219 public void nonfusedError() {
220 List<Throwable> errors = TestHelper.trackPluginErrors();
221 try {
222 TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.NONE);
223
224 TakeLast source = new TakeLast(to);
225
226 Disposable d = Disposable.empty();
227
228 source.onSubscribe(d);
229
230 source.onNext(1);
231 source.onError(new TestException());
232 source.onError(new TestException("second"));
233 source.onComplete();
234
235 to.assertFailure(TestException.class);
236
237 TestHelper.assertUndeliverable(errors, 0, TestException.class, "second");
238 } finally {
239 RxJavaPlugins.reset();
240 }
241 }
242
243 @Test
244 public void fusedTerminateMore() {
245 List<Throwable> errors = TestHelper.trackPluginErrors();
246 try {
247 TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ANY);
248
249 TakeLast source = new TakeLast(to);
250
251 Disposable d = Disposable.empty();
252
253 source.onSubscribe(d);
254
255 source.onNext(1);
256 source.onComplete();
257 source.onComplete();
258 source.onError(new TestException());
259
260 to.assertResult(1);
261
262 TestHelper.assertUndeliverable(errors, 0, TestException.class);
263 } finally {
264 RxJavaPlugins.reset();
265 }
266 }
267
268 @Test
269 public void fusedError() {
270 List<Throwable> errors = TestHelper.trackPluginErrors();
271 try {
272 TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ANY);
273
274 TakeLast source = new TakeLast(to);
275
276 Disposable d = Disposable.empty();
277
278 source.onSubscribe(d);
279
280 source.onNext(1);
281 source.onError(new TestException());
282 source.onError(new TestException("second"));
283 source.onComplete();
284
285 to.assertFailure(TestException.class);
286
287 TestHelper.assertUndeliverable(errors, 0, TestException.class, "second");
288 } finally {
289 RxJavaPlugins.reset();
290 }
291 }
292
293 @Test
294 public void disposed() {
295 TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.NONE);
296
297 TakeLast source = new TakeLast(to);
298
299 Disposable d = Disposable.empty();
300
301 source.onSubscribe(d);
302
303 to.dispose();
304
305 source.onNext(1);
306 source.onComplete();
307
308 to.assertNoValues().assertNoErrors().assertNotComplete();
309 }
310
311 @Test
312 public void disposedAfterOnNext() {
313 final TestObserver<Integer> to = new TestObserver<>();
314
315 TakeLast source = new TakeLast(new Observer<Integer>() {
316 Disposable upstream;
317
318 @Override
319 public void onSubscribe(Disposable d) {
320 this.upstream = d;
321 to.onSubscribe(d);
322 }
323
324 @Override
325 public void onNext(Integer value) {
326 to.onNext(value);
327 upstream.dispose();
328 }
329
330 @Override
331 public void onError(Throwable e) {
332 to.onError(e);
333 }
334
335 @Override
336 public void onComplete() {
337 to.onComplete();
338 }
339 });
340
341 source.onSubscribe(Disposable.empty());
342 source.onNext(1);
343 source.onComplete();
344
345 to.assertValue(1).assertNoErrors().assertNotComplete();
346 }
347
348 @Test
349 public void fusedEmpty() {
350 TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ANY);
351
352 TakeLast source = new TakeLast(to);
353
354 Disposable d = Disposable.empty();
355
356 source.onSubscribe(d);
357
358 source.onComplete();
359
360 to.assertResult();
361 }
362
363 @Test
364 public void nonfusedEmpty() {
365 TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.NONE);
366
367 TakeLast source = new TakeLast(to);
368
369 Disposable d = Disposable.empty();
370
371 source.onSubscribe(d);
372
373 source.onComplete();
374
375 to.assertResult();
376 }
377
378 @Test
379 public void customFusion() {
380 final TestObserver<Integer> to = new TestObserver<>();
381
382 TakeLast source = new TakeLast(new Observer<Integer>() {
383 QueueDisposable<Integer> d;
384
385 @SuppressWarnings("unchecked")
386 @Override
387 public void onSubscribe(Disposable d) {
388 this.d = (QueueDisposable<Integer>)d;
389 to.onSubscribe(d);
390 this.d.requestFusion(QueueFuseable.ANY);
391 }
392
393 @Override
394 public void onNext(Integer value) {
395 if (!d.isEmpty()) {
396 Integer v = null;
397 try {
398 to.onNext(d.poll());
399
400 v = d.poll();
401 } catch (Throwable ex) {
402 to.onError(ex);
403 }
404
405 assertNull(v);
406 assertTrue(d.isEmpty());
407 }
408 }
409
410 @Override
411 public void onError(Throwable e) {
412 to.onError(e);
413 }
414
415 @Override
416 public void onComplete() {
417 to.onComplete();
418 }
419 });
420
421 source.onSubscribe(Disposable.empty());
422 source.onNext(1);
423 source.onComplete();
424
425 to.assertResult(1);
426 }
427
428 @Test
429 public void customFusionClear() {
430 final TestObserver<Integer> to = new TestObserver<>();
431
432 TakeLast source = new TakeLast(new Observer<Integer>() {
433 QueueDisposable<Integer> d;
434
435 @SuppressWarnings("unchecked")
436 @Override
437 public void onSubscribe(Disposable d) {
438 this.d = (QueueDisposable<Integer>)d;
439 to.onSubscribe(d);
440 this.d.requestFusion(QueueFuseable.ANY);
441 }
442
443 @Override
444 public void onNext(Integer value) {
445 d.clear();
446 assertTrue(d.isEmpty());
447 }
448
449 @Override
450 public void onError(Throwable e) {
451 to.onError(e);
452 }
453
454 @Override
455 public void onComplete() {
456 to.onComplete();
457 }
458 });
459
460 source.onSubscribe(Disposable.empty());
461 source.onNext(1);
462 source.onComplete();
463
464 to.assertNoValues().assertNoErrors().assertComplete();
465 }
466
467 @Test
468 public void offerThrow() {
469 TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.NONE);
470
471 TakeLast source = new TakeLast(to);
472
473 TestHelper.assertNoOffer(source);
474 }
475
476 @Test
477 public void customFusionDontConsume() {
478 final TestObserver<Integer> to = new TestObserver<>();
479
480 TakeFirst source = new TakeFirst(new Observer<Integer>() {
481 QueueDisposable<Integer> d;
482
483 @SuppressWarnings("unchecked")
484 @Override
485 public void onSubscribe(Disposable d) {
486 this.d = (QueueDisposable<Integer>)d;
487 to.onSubscribe(d);
488 this.d.requestFusion(QueueFuseable.ANY);
489 }
490
491 @Override
492 public void onNext(Integer value) {
493 // not consuming
494 }
495
496 @Override
497 public void onError(Throwable e) {
498 to.onError(e);
499 }
500
501 @Override
502 public void onComplete() {
503 to.onComplete();
504 }
505 });
506
507 source.onSubscribe(Disposable.empty());
508 source.onNext(1);
509
510 to.assertNoValues().assertNoErrors().assertComplete();
511 }
512
513 }