Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableSwitchOnNextTest.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 import static org.mockito.Mockito.*;
18
19 import org.junit.Test;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.exceptions.TestException;
23 import io.reactivex.rxjava3.observers.TestObserver;
24 import io.reactivex.rxjava3.processors.PublishProcessor;
25 import io.reactivex.rxjava3.subjects.CompletableSubject;
26
27 public class CompletableSwitchOnNextTest extends RxJavaTest {
28
29 @Test
30 public void normal() {
31 Runnable run = mock(Runnable.class);
32
33 Completable.switchOnNext(
34 Flowable.range(1, 10)
35 .map(v -> {
36 if (v % 2 == 0) {
37 return Completable.fromRunnable(run);
38 }
39 return Completable.complete();
40 })
41 )
42 .test()
43 .assertResult();
44
45 verify(run, times(5)).run();
46 }
47
48 @Test
49 public void normalDelayError() {
50 Runnable run = mock(Runnable.class);
51
52 Completable.switchOnNextDelayError(
53 Flowable.range(1, 10)
54 .map(v -> {
55 if (v % 2 == 0) {
56 return Completable.fromRunnable(run);
57 }
58 return Completable.complete();
59 })
60 )
61 .test()
62 .assertResult();
63
64 verify(run, times(5)).run();
65 }
66
67 @Test
68 public void noDelaySwitch() {
69 PublishProcessor<Completable> pp = PublishProcessor.create();
70
71 TestObserver<Void> to = Completable.switchOnNext(pp).test();
72
73 assertTrue(pp.hasSubscribers());
74
75 to.assertEmpty();
76
77 CompletableSubject cs1 = CompletableSubject.create();
78 CompletableSubject cs2 = CompletableSubject.create();
79
80 pp.onNext(cs1);
81
82 assertTrue(cs1.hasObservers());
83
84 pp.onNext(cs2);
85
86 assertFalse(cs1.hasObservers());
87 assertTrue(cs2.hasObservers());
88
89 pp.onComplete();
90
91 assertTrue(cs2.hasObservers());
92
93 cs2.onComplete();
94
95 to.assertResult();
96 }
97
98 @Test
99 public void delaySwitch() {
100 PublishProcessor<Completable> pp = PublishProcessor.create();
101
102 TestObserver<Void> to = Completable.switchOnNextDelayError(pp).test();
103
104 assertTrue(pp.hasSubscribers());
105
106 to.assertEmpty();
107
108 CompletableSubject cs1 = CompletableSubject.create();
109 CompletableSubject cs2 = CompletableSubject.create();
110
111 pp.onNext(cs1);
112
113 assertTrue(cs1.hasObservers());
114
115 pp.onNext(cs2);
116
117 assertFalse(cs1.hasObservers());
118 assertTrue(cs2.hasObservers());
119
120 assertTrue(cs2.hasObservers());
121
122 cs2.onError(new TestException());
123
124 assertTrue(pp.hasSubscribers());
125
126 to.assertEmpty();
127
128 pp.onComplete();
129
130 to.assertFailure(TestException.class);
131 }
132 }