Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDematerializeTest.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.flowable;
15
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18
19 import java.util.List;
20
21 import org.junit.Test;
22 import org.reactivestreams.Subscriber;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.exceptions.TestException;
26 import io.reactivex.rxjava3.functions.Function;
27 import io.reactivex.rxjava3.internal.functions.Functions;
28 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30 import io.reactivex.rxjava3.testsupport.*;
31
32 public class FlowableDematerializeTest extends RxJavaTest {
33
34 @Test
35 public void simpleSelector() {
36 Flowable<Notification<Integer>> notifications = Flowable.just(1, 2).materialize();
37 Flowable<Integer> dematerialize = notifications.dematerialize(Functions.<Notification<Integer>>identity());
38
39 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
40
41 dematerialize.subscribe(subscriber);
42
43 verify(subscriber, times(1)).onNext(1);
44 verify(subscriber, times(1)).onNext(2);
45 verify(subscriber, times(1)).onComplete();
46 verify(subscriber, never()).onError(any(Throwable.class));
47 }
48
49 @Test
50 public void selectorCrash() {
51 Flowable.just(1, 2)
52 .materialize()
53 .dematerialize(new Function<Notification<Integer>, Notification<Object>>() {
54 @Override
55 public Notification<Object> apply(Notification<Integer> v) throws Exception {
56 throw new TestException();
57 }
58 })
59 .test()
60 .assertFailure(TestException.class);
61 }
62
63 @Test
64 public void selectorNull() {
65 Flowable.just(1, 2)
66 .materialize()
67 .dematerialize(new Function<Notification<Integer>, Notification<Object>>() {
68 @Override
69 public Notification<Object> apply(Notification<Integer> v) throws Exception {
70 return null;
71 }
72 })
73 .test()
74 .assertFailure(NullPointerException.class);
75 }
76
77 @Test
78 public void dematerialize1() {
79 Flowable<Notification<Integer>> notifications = Flowable.just(1, 2).materialize();
80 Flowable<Integer> dematerialize = notifications.dematerialize(Functions.<Notification<Integer>>identity());
81
82 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
83
84 dematerialize.subscribe(subscriber);
85
86 verify(subscriber, times(1)).onNext(1);
87 verify(subscriber, times(1)).onNext(2);
88 verify(subscriber, times(1)).onComplete();
89 verify(subscriber, never()).onError(any(Throwable.class));
90 }
91
92 @Test
93 public void dematerialize2() {
94 Throwable exception = new Throwable("test");
95 Flowable<Integer> flowable = Flowable.error(exception);
96 Flowable<Integer> dematerialize = flowable.materialize().dematerialize(Functions.<Notification<Integer>>identity());
97
98 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
99
100 dematerialize.subscribe(subscriber);
101
102 verify(subscriber, times(1)).onError(exception);
103 verify(subscriber, times(0)).onComplete();
104 verify(subscriber, times(0)).onNext(any(Integer.class));
105 }
106
107 @Test
108 public void dematerialize3() {
109 Exception exception = new Exception("test");
110 Flowable<Integer> flowable = Flowable.error(exception);
111 Flowable<Integer> dematerialize = flowable.materialize().dematerialize(Functions.<Notification<Integer>>identity());
112
113 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
114
115 dematerialize.subscribe(subscriber);
116
117 verify(subscriber, times(1)).onError(exception);
118 verify(subscriber, times(0)).onComplete();
119 verify(subscriber, times(0)).onNext(any(Integer.class));
120 }
121
122 @Test
123 public void errorPassThru() {
124 Exception exception = new Exception("test");
125 Flowable<Notification<Integer>> flowable = Flowable.error(exception);
126 Flowable<Integer> dematerialize = flowable.dematerialize(Functions.<Notification<Integer>>identity());
127
128 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
129
130 dematerialize.subscribe(subscriber);
131
132 verify(subscriber, times(1)).onError(exception);
133 verify(subscriber, times(0)).onComplete();
134 verify(subscriber, times(0)).onNext(any(Integer.class));
135 }
136
137 @Test
138 public void completePassThru() {
139 Flowable<Notification<Integer>> flowable = Flowable.empty();
140 Flowable<Integer> dematerialize = flowable.dematerialize(Functions.<Notification<Integer>>identity());
141
142 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
143
144 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(subscriber);
145 dematerialize.subscribe(ts);
146
147 System.out.println(ts.errors());
148
149 verify(subscriber, never()).onError(any(Throwable.class));
150 verify(subscriber, times(1)).onComplete();
151 verify(subscriber, times(0)).onNext(any(Integer.class));
152 }
153
154 @Test
155 public void honorsContractWhenCompleted() {
156 Flowable<Integer> source = Flowable.just(1);
157
158 Flowable<Integer> result = source.materialize().dematerialize(Functions.<Notification<Integer>>identity());
159
160 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
161
162 result.subscribe(subscriber);
163
164 verify(subscriber).onNext(1);
165 verify(subscriber).onComplete();
166 verify(subscriber, never()).onError(any(Throwable.class));
167 }
168
169 @Test
170 public void honorsContractWhenThrows() {
171 Flowable<Integer> source = Flowable.error(new TestException());
172
173 Flowable<Integer> result = source.materialize().dematerialize(Functions.<Notification<Integer>>identity());
174
175 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
176
177 result.subscribe(subscriber);
178
179 verify(subscriber, never()).onNext(any(Integer.class));
180 verify(subscriber, never()).onComplete();
181 verify(subscriber).onError(any(TestException.class));
182 }
183
184 @Test
185 public void dispose() {
186 TestHelper.checkDisposed(Flowable.just(Notification.createOnComplete())
187 .dematerialize(Functions.<Notification<Object>>identity()));
188 }
189
190 @Test
191 public void doubleOnSubscribe() {
192 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Notification<Object>>, Flowable<Object>>() {
193 @Override
194 public Flowable<Object> apply(Flowable<Notification<Object>> f) throws Exception {
195 return f.dematerialize(Functions.<Notification<Object>>identity());
196 }
197 });
198 }
199
200 @Test
201 public void eventsAfterDematerializedTerminal() {
202 List<Throwable> errors = TestHelper.trackPluginErrors();
203 try {
204 new Flowable<Notification<Object>>() {
205 @Override
206 protected void subscribeActual(Subscriber<? super Notification<Object>> subscriber) {
207 subscriber.onSubscribe(new BooleanSubscription());
208 subscriber.onNext(Notification.createOnComplete());
209 subscriber.onNext(Notification.<Object>createOnNext(1));
210 subscriber.onNext(Notification.createOnError(new TestException("First")));
211 subscriber.onError(new TestException("Second"));
212 }
213 }
214 .dematerialize(Functions.<Notification<Object>>identity())
215 .test()
216 .assertResult();
217
218 TestHelper.assertUndeliverable(errors, 0, TestException.class, "First");
219 TestHelper.assertUndeliverable(errors, 1, TestException.class, "Second");
220 } finally {
221 RxJavaPlugins.reset();
222 }
223 }
224
225 @Test
226 public void notificationInstanceAfterDispose() {
227 new Flowable<Notification<Object>>() {
228 @Override
229 protected void subscribeActual(Subscriber<? super Notification<Object>> subscriber) {
230 subscriber.onSubscribe(new BooleanSubscription());
231 subscriber.onNext(Notification.createOnComplete());
232 subscriber.onNext(Notification.<Object>createOnNext(1));
233 }
234 }
235 .dematerialize(Functions.<Notification<Object>>identity())
236 .test()
237 .assertResult();
238 }
239
240 @Test
241 @SuppressWarnings("unchecked")
242 public void nonNotificationInstanceAfterDispose() {
243 new Flowable<Object>() {
244 @Override
245 protected void subscribeActual(Subscriber<? super Object> subscriber) {
246 subscriber.onSubscribe(new BooleanSubscription());
247 subscriber.onNext(Notification.createOnComplete());
248 subscriber.onNext(1);
249 }
250 }
251 .dematerialize(v -> (Notification<Object>)v)
252 .test()
253 .assertResult();
254 }
255 }