Debian Codegraph @

src/test/java/io/reactivex/rxjava3/single/SingleSubscribeTest.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.single;
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.Disposable;
25 import io.reactivex.rxjava3.exceptions.*;
26 import io.reactivex.rxjava3.functions.*;
27 import io.reactivex.rxjava3.internal.functions.Functions;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.subjects.PublishSubject;
30 import io.reactivex.rxjava3.testsupport.TestHelper;
31
32 public class SingleSubscribeTest extends RxJavaTest {
33
34 @Test
35 public void consumer() {
36 final Integer[] value = { null };
37
38 Single.just(1).subscribe(new Consumer<Integer>() {
39 @Override
40 public void accept(Integer v) throws Exception {
41 value[0] = v;
42 }
43 });
44
45 assertEquals((Integer)1, value[0]);
46 }
47
48 @Test
49 public void biconsumer() {
50 final Object[] value = { null, null };
51
52 Single.just(1).subscribe(new BiConsumer<Integer, Throwable>() {
53 @Override
54 public void accept(Integer v, Throwable e) throws Exception {
55 value[0] = v;
56 value[1] = e;
57 }
58 });
59
60 assertEquals(1, value[0]);
61 assertNull(value[1]);
62 }
63
64 @Test
65 public void biconsumerError() {
66 final Object[] value = { null, null };
67
68 TestException ex = new TestException();
69
70 Single.error(ex).subscribe(new BiConsumer<Object, Throwable>() {
71 @Override
72 public void accept(Object v, Throwable e) throws Exception {
73 value[0] = v;
74 value[1] = e;
75 }
76 });
77
78 assertNull(value[0]);
79 assertEquals(ex, value[1]);
80 }
81
82 @Test
83 public void subscribeThrows() {
84 try {
85 new Single<Integer>() {
86 @Override
87 protected void subscribeActual(SingleObserver<? super Integer> observer) {
88 throw new IllegalArgumentException();
89 }
90 }.test();
91 } catch (NullPointerException ex) {
92 if (!(ex.getCause() instanceof IllegalArgumentException)) {
93 fail(ex.toString() + ": should have thrown NPE(IAE)");
94 }
95 }
96 }
97
98 @Test
99 public void biConsumerDispose() {
100 PublishSubject<Integer> ps = PublishSubject.create();
101
102 Disposable d = ps.single(-99).subscribe(new BiConsumer<Object, Object>() {
103 @Override
104 public void accept(Object t1, Object t2) throws Exception {
105
106 }
107 });
108
109 assertFalse(d.isDisposed());
110
111 d.dispose();
112
113 assertTrue(d.isDisposed());
114
115 assertFalse(ps.hasObservers());
116 }
117
118 @Test
119 public void consumerDispose() {
120 PublishSubject<Integer> ps = PublishSubject.create();
121
122 Disposable d = ps.single(-99).subscribe(Functions.<Integer>emptyConsumer());
123
124 assertFalse(d.isDisposed());
125
126 d.dispose();
127
128 assertTrue(d.isDisposed());
129
130 assertFalse(ps.hasObservers());
131 }
132
133 @Test
134 public void consumerSuccessThrows() {
135 List<Throwable> list = TestHelper.trackPluginErrors();
136
137 try {
138 Single.just(1).subscribe(new Consumer<Integer>() {
139 @Override
140 public void accept(Integer t) throws Exception {
141 throw new TestException();
142 }
143 });
144
145 TestHelper.assertUndeliverable(list, 0, TestException.class);
146 } finally {
147 RxJavaPlugins.reset();
148 }
149 }
150
151 @Test
152 public void consumerErrorThrows() {
153 List<Throwable> list = TestHelper.trackPluginErrors();
154
155 try {
156 Single.<Integer>error(new TestException("Outer failure")).subscribe(
157 Functions.<Integer>emptyConsumer(),
158 new Consumer<Throwable>() {
159 @Override
160 public void accept(Throwable t) throws Exception {
161 throw new TestException("Inner failure");
162 }
163 });
164
165 TestHelper.assertError(list, 0, CompositeException.class);
166 List<Throwable> cel = TestHelper.compositeList(list.get(0));
167 TestHelper.assertError(cel, 0, TestException.class, "Outer failure");
168 TestHelper.assertError(cel, 1, TestException.class, "Inner failure");
169 } finally {
170 RxJavaPlugins.reset();
171 }
172 }
173
174 @Test
175 public void biConsumerThrows() {
176 List<Throwable> list = TestHelper.trackPluginErrors();
177
178 try {
179 Single.just(1).subscribe(new BiConsumer<Integer, Throwable>() {
180 @Override
181 public void accept(Integer t, Throwable e) throws Exception {
182 throw new TestException();
183 }
184 });
185
186 TestHelper.assertUndeliverable(list, 0, TestException.class);
187 } finally {
188 RxJavaPlugins.reset();
189 }
190 }
191
192 @Test
193 public void biConsumerErrorThrows() {
194 List<Throwable> list = TestHelper.trackPluginErrors();
195
196 try {
197 Single.<Integer>error(new TestException("Outer failure")).subscribe(
198 new BiConsumer<Integer, Throwable>() {
199 @Override
200 public void accept(Integer a, Throwable t) throws Exception {
201 throw new TestException("Inner failure");
202 }
203 });
204
205 TestHelper.assertError(list, 0, CompositeException.class);
206 List<Throwable> cel = TestHelper.compositeList(list.get(0));
207 TestHelper.assertError(cel, 0, TestException.class, "Outer failure");
208 TestHelper.assertError(cel, 1, TestException.class, "Inner failure");
209 } finally {
210 RxJavaPlugins.reset();
211 }
212 }
213
214 @Test
215 public void methodTestNoCancel() {
216 PublishSubject<Integer> ps = PublishSubject.create();
217
218 ps.single(-99).test(false);
219
220 assertTrue(ps.hasObservers());
221 }
222
223 @Test
224 public void successIsDisposed() {
225 assertTrue(Single.just(1).subscribe().isDisposed());
226 }
227
228 @Test
229 public void errorIsDisposed() {
230 assertTrue(Single.error(new TestException()).subscribe(Functions.emptyConsumer(), Functions.emptyConsumer()).isDisposed());
231 }
232
233 @Test
234 public void biConsumerIsDisposedOnSuccess() {
235 final Object[] result = { null, null };
236
237 Disposable d = Single.just(1)
238 .subscribe(new BiConsumer<Integer, Throwable>() {
239 @Override
240 public void accept(Integer t1, Throwable t2) throws Exception {
241 result[0] = t1;
242 result[1] = t2;
243 }
244 });
245
246 assertTrue("Not disposed?!", d.isDisposed());
247 assertEquals(1, result[0]);
248 assertNull(result[1]);
249 }
250
251 @Test
252 public void biConsumerIsDisposedOnError() {
253 final Object[] result = { null, null };
254
255 Disposable d = Single.<Integer>error(new IOException())
256 .subscribe(new BiConsumer<Integer, Throwable>() {
257 @Override
258 public void accept(Integer t1, Throwable t2) throws Exception {
259 result[0] = t1;
260 result[1] = t2;
261 }
262 });
263
264 assertTrue("Not disposed?!", d.isDisposed());
265 assertNull(result[0]);
266 assertTrue("" + result[1], result[1] instanceof IOException);
267 }
268 }