| 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.testsupport; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.AtomicReference; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 21 |
import io.reactivex.rxjava3.operators.QueueDisposable; |
| 22 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 23 |
|
| 24 |
/** |
| 25 |
* An extended test Observer that records events and allows making assertions about them. |
| 26 |
* |
| 27 |
* <p>You can override the onSubscribe, onNext, onError, onComplete, onSuccess and |
| 28 |
* cancel methods but not the others (this is by design). |
| 29 |
* |
| 30 |
* <p>The TestObserver implements Disposable for convenience where dispose calls cancel. |
| 31 |
* |
| 32 |
* @param <T> the value type |
| 33 |
*/ |
| 34 |
public class TestObserverEx<T> |
| 35 |
extends BaseTestConsumerEx<T, TestObserverEx<T>> |
| 36 |
implements Observer<T>, Disposable, MaybeObserver<T>, SingleObserver<T>, CompletableObserver { |
| 37 |
/** The actual observer to forward events to. */ |
| 38 |
private final Observer<? super T> downstream; |
| 39 |
|
| 40 |
/** Holds the current subscription if any. */ |
| 41 |
private final AtomicReference<Disposable> upstream = new AtomicReference<>(); |
| 42 |
|
| 43 |
private QueueDisposable<T> qd; |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructs a non-forwarding TestObserver. |
| 47 |
*/ |
| 48 |
public TestObserverEx() { |
| 49 |
this(EmptyObserver.INSTANCE); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Constructs a forwarding TestObserver. |
| 54 |
* @param downstream the actual Observer to forward events to |
| 55 |
*/ |
| 56 |
public TestObserverEx(Observer<? super T> downstream) { |
| 57 |
this.downstream = downstream; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Constructs a TestObserverEx with the given initial fusion mode. |
| 62 |
* @param fusionMode the fusion mode, see {@link QueueFuseable} |
| 63 |
*/ |
| 64 |
public TestObserverEx(int fusionMode) { |
| 65 |
this(); |
| 66 |
setInitialFusionMode(fusionMode); |
| 67 |
} |
| 68 |
|
| 69 |
@SuppressWarnings("unchecked") |
| 70 |
@Override |
| 71 |
public void onSubscribe(Disposable d) { |
| 72 |
lastThread = Thread.currentThread(); |
| 73 |
|
| 74 |
if (d == null) { |
| 75 |
errors.add(new NullPointerException("onSubscribe received a null Subscription")); |
| 76 |
return; |
| 77 |
} |
| 78 |
if (!upstream.compareAndSet(null, d)) { |
| 79 |
d.dispose(); |
| 80 |
if (upstream.get() != DisposableHelper.DISPOSED) { |
| 81 |
errors.add(new IllegalStateException("onSubscribe received multiple subscriptions: " + d)); |
| 82 |
} |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
if (initialFusionMode != 0) { |
| 87 |
if (d instanceof QueueDisposable) { |
| 88 |
qd = (QueueDisposable<T>)d; |
| 89 |
|
| 90 |
int m = qd.requestFusion(initialFusionMode); |
| 91 |
establishedFusionMode = m; |
| 92 |
|
| 93 |
if (m == QueueFuseable.SYNC) { |
| 94 |
checkSubscriptionOnce = true; |
| 95 |
lastThread = Thread.currentThread(); |
| 96 |
try { |
| 97 |
T t; |
| 98 |
while ((t = qd.poll()) != null) { |
| 99 |
values.add(t); |
| 100 |
} |
| 101 |
completions++; |
| 102 |
|
| 103 |
upstream.lazySet(DisposableHelper.DISPOSED); |
| 104 |
} catch (Throwable ex) { |
| 105 |
errors.add(ex); |
| 106 |
} |
| 107 |
return; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
downstream.onSubscribe(d); |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public void onNext(T t) { |
| 117 |
if (!checkSubscriptionOnce) { |
| 118 |
checkSubscriptionOnce = true; |
| 119 |
if (upstream.get() == null) { |
| 120 |
errors.add(new IllegalStateException("onSubscribe not called in proper order")); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
lastThread = Thread.currentThread(); |
| 125 |
|
| 126 |
if (establishedFusionMode == QueueFuseable.ASYNC) { |
| 127 |
try { |
| 128 |
while ((t = qd.poll()) != null) { |
| 129 |
values.add(t); |
| 130 |
} |
| 131 |
} catch (Throwable ex) { |
| 132 |
errors.add(ex); |
| 133 |
qd.dispose(); |
| 134 |
} |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
values.add(t); |
| 139 |
|
| 140 |
if (t == null) { |
| 141 |
errors.add(new NullPointerException("onNext received a null value")); |
| 142 |
} |
| 143 |
|
| 144 |
downstream.onNext(t); |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public void onError(Throwable t) { |
| 149 |
if (!checkSubscriptionOnce) { |
| 150 |
checkSubscriptionOnce = true; |
| 151 |
if (upstream.get() == null) { |
| 152 |
errors.add(new IllegalStateException("onSubscribe not called in proper order")); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
try { |
| 157 |
lastThread = Thread.currentThread(); |
| 158 |
if (t == null) { |
| 159 |
errors.add(new NullPointerException("onError received a null Throwable")); |
| 160 |
} else { |
| 161 |
errors.add(t); |
| 162 |
} |
| 163 |
|
| 164 |
downstream.onError(t); |
| 165 |
} finally { |
| 166 |
done.countDown(); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public void onComplete() { |
| 172 |
if (!checkSubscriptionOnce) { |
| 173 |
checkSubscriptionOnce = true; |
| 174 |
if (upstream.get() == null) { |
| 175 |
errors.add(new IllegalStateException("onSubscribe not called in proper order")); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
try { |
| 180 |
lastThread = Thread.currentThread(); |
| 181 |
completions++; |
| 182 |
|
| 183 |
downstream.onComplete(); |
| 184 |
} finally { |
| 185 |
done.countDown(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public final void dispose() { |
| 191 |
DisposableHelper.dispose(upstream); |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public final boolean isDisposed() { |
| 196 |
return DisposableHelper.isDisposed(upstream.get()); |
| 197 |
} |
| 198 |
|
| 199 |
// state retrieval methods |
| 200 |
/** |
| 201 |
* Returns true if this TestObserver received a subscription. |
| 202 |
* @return true if this TestObserver received a subscription |
| 203 |
*/ |
| 204 |
public final boolean hasSubscription() { |
| 205 |
return upstream.get() != null; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Assert that the onSubscribe method was called exactly once. |
| 210 |
* @return this; |
| 211 |
*/ |
| 212 |
@Override |
| 213 |
public final TestObserverEx<T> assertSubscribed() { |
| 214 |
if (upstream.get() == null) { |
| 215 |
throw fail("Not subscribed!"); |
| 216 |
} |
| 217 |
return this; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Assert that the onSubscribe method hasn't been called at all. |
| 222 |
* @return this; |
| 223 |
*/ |
| 224 |
public final TestObserverEx<T> assertNotSubscribed() { |
| 225 |
if (upstream.get() != null) { |
| 226 |
throw fail("Subscribed!"); |
| 227 |
} else |
| 228 |
if (!errors.isEmpty()) { |
| 229 |
throw fail("Not subscribed but errors found"); |
| 230 |
} |
| 231 |
return this; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Sets the initial fusion mode if the upstream supports fusion. |
| 236 |
* <p>Package-private: avoid leaking the now internal fusion properties into the public API. |
| 237 |
* Use ObserverFusion to work with such tests. |
| 238 |
* @param mode the mode to establish, see the {@link QueueDisposable} constants |
| 239 |
* @return this |
| 240 |
*/ |
| 241 |
public final TestObserverEx<T> setInitialFusionMode(int mode) { |
| 242 |
this.initialFusionMode = mode; |
| 243 |
return this; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Asserts that the given fusion mode has been established |
| 248 |
* <p>Package-private: avoid leaking the now internal fusion properties into the public API. |
| 249 |
* Use ObserverFusion to work with such tests. |
| 250 |
* @param mode the expected mode |
| 251 |
* @return this |
| 252 |
*/ |
| 253 |
public final TestObserverEx<T> assertFusionMode(int mode) { |
| 254 |
int m = establishedFusionMode; |
| 255 |
if (m != mode) { |
| 256 |
if (qd != null) { |
| 257 |
throw new AssertionError("\nexpected: " + fusionModeToString(mode) |
| 258 |
+ "\ngot: " + fusionModeToString(m) + "; Fusion mode different"); |
| 259 |
} else { |
| 260 |
throw fail("Upstream is not fuseable"); |
| 261 |
} |
| 262 |
} |
| 263 |
return this; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Assert that the upstream is a fuseable source. |
| 268 |
* <p>Package-private: avoid leaking the now internal fusion properties into the public API. |
| 269 |
* Use ObserverFusion to work with such tests. |
| 270 |
* @return this |
| 271 |
*/ |
| 272 |
public final TestObserverEx<T> assertFuseable() { |
| 273 |
if (qd == null) { |
| 274 |
throw new AssertionError("Upstream is not fuseable."); |
| 275 |
} |
| 276 |
return this; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Assert that the upstream is not a fuseable source. |
| 281 |
* <p>Package-private: avoid leaking the now internal fusion properties into the public API. |
| 282 |
* Use ObserverFusion to work with such tests. |
| 283 |
* @return this |
| 284 |
*/ |
| 285 |
public final TestObserverEx<T> assertNotFuseable() { |
| 286 |
if (qd != null) { |
| 287 |
throw new AssertionError("Upstream is fuseable."); |
| 288 |
} |
| 289 |
return this; |
| 290 |
} |
| 291 |
|
| 292 |
@Override |
| 293 |
public void onSuccess(T value) { |
| 294 |
onNext(value); |
| 295 |
onComplete(); |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* An observer that ignores all events and does not report errors. |
| 300 |
*/ |
| 301 |
enum EmptyObserver implements Observer<Object> { |
| 302 |
INSTANCE; |
| 303 |
|
| 304 |
@Override |
| 305 |
public void onSubscribe(Disposable d) { |
| 306 |
} |
| 307 |
|
| 308 |
@Override |
| 309 |
public void onNext(Object t) { |
| 310 |
} |
| 311 |
|
| 312 |
@Override |
| 313 |
public void onError(Throwable t) { |
| 314 |
} |
| 315 |
|
| 316 |
@Override |
| 317 |
public void onComplete() { |
| 318 |
} |
| 319 |
} |
| 320 |
} |