| 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.observers; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.util.*; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.Observable; |
| 24 |
import io.reactivex.rxjava3.core.Observer; |
| 25 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 26 |
import io.reactivex.rxjava3.disposables.*; |
| 27 |
import io.reactivex.rxjava3.exceptions.*; |
| 28 |
import io.reactivex.rxjava3.functions.*; |
| 29 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 32 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 33 |
|
| 34 |
public class LambdaObserverTest extends RxJavaTest { |
| 35 |
|
| 36 |
@Test |
| 37 |
public void onSubscribeThrows() { |
| 38 |
final List<Object> received = new ArrayList<>(); |
| 39 |
|
| 40 |
LambdaObserver<Object> o = new LambdaObserver<>(new Consumer<Object>() { |
| 41 |
@Override |
| 42 |
public void accept(Object v) throws Exception { |
| 43 |
received.add(v); |
| 44 |
} |
| 45 |
}, |
| 46 |
new Consumer<Throwable>() { |
| 47 |
@Override |
| 48 |
public void accept(Throwable e) throws Exception { |
| 49 |
received.add(e); |
| 50 |
} |
| 51 |
}, new Action() { |
| 52 |
@Override |
| 53 |
public void run() throws Exception { |
| 54 |
received.add(100); |
| 55 |
} |
| 56 |
}, new Consumer<Disposable>() { |
| 57 |
@Override |
| 58 |
public void accept(Disposable d) throws Exception { |
| 59 |
throw new TestException(); |
| 60 |
} |
| 61 |
}); |
| 62 |
|
| 63 |
assertFalse(o.isDisposed()); |
| 64 |
|
| 65 |
Observable.just(1).subscribe(o); |
| 66 |
|
| 67 |
assertTrue(received.toString(), received.get(0) instanceof TestException); |
| 68 |
assertEquals(received.toString(), 1, received.size()); |
| 69 |
|
| 70 |
assertTrue(o.isDisposed()); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void onNextThrows() { |
| 75 |
final List<Object> received = new ArrayList<>(); |
| 76 |
|
| 77 |
LambdaObserver<Object> o = new LambdaObserver<>(new Consumer<Object>() { |
| 78 |
@Override |
| 79 |
public void accept(Object v) throws Exception { |
| 80 |
throw new TestException(); |
| 81 |
} |
| 82 |
}, |
| 83 |
new Consumer<Throwable>() { |
| 84 |
@Override |
| 85 |
public void accept(Throwable e) throws Exception { |
| 86 |
received.add(e); |
| 87 |
} |
| 88 |
}, new Action() { |
| 89 |
@Override |
| 90 |
public void run() throws Exception { |
| 91 |
received.add(100); |
| 92 |
} |
| 93 |
}, new Consumer<Disposable>() { |
| 94 |
@Override |
| 95 |
public void accept(Disposable d) throws Exception { |
| 96 |
} |
| 97 |
}); |
| 98 |
|
| 99 |
assertFalse(o.isDisposed()); |
| 100 |
|
| 101 |
Observable.just(1).subscribe(o); |
| 102 |
|
| 103 |
assertTrue(received.toString(), received.get(0) instanceof TestException); |
| 104 |
assertEquals(received.toString(), 1, received.size()); |
| 105 |
|
| 106 |
assertTrue(o.isDisposed()); |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void onErrorThrows() { |
| 111 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 112 |
|
| 113 |
try { |
| 114 |
final List<Object> received = new ArrayList<>(); |
| 115 |
|
| 116 |
LambdaObserver<Object> o = new LambdaObserver<>(new Consumer<Object>() { |
| 117 |
@Override |
| 118 |
public void accept(Object v) throws Exception { |
| 119 |
received.add(v); |
| 120 |
} |
| 121 |
}, |
| 122 |
new Consumer<Throwable>() { |
| 123 |
@Override |
| 124 |
public void accept(Throwable e) throws Exception { |
| 125 |
throw new TestException("Inner"); |
| 126 |
} |
| 127 |
}, new Action() { |
| 128 |
@Override |
| 129 |
public void run() throws Exception { |
| 130 |
received.add(100); |
| 131 |
} |
| 132 |
}, new Consumer<Disposable>() { |
| 133 |
@Override |
| 134 |
public void accept(Disposable d) throws Exception { |
| 135 |
} |
| 136 |
}); |
| 137 |
|
| 138 |
assertFalse(o.isDisposed()); |
| 139 |
|
| 140 |
Observable.<Integer>error(new TestException("Outer")).subscribe(o); |
| 141 |
|
| 142 |
assertTrue(received.toString(), received.isEmpty()); |
| 143 |
|
| 144 |
assertTrue(o.isDisposed()); |
| 145 |
|
| 146 |
TestHelper.assertError(errors, 0, CompositeException.class); |
| 147 |
List<Throwable> ce = TestHelper.compositeList(errors.get(0)); |
| 148 |
TestHelper.assertError(ce, 0, TestException.class, "Outer"); |
| 149 |
TestHelper.assertError(ce, 1, TestException.class, "Inner"); |
| 150 |
} finally { |
| 151 |
RxJavaPlugins.reset(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
@Test |
| 156 |
public void onCompleteThrows() { |
| 157 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 158 |
|
| 159 |
try { |
| 160 |
final List<Object> received = new ArrayList<>(); |
| 161 |
|
| 162 |
LambdaObserver<Object> o = new LambdaObserver<>(new Consumer<Object>() { |
| 163 |
@Override |
| 164 |
public void accept(Object v) throws Exception { |
| 165 |
received.add(v); |
| 166 |
} |
| 167 |
}, |
| 168 |
new Consumer<Throwable>() { |
| 169 |
@Override |
| 170 |
public void accept(Throwable e) throws Exception { |
| 171 |
received.add(e); |
| 172 |
} |
| 173 |
}, new Action() { |
| 174 |
@Override |
| 175 |
public void run() throws Exception { |
| 176 |
throw new TestException(); |
| 177 |
} |
| 178 |
}, new Consumer<Disposable>() { |
| 179 |
@Override |
| 180 |
public void accept(Disposable d) throws Exception { |
| 181 |
} |
| 182 |
}); |
| 183 |
|
| 184 |
assertFalse(o.isDisposed()); |
| 185 |
|
| 186 |
Observable.<Integer>empty().subscribe(o); |
| 187 |
|
| 188 |
assertTrue(received.toString(), received.isEmpty()); |
| 189 |
|
| 190 |
assertTrue(o.isDisposed()); |
| 191 |
|
| 192 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 193 |
} finally { |
| 194 |
RxJavaPlugins.reset(); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
@Test |
| 199 |
public void badSourceOnSubscribe() { |
| 200 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 201 |
try { |
| 202 |
Observable<Integer> source = new Observable<Integer>() { |
| 203 |
@Override |
| 204 |
public void subscribeActual(Observer<? super Integer> observer) { |
| 205 |
Disposable d1 = Disposable.empty(); |
| 206 |
observer.onSubscribe(d1); |
| 207 |
Disposable d2 = Disposable.empty(); |
| 208 |
observer.onSubscribe(d2); |
| 209 |
|
| 210 |
assertFalse(d1.isDisposed()); |
| 211 |
assertTrue(d2.isDisposed()); |
| 212 |
|
| 213 |
observer.onNext(1); |
| 214 |
observer.onComplete(); |
| 215 |
} |
| 216 |
}; |
| 217 |
|
| 218 |
final List<Object> received = new ArrayList<>(); |
| 219 |
|
| 220 |
LambdaObserver<Object> o = new LambdaObserver<>(new Consumer<Object>() { |
| 221 |
@Override |
| 222 |
public void accept(Object v) throws Exception { |
| 223 |
received.add(v); |
| 224 |
} |
| 225 |
}, |
| 226 |
new Consumer<Throwable>() { |
| 227 |
@Override |
| 228 |
public void accept(Throwable e) throws Exception { |
| 229 |
received.add(e); |
| 230 |
} |
| 231 |
}, new Action() { |
| 232 |
@Override |
| 233 |
public void run() throws Exception { |
| 234 |
received.add(100); |
| 235 |
} |
| 236 |
}, new Consumer<Disposable>() { |
| 237 |
@Override |
| 238 |
public void accept(Disposable d) throws Exception { |
| 239 |
} |
| 240 |
}); |
| 241 |
|
| 242 |
source.subscribe(o); |
| 243 |
|
| 244 |
assertEquals(Arrays.asList(1, 100), received); |
| 245 |
|
| 246 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 247 |
} finally { |
| 248 |
RxJavaPlugins.reset(); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
@Test |
| 253 |
public void badSourceEmitAfterDone() { |
| 254 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 255 |
try { |
| 256 |
Observable<Integer> source = new Observable<Integer>() { |
| 257 |
@Override |
| 258 |
public void subscribeActual(Observer<? super Integer> observer) { |
| 259 |
observer.onSubscribe(Disposable.empty()); |
| 260 |
|
| 261 |
observer.onNext(1); |
| 262 |
observer.onComplete(); |
| 263 |
observer.onNext(2); |
| 264 |
observer.onError(new TestException()); |
| 265 |
observer.onComplete(); |
| 266 |
} |
| 267 |
}; |
| 268 |
|
| 269 |
final List<Object> received = new ArrayList<>(); |
| 270 |
|
| 271 |
LambdaObserver<Object> o = new LambdaObserver<>(new Consumer<Object>() { |
| 272 |
@Override |
| 273 |
public void accept(Object v) throws Exception { |
| 274 |
received.add(v); |
| 275 |
} |
| 276 |
}, |
| 277 |
new Consumer<Throwable>() { |
| 278 |
@Override |
| 279 |
public void accept(Throwable e) throws Exception { |
| 280 |
received.add(e); |
| 281 |
} |
| 282 |
}, new Action() { |
| 283 |
@Override |
| 284 |
public void run() throws Exception { |
| 285 |
received.add(100); |
| 286 |
} |
| 287 |
}, new Consumer<Disposable>() { |
| 288 |
@Override |
| 289 |
public void accept(Disposable d) throws Exception { |
| 290 |
} |
| 291 |
}); |
| 292 |
|
| 293 |
source.subscribe(o); |
| 294 |
|
| 295 |
assertEquals(Arrays.asList(1, 100), received); |
| 296 |
|
| 297 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 298 |
} finally { |
| 299 |
RxJavaPlugins.reset(); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
@Test |
| 304 |
public void onNextThrowsCancelsUpstream() { |
| 305 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 306 |
|
| 307 |
final List<Throwable> errors = new ArrayList<>(); |
| 308 |
|
| 309 |
ps.subscribe(new Consumer<Integer>() { |
| 310 |
@Override |
| 311 |
public void accept(Integer v) throws Exception { |
| 312 |
throw new TestException(); |
| 313 |
} |
| 314 |
}, new Consumer<Throwable>() { |
| 315 |
@Override |
| 316 |
public void accept(Throwable e) throws Exception { |
| 317 |
errors.add(e); |
| 318 |
} |
| 319 |
}); |
| 320 |
|
| 321 |
assertTrue("No observers?!", ps.hasObservers()); |
| 322 |
assertTrue("Has errors already?!", errors.isEmpty()); |
| 323 |
|
| 324 |
ps.onNext(1); |
| 325 |
|
| 326 |
assertFalse("Has observers?!", ps.hasObservers()); |
| 327 |
assertFalse("No errors?!", errors.isEmpty()); |
| 328 |
|
| 329 |
assertTrue(errors.toString(), errors.get(0) instanceof TestException); |
| 330 |
} |
| 331 |
|
| 332 |
@Test |
| 333 |
public void onSubscribeThrowsCancelsUpstream() { |
| 334 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 335 |
|
| 336 |
final List<Throwable> errors = new ArrayList<>(); |
| 337 |
|
| 338 |
ps.subscribe(new LambdaObserver<>(new Consumer<Integer>() { |
| 339 |
@Override |
| 340 |
public void accept(Integer v) throws Exception { |
| 341 |
} |
| 342 |
}, new Consumer<Throwable>() { |
| 343 |
@Override |
| 344 |
public void accept(Throwable e) throws Exception { |
| 345 |
errors.add(e); |
| 346 |
} |
| 347 |
}, new Action() { |
| 348 |
@Override |
| 349 |
public void run() throws Exception { |
| 350 |
} |
| 351 |
}, new Consumer<Disposable>() { |
| 352 |
@Override |
| 353 |
public void accept(Disposable d) throws Exception { |
| 354 |
throw new TestException(); |
| 355 |
} |
| 356 |
})); |
| 357 |
|
| 358 |
assertFalse("Has observers?!", ps.hasObservers()); |
| 359 |
assertFalse("No errors?!", errors.isEmpty()); |
| 360 |
|
| 361 |
assertTrue(errors.toString(), errors.get(0) instanceof TestException); |
| 362 |
} |
| 363 |
|
| 364 |
@Test |
| 365 |
public void onErrorMissingShouldReportNoCustomOnError() { |
| 366 |
LambdaObserver<Integer> o = new LambdaObserver<>(Functions.<Integer>emptyConsumer(), |
| 367 |
Functions.ON_ERROR_MISSING, |
| 368 |
Functions.EMPTY_ACTION, |
| 369 |
Functions.<Disposable>emptyConsumer()); |
| 370 |
|
| 371 |
assertFalse(o.hasCustomOnError()); |
| 372 |
} |
| 373 |
|
| 374 |
@Test |
| 375 |
public void customOnErrorShouldReportCustomOnError() { |
| 376 |
LambdaObserver<Integer> o = new LambdaObserver<>(Functions.<Integer>emptyConsumer(), |
| 377 |
Functions.<Throwable>emptyConsumer(), |
| 378 |
Functions.EMPTY_ACTION, |
| 379 |
Functions.<Disposable>emptyConsumer()); |
| 380 |
|
| 381 |
assertTrue(o.hasCustomOnError()); |
| 382 |
} |
| 383 |
|
| 384 |
@Test |
| 385 |
public void disposedObserverShouldReportErrorOnGlobalErrorHandler() { |
| 386 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 387 |
try { |
| 388 |
final List<Throwable> observerErrors = Collections.synchronizedList(new ArrayList<>()); |
| 389 |
|
| 390 |
LambdaObserver<Integer> o = new LambdaObserver<>(Functions.<Integer>emptyConsumer(), |
| 391 |
new Consumer<Throwable>() { |
| 392 |
@Override |
| 393 |
public void accept(Throwable t) { |
| 394 |
observerErrors.add(t); |
| 395 |
} |
| 396 |
}, |
| 397 |
Functions.EMPTY_ACTION, |
| 398 |
Functions.<Disposable>emptyConsumer()); |
| 399 |
|
| 400 |
o.dispose(); |
| 401 |
o.onError(new IOException()); |
| 402 |
o.onError(new IOException()); |
| 403 |
|
| 404 |
assertTrue(observerErrors.isEmpty()); |
| 405 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 406 |
TestHelper.assertUndeliverable(errors, 1, IOException.class); |
| 407 |
} finally { |
| 408 |
RxJavaPlugins.reset(); |
| 409 |
} |
| 410 |
} |
| 411 |
} |