use of io.reactivex.Scheduler in project RxAndroid by ReactiveX.
the class RxAndroidPluginsTest method initMainThreadHandlerCalled.
@Test
public void initMainThreadHandlerCalled() {
final AtomicReference<Callable<Scheduler>> schedulerRef = new AtomicReference<>();
final Scheduler newScheduler = new EmptyScheduler();
RxAndroidPlugins.setInitMainThreadSchedulerHandler(new Function<Callable<Scheduler>, Scheduler>() {
@Override
public Scheduler apply(Callable<Scheduler> scheduler) {
schedulerRef.set(scheduler);
return newScheduler;
}
});
Callable<Scheduler> scheduler = new Callable<Scheduler>() {
@Override
public Scheduler call() throws Exception {
throw new AssertionError();
}
};
Scheduler actual = RxAndroidPlugins.initMainThreadScheduler(scheduler);
assertSame(newScheduler, actual);
assertSame(scheduler, schedulerRef.get());
}
use of io.reactivex.Scheduler in project RxAndroid by ReactiveX.
the class RxAndroidPluginsTest method mainThreadHandlerCalled.
@Test
public void mainThreadHandlerCalled() {
final AtomicReference<Scheduler> schedulerRef = new AtomicReference<>();
final Scheduler newScheduler = new EmptyScheduler();
RxAndroidPlugins.setMainThreadSchedulerHandler(new Function<Scheduler, Scheduler>() {
@Override
public Scheduler apply(Scheduler scheduler) {
schedulerRef.set(scheduler);
return newScheduler;
}
});
Scheduler scheduler = new EmptyScheduler();
Scheduler actual = RxAndroidPlugins.onMainThreadScheduler(scheduler);
assertSame(newScheduler, actual);
assertSame(scheduler, schedulerRef.get());
}
use of io.reactivex.Scheduler in project RxAndroid by ReactiveX.
the class RxAndroidPluginsTest method resetClearsInitMainThreadHandler.
@Test
public void resetClearsInitMainThreadHandler() throws Exception {
RxAndroidPlugins.setInitMainThreadSchedulerHandler(new Function<Callable<Scheduler>, Scheduler>() {
@Override
public Scheduler apply(Callable<Scheduler> scheduler) {
throw new AssertionError();
}
});
final Scheduler scheduler = new EmptyScheduler();
Callable<Scheduler> schedulerCallable = new Callable<Scheduler>() {
@Override
public Scheduler call() throws Exception {
return scheduler;
}
};
RxAndroidPlugins.reset();
Scheduler actual = RxAndroidPlugins.initMainThreadScheduler(schedulerCallable);
assertSame(schedulerCallable.call(), actual);
}
use of io.reactivex.Scheduler in project RxJava by ReactiveX.
the class BehaviorProcessorTest method testEmissionSubscriptionRace.
// FIXME RS subscribers are not allowed to throw
// @Test
// public void testOnErrorThrowsDoesntPreventDelivery() {
// BehaviorSubject<String> ps = BehaviorSubject.create();
//
// ps.subscribe();
// TestSubscriber<String> ts = new TestSubscriber<String>();
// ps.subscribe(ts);
//
// try {
// ps.onError(new RuntimeException("an exception"));
// fail("expect OnErrorNotImplementedException");
// } catch (OnErrorNotImplementedException e) {
// // ignore
// }
// // even though the onError above throws we should still receive it on the other subscriber
// assertEquals(1, ts.getOnErrorEvents().size());
// }
// FIXME RS subscribers are not allowed to throw
// /**
// * This one has multiple failures so should get a CompositeException
// */
// @Test
// public void testOnErrorThrowsDoesntPreventDelivery2() {
// BehaviorSubject<String> ps = BehaviorSubject.create();
//
// ps.subscribe();
// ps.subscribe();
// TestSubscriber<String> ts = new TestSubscriber<String>();
// ps.subscribe(ts);
// ps.subscribe();
// ps.subscribe();
// ps.subscribe();
//
// try {
// ps.onError(new RuntimeException("an exception"));
// fail("expect OnErrorNotImplementedException");
// } catch (CompositeException e) {
// // we should have 5 of them
// assertEquals(5, e.getExceptions().size());
// }
// // even though the onError above throws we should still receive it on the other subscriber
// assertEquals(1, ts.getOnErrorEvents().size());
// }
@Test
public void testEmissionSubscriptionRace() throws Exception {
Scheduler s = Schedulers.io();
Scheduler.Worker worker = Schedulers.io().createWorker();
try {
for (int i = 0; i < 50000; i++) {
if (i % 1000 == 0) {
System.out.println(i);
}
final BehaviorProcessor<Object> rs = BehaviorProcessor.create();
final CountDownLatch finish = new CountDownLatch(1);
final CountDownLatch start = new CountDownLatch(1);
worker.schedule(new Runnable() {
@Override
public void run() {
try {
start.await();
} catch (Exception e1) {
e1.printStackTrace();
}
rs.onNext(1);
}
});
final AtomicReference<Object> o = new AtomicReference<Object>();
rs.subscribeOn(s).observeOn(Schedulers.io()).subscribe(new DefaultSubscriber<Object>() {
@Override
public void onComplete() {
o.set(-1);
finish.countDown();
}
@Override
public void onError(Throwable e) {
o.set(e);
finish.countDown();
}
@Override
public void onNext(Object t) {
o.set(t);
finish.countDown();
}
});
start.countDown();
if (!finish.await(5, TimeUnit.SECONDS)) {
System.out.println(o.get());
System.out.println(rs.hasSubscribers());
rs.onComplete();
Assert.fail("Timeout @ " + i);
break;
} else {
Assert.assertEquals(1, o.get());
worker.schedule(new Runnable() {
@Override
public void run() {
rs.onComplete();
}
});
}
}
} finally {
worker.dispose();
}
}
use of io.reactivex.Scheduler in project RxJava by ReactiveX.
the class NewThreadSchedulerTest method npeRegression.
/**
* Regression test to ensure there is no NPE when the worker has been disposed.
* @throws Exception on error
*/
@Test
public void npeRegression() throws Exception {
Scheduler s = getScheduler();
NewThreadWorker w = (NewThreadWorker) s.createWorker();
w.dispose();
//This method used to throw a NPE when the worker has been disposed and the parent is null
w.scheduleActual(new Runnable() {
@Override
public void run() {
}
}, 0, TimeUnit.MILLISECONDS, null);
}
Aggregations