Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDefaultIfEmptyTest.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 org.junit.Test;
20 import org.reactivestreams.Subscriber;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.testsupport.*;
24
25 public class FlowableDefaultIfEmptyTest extends RxJavaTest {
26
27 @Test
28 public void defaultIfEmpty() {
29 Flowable<Integer> source = Flowable.just(1, 2, 3);
30 Flowable<Integer> flowable = source.defaultIfEmpty(10);
31
32 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
33
34 flowable.subscribe(subscriber);
35
36 verify(subscriber, never()).onNext(10);
37 verify(subscriber).onNext(1);
38 verify(subscriber).onNext(2);
39 verify(subscriber).onNext(3);
40 verify(subscriber).onComplete();
41 verify(subscriber, never()).onError(any(Throwable.class));
42 }
43
44 @Test
45 public void defaultIfEmptyWithEmpty() {
46 Flowable<Integer> source = Flowable.empty();
47 Flowable<Integer> flowable = source.defaultIfEmpty(10);
48
49 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
50
51 flowable.subscribe(subscriber);
52
53 verify(subscriber).onNext(10);
54 verify(subscriber).onComplete();
55 verify(subscriber, never()).onError(any(Throwable.class));
56 }
57
58 @Test
59 public void backpressureEmpty() {
60 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0L);
61 Flowable.<Integer>empty().defaultIfEmpty(1).subscribe(ts);
62 ts.assertNoValues();
63 ts.assertNotTerminated();
64 ts.request(1);
65 ts.assertValue(1);
66 ts.assertNoErrors();
67 ts.assertComplete();
68 }
69
70 @Test
71 public void backpressureNonEmpty() {
72 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0L);
73 Flowable.just(1, 2, 3).defaultIfEmpty(1).subscribe(ts);
74 ts.assertNoValues();
75 ts.assertNotTerminated();
76 ts.request(2);
77 ts.assertValues(1, 2);
78 ts.request(1);
79 ts.assertValues(1, 2, 3);
80 ts.assertNoErrors();
81 ts.assertComplete();
82 }
83 }