Debian Codegraph @

src/test/java/io/reactivex/rxjava3/subjects/CompletableSubjectTest.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.subjects;
15
16 import static org.junit.Assert.*;
17
18 import java.io.IOException;
19 import java.util.List;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.disposables.*;
25 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
26 import io.reactivex.rxjava3.observers.TestObserver;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.testsupport.TestHelper;
29
30 public class CompletableSubjectTest extends RxJavaTest {
31
32 @Test
33 public void once() {
34 CompletableSubject cs = CompletableSubject.create();
35
36 TestObserver<Void> to = cs.test();
37
38 cs.onComplete();
39
40 List<Throwable> errors = TestHelper.trackPluginErrors();
41 try {
42 cs.onError(new IOException());
43
44 TestHelper.assertUndeliverable(errors, 0, IOException.class);
45 } finally {
46 RxJavaPlugins.reset();
47 }
48 cs.onComplete();
49
50 to.assertResult();
51 }
52
53 @Test
54 public void error() {
55 CompletableSubject cs = CompletableSubject.create();
56
57 assertFalse(cs.hasComplete());
58 assertFalse(cs.hasThrowable());
59 assertNull(cs.getThrowable());
60 assertFalse(cs.hasObservers());
61 assertEquals(0, cs.observerCount());
62
63 TestObserver<Void> to = cs.test();
64
65 to.assertEmpty();
66
67 assertTrue(cs.hasObservers());
68 assertEquals(1, cs.observerCount());
69
70 cs.onError(new IOException());
71
72 assertFalse(cs.hasComplete());
73 assertTrue(cs.hasThrowable());
74 assertTrue(cs.getThrowable().toString(), cs.getThrowable() instanceof IOException);
75 assertFalse(cs.hasObservers());
76 assertEquals(0, cs.observerCount());
77
78 to.assertFailure(IOException.class);
79
80 cs.test().assertFailure(IOException.class);
81
82 assertFalse(cs.hasComplete());
83 assertTrue(cs.hasThrowable());
84 assertTrue(cs.getThrowable().toString(), cs.getThrowable() instanceof IOException);
85 assertFalse(cs.hasObservers());
86 assertEquals(0, cs.observerCount());
87 }
88
89 @Test
90 public void complete() {
91 CompletableSubject cs = CompletableSubject.create();
92
93 assertFalse(cs.hasComplete());
94 assertFalse(cs.hasThrowable());
95 assertNull(cs.getThrowable());
96 assertFalse(cs.hasObservers());
97 assertEquals(0, cs.observerCount());
98
99 TestObserver<Void> to = cs.test();
100
101 to.assertEmpty();
102
103 assertTrue(cs.hasObservers());
104 assertEquals(1, cs.observerCount());
105
106 cs.onComplete();
107
108 assertTrue(cs.hasComplete());
109 assertFalse(cs.hasThrowable());
110 assertNull(cs.getThrowable());
111 assertFalse(cs.hasObservers());
112 assertEquals(0, cs.observerCount());
113
114 to.assertResult();
115
116 cs.test().assertResult();
117
118 assertTrue(cs.hasComplete());
119 assertFalse(cs.hasThrowable());
120 assertNull(cs.getThrowable());
121 assertFalse(cs.hasObservers());
122 assertEquals(0, cs.observerCount());
123 }
124
125 @Test
126 public void nullThrowable() {
127 CompletableSubject cs = CompletableSubject.create();
128
129 try {
130 cs.onError(null);
131 fail("No NullPointerException thrown");
132 } catch (NullPointerException ex) {
133 assertEquals(ExceptionHelper.nullWarning("onError called with a null Throwable."), ex.getMessage());
134 }
135
136 cs.test().assertEmpty().dispose();
137 }
138
139 @Test
140 public void cancelOnArrival() {
141 CompletableSubject.create()
142 .test(true)
143 .assertEmpty();
144 }
145
146 @Test
147 public void cancelOnArrival2() {
148 CompletableSubject cs = CompletableSubject.create();
149
150 cs.test();
151
152 cs
153 .test(true)
154 .assertEmpty();
155 }
156
157 @Test
158 public void dispose() {
159 TestHelper.checkDisposed(CompletableSubject.create());
160 }
161
162 @Test
163 public void disposeTwice() {
164 CompletableSubject.create()
165 .subscribe(new CompletableObserver() {
166 @Override
167 public void onSubscribe(Disposable d) {
168 assertFalse(d.isDisposed());
169
170 d.dispose();
171 d.dispose();
172
173 assertTrue(d.isDisposed());
174 }
175
176 @Override
177 public void onError(Throwable e) {
178
179 }
180
181 @Override
182 public void onComplete() {
183
184 }
185 });
186 }
187
188 @Test
189 public void onSubscribeDispose() {
190 CompletableSubject cs = CompletableSubject.create();
191
192 Disposable d = Disposable.empty();
193
194 cs.onSubscribe(d);
195
196 assertFalse(d.isDisposed());
197
198 cs.onComplete();
199
200 d = Disposable.empty();
201
202 cs.onSubscribe(d);
203
204 assertTrue(d.isDisposed());
205 }
206
207 @Test
208 public void addRemoveRace() {
209 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
210 final CompletableSubject cs = CompletableSubject.create();
211
212 final TestObserver<Void> to = cs.test();
213
214 Runnable r1 = new Runnable() {
215 @Override
216 public void run() {
217 cs.test();
218 }
219 };
220
221 Runnable r2 = new Runnable() {
222 @Override
223 public void run() {
224 to.dispose();
225 }
226 };
227 TestHelper.race(r1, r2);
228 }
229 }
230 }