use of io.reactivex.Scheduler in project RxRelay by JakeWharton.
the class ReplayRelayBoundedConcurrencyTest method testReplaySubjectEmissionSubscriptionRace.
@Test
public void testReplaySubjectEmissionSubscriptionRace() 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 ReplayRelay<Object> rs = ReplayRelay.createWithSize(2);
final CountDownLatch finish = new CountDownLatch(1);
final CountDownLatch start = new CountDownLatch(1);
// int j = i;
worker.schedule(new Runnable() {
@Override
public void run() {
try {
start.await();
} catch (Exception e1) {
e1.printStackTrace();
}
// System.out.println("> " + j);
rs.accept(1);
}
});
final AtomicReference<Object> o = new AtomicReference<Object>();
rs.subscribeOn(s).observeOn(Schedulers.io()).subscribe(new DefaultObserver<Object>() {
@Override
protected void onStart() {
super.onStart();
}
@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.hasObservers());
Assert.fail("Timeout @ " + i);
break;
} else {
Assert.assertEquals(1, o.get());
}
}
} finally {
worker.dispose();
}
}
use of io.reactivex.Scheduler in project RxAndroid by ReactiveX.
the class RxAndroidPluginsTest method resetClearsMainThreadHandler.
@Test
public void resetClearsMainThreadHandler() {
RxAndroidPlugins.setMainThreadSchedulerHandler(new Function<Scheduler, Scheduler>() {
@Override
public Scheduler apply(Scheduler scheduler) {
throw new AssertionError();
}
});
RxAndroidPlugins.reset();
Scheduler scheduler = new EmptyScheduler();
Scheduler actual = RxAndroidPlugins.onMainThreadScheduler(scheduler);
assertSame(scheduler, actual);
}
use of io.reactivex.Scheduler in project RxAndroid by ReactiveX.
the class AndroidSchedulersTest method mainThreadCallsThroughToHook.
@Test
public void mainThreadCallsThroughToHook() {
final AtomicInteger called = new AtomicInteger();
final Scheduler newScheduler = new EmptyScheduler();
RxAndroidPlugins.setMainThreadSchedulerHandler(new Function<Scheduler, Scheduler>() {
@Override
public Scheduler apply(Scheduler scheduler) {
called.getAndIncrement();
return newScheduler;
}
});
assertSame(newScheduler, AndroidSchedulers.mainThread());
assertEquals(1, called.get());
assertSame(newScheduler, AndroidSchedulers.mainThread());
assertEquals(2, called.get());
}
use of io.reactivex.Scheduler in project RxJava by ReactiveX.
the class SchedulerWhenTest method testRaceConditions.
@Test(timeout = 1000)
public void testRaceConditions() {
Scheduler comp = Schedulers.computation();
Scheduler limited = comp.when(new Function<Flowable<Flowable<Completable>>, Completable>() {
@Override
public Completable apply(Flowable<Flowable<Completable>> t) {
return Completable.merge(Flowable.merge(t, 10));
}
});
merge(just(just(1).subscribeOn(limited).observeOn(comp)).repeat(1000)).blockingSubscribe();
}
use of io.reactivex.Scheduler in project RxJava by ReactiveX.
the class NewThreadSchedulerTest method shutdownRejects.
// FIXME no longer testable due to internal changes
// @Test(timeout = 3000)
// public void testNoSelfInterrupt() throws InterruptedException {
// Scheduler.Worker worker = Schedulers.newThread().createWorker();
// try {
// final CountDownLatch run = new CountDownLatch(1);
// final CountDownLatch done = new CountDownLatch(1);
// final AtomicReference<Throwable> exception = new AtomicReference<T>();
// final AtomicBoolean interruptFlag = new AtomicBoolean();
//
// ScheduledRunnable sa = (ScheduledRunnable)worker.schedule(new Runnable() {
// @Override
// public void run() {
// try {
// run.await();
// } catch (InterruptedException ex) {
// exception.set(ex);
// }
// }
// });
//
// sa.add(new Disposable() {
// @Override
// public void dispose() {
// interruptFlag.set(Thread.currentThread().isInterrupted());
// done.countDown();
// }
// });
//
// run.countDown();
//
// done.await();
//
// Assert.assertEquals(null, exception.get());
// Assert.assertFalse("Interrupted?!", interruptFlag.get());
// } finally {
// worker.dispose();
// }
// }
@Test
public void shutdownRejects() {
final int[] calls = { 0 };
Runnable r = new Runnable() {
@Override
public void run() {
calls[0]++;
}
};
Scheduler s = getScheduler();
Worker w = s.createWorker();
w.dispose();
assertTrue(w.isDisposed());
assertEquals(Disposables.disposed(), w.schedule(r));
assertEquals(Disposables.disposed(), w.schedule(r, 1, TimeUnit.SECONDS));
assertEquals(Disposables.disposed(), w.schedulePeriodically(r, 1, 1, TimeUnit.SECONDS));
NewThreadWorker actual = (NewThreadWorker) w;
CompositeDisposable cd = new CompositeDisposable();
actual.scheduleActual(r, 1, TimeUnit.SECONDS, cd);
assertEquals(0, cd.size());
assertEquals(0, calls[0]);
}
Aggregations