Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDistinctUntilChangedTest.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.*;
17 import static org.mockito.Mockito.*;
18
19 import java.io.IOException;
20 import java.util.List;
21 import java.util.concurrent.atomic.AtomicBoolean;
22
23 import org.junit.*;
24 import org.mockito.InOrder;
25 import org.reactivestreams.Subscriber;
26
27 import io.reactivex.rxjava3.core.*;
28 import io.reactivex.rxjava3.exceptions.TestException;
29 import io.reactivex.rxjava3.functions.*;
30 import io.reactivex.rxjava3.internal.functions.Functions;
31 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
32 import io.reactivex.rxjava3.operators.QueueFuseable;
33 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
34 import io.reactivex.rxjava3.processors.*;
35 import io.reactivex.rxjava3.subscribers.TestSubscriber;
36 import io.reactivex.rxjava3.testsupport.*;
37
38 public class FlowableDistinctUntilChangedTest extends RxJavaTest {
39
40 Subscriber<String> w;
41 Subscriber<String> w2;
42
43 // nulls lead to exceptions
44 final Function<String, String> TO_UPPER_WITH_EXCEPTION = new Function<String, String>() {
45 @Override
46 public String apply(String s) {
47 if (s.equals("x")) {
48 return "xx";
49 }
50 return s.toUpperCase();
51 }
52 };
53
54 @Before
55 public void before() {
56 w = TestHelper.mockSubscriber();
57 w2 = TestHelper.mockSubscriber();
58 }
59
60 @Test
61 public void distinctUntilChangedOfNone() {
62 Flowable<String> src = Flowable.empty();
63 src.distinctUntilChanged().subscribe(w);
64
65 verify(w, never()).onNext(anyString());
66 verify(w, never()).onError(any(Throwable.class));
67 verify(w, times(1)).onComplete();
68 }
69
70 @Test
71 public void distinctUntilChangedOfNoneWithKeySelector() {
72 Flowable<String> src = Flowable.empty();
73 src.distinctUntilChanged(TO_UPPER_WITH_EXCEPTION).subscribe(w);
74
75 verify(w, never()).onNext(anyString());
76 verify(w, never()).onError(any(Throwable.class));
77 verify(w, times(1)).onComplete();
78 }
79
80 @Test
81 public void distinctUntilChangedOfNormalSource() {
82 Flowable<String> src = Flowable.just("a", "b", "c", "c", "c", "b", "b", "a", "e");
83 src.distinctUntilChanged().subscribe(w);
84
85 InOrder inOrder = inOrder(w);
86 inOrder.verify(w, times(1)).onNext("a");
87 inOrder.verify(w, times(1)).onNext("b");
88 inOrder.verify(w, times(1)).onNext("c");
89 inOrder.verify(w, times(1)).onNext("b");
90 inOrder.verify(w, times(1)).onNext("a");
91 inOrder.verify(w, times(1)).onNext("e");
92 inOrder.verify(w, times(1)).onComplete();
93 inOrder.verify(w, never()).onNext(anyString());
94 verify(w, never()).onError(any(Throwable.class));
95 }
96
97 @Test
98 public void distinctUntilChangedOfNormalSourceWithKeySelector() {
99 Flowable<String> src = Flowable.just("a", "b", "c", "C", "c", "B", "b", "a", "e");
100 src.distinctUntilChanged(TO_UPPER_WITH_EXCEPTION).subscribe(w);
101
102 InOrder inOrder = inOrder(w);
103 inOrder.verify(w, times(1)).onNext("a");
104 inOrder.verify(w, times(1)).onNext("b");
105 inOrder.verify(w, times(1)).onNext("c");
106 inOrder.verify(w, times(1)).onNext("B");
107 inOrder.verify(w, times(1)).onNext("a");
108 inOrder.verify(w, times(1)).onNext("e");
109 inOrder.verify(w, times(1)).onComplete();
110 inOrder.verify(w, never()).onNext(anyString());
111 verify(w, never()).onError(any(Throwable.class));
112 }
113
114 @Test
115 public void directComparer() {
116 Flowable.fromArray(1, 2, 2, 3, 2, 4, 1, 1, 2)
117 .distinctUntilChanged(new BiPredicate<Integer, Integer>() {
118 @Override
119 public boolean test(Integer a, Integer b) {
120 return a.equals(b);
121 }
122 })
123 .test()
124 .assertResult(1, 2, 3, 2, 4, 1, 2);
125 }
126
127 @Test
128 public void directComparerConditional() {
129 Flowable.fromArray(1, 2, 2, 3, 2, 4, 1, 1, 2)
130 .distinctUntilChanged(new BiPredicate<Integer, Integer>() {
131 @Override
132 public boolean test(Integer a, Integer b) {
133 return a.equals(b);
134 }
135 })
136 .filter(new Predicate<Integer>() {
137 @Override
138 public boolean test(Integer v) {
139 return true;
140 }
141 })
142 .test()
143 .assertResult(1, 2, 3, 2, 4, 1, 2);
144 }
145
146 @Test
147 public void directComparerFused() {
148 Flowable.fromArray(1, 2, 2, 3, 2, 4, 1, 1, 2)
149 .distinctUntilChanged(new BiPredicate<Integer, Integer>() {
150 @Override
151 public boolean test(Integer a, Integer b) {
152 return a.equals(b);
153 }
154 })
155 .to(TestHelper.<Integer>testSubscriber(Long.MAX_VALUE, QueueFuseable.ANY, false))
156 .assertFuseable()
157 .assertFusionMode(QueueFuseable.SYNC)
158 .assertResult(1, 2, 3, 2, 4, 1, 2);
159 }
160
161 @Test
162 public void directComparerConditionalFused() {
163 Flowable.fromArray(1, 2, 2, 3, 2, 4, 1, 1, 2)
164 .distinctUntilChanged(new BiPredicate<Integer, Integer>() {
165 @Override
166 public boolean test(Integer a, Integer b) {
167 return a.equals(b);
168 }
169 })
170 .filter(new Predicate<Integer>() {
171 @Override
172 public boolean test(Integer v) {
173 return true;
174 }
175 })
176 .to(TestHelper.<Integer>testSubscriber(Long.MAX_VALUE, QueueFuseable.ANY, false))
177 .assertFuseable()
178 .assertFusionMode(QueueFuseable.SYNC)
179 .assertResult(1, 2, 3, 2, 4, 1, 2);
180 }
181
182 private static final Function<String, String> THROWS_NON_FATAL = new Function<String, String>() {
183 @Override
184 public String apply(String s) {
185 throw new RuntimeException();
186 }
187 };
188
189 @Test
190 public void distinctUntilChangedWhenNonFatalExceptionThrownByKeySelectorIsNotReportedByUpstream() {
191 Flowable<String> src = Flowable.just("a", "b", "null", "c");
192 final AtomicBoolean errorOccurred = new AtomicBoolean(false);
193 src
194 .doOnError(new Consumer<Throwable>() {
195 @Override
196 public void accept(Throwable t) {
197 errorOccurred.set(true);
198 }
199 })
200 .distinctUntilChanged(THROWS_NON_FATAL)
201 .subscribe(w);
202 Assert.assertFalse(errorOccurred.get());
203 }
204
205 @Test
206 public void customComparator() {
207 Flowable<String> source = Flowable.just("a", "b", "B", "A", "a", "C");
208
209 TestSubscriber<String> ts = TestSubscriber.create();
210
211 source.distinctUntilChanged(new BiPredicate<String, String>() {
212 @Override
213 public boolean test(String a, String b) {
214 return a.compareToIgnoreCase(b) == 0;
215 }
216 })
217 .subscribe(ts);
218
219 ts.assertValues("a", "b", "A", "C");
220 ts.assertNoErrors();
221 ts.assertComplete();
222 }
223
224 @Test
225 public void customComparatorThrows() {
226 Flowable<String> source = Flowable.just("a", "b", "B", "A", "a", "C");
227
228 TestSubscriber<String> ts = TestSubscriber.create();
229
230 source.distinctUntilChanged(new BiPredicate<String, String>() {
231 @Override
232 public boolean test(String a, String b) {
233 throw new TestException();
234 }
235 })
236 .subscribe(ts);
237
238 ts.assertValue("a");
239 ts.assertNotComplete();
240 ts.assertError(TestException.class);
241 }
242
243 @Test
244 public void fused() {
245 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
246
247 Flowable.just(1, 2, 2, 3, 3, 4, 5)
248 .distinctUntilChanged(new BiPredicate<Integer, Integer>() {
249 @Override
250 public boolean test(Integer a, Integer b) throws Exception {
251 return a.equals(b);
252 }
253 })
254 .subscribe(ts);
255
256 ts.assertFuseable()
257 .assertFusionMode(QueueFuseable.SYNC)
258 .assertResult(1, 2, 3, 4, 5)
259 ;
260 }
261
262 @Test
263 public void fusedAsync() {
264 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
265
266 UnicastProcessor<Integer> up = UnicastProcessor.create();
267
268 up
269 .distinctUntilChanged(new BiPredicate<Integer, Integer>() {
270 @Override
271 public boolean test(Integer a, Integer b) throws Exception {
272 return a.equals(b);
273 }
274 })
275 .subscribe(ts);
276
277 TestHelper.emit(up, 1, 2, 2, 3, 3, 4, 5);
278
279 ts.assertFuseable()
280 .assertFusionMode(QueueFuseable.ASYNC)
281 .assertResult(1, 2, 3, 4, 5)
282 ;
283 }
284
285 @Test
286 public void ignoreCancel() {
287 List<Throwable> errors = TestHelper.trackPluginErrors();
288
289 try {
290 new Flowable<Integer>() {
291 @Override
292 public void subscribeActual(Subscriber<? super Integer> s) {
293 s.onSubscribe(new BooleanSubscription());
294 s.onNext(1);
295 s.onNext(2);
296 s.onNext(3);
297 s.onError(new IOException());
298 s.onComplete();
299 }
300 }
301 .distinctUntilChanged(new BiPredicate<Integer, Integer>() {
302 @Override
303 public boolean test(Integer a, Integer b) throws Exception {
304 throw new TestException();
305 }
306 })
307 .test()
308 .assertFailure(TestException.class, 1);
309
310 TestHelper.assertUndeliverable(errors, 0, IOException.class);
311 } finally {
312 RxJavaPlugins.reset();
313 }
314 }
315
316 class Mutable {
317 int value;
318 }
319
320 @Test
321 public void mutableWithSelector() {
322 Mutable m = new Mutable();
323
324 PublishProcessor<Mutable> pp = PublishProcessor.create();
325
326 TestSubscriber<Mutable> ts = pp.distinctUntilChanged(new Function<Mutable, Object>() {
327 @Override
328 public Object apply(Mutable m) throws Exception {
329 return m.value;
330 }
331 })
332 .test();
333
334 pp.onNext(m);
335 m.value = 1;
336 pp.onNext(m);
337 pp.onComplete();
338
339 ts.assertResult(m, m);
340 }
341
342 @Test
343 public void conditionalNormal() {
344 Flowable.just(1, 2, 1, 3, 3, 4, 3, 5, 5)
345 .distinctUntilChanged()
346 .filter(new Predicate<Integer>() {
347 @Override
348 public boolean test(Integer v) throws Exception {
349 return v % 2 == 0;
350 }
351 })
352 .test()
353 .assertResult(2, 4);
354 }
355
356 @Test
357 public void conditionalNormal2() {
358 Flowable.just(1, 2, 1, 3, 3, 4, 3, 5, 5).hide()
359 .distinctUntilChanged()
360 .filter(new Predicate<Integer>() {
361 @Override
362 public boolean test(Integer v) throws Exception {
363 return v % 2 == 0;
364 }
365 })
366 .test()
367 .assertResult(2, 4);
368 }
369
370 @Test
371 public void conditionalNormal3() {
372 UnicastProcessor<Integer> up = UnicastProcessor.create();
373
374 TestSubscriber<Integer> ts = up.hide()
375 .distinctUntilChanged()
376 .filter(new Predicate<Integer>() {
377 @Override
378 public boolean test(Integer v) throws Exception {
379 return v % 2 == 0;
380 }
381 })
382 .test();
383
384 TestHelper.emit(up, 1, 2, 1, 3, 3, 4, 3, 5, 5);
385
386 ts
387 .assertResult(2, 4);
388 }
389
390 @Test
391 public void conditionalSelectorCrash() {
392 Flowable.just(1, 2, 1, 3, 3, 4, 3, 5, 5)
393 .distinctUntilChanged(new BiPredicate<Integer, Integer>() {
394 @Override
395 public boolean test(Integer a, Integer b) throws Exception {
396 throw new TestException();
397 }
398 })
399 .filter(new Predicate<Integer>() {
400 @Override
401 public boolean test(Integer v) throws Exception {
402 return v % 2 == 0;
403 }
404 })
405 .test()
406 .assertFailure(TestException.class);
407 }
408
409 @Test
410 public void conditionalFused() {
411 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
412
413 Flowable.just(1, 2, 1, 3, 3, 4, 3, 5, 5)
414 .distinctUntilChanged()
415 .filter(new Predicate<Integer>() {
416 @Override
417 public boolean test(Integer v) throws Exception {
418 return v % 2 == 0;
419 }
420 })
421 .subscribe(ts);
422
423 ts.assertFusionMode(QueueFuseable.SYNC)
424 .assertResult(2, 4);
425 }
426
427 @Test
428 public void conditionalAsyncFused() {
429 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
430 UnicastProcessor<Integer> up = UnicastProcessor.create();
431
432 up
433 .distinctUntilChanged()
434 .filter(new Predicate<Integer>() {
435 @Override
436 public boolean test(Integer v) throws Exception {
437 return v % 2 == 0;
438 }
439 })
440 .subscribe(ts);
441
442 TestHelper.emit(up, 1, 2, 1, 3, 3, 4, 3, 5, 5);
443
444 ts.assertFusionMode(QueueFuseable.ASYNC)
445 .assertResult(2, 4);
446 }
447
448 @Test
449 public void badSource() {
450 TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() {
451 @Override
452 public Object apply(Flowable<Integer> f) throws Exception {
453 return f.distinctUntilChanged().filter(Functions.alwaysTrue());
454 }
455 }, false, 1, 1, 1);
456 }
457 }