| 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.plugins; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.io.*; |
| 19 |
import java.lang.Thread.UncaughtExceptionHandler; |
| 20 |
import java.lang.reflect.*; |
| 21 |
import java.util.*; |
| 22 |
import java.util.concurrent.*; |
| 23 |
import java.util.concurrent.atomic.*; |
| 24 |
|
| 25 |
import org.junit.Test; |
| 26 |
import org.reactivestreams.*; |
| 27 |
|
| 28 |
import io.reactivex.rxjava3.core.*; |
| 29 |
import io.reactivex.rxjava3.core.Observable; |
| 30 |
import io.reactivex.rxjava3.core.Observer; |
| 31 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 32 |
import io.reactivex.rxjava3.disposables.*; |
| 33 |
import io.reactivex.rxjava3.exceptions.*; |
| 34 |
import io.reactivex.rxjava3.flowables.ConnectableFlowable; |
| 35 |
import io.reactivex.rxjava3.functions.*; |
| 36 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 37 |
import io.reactivex.rxjava3.internal.operators.completable.CompletableError; |
| 38 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableRange; |
| 39 |
import io.reactivex.rxjava3.internal.operators.maybe.MaybeError; |
| 40 |
import io.reactivex.rxjava3.internal.operators.observable.ObservableRange; |
| 41 |
import io.reactivex.rxjava3.internal.operators.parallel.ParallelFromPublisher; |
| 42 |
import io.reactivex.rxjava3.internal.operators.single.SingleJust; |
| 43 |
import io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler; |
| 44 |
import io.reactivex.rxjava3.internal.subscriptions.ScalarSubscription; |
| 45 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 46 |
import io.reactivex.rxjava3.observables.ConnectableObservable; |
| 47 |
import io.reactivex.rxjava3.parallel.ParallelFlowable; |
| 48 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 49 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 50 |
|
| 51 |
public class RxJavaPluginsTest extends RxJavaTest { |
| 52 |
|
| 53 |
@Test |
| 54 |
public void constructorShouldBePrivate() { |
| 55 |
TestHelper.checkUtilityClass(RxJavaPlugins.class); |
| 56 |
} |
| 57 |
|
| 58 |
@SuppressWarnings({ "rawtypes" }) |
| 59 |
@Test |
| 60 |
public void lockdown() throws Exception { |
| 61 |
RxJavaPlugins.reset(); |
| 62 |
RxJavaPlugins.lockdown(); |
| 63 |
try { |
| 64 |
assertTrue(RxJavaPlugins.isLockdown()); |
| 65 |
Consumer a1 = Functions.emptyConsumer(); |
| 66 |
Supplier f0 = new Supplier() { |
| 67 |
@Override |
| 68 |
public Object get() { |
| 69 |
return null; |
| 70 |
} |
| 71 |
}; |
| 72 |
Function f1 = Functions.identity(); |
| 73 |
BiFunction f2 = new BiFunction() { |
| 74 |
@Override |
| 75 |
public Object apply(Object t1, Object t2) { |
| 76 |
return t2; |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
BooleanSupplier bs = new BooleanSupplier() { |
| 81 |
@Override |
| 82 |
public boolean getAsBoolean() throws Exception { |
| 83 |
return true; |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
for (Method m : RxJavaPlugins.class.getMethods()) { |
| 88 |
if (m.getName().startsWith("set")) { |
| 89 |
|
| 90 |
Method getter; |
| 91 |
|
| 92 |
Class<?> paramType = m.getParameterTypes()[0]; |
| 93 |
|
| 94 |
if (paramType == Boolean.TYPE) { |
| 95 |
getter = RxJavaPlugins.class.getMethod("is" + m.getName().substring(3)); |
| 96 |
} else { |
| 97 |
getter = RxJavaPlugins.class.getMethod("get" + m.getName().substring(3)); |
| 98 |
} |
| 99 |
|
| 100 |
Object before = getter.invoke(null); |
| 101 |
|
| 102 |
try { |
| 103 |
if (paramType.isAssignableFrom(Boolean.TYPE)) { |
| 104 |
m.invoke(null, true); |
| 105 |
} else |
| 106 |
if (paramType.isAssignableFrom(Supplier.class)) { |
| 107 |
m.invoke(null, f0); |
| 108 |
} else |
| 109 |
if (paramType.isAssignableFrom(Function.class)) { |
| 110 |
m.invoke(null, f1); |
| 111 |
} else |
| 112 |
if (paramType.isAssignableFrom(Consumer.class)) { |
| 113 |
m.invoke(null, a1); |
| 114 |
} else |
| 115 |
if (paramType.isAssignableFrom(BooleanSupplier.class)) { |
| 116 |
m.invoke(null, bs); |
| 117 |
} else { |
| 118 |
m.invoke(null, f2); |
| 119 |
} |
| 120 |
fail("Should have thrown InvocationTargetException(IllegalStateException)"); |
| 121 |
} catch (InvocationTargetException ex) { |
| 122 |
if (ex.getCause() instanceof IllegalStateException) { |
| 123 |
assertEquals("Plugins can't be changed anymore", ex.getCause().getMessage()); |
| 124 |
} else { |
| 125 |
fail("Should have thrown InvocationTargetException(IllegalStateException)"); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
Object after = getter.invoke(null); |
| 130 |
|
| 131 |
if (paramType.isPrimitive()) { |
| 132 |
assertEquals(m.toString(), before, after); |
| 133 |
} else { |
| 134 |
assertSame(m.toString(), before, after); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} finally { |
| 139 |
RxJavaPlugins.unlock(); |
| 140 |
RxJavaPlugins.reset(); |
| 141 |
assertFalse(RxJavaPlugins.isLockdown()); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
Function<Scheduler, Scheduler> replaceWithImmediate = new Function<Scheduler, Scheduler>() { |
| 146 |
@Override |
| 147 |
public Scheduler apply(Scheduler t) { |
| 148 |
return ImmediateThinScheduler.INSTANCE; |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
@Test |
| 153 |
public void overrideSingleScheduler() { |
| 154 |
try { |
| 155 |
RxJavaPlugins.setSingleSchedulerHandler(replaceWithImmediate); |
| 156 |
|
| 157 |
assertSame(ImmediateThinScheduler.INSTANCE, Schedulers.single()); |
| 158 |
} finally { |
| 159 |
RxJavaPlugins.reset(); |
| 160 |
} |
| 161 |
// make sure the reset worked |
| 162 |
assertNotSame(ImmediateThinScheduler.INSTANCE, Schedulers.single()); |
| 163 |
} |
| 164 |
|
| 165 |
@Test |
| 166 |
public void overrideComputationScheduler() { |
| 167 |
try { |
| 168 |
RxJavaPlugins.setComputationSchedulerHandler(replaceWithImmediate); |
| 169 |
|
| 170 |
assertSame(ImmediateThinScheduler.INSTANCE, Schedulers.computation()); |
| 171 |
} finally { |
| 172 |
RxJavaPlugins.reset(); |
| 173 |
} |
| 174 |
// make sure the reset worked |
| 175 |
assertNotSame(ImmediateThinScheduler.INSTANCE, Schedulers.computation()); |
| 176 |
} |
| 177 |
|
| 178 |
@Test |
| 179 |
public void overrideIoScheduler() { |
| 180 |
try { |
| 181 |
RxJavaPlugins.setIoSchedulerHandler(replaceWithImmediate); |
| 182 |
|
| 183 |
assertSame(ImmediateThinScheduler.INSTANCE, Schedulers.io()); |
| 184 |
} finally { |
| 185 |
RxJavaPlugins.reset(); |
| 186 |
} |
| 187 |
// make sure the reset worked |
| 188 |
assertNotSame(ImmediateThinScheduler.INSTANCE, Schedulers.io()); |
| 189 |
} |
| 190 |
|
| 191 |
@Test |
| 192 |
public void overrideNewThreadScheduler() { |
| 193 |
try { |
| 194 |
RxJavaPlugins.setNewThreadSchedulerHandler(replaceWithImmediate); |
| 195 |
|
| 196 |
assertSame(ImmediateThinScheduler.INSTANCE, Schedulers.newThread()); |
| 197 |
} finally { |
| 198 |
RxJavaPlugins.reset(); |
| 199 |
} |
| 200 |
// make sure the reset worked |
| 201 |
assertNotSame(ImmediateThinScheduler.INSTANCE, Schedulers.newThread()); |
| 202 |
} |
| 203 |
|
| 204 |
Function<Supplier<Scheduler>, Scheduler> initReplaceWithImmediate = new Function<Supplier<Scheduler>, Scheduler>() { |
| 205 |
@Override |
| 206 |
public Scheduler apply(Supplier<Scheduler> t) { |
| 207 |
return ImmediateThinScheduler.INSTANCE; |
| 208 |
} |
| 209 |
}; |
| 210 |
|
| 211 |
@Test |
| 212 |
public void overrideInitSingleScheduler() { |
| 213 |
final Scheduler s = Schedulers.single(); // make sure the Schedulers is initialized |
| 214 |
Supplier<Scheduler> c = new Supplier<Scheduler>() { |
| 215 |
@Override |
| 216 |
public Scheduler get() throws Exception { |
| 217 |
return s; |
| 218 |
} |
| 219 |
}; |
| 220 |
try { |
| 221 |
RxJavaPlugins.setInitSingleSchedulerHandler(initReplaceWithImmediate); |
| 222 |
|
| 223 |
assertSame(ImmediateThinScheduler.INSTANCE, RxJavaPlugins.initSingleScheduler(c)); |
| 224 |
} finally { |
| 225 |
RxJavaPlugins.reset(); |
| 226 |
} |
| 227 |
// make sure the reset worked |
| 228 |
assertSame(s, RxJavaPlugins.initSingleScheduler(c)); |
| 229 |
} |
| 230 |
|
| 231 |
@Test |
| 232 |
public void overrideInitComputationScheduler() { |
| 233 |
final Scheduler s = Schedulers.computation(); // make sure the Schedulers is initialized |
| 234 |
Supplier<Scheduler> c = new Supplier<Scheduler>() { |
| 235 |
@Override |
| 236 |
public Scheduler get() throws Exception { |
| 237 |
return s; |
| 238 |
} |
| 239 |
}; |
| 240 |
try { |
| 241 |
RxJavaPlugins.setInitComputationSchedulerHandler(initReplaceWithImmediate); |
| 242 |
|
| 243 |
assertSame(ImmediateThinScheduler.INSTANCE, RxJavaPlugins.initComputationScheduler(c)); |
| 244 |
} finally { |
| 245 |
RxJavaPlugins.reset(); |
| 246 |
} |
| 247 |
// make sure the reset worked |
| 248 |
assertSame(s, RxJavaPlugins.initComputationScheduler(c)); |
| 249 |
} |
| 250 |
|
| 251 |
@Test |
| 252 |
public void overrideInitIoScheduler() { |
| 253 |
final Scheduler s = Schedulers.io(); // make sure the Schedulers is initialized; |
| 254 |
Supplier<Scheduler> c = new Supplier<Scheduler>() { |
| 255 |
@Override |
| 256 |
public Scheduler get() throws Exception { |
| 257 |
return s; |
| 258 |
} |
| 259 |
}; |
| 260 |
try { |
| 261 |
RxJavaPlugins.setInitIoSchedulerHandler(initReplaceWithImmediate); |
| 262 |
|
| 263 |
assertSame(ImmediateThinScheduler.INSTANCE, RxJavaPlugins.initIoScheduler(c)); |
| 264 |
} finally { |
| 265 |
RxJavaPlugins.reset(); |
| 266 |
} |
| 267 |
// make sure the reset worked |
| 268 |
assertSame(s, RxJavaPlugins.initIoScheduler(c)); |
| 269 |
} |
| 270 |
|
| 271 |
@Test |
| 272 |
public void overrideInitNewThreadScheduler() { |
| 273 |
final Scheduler s = Schedulers.newThread(); // make sure the Schedulers is initialized; |
| 274 |
Supplier<Scheduler> c = new Supplier<Scheduler>() { |
| 275 |
@Override |
| 276 |
public Scheduler get() throws Exception { |
| 277 |
return s; |
| 278 |
} |
| 279 |
}; |
| 280 |
try { |
| 281 |
RxJavaPlugins.setInitNewThreadSchedulerHandler(initReplaceWithImmediate); |
| 282 |
|
| 283 |
assertSame(ImmediateThinScheduler.INSTANCE, RxJavaPlugins.initNewThreadScheduler(c)); |
| 284 |
} finally { |
| 285 |
RxJavaPlugins.reset(); |
| 286 |
} |
| 287 |
// make sure the reset worked |
| 288 |
assertSame(s, RxJavaPlugins.initNewThreadScheduler(c)); |
| 289 |
} |
| 290 |
|
| 291 |
Supplier<Scheduler> nullResultSupplier = new Supplier<Scheduler>() { |
| 292 |
@Override |
| 293 |
public Scheduler get() throws Exception { |
| 294 |
return null; |
| 295 |
} |
| 296 |
}; |
| 297 |
|
| 298 |
@Test |
| 299 |
public void overrideInitSingleSchedulerCrashes() { |
| 300 |
// fail when Supplier is null |
| 301 |
try { |
| 302 |
RxJavaPlugins.initSingleScheduler(null); |
| 303 |
fail("Should have thrown NullPointerException"); |
| 304 |
} catch (NullPointerException npe) { |
| 305 |
assertEquals("Scheduler Supplier can't be null", npe.getMessage()); |
| 306 |
} |
| 307 |
|
| 308 |
// fail when Supplier result is null |
| 309 |
try { |
| 310 |
RxJavaPlugins.initSingleScheduler(nullResultSupplier); |
| 311 |
fail("Should have thrown NullPointerException"); |
| 312 |
} catch (NullPointerException npe) { |
| 313 |
assertEquals("Scheduler Supplier result can't be null", npe.getMessage()); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
@Test |
| 318 |
public void overrideInitComputationSchedulerCrashes() { |
| 319 |
// fail when Supplier is null |
| 320 |
try { |
| 321 |
RxJavaPlugins.initComputationScheduler(null); |
| 322 |
fail("Should have thrown NullPointerException"); |
| 323 |
} catch (NullPointerException npe) { |
| 324 |
assertEquals("Scheduler Supplier can't be null", npe.getMessage()); |
| 325 |
} |
| 326 |
|
| 327 |
// fail when Supplier result is null |
| 328 |
try { |
| 329 |
RxJavaPlugins.initComputationScheduler(nullResultSupplier); |
| 330 |
fail("Should have thrown NullPointerException"); |
| 331 |
} catch (NullPointerException npe) { |
| 332 |
assertEquals("Scheduler Supplier result can't be null", npe.getMessage()); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
@Test |
| 337 |
public void overrideInitIoSchedulerCrashes() { |
| 338 |
// fail when Supplier is null |
| 339 |
try { |
| 340 |
RxJavaPlugins.initIoScheduler(null); |
| 341 |
fail("Should have thrown NullPointerException"); |
| 342 |
} catch (NullPointerException npe) { |
| 343 |
assertEquals("Scheduler Supplier can't be null", npe.getMessage()); |
| 344 |
} |
| 345 |
|
| 346 |
// fail when Supplier result is null |
| 347 |
try { |
| 348 |
RxJavaPlugins.initIoScheduler(nullResultSupplier); |
| 349 |
fail("Should have thrown NullPointerException"); |
| 350 |
} catch (NullPointerException npe) { |
| 351 |
assertEquals("Scheduler Supplier result can't be null", npe.getMessage()); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
@Test |
| 356 |
public void overrideInitNewThreadSchedulerCrashes() { |
| 357 |
// fail when Supplier is null |
| 358 |
try { |
| 359 |
RxJavaPlugins.initNewThreadScheduler(null); |
| 360 |
fail("Should have thrown NullPointerException"); |
| 361 |
} catch (NullPointerException npe) { |
| 362 |
// expected |
| 363 |
assertEquals("Scheduler Supplier can't be null", npe.getMessage()); |
| 364 |
} |
| 365 |
|
| 366 |
// fail when Supplier result is null |
| 367 |
try { |
| 368 |
RxJavaPlugins.initNewThreadScheduler(nullResultSupplier); |
| 369 |
fail("Should have thrown NullPointerException"); |
| 370 |
} catch (NullPointerException npe) { |
| 371 |
assertEquals("Scheduler Supplier result can't be null", npe.getMessage()); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
Supplier<Scheduler> unsafeDefault = new Supplier<Scheduler>() { |
| 376 |
@Override |
| 377 |
public Scheduler get() throws Exception { |
| 378 |
throw new AssertionError("Default Scheduler instance should not have been evaluated"); |
| 379 |
} |
| 380 |
}; |
| 381 |
|
| 382 |
@Test |
| 383 |
public void defaultSingleSchedulerIsInitializedLazily() { |
| 384 |
// unsafe default Scheduler Supplier should not be evaluated |
| 385 |
try { |
| 386 |
RxJavaPlugins.setInitSingleSchedulerHandler(initReplaceWithImmediate); |
| 387 |
RxJavaPlugins.initSingleScheduler(unsafeDefault); |
| 388 |
} finally { |
| 389 |
RxJavaPlugins.reset(); |
| 390 |
} |
| 391 |
|
| 392 |
// make sure the reset worked |
| 393 |
assertNotSame(ImmediateThinScheduler.INSTANCE, Schedulers.single()); |
| 394 |
} |
| 395 |
|
| 396 |
@Test |
| 397 |
public void defaultIoSchedulerIsInitializedLazily() { |
| 398 |
// unsafe default Scheduler Supplier should not be evaluated |
| 399 |
try { |
| 400 |
RxJavaPlugins.setInitIoSchedulerHandler(initReplaceWithImmediate); |
| 401 |
RxJavaPlugins.initIoScheduler(unsafeDefault); |
| 402 |
} finally { |
| 403 |
RxJavaPlugins.reset(); |
| 404 |
} |
| 405 |
|
| 406 |
// make sure the reset worked |
| 407 |
assertNotSame(ImmediateThinScheduler.INSTANCE, Schedulers.io()); |
| 408 |
} |
| 409 |
|
| 410 |
@Test |
| 411 |
public void defaultComputationSchedulerIsInitializedLazily() { |
| 412 |
// unsafe default Scheduler Supplier should not be evaluated |
| 413 |
try { |
| 414 |
RxJavaPlugins.setInitComputationSchedulerHandler(initReplaceWithImmediate); |
| 415 |
RxJavaPlugins.initComputationScheduler(unsafeDefault); |
| 416 |
} finally { |
| 417 |
RxJavaPlugins.reset(); |
| 418 |
} |
| 419 |
|
| 420 |
// make sure the reset worked |
| 421 |
assertNotSame(ImmediateThinScheduler.INSTANCE, Schedulers.computation()); |
| 422 |
} |
| 423 |
|
| 424 |
@Test |
| 425 |
public void defaultNewThreadSchedulerIsInitializedLazily() { |
| 426 |
// unsafe default Scheduler Supplier should not be evaluated |
| 427 |
try { |
| 428 |
RxJavaPlugins.setInitNewThreadSchedulerHandler(initReplaceWithImmediate); |
| 429 |
RxJavaPlugins.initNewThreadScheduler(unsafeDefault); |
| 430 |
} finally { |
| 431 |
RxJavaPlugins.reset(); |
| 432 |
} |
| 433 |
|
| 434 |
// make sure the reset worked |
| 435 |
assertNotSame(ImmediateThinScheduler.INSTANCE, Schedulers.newThread()); |
| 436 |
} |
| 437 |
|
| 438 |
@SuppressWarnings("rawtypes") |
| 439 |
@Test |
| 440 |
public void observableCreate() { |
| 441 |
try { |
| 442 |
RxJavaPlugins.setOnObservableAssembly(new Function<Observable, Observable>() { |
| 443 |
@Override |
| 444 |
public Observable apply(Observable t) { |
| 445 |
return new ObservableRange(1, 2); |
| 446 |
} |
| 447 |
}); |
| 448 |
|
| 449 |
Observable.range(10, 3) |
| 450 |
.test() |
| 451 |
.assertValues(1, 2) |
| 452 |
.assertNoErrors() |
| 453 |
.assertComplete(); |
| 454 |
} finally { |
| 455 |
RxJavaPlugins.reset(); |
| 456 |
} |
| 457 |
// make sure the reset worked |
| 458 |
Observable.range(10, 3) |
| 459 |
.test() |
| 460 |
.assertValues(10, 11, 12) |
| 461 |
.assertNoErrors() |
| 462 |
.assertComplete(); |
| 463 |
} |
| 464 |
|
| 465 |
@SuppressWarnings("rawtypes") |
| 466 |
@Test |
| 467 |
public void flowableCreate() { |
| 468 |
try { |
| 469 |
RxJavaPlugins.setOnFlowableAssembly(new Function<Flowable, Flowable>() { |
| 470 |
@Override |
| 471 |
public Flowable apply(Flowable t) { |
| 472 |
return new FlowableRange(1, 2); |
| 473 |
} |
| 474 |
}); |
| 475 |
|
| 476 |
Flowable.range(10, 3) |
| 477 |
.test() |
| 478 |
.assertValues(1, 2) |
| 479 |
.assertNoErrors() |
| 480 |
.assertComplete(); |
| 481 |
} finally { |
| 482 |
RxJavaPlugins.reset(); |
| 483 |
} |
| 484 |
// make sure the reset worked |
| 485 |
Flowable.range(10, 3) |
| 486 |
.test() |
| 487 |
.assertValues(10, 11, 12) |
| 488 |
.assertNoErrors() |
| 489 |
.assertComplete(); |
| 490 |
} |
| 491 |
|
| 492 |
@SuppressWarnings("rawtypes") |
| 493 |
@Test |
| 494 |
public void observableStart() { |
| 495 |
try { |
| 496 |
RxJavaPlugins.setOnObservableSubscribe(new BiFunction<Observable, Observer, Observer>() { |
| 497 |
@Override |
| 498 |
public Observer apply(Observable o, final Observer t) { |
| 499 |
return new Observer() { |
| 500 |
|
| 501 |
@Override |
| 502 |
public void onSubscribe(Disposable d) { |
| 503 |
t.onSubscribe(d); |
| 504 |
} |
| 505 |
|
| 506 |
@SuppressWarnings("unchecked") |
| 507 |
@Override |
| 508 |
public void onNext(Object value) { |
| 509 |
t.onNext((Integer)value - 9); |
| 510 |
} |
| 511 |
|
| 512 |
@Override |
| 513 |
public void onError(Throwable e) { |
| 514 |
t.onError(e); |
| 515 |
} |
| 516 |
|
| 517 |
@Override |
| 518 |
public void onComplete() { |
| 519 |
t.onComplete(); |
| 520 |
} |
| 521 |
|
| 522 |
}; |
| 523 |
} |
| 524 |
}); |
| 525 |
|
| 526 |
Observable.range(10, 3) |
| 527 |
.test() |
| 528 |
.assertValues(1, 2, 3) |
| 529 |
.assertNoErrors() |
| 530 |
.assertComplete(); |
| 531 |
} finally { |
| 532 |
RxJavaPlugins.reset(); |
| 533 |
} |
| 534 |
// make sure the reset worked |
| 535 |
Observable.range(10, 3) |
| 536 |
.test() |
| 537 |
.assertValues(10, 11, 12) |
| 538 |
.assertNoErrors() |
| 539 |
.assertComplete(); |
| 540 |
} |
| 541 |
|
| 542 |
@SuppressWarnings("rawtypes") |
| 543 |
@Test |
| 544 |
public void flowableStart() { |
| 545 |
try { |
| 546 |
RxJavaPlugins.setOnFlowableSubscribe(new BiFunction<Flowable, Subscriber, Subscriber>() { |
| 547 |
@Override |
| 548 |
public Subscriber apply(Flowable f, final Subscriber t) { |
| 549 |
return new Subscriber() { |
| 550 |
|
| 551 |
@Override |
| 552 |
public void onSubscribe(Subscription s) { |
| 553 |
t.onSubscribe(s); |
| 554 |
} |
| 555 |
|
| 556 |
@SuppressWarnings("unchecked") |
| 557 |
@Override |
| 558 |
public void onNext(Object value) { |
| 559 |
t.onNext((Integer)value - 9); |
| 560 |
} |
| 561 |
|
| 562 |
@Override |
| 563 |
public void onError(Throwable e) { |
| 564 |
t.onError(e); |
| 565 |
} |
| 566 |
|
| 567 |
@Override |
| 568 |
public void onComplete() { |
| 569 |
t.onComplete(); |
| 570 |
} |
| 571 |
|
| 572 |
}; |
| 573 |
} |
| 574 |
}); |
| 575 |
|
| 576 |
Flowable.range(10, 3) |
| 577 |
.test() |
| 578 |
.assertValues(1, 2, 3) |
| 579 |
.assertNoErrors() |
| 580 |
.assertComplete(); |
| 581 |
} finally { |
| 582 |
RxJavaPlugins.reset(); |
| 583 |
} |
| 584 |
// make sure the reset worked |
| 585 |
Flowable.range(10, 3) |
| 586 |
.test() |
| 587 |
.assertValues(10, 11, 12) |
| 588 |
.assertNoErrors() |
| 589 |
.assertComplete(); |
| 590 |
} |
| 591 |
|
| 592 |
@SuppressWarnings("rawtypes") |
| 593 |
@Test |
| 594 |
public void parallelFlowableStart() { |
| 595 |
try { |
| 596 |
RxJavaPlugins.setOnParallelSubscribe(new BiFunction<ParallelFlowable, Subscriber[], Subscriber[]>() { |
| 597 |
@Override |
| 598 |
public Subscriber[] apply(ParallelFlowable f, final Subscriber[] t) { |
| 599 |
return new Subscriber[] { new Subscriber() { |
| 600 |
|
| 601 |
@Override |
| 602 |
public void onSubscribe(Subscription s) { |
| 603 |
t[0].onSubscribe(s); |
| 604 |
} |
| 605 |
|
| 606 |
@SuppressWarnings("unchecked") |
| 607 |
@Override |
| 608 |
public void onNext(Object value) { |
| 609 |
t[0].onNext((Integer)value - 9); |
| 610 |
} |
| 611 |
|
| 612 |
@Override |
| 613 |
public void onError(Throwable e) { |
| 614 |
t[0].onError(e); |
| 615 |
} |
| 616 |
|
| 617 |
@Override |
| 618 |
public void onComplete() { |
| 619 |
t[0].onComplete(); |
| 620 |
} |
| 621 |
|
| 622 |
} |
| 623 |
}; |
| 624 |
} |
| 625 |
}); |
| 626 |
|
| 627 |
Flowable.range(10, 3) |
| 628 |
.parallel(1) |
| 629 |
.sequential() |
| 630 |
.test() |
| 631 |
.assertValues(1, 2, 3) |
| 632 |
.assertNoErrors() |
| 633 |
.assertComplete(); |
| 634 |
} finally { |
| 635 |
RxJavaPlugins.reset(); |
| 636 |
} |
| 637 |
// make sure the reset worked |
| 638 |
Flowable.range(10, 3) |
| 639 |
.parallel(1) |
| 640 |
.sequential() |
| 641 |
.test() |
| 642 |
.assertValues(10, 11, 12) |
| 643 |
.assertNoErrors() |
| 644 |
.assertComplete(); |
| 645 |
} |
| 646 |
|
| 647 |
@SuppressWarnings("rawtypes") |
| 648 |
@Test |
| 649 |
public void singleCreate() { |
| 650 |
try { |
| 651 |
RxJavaPlugins.setOnSingleAssembly(new Function<Single, Single>() { |
| 652 |
@Override |
| 653 |
public Single apply(Single t) { |
| 654 |
return new SingleJust<>(10); |
| 655 |
} |
| 656 |
}); |
| 657 |
|
| 658 |
Single.just(1) |
| 659 |
.test() |
| 660 |
.assertValue(10) |
| 661 |
.assertNoErrors() |
| 662 |
.assertComplete(); |
| 663 |
} finally { |
| 664 |
RxJavaPlugins.reset(); |
| 665 |
} |
| 666 |
// make sure the reset worked |
| 667 |
Single.just(1) |
| 668 |
.test() |
| 669 |
.assertValue(1) |
| 670 |
.assertNoErrors() |
| 671 |
.assertComplete(); |
| 672 |
} |
| 673 |
|
| 674 |
@SuppressWarnings("rawtypes") |
| 675 |
@Test |
| 676 |
public void singleStart() { |
| 677 |
try { |
| 678 |
RxJavaPlugins.setOnSingleSubscribe(new BiFunction<Single, SingleObserver, SingleObserver>() { |
| 679 |
@Override |
| 680 |
public SingleObserver apply(Single o, final SingleObserver t) { |
| 681 |
return new SingleObserver<Object>() { |
| 682 |
|
| 683 |
@Override |
| 684 |
public void onSubscribe(Disposable d) { |
| 685 |
t.onSubscribe(d); |
| 686 |
} |
| 687 |
|
| 688 |
@SuppressWarnings("unchecked") |
| 689 |
@Override |
| 690 |
public void onSuccess(Object value) { |
| 691 |
t.onSuccess(10); |
| 692 |
} |
| 693 |
|
| 694 |
@Override |
| 695 |
public void onError(Throwable e) { |
| 696 |
t.onError(e); |
| 697 |
} |
| 698 |
|
| 699 |
}; |
| 700 |
} |
| 701 |
}); |
| 702 |
|
| 703 |
Single.just(1) |
| 704 |
.test() |
| 705 |
.assertValue(10) |
| 706 |
.assertNoErrors() |
| 707 |
.assertComplete(); |
| 708 |
} finally { |
| 709 |
RxJavaPlugins.reset(); |
| 710 |
} |
| 711 |
// make sure the reset worked |
| 712 |
Single.just(1) |
| 713 |
.test() |
| 714 |
.assertValue(1) |
| 715 |
.assertNoErrors() |
| 716 |
.assertComplete(); |
| 717 |
} |
| 718 |
|
| 719 |
@Test |
| 720 |
public void completableCreate() { |
| 721 |
try { |
| 722 |
RxJavaPlugins.setOnCompletableAssembly(new Function<Completable, Completable>() { |
| 723 |
@Override |
| 724 |
public Completable apply(Completable t) { |
| 725 |
return new CompletableError(new TestException()); |
| 726 |
} |
| 727 |
}); |
| 728 |
|
| 729 |
Completable.complete() |
| 730 |
.test() |
| 731 |
.assertNoValues() |
| 732 |
.assertNotComplete() |
| 733 |
.assertError(TestException.class); |
| 734 |
} finally { |
| 735 |
RxJavaPlugins.reset(); |
| 736 |
} |
| 737 |
// make sure the reset worked |
| 738 |
Completable.complete() |
| 739 |
.test() |
| 740 |
.assertNoValues() |
| 741 |
.assertNoErrors() |
| 742 |
.assertComplete(); |
| 743 |
} |
| 744 |
|
| 745 |
@Test |
| 746 |
public void completableStart() { |
| 747 |
try { |
| 748 |
RxJavaPlugins.setOnCompletableSubscribe(new BiFunction<Completable, CompletableObserver, CompletableObserver>() { |
| 749 |
@Override |
| 750 |
public CompletableObserver apply(Completable o, final CompletableObserver t) { |
| 751 |
return new CompletableObserver() { |
| 752 |
@Override |
| 753 |
public void onSubscribe(Disposable d) { |
| 754 |
t.onSubscribe(d); |
| 755 |
} |
| 756 |
|
| 757 |
@Override |
| 758 |
public void onError(Throwable e) { |
| 759 |
t.onError(e); |
| 760 |
} |
| 761 |
|
| 762 |
@Override |
| 763 |
public void onComplete() { |
| 764 |
t.onError(new TestException()); |
| 765 |
} |
| 766 |
}; |
| 767 |
} |
| 768 |
}); |
| 769 |
|
| 770 |
Completable.complete() |
| 771 |
.test() |
| 772 |
.assertNoValues() |
| 773 |
.assertNotComplete() |
| 774 |
.assertError(TestException.class); |
| 775 |
} finally { |
| 776 |
RxJavaPlugins.reset(); |
| 777 |
} |
| 778 |
// make sure the reset worked |
| 779 |
|
| 780 |
Completable.complete() |
| 781 |
.test() |
| 782 |
.assertNoValues() |
| 783 |
.assertNoErrors() |
| 784 |
.assertComplete(); |
| 785 |
} |
| 786 |
|
| 787 |
void onSchedule(Worker w) throws InterruptedException { |
| 788 |
try { |
| 789 |
try { |
| 790 |
final AtomicInteger value = new AtomicInteger(); |
| 791 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 792 |
|
| 793 |
RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() { |
| 794 |
@Override |
| 795 |
public Runnable apply(Runnable t) { |
| 796 |
return new Runnable() { |
| 797 |
@Override |
| 798 |
public void run() { |
| 799 |
value.set(10); |
| 800 |
cdl.countDown(); |
| 801 |
} |
| 802 |
}; |
| 803 |
} |
| 804 |
}); |
| 805 |
|
| 806 |
w.schedule(new Runnable() { |
| 807 |
@Override |
| 808 |
public void run() { |
| 809 |
value.set(1); |
| 810 |
cdl.countDown(); |
| 811 |
} |
| 812 |
}); |
| 813 |
|
| 814 |
cdl.await(); |
| 815 |
|
| 816 |
assertEquals(10, value.get()); |
| 817 |
|
| 818 |
} finally { |
| 819 |
|
| 820 |
RxJavaPlugins.reset(); |
| 821 |
} |
| 822 |
|
| 823 |
// make sure the reset worked |
| 824 |
final AtomicInteger value = new AtomicInteger(); |
| 825 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 826 |
|
| 827 |
w.schedule(new Runnable() { |
| 828 |
@Override |
| 829 |
public void run() { |
| 830 |
value.set(1); |
| 831 |
cdl.countDown(); |
| 832 |
} |
| 833 |
}); |
| 834 |
|
| 835 |
cdl.await(); |
| 836 |
|
| 837 |
assertEquals(1, value.get()); |
| 838 |
} finally { |
| 839 |
w.dispose(); |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
@Test |
| 844 |
public void onScheduleComputation() throws InterruptedException { |
| 845 |
onSchedule(Schedulers.computation().createWorker()); |
| 846 |
} |
| 847 |
|
| 848 |
@Test |
| 849 |
public void onScheduleIO() throws InterruptedException { |
| 850 |
onSchedule(Schedulers.io().createWorker()); |
| 851 |
} |
| 852 |
|
| 853 |
@Test |
| 854 |
public void onScheduleNewThread() throws InterruptedException { |
| 855 |
onSchedule(Schedulers.newThread().createWorker()); |
| 856 |
} |
| 857 |
|
| 858 |
@Test |
| 859 |
public void onError() { |
| 860 |
try { |
| 861 |
final List<Throwable> list = new ArrayList<>(); |
| 862 |
|
| 863 |
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { |
| 864 |
@Override |
| 865 |
public void accept(Throwable t) { |
| 866 |
list.add(t); |
| 867 |
} |
| 868 |
}); |
| 869 |
|
| 870 |
RxJavaPlugins.onError(new TestException("Forced failure")); |
| 871 |
|
| 872 |
assertEquals(1, list.size()); |
| 873 |
assertUndeliverableTestException(list, 0, "Forced failure"); |
| 874 |
} finally { |
| 875 |
RxJavaPlugins.reset(); |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
@Test |
| 880 |
public void onErrorNoHandler() { |
| 881 |
try { |
| 882 |
final List<Throwable> list = new ArrayList<>(); |
| 883 |
|
| 884 |
RxJavaPlugins.setErrorHandler(null); |
| 885 |
|
| 886 |
Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() { |
| 887 |
|
| 888 |
@Override |
| 889 |
public void uncaughtException(Thread t, Throwable e) { |
| 890 |
list.add(e); |
| 891 |
|
| 892 |
} |
| 893 |
}); |
| 894 |
|
| 895 |
RxJavaPlugins.onError(new TestException("Forced failure")); |
| 896 |
|
| 897 |
Thread.currentThread().setUncaughtExceptionHandler(null); |
| 898 |
|
| 899 |
// this will be printed on the console and should not crash |
| 900 |
RxJavaPlugins.onError(new TestException("Forced failure 3")); |
| 901 |
|
| 902 |
assertEquals(1, list.size()); |
| 903 |
assertUndeliverableTestException(list, 0, "Forced failure"); |
| 904 |
} finally { |
| 905 |
RxJavaPlugins.reset(); |
| 906 |
Thread.currentThread().setUncaughtExceptionHandler(null); |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
@Test |
| 911 |
public void onErrorCrashes() { |
| 912 |
try { |
| 913 |
final List<Throwable> list = new ArrayList<>(); |
| 914 |
|
| 915 |
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { |
| 916 |
@Override |
| 917 |
public void accept(Throwable t) { |
| 918 |
throw new TestException("Forced failure 2"); |
| 919 |
} |
| 920 |
}); |
| 921 |
|
| 922 |
Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() { |
| 923 |
|
| 924 |
@Override |
| 925 |
public void uncaughtException(Thread t, Throwable e) { |
| 926 |
list.add(e); |
| 927 |
|
| 928 |
} |
| 929 |
}); |
| 930 |
|
| 931 |
RxJavaPlugins.onError(new TestException("Forced failure")); |
| 932 |
|
| 933 |
assertEquals(2, list.size()); |
| 934 |
assertTestException(list, 0, "Forced failure 2"); |
| 935 |
assertUndeliverableTestException(list, 1, "Forced failure"); |
| 936 |
|
| 937 |
Thread.currentThread().setUncaughtExceptionHandler(null); |
| 938 |
|
| 939 |
} finally { |
| 940 |
RxJavaPlugins.reset(); |
| 941 |
Thread.currentThread().setUncaughtExceptionHandler(null); |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
@Test |
| 946 |
public void onErrorWithNull() { |
| 947 |
try { |
| 948 |
final List<Throwable> list = new ArrayList<>(); |
| 949 |
|
| 950 |
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { |
| 951 |
@Override |
| 952 |
public void accept(Throwable t) { |
| 953 |
throw new TestException("Forced failure 2"); |
| 954 |
} |
| 955 |
}); |
| 956 |
|
| 957 |
Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() { |
| 958 |
|
| 959 |
@Override |
| 960 |
public void uncaughtException(Thread t, Throwable e) { |
| 961 |
list.add(e); |
| 962 |
|
| 963 |
} |
| 964 |
}); |
| 965 |
|
| 966 |
RxJavaPlugins.onError(null); |
| 967 |
|
| 968 |
assertEquals(2, list.size()); |
| 969 |
assertTestException(list, 0, "Forced failure 2"); |
| 970 |
assertNPE(list, 1); |
| 971 |
|
| 972 |
RxJavaPlugins.reset(); |
| 973 |
|
| 974 |
RxJavaPlugins.onError(null); |
| 975 |
|
| 976 |
assertNPE(list, 2); |
| 977 |
|
| 978 |
} finally { |
| 979 |
RxJavaPlugins.reset(); |
| 980 |
|
| 981 |
Thread.currentThread().setUncaughtExceptionHandler(null); |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
/** |
| 986 |
* Ensure set*() accepts a consumers/functions with wider bounds. |
| 987 |
* @throws Exception on error |
| 988 |
*/ |
| 989 |
@Test |
| 990 |
@SuppressWarnings("rawtypes") |
| 991 |
public void onErrorWithSuper() throws Exception { |
| 992 |
try { |
| 993 |
Consumer<? super Throwable> errorHandler = new Consumer<Throwable>() { |
| 994 |
@Override |
| 995 |
public void accept(Throwable t) { |
| 996 |
throw new TestException("Forced failure 2"); |
| 997 |
} |
| 998 |
}; |
| 999 |
RxJavaPlugins.setErrorHandler(errorHandler); |
| 1000 |
|
| 1001 |
Consumer<? super Throwable> errorHandler1 = RxJavaPlugins.getErrorHandler(); |
| 1002 |
assertSame(errorHandler, errorHandler1); |
| 1003 |
|
| 1004 |
Function<? super Scheduler, ? extends Scheduler> scheduler2scheduler = new Function<Scheduler, Scheduler>() { |
| 1005 |
@Override |
| 1006 |
public Scheduler apply(Scheduler scheduler) throws Exception { |
| 1007 |
return scheduler; |
| 1008 |
} |
| 1009 |
}; |
| 1010 |
Function<? super Supplier<Scheduler>, ? extends Scheduler> callable2scheduler = new Function<Supplier<Scheduler>, Scheduler>() { |
| 1011 |
@Override |
| 1012 |
public Scheduler apply(Supplier<Scheduler> schedulerSupplier) throws Throwable { |
| 1013 |
return schedulerSupplier.get(); |
| 1014 |
} |
| 1015 |
}; |
| 1016 |
Function<? super ConnectableFlowable, ? extends ConnectableFlowable> connectableFlowable2ConnectableFlowable = new Function<ConnectableFlowable, ConnectableFlowable>() { |
| 1017 |
@Override |
| 1018 |
public ConnectableFlowable apply(ConnectableFlowable connectableFlowable) throws Exception { |
| 1019 |
return connectableFlowable; |
| 1020 |
} |
| 1021 |
}; |
| 1022 |
Function<? super ConnectableObservable, ? extends ConnectableObservable> connectableObservable2ConnectableObservable = new Function<ConnectableObservable, ConnectableObservable>() { |
| 1023 |
@Override |
| 1024 |
public ConnectableObservable apply(ConnectableObservable connectableObservable) throws Exception { |
| 1025 |
return connectableObservable; |
| 1026 |
} |
| 1027 |
}; |
| 1028 |
Function<? super Flowable, ? extends Flowable> flowable2Flowable = new Function<Flowable, Flowable>() { |
| 1029 |
@Override |
| 1030 |
public Flowable apply(Flowable flowable) throws Exception { |
| 1031 |
return flowable; |
| 1032 |
} |
| 1033 |
}; |
| 1034 |
BiFunction<? super Flowable, ? super Subscriber, ? extends Subscriber> flowable2subscriber = new BiFunction<Flowable, Subscriber, Subscriber>() { |
| 1035 |
@Override |
| 1036 |
public Subscriber apply(Flowable flowable, Subscriber subscriber) throws Exception { |
| 1037 |
return subscriber; |
| 1038 |
} |
| 1039 |
}; |
| 1040 |
Function<Maybe, Maybe> maybe2maybe = new Function<Maybe, Maybe>() { |
| 1041 |
@Override |
| 1042 |
public Maybe apply(Maybe maybe) throws Exception { |
| 1043 |
return maybe; |
| 1044 |
} |
| 1045 |
}; |
| 1046 |
BiFunction<Maybe, MaybeObserver, MaybeObserver> maybe2observer = new BiFunction<Maybe, MaybeObserver, MaybeObserver>() { |
| 1047 |
@Override |
| 1048 |
public MaybeObserver apply(Maybe maybe, MaybeObserver maybeObserver) throws Exception { |
| 1049 |
return maybeObserver; |
| 1050 |
} |
| 1051 |
}; |
| 1052 |
Function<Observable, Observable> observable2observable = new Function<Observable, Observable>() { |
| 1053 |
@Override |
| 1054 |
public Observable apply(Observable observable) throws Exception { |
| 1055 |
return observable; |
| 1056 |
} |
| 1057 |
}; |
| 1058 |
BiFunction<? super Observable, ? super Observer, ? extends Observer> observable2observer = new BiFunction<Observable, Observer, Observer>() { |
| 1059 |
@Override |
| 1060 |
public Observer apply(Observable observable, Observer observer) throws Exception { |
| 1061 |
return observer; |
| 1062 |
} |
| 1063 |
}; |
| 1064 |
Function<? super ParallelFlowable, ? extends ParallelFlowable> parallelFlowable2parallelFlowable = new Function<ParallelFlowable, ParallelFlowable>() { |
| 1065 |
@Override |
| 1066 |
public ParallelFlowable apply(ParallelFlowable parallelFlowable) throws Exception { |
| 1067 |
return parallelFlowable; |
| 1068 |
} |
| 1069 |
}; |
| 1070 |
Function<Single, Single> single2single = new Function<Single, Single>() { |
| 1071 |
@Override |
| 1072 |
public Single apply(Single single) throws Exception { |
| 1073 |
return single; |
| 1074 |
} |
| 1075 |
}; |
| 1076 |
BiFunction<? super Single, ? super SingleObserver, ? extends SingleObserver> single2observer = new BiFunction<Single, SingleObserver, SingleObserver>() { |
| 1077 |
@Override |
| 1078 |
public SingleObserver apply(Single single, SingleObserver singleObserver) throws Exception { |
| 1079 |
return singleObserver; |
| 1080 |
} |
| 1081 |
}; |
| 1082 |
Function<? super Runnable, ? extends Runnable> runnable2runnable = new Function<Runnable, Runnable>() { |
| 1083 |
@Override |
| 1084 |
public Runnable apply(Runnable runnable) throws Exception { |
| 1085 |
return runnable; |
| 1086 |
} |
| 1087 |
}; |
| 1088 |
BiFunction<? super Completable, ? super CompletableObserver, ? extends CompletableObserver> completableObserver2completableObserver = new BiFunction<Completable, CompletableObserver, CompletableObserver>() { |
| 1089 |
@Override |
| 1090 |
public CompletableObserver apply(Completable completable, CompletableObserver completableObserver) throws Exception { |
| 1091 |
return completableObserver; |
| 1092 |
} |
| 1093 |
}; |
| 1094 |
Function<? super Completable, ? extends Completable> completable2completable = new Function<Completable, Completable>() { |
| 1095 |
@Override |
| 1096 |
public Completable apply(Completable completable) throws Exception { |
| 1097 |
return completable; |
| 1098 |
} |
| 1099 |
}; |
| 1100 |
|
| 1101 |
RxJavaPlugins.setInitComputationSchedulerHandler(callable2scheduler); |
| 1102 |
RxJavaPlugins.setComputationSchedulerHandler(scheduler2scheduler); |
| 1103 |
RxJavaPlugins.setIoSchedulerHandler(scheduler2scheduler); |
| 1104 |
RxJavaPlugins.setNewThreadSchedulerHandler(scheduler2scheduler); |
| 1105 |
RxJavaPlugins.setOnConnectableFlowableAssembly(connectableFlowable2ConnectableFlowable); |
| 1106 |
RxJavaPlugins.setOnConnectableObservableAssembly(connectableObservable2ConnectableObservable); |
| 1107 |
RxJavaPlugins.setOnFlowableAssembly(flowable2Flowable); |
| 1108 |
RxJavaPlugins.setOnFlowableSubscribe(flowable2subscriber); |
| 1109 |
RxJavaPlugins.setOnMaybeAssembly(maybe2maybe); |
| 1110 |
RxJavaPlugins.setOnMaybeSubscribe(maybe2observer); |
| 1111 |
RxJavaPlugins.setOnObservableAssembly(observable2observable); |
| 1112 |
RxJavaPlugins.setOnObservableSubscribe(observable2observer); |
| 1113 |
RxJavaPlugins.setOnParallelAssembly(parallelFlowable2parallelFlowable); |
| 1114 |
RxJavaPlugins.setOnSingleAssembly(single2single); |
| 1115 |
RxJavaPlugins.setOnSingleSubscribe(single2observer); |
| 1116 |
RxJavaPlugins.setScheduleHandler(runnable2runnable); |
| 1117 |
RxJavaPlugins.setSingleSchedulerHandler(scheduler2scheduler); |
| 1118 |
RxJavaPlugins.setOnCompletableSubscribe(completableObserver2completableObserver); |
| 1119 |
RxJavaPlugins.setOnCompletableAssembly(completable2completable); |
| 1120 |
RxJavaPlugins.setInitSingleSchedulerHandler(callable2scheduler); |
| 1121 |
RxJavaPlugins.setInitNewThreadSchedulerHandler(callable2scheduler); |
| 1122 |
RxJavaPlugins.setInitIoSchedulerHandler(callable2scheduler); |
| 1123 |
} finally { |
| 1124 |
RxJavaPlugins.reset(); |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
@SuppressWarnings({"rawtypes", "unchecked" }) |
| 1129 |
@Test |
| 1130 |
public void clearIsPassthrough() { |
| 1131 |
try { |
| 1132 |
RxJavaPlugins.reset(); |
| 1133 |
|
| 1134 |
assertNull(RxJavaPlugins.onAssembly((Observable)null)); |
| 1135 |
|
| 1136 |
assertNull(RxJavaPlugins.onAssembly((ConnectableObservable)null)); |
| 1137 |
|
| 1138 |
assertNull(RxJavaPlugins.onAssembly((Flowable)null)); |
| 1139 |
|
| 1140 |
assertNull(RxJavaPlugins.onAssembly((ConnectableFlowable)null)); |
| 1141 |
|
| 1142 |
Observable oos = new Observable() { |
| 1143 |
@Override |
| 1144 |
public void subscribeActual(Observer t) { |
| 1145 |
|
| 1146 |
} |
| 1147 |
}; |
| 1148 |
|
| 1149 |
Flowable fos = new Flowable() { |
| 1150 |
@Override |
| 1151 |
public void subscribeActual(Subscriber t) { |
| 1152 |
|
| 1153 |
} |
| 1154 |
}; |
| 1155 |
|
| 1156 |
assertSame(oos, RxJavaPlugins.onAssembly(oos)); |
| 1157 |
|
| 1158 |
assertSame(fos, RxJavaPlugins.onAssembly(fos)); |
| 1159 |
|
| 1160 |
assertNull(RxJavaPlugins.onAssembly((Single)null)); |
| 1161 |
|
| 1162 |
Single sos = new Single() { |
| 1163 |
@Override |
| 1164 |
public void subscribeActual(SingleObserver t) { |
| 1165 |
|
| 1166 |
} |
| 1167 |
}; |
| 1168 |
|
| 1169 |
assertSame(sos, RxJavaPlugins.onAssembly(sos)); |
| 1170 |
|
| 1171 |
assertNull(RxJavaPlugins.onAssembly((Completable)null)); |
| 1172 |
|
| 1173 |
Completable cos = new Completable() { |
| 1174 |
@Override |
| 1175 |
public void subscribeActual(CompletableObserver t) { |
| 1176 |
|
| 1177 |
} |
| 1178 |
}; |
| 1179 |
|
| 1180 |
assertSame(cos, RxJavaPlugins.onAssembly(cos)); |
| 1181 |
|
| 1182 |
assertNull(RxJavaPlugins.onAssembly((Maybe)null)); |
| 1183 |
|
| 1184 |
Maybe myb = new Maybe() { |
| 1185 |
@Override |
| 1186 |
public void subscribeActual(MaybeObserver t) { |
| 1187 |
|
| 1188 |
} |
| 1189 |
}; |
| 1190 |
|
| 1191 |
assertSame(myb, RxJavaPlugins.onAssembly(myb)); |
| 1192 |
|
| 1193 |
Runnable action = Functions.EMPTY_RUNNABLE; |
| 1194 |
assertSame(action, RxJavaPlugins.onSchedule(action)); |
| 1195 |
|
| 1196 |
class AllSubscriber implements Subscriber, Observer, SingleObserver, CompletableObserver, MaybeObserver { |
| 1197 |
|
| 1198 |
@Override |
| 1199 |
public void onSuccess(Object value) { |
| 1200 |
|
| 1201 |
} |
| 1202 |
|
| 1203 |
@Override |
| 1204 |
public void onSubscribe(Disposable d) { |
| 1205 |
|
| 1206 |
} |
| 1207 |
|
| 1208 |
@Override |
| 1209 |
public void onSubscribe(Subscription s) { |
| 1210 |
|
| 1211 |
} |
| 1212 |
|
| 1213 |
@Override |
| 1214 |
public void onNext(Object t) { |
| 1215 |
|
| 1216 |
} |
| 1217 |
|
| 1218 |
@Override |
| 1219 |
public void onError(Throwable t) { |
| 1220 |
|
| 1221 |
} |
| 1222 |
|
| 1223 |
@Override |
| 1224 |
public void onComplete() { |
| 1225 |
|
| 1226 |
} |
| 1227 |
|
| 1228 |
} |
| 1229 |
|
| 1230 |
AllSubscriber all = new AllSubscriber(); |
| 1231 |
Subscriber[] allArray = { all }; |
| 1232 |
|
| 1233 |
assertNull(RxJavaPlugins.onSubscribe(Observable.never(), null)); |
| 1234 |
|
| 1235 |
assertSame(all, RxJavaPlugins.onSubscribe(Observable.never(), all)); |
| 1236 |
|
| 1237 |
assertNull(RxJavaPlugins.onSubscribe(Flowable.never(), null)); |
| 1238 |
|
| 1239 |
assertSame(all, RxJavaPlugins.onSubscribe(Flowable.never(), all)); |
| 1240 |
|
| 1241 |
assertNull(RxJavaPlugins.onSubscribe(Single.just(1), null)); |
| 1242 |
|
| 1243 |
assertSame(all, RxJavaPlugins.onSubscribe(Single.just(1), all)); |
| 1244 |
|
| 1245 |
assertNull(RxJavaPlugins.onSubscribe(Completable.never(), null)); |
| 1246 |
|
| 1247 |
assertSame(all, RxJavaPlugins.onSubscribe(Completable.never(), all)); |
| 1248 |
|
| 1249 |
assertNull(RxJavaPlugins.onSubscribe(Maybe.never(), null)); |
| 1250 |
|
| 1251 |
assertSame(all, RxJavaPlugins.onSubscribe(Maybe.never(), all)); |
| 1252 |
|
| 1253 |
assertNull(RxJavaPlugins.onSubscribe(Flowable.never().parallel(), null)); |
| 1254 |
|
| 1255 |
assertSame(allArray, RxJavaPlugins.onSubscribe(Flowable.never().parallel(), allArray)); |
| 1256 |
|
| 1257 |
final Scheduler s = ImmediateThinScheduler.INSTANCE; |
| 1258 |
Supplier<Scheduler> c = new Supplier<Scheduler>() { |
| 1259 |
@Override |
| 1260 |
public Scheduler get() throws Exception { |
| 1261 |
return s; |
| 1262 |
} |
| 1263 |
}; |
| 1264 |
assertSame(s, RxJavaPlugins.onComputationScheduler(s)); |
| 1265 |
|
| 1266 |
assertSame(s, RxJavaPlugins.onIoScheduler(s)); |
| 1267 |
|
| 1268 |
assertSame(s, RxJavaPlugins.onNewThreadScheduler(s)); |
| 1269 |
|
| 1270 |
assertSame(s, RxJavaPlugins.onSingleScheduler(s)); |
| 1271 |
|
| 1272 |
assertSame(s, RxJavaPlugins.initComputationScheduler(c)); |
| 1273 |
|
| 1274 |
assertSame(s, RxJavaPlugins.initIoScheduler(c)); |
| 1275 |
|
| 1276 |
assertSame(s, RxJavaPlugins.initNewThreadScheduler(c)); |
| 1277 |
|
| 1278 |
assertSame(s, RxJavaPlugins.initSingleScheduler(c)); |
| 1279 |
|
| 1280 |
} finally { |
| 1281 |
RxJavaPlugins.reset(); |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
static void assertTestException(List<Throwable> list, int index, String message) { |
| 1286 |
assertTrue(list.get(index).toString(), list.get(index) instanceof TestException); |
| 1287 |
assertEquals(message, list.get(index).getMessage()); |
| 1288 |
} |
| 1289 |
|
| 1290 |
static void assertUndeliverableTestException(List<Throwable> list, int index, String message) { |
| 1291 |
assertTrue(list.get(index).toString(), list.get(index).getCause() instanceof TestException); |
| 1292 |
assertEquals(message, list.get(index).getCause().getMessage()); |
| 1293 |
} |
| 1294 |
|
| 1295 |
static void assertNPE(List<Throwable> list, int index) { |
| 1296 |
assertTrue(list.get(index).toString(), list.get(index) instanceof NullPointerException); |
| 1297 |
} |
| 1298 |
|
| 1299 |
@SuppressWarnings("rawtypes") |
| 1300 |
@Test |
| 1301 |
public void overrideConnectableObservable() { |
| 1302 |
try { |
| 1303 |
RxJavaPlugins.setOnConnectableObservableAssembly(new Function<ConnectableObservable, ConnectableObservable>() { |
| 1304 |
@Override |
| 1305 |
public ConnectableObservable apply(ConnectableObservable co) throws Exception { |
| 1306 |
return new ConnectableObservable() { |
| 1307 |
|
| 1308 |
@Override |
| 1309 |
public void connect(Consumer connection) { |
| 1310 |
|
| 1311 |
} |
| 1312 |
|
| 1313 |
@Override |
| 1314 |
public void reset() { |
| 1315 |
// nothing to do in this test |
| 1316 |
} |
| 1317 |
|
| 1318 |
@SuppressWarnings("unchecked") |
| 1319 |
@Override |
| 1320 |
protected void subscribeActual(Observer observer) { |
| 1321 |
observer.onSubscribe(Disposable.empty()); |
| 1322 |
observer.onNext(10); |
| 1323 |
observer.onComplete(); |
| 1324 |
} |
| 1325 |
}; |
| 1326 |
} |
| 1327 |
}); |
| 1328 |
|
| 1329 |
Observable |
| 1330 |
.just(1) |
| 1331 |
.publish() |
| 1332 |
.autoConnect() |
| 1333 |
.test() |
| 1334 |
.assertResult(10); |
| 1335 |
|
| 1336 |
} finally { |
| 1337 |
RxJavaPlugins.reset(); |
| 1338 |
} |
| 1339 |
|
| 1340 |
Observable |
| 1341 |
.just(1) |
| 1342 |
.publish() |
| 1343 |
.autoConnect() |
| 1344 |
.test() |
| 1345 |
.assertResult(1); |
| 1346 |
} |
| 1347 |
|
| 1348 |
@SuppressWarnings("rawtypes") |
| 1349 |
@Test |
| 1350 |
public void overrideConnectableFlowable() { |
| 1351 |
try { |
| 1352 |
RxJavaPlugins.setOnConnectableFlowableAssembly(new Function<ConnectableFlowable, ConnectableFlowable>() { |
| 1353 |
@Override |
| 1354 |
public ConnectableFlowable apply(ConnectableFlowable co) throws Exception { |
| 1355 |
return new ConnectableFlowable() { |
| 1356 |
|
| 1357 |
@Override |
| 1358 |
public void connect(Consumer connection) { |
| 1359 |
|
| 1360 |
} |
| 1361 |
|
| 1362 |
@Override |
| 1363 |
public void reset() { |
| 1364 |
// nothing to do in this test |
| 1365 |
} |
| 1366 |
|
| 1367 |
@SuppressWarnings("unchecked") |
| 1368 |
@Override |
| 1369 |
protected void subscribeActual(Subscriber subscriber) { |
| 1370 |
subscriber.onSubscribe(new ScalarSubscription(subscriber, 10)); |
| 1371 |
} |
| 1372 |
}; |
| 1373 |
} |
| 1374 |
}); |
| 1375 |
|
| 1376 |
Flowable |
| 1377 |
.just(1) |
| 1378 |
.publish() |
| 1379 |
.autoConnect() |
| 1380 |
.test() |
| 1381 |
.assertResult(10); |
| 1382 |
|
| 1383 |
} finally { |
| 1384 |
RxJavaPlugins.reset(); |
| 1385 |
} |
| 1386 |
|
| 1387 |
Flowable |
| 1388 |
.just(1) |
| 1389 |
.publish() |
| 1390 |
.autoConnect() |
| 1391 |
.test() |
| 1392 |
.assertResult(1); |
| 1393 |
} |
| 1394 |
|
| 1395 |
@SuppressWarnings("rawtypes") |
| 1396 |
@Test |
| 1397 |
public void assemblyHookCrashes() { |
| 1398 |
try { |
| 1399 |
RxJavaPlugins.setOnFlowableAssembly(new Function<Flowable, Flowable>() { |
| 1400 |
@Override |
| 1401 |
public Flowable apply(Flowable f) throws Exception { |
| 1402 |
throw new IllegalArgumentException(); |
| 1403 |
} |
| 1404 |
}); |
| 1405 |
|
| 1406 |
try { |
| 1407 |
Flowable.empty(); |
| 1408 |
fail("Should have thrown!"); |
| 1409 |
} catch (IllegalArgumentException ex) { |
| 1410 |
// expected |
| 1411 |
} |
| 1412 |
|
| 1413 |
RxJavaPlugins.setOnFlowableAssembly(new Function<Flowable, Flowable>() { |
| 1414 |
@Override |
| 1415 |
public Flowable apply(Flowable f) throws Exception { |
| 1416 |
throw new InternalError(); |
| 1417 |
} |
| 1418 |
}); |
| 1419 |
|
| 1420 |
try { |
| 1421 |
Flowable.empty(); |
| 1422 |
fail("Should have thrown!"); |
| 1423 |
} catch (InternalError ex) { |
| 1424 |
// expected |
| 1425 |
} |
| 1426 |
|
| 1427 |
RxJavaPlugins.setOnFlowableAssembly(new Function<Flowable, Flowable>() { |
| 1428 |
@Override |
| 1429 |
public Flowable apply(Flowable f) throws Exception { |
| 1430 |
throw new IOException(); |
| 1431 |
} |
| 1432 |
}); |
| 1433 |
|
| 1434 |
try { |
| 1435 |
Flowable.empty(); |
| 1436 |
fail("Should have thrown!"); |
| 1437 |
} catch (RuntimeException ex) { |
| 1438 |
if (!(ex.getCause() instanceof IOException)) { |
| 1439 |
fail(ex.getCause().toString() + ": Should have thrown RuntimeException(IOException)"); |
| 1440 |
} |
| 1441 |
} |
| 1442 |
} finally { |
| 1443 |
RxJavaPlugins.reset(); |
| 1444 |
} |
| 1445 |
} |
| 1446 |
|
| 1447 |
@SuppressWarnings("rawtypes") |
| 1448 |
@Test |
| 1449 |
public void subscribeHookCrashes() { |
| 1450 |
try { |
| 1451 |
RxJavaPlugins.setOnFlowableSubscribe(new BiFunction<Flowable, Subscriber, Subscriber>() { |
| 1452 |
@Override |
| 1453 |
public Subscriber apply(Flowable f, Subscriber s) throws Exception { |
| 1454 |
throw new IllegalArgumentException(); |
| 1455 |
} |
| 1456 |
}); |
| 1457 |
|
| 1458 |
try { |
| 1459 |
Flowable.empty().test(); |
| 1460 |
fail("Should have thrown!"); |
| 1461 |
} catch (NullPointerException ex) { |
| 1462 |
if (!(ex.getCause() instanceof IllegalArgumentException)) { |
| 1463 |
fail(ex.getCause().toString() + ": Should have thrown NullPointerException(IllegalArgumentException)"); |
| 1464 |
} |
| 1465 |
} |
| 1466 |
|
| 1467 |
RxJavaPlugins.setOnFlowableSubscribe(new BiFunction<Flowable, Subscriber, Subscriber>() { |
| 1468 |
@Override |
| 1469 |
public Subscriber apply(Flowable f, Subscriber s) throws Exception { |
| 1470 |
throw new InternalError(); |
| 1471 |
} |
| 1472 |
}); |
| 1473 |
|
| 1474 |
try { |
| 1475 |
Flowable.empty().test(); |
| 1476 |
fail("Should have thrown!"); |
| 1477 |
} catch (InternalError ex) { |
| 1478 |
// expected |
| 1479 |
} |
| 1480 |
|
| 1481 |
RxJavaPlugins.setOnFlowableSubscribe(new BiFunction<Flowable, Subscriber, Subscriber>() { |
| 1482 |
@Override |
| 1483 |
public Subscriber apply(Flowable f, Subscriber s) throws Exception { |
| 1484 |
throw new IOException(); |
| 1485 |
} |
| 1486 |
}); |
| 1487 |
|
| 1488 |
try { |
| 1489 |
Flowable.empty().test(); |
| 1490 |
fail("Should have thrown!"); |
| 1491 |
} catch (NullPointerException ex) { |
| 1492 |
if (!(ex.getCause() instanceof RuntimeException)) { |
| 1493 |
fail(ex.getCause().toString() + ": Should have thrown NullPointerException(RuntimeException(IOException))"); |
| 1494 |
} |
| 1495 |
if (!(ex.getCause().getCause() instanceof IOException)) { |
| 1496 |
fail(ex.getCause().toString() + ": Should have thrown NullPointerException(RuntimeException(IOException))"); |
| 1497 |
} |
| 1498 |
} |
| 1499 |
} finally { |
| 1500 |
RxJavaPlugins.reset(); |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
@SuppressWarnings("rawtypes") |
| 1505 |
@Test |
| 1506 |
public void maybeCreate() { |
| 1507 |
try { |
| 1508 |
RxJavaPlugins.setOnMaybeAssembly(new Function<Maybe, Maybe>() { |
| 1509 |
@Override |
| 1510 |
public Maybe apply(Maybe t) { |
| 1511 |
return new MaybeError(new TestException()); |
| 1512 |
} |
| 1513 |
}); |
| 1514 |
|
| 1515 |
Maybe.empty() |
| 1516 |
.test() |
| 1517 |
.assertNoValues() |
| 1518 |
.assertNotComplete() |
| 1519 |
.assertError(TestException.class); |
| 1520 |
} finally { |
| 1521 |
RxJavaPlugins.reset(); |
| 1522 |
} |
| 1523 |
// make sure the reset worked |
| 1524 |
Maybe.empty() |
| 1525 |
.test() |
| 1526 |
.assertNoValues() |
| 1527 |
.assertNoErrors() |
| 1528 |
.assertComplete(); |
| 1529 |
} |
| 1530 |
|
| 1531 |
@Test |
| 1532 |
@SuppressWarnings("rawtypes") |
| 1533 |
public void maybeStart() { |
| 1534 |
try { |
| 1535 |
RxJavaPlugins.setOnMaybeSubscribe(new BiFunction<Maybe, MaybeObserver, MaybeObserver>() { |
| 1536 |
@Override |
| 1537 |
public MaybeObserver apply(Maybe o, final MaybeObserver t) { |
| 1538 |
return new MaybeObserver() { |
| 1539 |
@Override |
| 1540 |
public void onSubscribe(Disposable d) { |
| 1541 |
t.onSubscribe(d); |
| 1542 |
} |
| 1543 |
|
| 1544 |
@SuppressWarnings("unchecked") |
| 1545 |
@Override |
| 1546 |
public void onSuccess(Object value) { |
| 1547 |
t.onSuccess(value); |
| 1548 |
} |
| 1549 |
|
| 1550 |
@Override |
| 1551 |
public void onError(Throwable e) { |
| 1552 |
t.onError(e); |
| 1553 |
} |
| 1554 |
|
| 1555 |
@Override |
| 1556 |
public void onComplete() { |
| 1557 |
t.onError(new TestException()); |
| 1558 |
} |
| 1559 |
}; |
| 1560 |
} |
| 1561 |
}); |
| 1562 |
|
| 1563 |
Maybe.empty() |
| 1564 |
.test() |
| 1565 |
.assertNoValues() |
| 1566 |
.assertNotComplete() |
| 1567 |
.assertError(TestException.class); |
| 1568 |
} finally { |
| 1569 |
RxJavaPlugins.reset(); |
| 1570 |
} |
| 1571 |
// make sure the reset worked |
| 1572 |
|
| 1573 |
Maybe.empty() |
| 1574 |
.test() |
| 1575 |
.assertNoValues() |
| 1576 |
.assertNoErrors() |
| 1577 |
.assertComplete(); |
| 1578 |
} |
| 1579 |
|
| 1580 |
@Test |
| 1581 |
public void onErrorNull() { |
| 1582 |
try { |
| 1583 |
final AtomicReference<Throwable> t = new AtomicReference<>(); |
| 1584 |
|
| 1585 |
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { |
| 1586 |
@Override |
| 1587 |
public void accept(final Throwable throwable) throws Exception { |
| 1588 |
t.set(throwable); |
| 1589 |
} |
| 1590 |
}); |
| 1591 |
|
| 1592 |
RxJavaPlugins.onError(null); |
| 1593 |
|
| 1594 |
final Throwable throwable = t.get(); |
| 1595 |
assertEquals(ExceptionHelper.nullWarning("onError called with a null Throwable."), throwable.getMessage()); |
| 1596 |
assertTrue(throwable instanceof NullPointerException); |
| 1597 |
} finally { |
| 1598 |
RxJavaPlugins.reset(); |
| 1599 |
} |
| 1600 |
} |
| 1601 |
|
| 1602 |
private static void verifyThread(Scheduler scheduler, String expectedThreadName) |
| 1603 |
throws AssertionError { |
| 1604 |
assertNotNull(scheduler); |
| 1605 |
Worker w = scheduler.createWorker(); |
| 1606 |
try { |
| 1607 |
final AtomicReference<Thread> value = new AtomicReference<>(); |
| 1608 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 1609 |
|
| 1610 |
w.schedule(new Runnable() { |
| 1611 |
@Override |
| 1612 |
public void run() { |
| 1613 |
value.set(Thread.currentThread()); |
| 1614 |
cdl.countDown(); |
| 1615 |
} |
| 1616 |
}); |
| 1617 |
|
| 1618 |
cdl.await(); |
| 1619 |
|
| 1620 |
Thread t = value.get(); |
| 1621 |
assertNotNull(t); |
| 1622 |
assertEquals(expectedThreadName, t.getName()); |
| 1623 |
} catch (Exception e) { |
| 1624 |
fail(); |
| 1625 |
} finally { |
| 1626 |
w.dispose(); |
| 1627 |
} |
| 1628 |
} |
| 1629 |
|
| 1630 |
@Test |
| 1631 |
public void createComputationScheduler() { |
| 1632 |
final String name = "ComputationSchedulerTest"; |
| 1633 |
ThreadFactory factory = new ThreadFactory() { |
| 1634 |
@Override |
| 1635 |
public Thread newThread(Runnable r) { |
| 1636 |
return new Thread(r, name); |
| 1637 |
} |
| 1638 |
}; |
| 1639 |
|
| 1640 |
final Scheduler customScheduler = RxJavaPlugins.createComputationScheduler(factory); |
| 1641 |
RxJavaPlugins.setComputationSchedulerHandler(new Function<Scheduler, Scheduler>() { |
| 1642 |
@Override |
| 1643 |
public Scheduler apply(Scheduler scheduler) throws Exception { |
| 1644 |
return customScheduler; |
| 1645 |
} |
| 1646 |
}); |
| 1647 |
|
| 1648 |
try { |
| 1649 |
verifyThread(Schedulers.computation(), name); |
| 1650 |
} finally { |
| 1651 |
customScheduler.shutdown(); |
| 1652 |
RxJavaPlugins.reset(); |
| 1653 |
} |
| 1654 |
} |
| 1655 |
|
| 1656 |
@Test |
| 1657 |
public void createIoScheduler() { |
| 1658 |
final String name = "IoSchedulerTest"; |
| 1659 |
ThreadFactory factory = new ThreadFactory() { |
| 1660 |
@Override |
| 1661 |
public Thread newThread(Runnable r) { |
| 1662 |
return new Thread(r, name); |
| 1663 |
} |
| 1664 |
}; |
| 1665 |
|
| 1666 |
final Scheduler customScheduler = RxJavaPlugins.createIoScheduler(factory); |
| 1667 |
RxJavaPlugins.setIoSchedulerHandler(new Function<Scheduler, Scheduler>() { |
| 1668 |
@Override |
| 1669 |
public Scheduler apply(Scheduler scheduler) throws Exception { |
| 1670 |
return customScheduler; |
| 1671 |
} |
| 1672 |
}); |
| 1673 |
|
| 1674 |
try { |
| 1675 |
verifyThread(Schedulers.io(), name); |
| 1676 |
} finally { |
| 1677 |
customScheduler.shutdown(); |
| 1678 |
RxJavaPlugins.reset(); |
| 1679 |
} |
| 1680 |
} |
| 1681 |
|
| 1682 |
@Test |
| 1683 |
public void createNewThreadScheduler() { |
| 1684 |
final String name = "NewThreadSchedulerTest"; |
| 1685 |
ThreadFactory factory = new ThreadFactory() { |
| 1686 |
@Override |
| 1687 |
public Thread newThread(Runnable r) { |
| 1688 |
return new Thread(r, name); |
| 1689 |
} |
| 1690 |
}; |
| 1691 |
|
| 1692 |
final Scheduler customScheduler = RxJavaPlugins.createNewThreadScheduler(factory); |
| 1693 |
RxJavaPlugins.setNewThreadSchedulerHandler(new Function<Scheduler, Scheduler>() { |
| 1694 |
@Override |
| 1695 |
public Scheduler apply(Scheduler scheduler) throws Exception { |
| 1696 |
return customScheduler; |
| 1697 |
} |
| 1698 |
}); |
| 1699 |
|
| 1700 |
try { |
| 1701 |
verifyThread(Schedulers.newThread(), name); |
| 1702 |
} finally { |
| 1703 |
customScheduler.shutdown(); |
| 1704 |
RxJavaPlugins.reset(); |
| 1705 |
} |
| 1706 |
} |
| 1707 |
|
| 1708 |
@Test |
| 1709 |
public void createSingleScheduler() { |
| 1710 |
final String name = "SingleSchedulerTest"; |
| 1711 |
ThreadFactory factory = new ThreadFactory() { |
| 1712 |
@Override |
| 1713 |
public Thread newThread(Runnable r) { |
| 1714 |
return new Thread(r, name); |
| 1715 |
} |
| 1716 |
}; |
| 1717 |
|
| 1718 |
final Scheduler customScheduler = RxJavaPlugins.createSingleScheduler(factory); |
| 1719 |
|
| 1720 |
RxJavaPlugins.setSingleSchedulerHandler(new Function<Scheduler, Scheduler>() { |
| 1721 |
@Override |
| 1722 |
public Scheduler apply(Scheduler scheduler) throws Exception { |
| 1723 |
return customScheduler; |
| 1724 |
} |
| 1725 |
}); |
| 1726 |
|
| 1727 |
try { |
| 1728 |
verifyThread(Schedulers.single(), name); |
| 1729 |
} finally { |
| 1730 |
customScheduler.shutdown(); |
| 1731 |
RxJavaPlugins.reset(); |
| 1732 |
} |
| 1733 |
} |
| 1734 |
|
| 1735 |
@Test |
| 1736 |
public void onBeforeBlocking() { |
| 1737 |
try { |
| 1738 |
RxJavaPlugins.setOnBeforeBlocking(new BooleanSupplier() { |
| 1739 |
@Override |
| 1740 |
public boolean getAsBoolean() throws Exception { |
| 1741 |
throw new IllegalArgumentException(); |
| 1742 |
} |
| 1743 |
}); |
| 1744 |
|
| 1745 |
try { |
| 1746 |
RxJavaPlugins.onBeforeBlocking(); |
| 1747 |
fail("Should have thrown"); |
| 1748 |
} catch (IllegalArgumentException ex) { |
| 1749 |
// expected |
| 1750 |
} |
| 1751 |
} finally { |
| 1752 |
RxJavaPlugins.reset(); |
| 1753 |
} |
| 1754 |
} |
| 1755 |
|
| 1756 |
@SuppressWarnings("rawtypes") |
| 1757 |
@Test |
| 1758 |
public void onParallelAssembly() { |
| 1759 |
try { |
| 1760 |
RxJavaPlugins.setOnParallelAssembly(new Function<ParallelFlowable, ParallelFlowable>() { |
| 1761 |
@Override |
| 1762 |
public ParallelFlowable apply(ParallelFlowable pf) throws Exception { |
| 1763 |
return new ParallelFromPublisher<>(Flowable.just(2), 2, 2); |
| 1764 |
} |
| 1765 |
}); |
| 1766 |
|
| 1767 |
Flowable.just(1) |
| 1768 |
.parallel() |
| 1769 |
.sequential() |
| 1770 |
.test() |
| 1771 |
.assertResult(2); |
| 1772 |
} finally { |
| 1773 |
RxJavaPlugins.reset(); |
| 1774 |
} |
| 1775 |
|
| 1776 |
Flowable.just(1) |
| 1777 |
.parallel() |
| 1778 |
.sequential() |
| 1779 |
.test() |
| 1780 |
.assertResult(1); |
| 1781 |
} |
| 1782 |
|
| 1783 |
@Test |
| 1784 |
public void isBug() { |
| 1785 |
assertFalse(RxJavaPlugins.isBug(new RuntimeException())); |
| 1786 |
assertFalse(RxJavaPlugins.isBug(new IOException())); |
| 1787 |
assertFalse(RxJavaPlugins.isBug(new InterruptedException())); |
| 1788 |
assertFalse(RxJavaPlugins.isBug(new InterruptedIOException())); |
| 1789 |
|
| 1790 |
assertTrue(RxJavaPlugins.isBug(new NullPointerException())); |
| 1791 |
assertTrue(RxJavaPlugins.isBug(new IllegalArgumentException())); |
| 1792 |
assertTrue(RxJavaPlugins.isBug(new IllegalStateException())); |
| 1793 |
assertTrue(RxJavaPlugins.isBug(new MissingBackpressureException())); |
| 1794 |
assertTrue(RxJavaPlugins.isBug(new ProtocolViolationException(""))); |
| 1795 |
assertTrue(RxJavaPlugins.isBug(new UndeliverableException(new TestException()))); |
| 1796 |
assertTrue(RxJavaPlugins.isBug(new CompositeException(new TestException()))); |
| 1797 |
assertTrue(RxJavaPlugins.isBug(new OnErrorNotImplementedException(new TestException()))); |
| 1798 |
} |
| 1799 |
} |