Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableCreateTest.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.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.exceptions.TestException;
26 import io.reactivex.rxjava3.functions.Cancellable;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.testsupport.TestHelper;
29
30 public class CompletableCreateTest extends RxJavaTest {
31
32 @Test
33 public void basic() {
34 List<Throwable> errors = TestHelper.trackPluginErrors();
35 try {
36 final Disposable d = Disposable.empty();
37
38 Completable.create(new CompletableOnSubscribe() {
39 @Override
40 public void subscribe(CompletableEmitter e) throws Exception {
41 e.setDisposable(d);
42
43 e.onComplete();
44 e.onError(new TestException());
45 e.onComplete();
46 }
47 })
48 .test()
49 .assertResult();
50
51 assertTrue(d.isDisposed());
52
53 TestHelper.assertUndeliverable(errors, 0, TestException.class);
54 } finally {
55 RxJavaPlugins.reset();
56 }
57 }
58
59 @Test
60 public void basicWithCancellable() {
61 List<Throwable> errors = TestHelper.trackPluginErrors();
62 try {
63 final Disposable d1 = Disposable.empty();
64 final Disposable d2 = Disposable.empty();
65
66 Completable.create(new CompletableOnSubscribe() {
67 @Override
68 public void subscribe(CompletableEmitter e) throws Exception {
69 e.setDisposable(d1);
70 e.setCancellable(new Cancellable() {
71 @Override
72 public void cancel() throws Exception {
73 d2.dispose();
74 }
75 });
76
77 e.onComplete();
78 e.onError(new TestException());
79 e.onComplete();
80 }
81 })
82 .test()
83 .assertResult();
84
85 assertTrue(d1.isDisposed());
86 assertTrue(d2.isDisposed());
87
88 TestHelper.assertUndeliverable(errors, 0, TestException.class);
89 } finally {
90 RxJavaPlugins.reset();
91 }
92 }
93
94 @Test
95 public void basicWithError() {
96 List<Throwable> errors = TestHelper.trackPluginErrors();
97 try {
98 final Disposable d = Disposable.empty();
99
100 Completable.create(new CompletableOnSubscribe() {
101 @Override
102 public void subscribe(CompletableEmitter e) throws Exception {
103 e.setDisposable(d);
104
105 e.onError(new TestException());
106 e.onComplete();
107 e.onError(new TestException("second"));
108 }
109 })
110 .test()
111 .assertFailure(TestException.class);
112
113 assertTrue(d.isDisposed());
114
115 TestHelper.assertUndeliverable(errors, 0, TestException.class, "second");
116 } finally {
117 RxJavaPlugins.reset();
118 }
119 }
120
121 @Test
122 public void callbackThrows() {
123 Completable.create(new CompletableOnSubscribe() {
124 @Override
125 public void subscribe(CompletableEmitter e) throws Exception {
126 throw new TestException();
127 }
128 })
129 .test()
130 .assertFailure(TestException.class);
131 }
132
133 @Test
134 public void onErrorNull() {
135 Completable.create(new CompletableOnSubscribe() {
136 @Override
137 public void subscribe(CompletableEmitter e) throws Exception {
138 e.onError(null);
139 }
140 })
141 .test()
142 .assertFailure(NullPointerException.class);
143 }
144
145 @Test
146 public void dispose() {
147 TestHelper.checkDisposed(Completable.create(new CompletableOnSubscribe() {
148 @Override
149 public void subscribe(CompletableEmitter e) throws Exception {
150 e.onComplete();
151 }
152 }));
153 }
154
155 @Test
156 public void onErrorThrows() {
157 Completable.create(new CompletableOnSubscribe() {
158 @Override
159 public void subscribe(CompletableEmitter e) throws Exception {
160 Disposable d = Disposable.empty();
161 e.setDisposable(d);
162
163 try {
164 e.onError(new IOException());
165 fail("Should have thrown");
166 } catch (TestException ex) {
167 // expected
168 }
169
170 assertTrue(d.isDisposed());
171 assertTrue(e.isDisposed());
172 }
173 }).subscribe(new CompletableObserver() {
174
175 @Override
176 public void onSubscribe(Disposable d) {
177
178 }
179
180 @Override
181 public void onError(Throwable e) {
182 throw new TestException();
183 }
184
185 @Override
186 public void onComplete() {
187
188 }
189 });
190 }
191
192 @Test
193 public void onCompleteThrows() {
194 Completable.create(new CompletableOnSubscribe() {
195 @Override
196 public void subscribe(CompletableEmitter e) throws Exception {
197 Disposable d = Disposable.empty();
198 e.setDisposable(d);
199
200 try {
201 e.onComplete();
202 fail("Should have thrown");
203 } catch (TestException ex) {
204 // expected
205 }
206
207 assertTrue(d.isDisposed());
208 assertTrue(e.isDisposed());
209 }
210 }).subscribe(new CompletableObserver() {
211
212 @Override
213 public void onSubscribe(Disposable d) {
214
215 }
216
217 @Override
218 public void onError(Throwable e) {
219
220 }
221
222 @Override
223 public void onComplete() {
224 throw new TestException();
225 }
226 });
227 }
228
229 @Test
230 public void onErrorThrows2() {
231 Completable.create(new CompletableOnSubscribe() {
232 @Override
233 public void subscribe(CompletableEmitter e) throws Exception {
234 try {
235 e.onError(new IOException());
236 fail("Should have thrown");
237 } catch (TestException ex) {
238 // expected
239 }
240
241 assertTrue(e.isDisposed());
242 }
243 }).subscribe(new CompletableObserver() {
244
245 @Override
246 public void onSubscribe(Disposable d) {
247
248 }
249
250 @Override
251 public void onError(Throwable e) {
252 throw new TestException();
253 }
254
255 @Override
256 public void onComplete() {
257
258 }
259 });
260 }
261
262 @Test
263 public void onCompleteThrows2() {
264 Completable.create(new CompletableOnSubscribe() {
265 @Override
266 public void subscribe(CompletableEmitter e) throws Exception {
267 try {
268 e.onComplete();
269 fail("Should have thrown");
270 } catch (TestException ex) {
271 // expected
272 }
273
274 assertTrue(e.isDisposed());
275 }
276 }).subscribe(new CompletableObserver() {
277
278 @Override
279 public void onSubscribe(Disposable d) {
280
281 }
282
283 @Override
284 public void onError(Throwable e) {
285
286 }
287
288 @Override
289 public void onComplete() {
290 throw new TestException();
291 }
292 });
293 }
294
295 @Test
296 public void tryOnError() {
297 List<Throwable> errors = TestHelper.trackPluginErrors();
298 try {
299 final Boolean[] response = { null };
300 Completable.create(new CompletableOnSubscribe() {
301 @Override
302 public void subscribe(CompletableEmitter e) throws Exception {
303 e.onComplete();
304 response[0] = e.tryOnError(new TestException());
305 }
306 })
307 .test()
308 .assertResult();
309
310 assertFalse(response[0]);
311
312 assertTrue(errors.toString(), errors.isEmpty());
313 } finally {
314 RxJavaPlugins.reset();
315 }
316 }
317
318 @Test
319 public void emitterHasToString() {
320 Completable.create(new CompletableOnSubscribe() {
321 @Override
322 public void subscribe(CompletableEmitter emitter) throws Exception {
323 assertTrue(emitter.toString().contains(CompletableCreate.Emitter.class.getSimpleName()));
324 }
325 }).test().assertEmpty();
326 }
327 }