@Test public void testInterruptTerminalEventAwaitAndUnsubscribe() { TestSubscriber<Integer> ts = TestSubscriber.create(); final Thread t0 = Thread.currentThread(); Worker w = Schedulers.computation().createWorker(); try { w.schedule( new Action0() { @Override public void call() { t0.interrupt(); } }, 200, TimeUnit.MILLISECONDS); ts.awaitTerminalEventAndUnsubscribeOnTimeout(5, TimeUnit.SECONDS); if (!ts.isUnsubscribed()) { fail("Did not unsubscribe!"); } } finally { w.unsubscribe(); } }
@Test public void testInterruptTerminalEventAwaitTimed() { TestSubscriber<Integer> ts = TestSubscriber.create(); final Thread t0 = Thread.currentThread(); Worker w = Schedulers.computation().createWorker(); try { w.schedule( new Action0() { @Override public void call() { t0.interrupt(); } }, 200, TimeUnit.MILLISECONDS); try { ts.awaitTerminalEvent(5, TimeUnit.SECONDS); fail("Did not interrupt wait!"); } catch (RuntimeException ex) { if (!(ex.getCause() instanceof InterruptedException)) { fail("The cause is not InterruptedException! " + ex.getCause()); } } } finally { w.unsubscribe(); } }
@Test public void shouldScheduleDelayedActionOnHandlerThread() { Handler handler = mock(Handler.class); Action0 action = mock(Action0.class); Scheduler scheduler = AndroidSchedulers.from(handler); Worker inner = scheduler.createWorker(); inner.schedule(action, 1, SECONDS); // verify that we post to the given Handler ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class); verify(handler).postDelayed(runnable.capture(), eq(1000L)); // verify that the given handler delegates to our action runnable.getValue().run(); verify(action).call(); }
void scheduleExact() { inner.schedulePeriodically( new Action0() { @Override public void call() { emit(); } }, timespan, timespan, unit); }
void scheduleChunk() { inner.schedulePeriodically( new Action0() { @Override public void call() { startNewChunk(); } }, timeshift, timeshift, unit); }
void startNewChunk() { final List<T> chunk = new ArrayList<T>(); synchronized (this) { if (done) { return; } chunks.add(chunk); } inner.schedule( new Action0() { @Override public void call() { emitChunk(chunk); } }, timespan, unit); }
@Override public void onCompleted() { try { inner.unsubscribe(); List<T> toEmit; synchronized (this) { if (done) { return; } done = true; toEmit = chunk; chunk = null; } child.onNext(toEmit); } catch (Throwable t) { child.onError(t); return; } child.onCompleted(); unsubscribe(); }
@Override public void call(SingleSubscriber<? super T> singleSubscriber) { Worker worker = scheduler.createWorker(); singleSubscriber.add(worker); worker.schedule(new ScalarSynchronousSingleAction<T>(singleSubscriber, value)); }