Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/subscribers/StrictSubscriberTest.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.subscribers;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19
20 import org.junit.Test;
21 import org.reactivestreams.*;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
26 import io.reactivex.rxjava3.testsupport.TestSubscriberEx;
27
28 public class StrictSubscriberTest extends RxJavaTest {
29
30 @Test
31 public void strictMode() {
32 final List<Object> list = new ArrayList<>();
33 Subscriber<Object> sub = new Subscriber<Object>() {
34
35 @Override
36 public void onSubscribe(Subscription s) {
37 s.request(10);
38 }
39
40 @Override
41 public void onNext(Object t) {
42 list.add(t);
43 }
44
45 @Override
46 public void onError(Throwable t) {
47 list.add(t);
48 }
49
50 @Override
51 public void onComplete() {
52 list.add("Done");
53 }
54 };
55
56 new Flowable<Object>() {
57 @Override
58 protected void subscribeActual(Subscriber<? super Object> s) {
59 s.onSubscribe(new BooleanSubscription());
60 s.onNext(s);
61 }
62 }.subscribe(sub);
63
64 assertTrue(list.toString(), list.get(0) instanceof StrictSubscriber);
65 }
66
67 static final class SubscriberWrapper<T> implements Subscriber<T> {
68 final TestSubscriberEx<T> tester;
69
70 SubscriberWrapper(TestSubscriberEx<T> tester) {
71 this.tester = tester;
72 }
73
74 @Override
75 public void onSubscribe(Subscription s) {
76 tester.onSubscribe(s);
77 }
78
79 @Override
80 public void onNext(T t) {
81 tester.onNext(t);
82 }
83
84 @Override
85 public void onError(Throwable t) {
86 tester.onError(t);
87 }
88
89 @Override
90 public void onComplete() {
91 tester.onComplete();
92 }
93 }
94
95 @Test
96 public void normalOnNext() {
97 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
98 SubscriberWrapper<Integer> wrapper = new SubscriberWrapper<>(ts);
99
100 Flowable.range(1, 5).subscribe(wrapper);
101
102 ts.assertResult(1, 2, 3, 4, 5);
103 }
104
105 @Test
106 public void normalOnNextBackpressured() {
107 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0);
108 SubscriberWrapper<Integer> wrapper = new SubscriberWrapper<>(ts);
109
110 Flowable.range(1, 5).subscribe(wrapper);
111
112 ts.assertEmpty()
113 .requestMore(1)
114 .assertValue(1)
115 .requestMore(2)
116 .assertValues(1, 2, 3)
117 .requestMore(2)
118 .assertResult(1, 2, 3, 4, 5);
119 }
120
121 @Test
122 public void normalOnError() {
123 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
124 SubscriberWrapper<Integer> wrapper = new SubscriberWrapper<>(ts);
125
126 Flowable.range(1, 5).concatWith(Flowable.<Integer>error(new TestException()))
127 .subscribe(wrapper);
128
129 ts.assertFailure(TestException.class, 1, 2, 3, 4, 5);
130 }
131
132 @Test
133 public void deferredRequest() {
134 final List<Object> list = new ArrayList<>();
135 Subscriber<Object> sub = new Subscriber<Object>() {
136
137 @Override
138 public void onSubscribe(Subscription s) {
139 s.request(5);
140 list.add(0);
141 }
142
143 @Override
144 public void onNext(Object t) {
145 list.add(t);
146 }
147
148 @Override
149 public void onError(Throwable t) {
150 list.add(t);
151 }
152
153 @Override
154 public void onComplete() {
155 list.add("Done");
156 }
157 };
158
159 Flowable.range(1, 5).subscribe(sub);
160
161 assertEquals(Arrays.<Object>asList(0, 1, 2, 3, 4, 5, "Done"), list);
162 }
163
164 @Test
165 public void requestZero() {
166 final List<Object> list = new ArrayList<>();
167 Subscriber<Object> sub = new Subscriber<Object>() {
168
169 @Override
170 public void onSubscribe(Subscription s) {
171 s.request(0);
172 }
173
174 @Override
175 public void onNext(Object t) {
176 list.add(t);
177 }
178
179 @Override
180 public void onError(Throwable t) {
181 list.add(t);
182 }
183
184 @Override
185 public void onComplete() {
186 list.add("Done");
187 }
188 };
189
190 Flowable.range(1, 5).subscribe(sub);
191
192 assertTrue(list.toString(), list.get(0) instanceof IllegalArgumentException);
193 assertTrue(list.toString(), list.get(0).toString().contains("3.9"));
194 }
195
196 @Test
197 public void requestNegative() {
198 final List<Object> list = new ArrayList<>();
199 Subscriber<Object> sub = new Subscriber<Object>() {
200
201 @Override
202 public void onSubscribe(Subscription s) {
203 s.request(-99);
204 }
205
206 @Override
207 public void onNext(Object t) {
208 list.add(t);
209 }
210
211 @Override
212 public void onError(Throwable t) {
213 list.add(t);
214 }
215
216 @Override
217 public void onComplete() {
218 list.add("Done");
219 }
220 };
221
222 Flowable.range(1, 5).subscribe(sub);
223
224 assertTrue(list.toString(), list.get(0) instanceof IllegalArgumentException);
225 assertTrue(list.toString(), list.get(0).toString().contains("3.9"));
226 }
227
228 @Test
229 public void cancelAfterOnComplete() {
230 final List<Object> list = new ArrayList<>();
231 Subscriber<Object> sub = new Subscriber<Object>() {
232
233 Subscription upstream;
234 @Override
235 public void onSubscribe(Subscription s) {
236 this.upstream = s;
237 }
238
239 @Override
240 public void onNext(Object t) {
241 list.add(t);
242 }
243
244 @Override
245 public void onError(Throwable t) {
246 upstream.cancel();
247 list.add(t);
248 }
249
250 @Override
251 public void onComplete() {
252 upstream.cancel();
253 list.add("Done");
254 }
255 };
256
257 new Flowable<Object>() {
258 @Override
259 protected void subscribeActual(Subscriber<? super Object> s) {
260 BooleanSubscription b = new BooleanSubscription();
261 s.onSubscribe(b);
262 s.onComplete();
263 list.add(b.isCancelled());
264 }
265 }.subscribe(sub);
266
267 assertEquals(Arrays.<Object>asList("Done", false), list);
268 }
269
270 @Test
271 public void cancelAfterOnError() {
272 final List<Object> list = new ArrayList<>();
273 Subscriber<Object> sub = new Subscriber<Object>() {
274
275 Subscription upstream;
276 @Override
277 public void onSubscribe(Subscription s) {
278 this.upstream = s;
279 }
280
281 @Override
282 public void onNext(Object t) {
283 list.add(t);
284 }
285
286 @Override
287 public void onError(Throwable t) {
288 upstream.cancel();
289 list.add(t.getMessage());
290 }
291
292 @Override
293 public void onComplete() {
294 upstream.cancel();
295 list.add("Done");
296 }
297 };
298
299 new Flowable<Object>() {
300 @Override
301 protected void subscribeActual(Subscriber<? super Object> s) {
302 BooleanSubscription b = new BooleanSubscription();
303 s.onSubscribe(b);
304 s.onError(new TestException("Forced failure"));
305 list.add(b.isCancelled());
306 }
307 }.subscribe(sub);
308
309 assertEquals(Arrays.<Object>asList("Forced failure", false), list);
310 }
311
312 @Test
313 public void doubleOnSubscribe() {
314 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
315 SubscriberWrapper<Integer> wrapper = new SubscriberWrapper<>(ts);
316
317 new Flowable<Integer>() {
318 @Override
319 protected void subscribeActual(Subscriber<? super Integer> s) {
320 BooleanSubscription b1 = new BooleanSubscription();
321 s.onSubscribe(b1);
322
323 BooleanSubscription b2 = new BooleanSubscription();
324 s.onSubscribe(b2);
325
326 assertTrue(b1.isCancelled());
327 assertTrue(b2.isCancelled());
328 }
329 }.subscribe(wrapper);
330
331 ts.assertFailure(IllegalStateException.class);
332 assertTrue(ts.errors().toString(), ts.errors().get(0).getMessage().contains("2.12"));
333 }
334 }