Debian Codegraph @

src/main/java/io/reactivex/rxjava3/observers/TestObserver.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.observers;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import io.reactivex.rxjava3.annotations.NonNull;
19 import io.reactivex.rxjava3.core.*;
20 import io.reactivex.rxjava3.disposables.Disposable;
21 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
22
23 /**
24 * An {@link Observer}, {@link MaybeObserver}, {@link SingleObserver} and
25 * {@link CompletableObserver} composite that can record events from
26 * {@link Observable}s, {@link Maybe}s, {@link Single}s and {@link Completable}s
27 * and allows making assertions about them.
28 *
29 * <p>You can override the {@link #onSubscribe(Disposable)}, {@link #onNext(Object)}, {@link #onError(Throwable)},
30 * {@link #onComplete()} and {@link #onSuccess(Object)} methods but not the others (this is by design).
31 *
32 * <p>The {@code TestObserver} implements {@link Disposable} for convenience where dispose calls cancel.
33 *
34 * @param <T> the value type
35 * @see io.reactivex.rxjava3.subscribers.TestSubscriber
36 */
37 public class TestObserver<T>
38 extends BaseTestConsumer<T, TestObserver<T>>
39 implements Observer<T>, Disposable, MaybeObserver<T>, SingleObserver<T>, CompletableObserver {
40 /** The actual observer to forward events to. */
41 private final Observer<? super T> downstream;
42
43 /** Holds the current subscription if any. */
44 private final AtomicReference<Disposable> upstream = new AtomicReference<>();
45
46 /**
47 * Constructs a non-forwarding {@code TestObserver}.
48 * @param <T> the value type received
49 * @return the new {@code TestObserver} instance
50 */
51 @NonNull
52 public static <T> TestObserver<T> create() {
53 return new TestObserver<>();
54 }
55
56 /**
57 * Constructs a forwarding {@code TestObserver}.
58 * @param <T> the value type received
59 * @param delegate the actual {@link Observer} to forward events to
60 * @return the new {@code TestObserver} instance
61 */
62 @NonNull
63 public static <T> TestObserver<T> create(@NonNull Observer<? super T> delegate) {
64 return new TestObserver<>(delegate);
65 }
66
67 /**
68 * Constructs a non-forwarding TestObserver.
69 */
70 public TestObserver() {
71 this(EmptyObserver.INSTANCE);
72 }
73
74 /**
75 * Constructs a forwarding {@code TestObserver}.
76 * @param downstream the actual {@link Observer} to forward events to
77 */
78 public TestObserver(@NonNull Observer<? super T> downstream) {
79 this.downstream = downstream;
80 }
81
82 @Override
83 public void onSubscribe(@NonNull Disposable d) {
84 lastThread = Thread.currentThread();
85
86 if (d == null) {
87 errors.add(new NullPointerException("onSubscribe received a null Subscription"));
88 return;
89 }
90 if (!upstream.compareAndSet(null, d)) {
91 d.dispose();
92 if (upstream.get() != DisposableHelper.DISPOSED) {
93 errors.add(new IllegalStateException("onSubscribe received multiple subscriptions: " + d));
94 }
95 return;
96 }
97
98 downstream.onSubscribe(d);
99 }
100
101 @Override
102 public void onNext(@NonNull T t) {
103 if (!checkSubscriptionOnce) {
104 checkSubscriptionOnce = true;
105 if (upstream.get() == null) {
106 errors.add(new IllegalStateException("onSubscribe not called in proper order"));
107 }
108 }
109
110 lastThread = Thread.currentThread();
111
112 values.add(t);
113
114 if (t == null) {
115 errors.add(new NullPointerException("onNext received a null value"));
116 }
117
118 downstream.onNext(t);
119 }
120
121 @Override
122 public void onError(@NonNull Throwable t) {
123 if (!checkSubscriptionOnce) {
124 checkSubscriptionOnce = true;
125 if (upstream.get() == null) {
126 errors.add(new IllegalStateException("onSubscribe not called in proper order"));
127 }
128 }
129
130 try {
131 lastThread = Thread.currentThread();
132 if (t == null) {
133 errors.add(new NullPointerException("onError received a null Throwable"));
134 } else {
135 errors.add(t);
136 }
137
138 downstream.onError(t);
139 } finally {
140 done.countDown();
141 }
142 }
143
144 @Override
145 public void onComplete() {
146 if (!checkSubscriptionOnce) {
147 checkSubscriptionOnce = true;
148 if (upstream.get() == null) {
149 errors.add(new IllegalStateException("onSubscribe not called in proper order"));
150 }
151 }
152
153 try {
154 lastThread = Thread.currentThread();
155 completions++;
156
157 downstream.onComplete();
158 } finally {
159 done.countDown();
160 }
161 }
162
163 @Override
164 public final void dispose() {
165 DisposableHelper.dispose(upstream);
166 }
167
168 @Override
169 public final boolean isDisposed() {
170 return DisposableHelper.isDisposed(upstream.get());
171 }
172
173 // state retrieval methods
174 /**
175 * Returns true if this {@code TestObserver} received a subscription.
176 * @return true if this {@code TestObserver} received a subscription
177 */
178 public final boolean hasSubscription() {
179 return upstream.get() != null;
180 }
181
182 /**
183 * Assert that the {@link #onSubscribe(Disposable)} method was called exactly once.
184 * @return this
185 */
186 @Override
187 @NonNull
188 protected final TestObserver<T> assertSubscribed() {
189 if (upstream.get() == null) {
190 throw fail("Not subscribed!");
191 }
192 return this;
193 }
194
195 @Override
196 public void onSuccess(@NonNull T value) {
197 onNext(value);
198 onComplete();
199 }
200
201 /**
202 * An observer that ignores all events and does not report errors.
203 */
204 enum EmptyObserver implements Observer<Object> {
205 INSTANCE;
206
207 @Override
208 public void onSubscribe(Disposable d) {
209 }
210
211 @Override
212 public void onNext(Object t) {
213 }
214
215 @Override
216 public void onError(Throwable t) {
217 }
218
219 @Override
220 public void onComplete() {
221 }
222 }
223 }