Debian Codegraph @

src/test/java/io/reactivex/rxjava3/subscribers/DisposableSubscriberTest.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.subscribers;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
24 import io.reactivex.rxjava3.internal.util.EndConsumerHelper;
25 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
26 import io.reactivex.rxjava3.testsupport.TestHelper;
27
28 public class DisposableSubscriberTest extends RxJavaTest {
29
30 static final class TestDisposableSubscriber<T> extends DisposableSubscriber<T> {
31
32 int start;
33
34 final List<T> values = new ArrayList<>();
35
36 final List<Throwable> errors = new ArrayList<>();
37
38 int completions;
39
40 @Override
41 protected void onStart() {
42 request(1);
43
44 start++;
45 }
46
47 @Override
48 public void onNext(T value) {
49 values.add(value);
50 }
51
52 @Override
53 public void onError(Throwable e) {
54 errors.add(e);
55 }
56
57 @Override
58 public void onComplete() {
59 completions++;
60 }
61 }
62
63 @Test
64 public void normal() {
65 TestDisposableSubscriber<Integer> tc = new TestDisposableSubscriber<>();
66
67 assertFalse(tc.isDisposed());
68 assertEquals(0, tc.start);
69 assertTrue(tc.values.isEmpty());
70 assertTrue(tc.errors.isEmpty());
71
72 Flowable.just(1).subscribe(tc);
73
74 assertFalse(tc.isDisposed());
75 assertEquals(1, tc.start);
76 assertEquals(1, tc.values.get(0).intValue());
77 assertTrue(tc.errors.isEmpty());
78 }
79
80 @Test
81 public void startOnce() {
82
83 List<Throwable> error = TestHelper.trackPluginErrors();
84
85 try {
86 TestDisposableSubscriber<Integer> tc = new TestDisposableSubscriber<>();
87
88 tc.onSubscribe(new BooleanSubscription());
89
90 BooleanSubscription bs = new BooleanSubscription();
91
92 tc.onSubscribe(bs);
93
94 assertTrue(bs.isCancelled());
95
96 assertEquals(1, tc.start);
97
98 TestHelper.assertError(error, 0, IllegalStateException.class, EndConsumerHelper.composeMessage(tc.getClass().getName()));
99 } finally {
100 RxJavaPlugins.reset();
101 }
102 }
103
104 @Test
105 public void dispose() {
106 TestDisposableSubscriber<Integer> tc = new TestDisposableSubscriber<>();
107
108 assertFalse(tc.isDisposed());
109
110 tc.dispose();
111
112 assertTrue(tc.isDisposed());
113
114 BooleanSubscription bs = new BooleanSubscription();
115
116 tc.onSubscribe(bs);
117
118 assertTrue(bs.isCancelled());
119
120 assertEquals(0, tc.start);
121 }
122 }