@Test public void scheduledDoesntReject() { Scheduler s = Schedulers.fromExecutorService(Executors.newSingleThreadScheduledExecutor()); assertThat(s.schedule(() -> {}, 100, TimeUnit.MILLISECONDS)) .describedAs("direct delayed scheduling") .isNotNull(); assertThat(s.schedulePeriodically(() -> {}, 100, 100, TimeUnit.MILLISECONDS)) .describedAs("direct periodic scheduling") .isNotNull(); Worker w = s.createWorker(); assertThat(w.schedule(() -> {}, 100, TimeUnit.MILLISECONDS)) .describedAs("worker delayed scheduling") .isNotNull(); assertThat(w.schedulePeriodically(() -> {}, 100, 100, TimeUnit.MILLISECONDS)) .describedAs("worker periodic scheduling") .isNotNull(); }
@Test public void scheduledDoesntReject() { Scheduler s = scheduler(); assertThat(s.schedule(() -> {}, 100, TimeUnit.MILLISECONDS)) .describedAs("direct delayed scheduling") .isNotNull(); assertThat(s.schedulePeriodically(() -> {}, 100, 100, TimeUnit.MILLISECONDS)) .describedAs("direct periodic scheduling") .isNotNull(); Scheduler.Worker w = s.createWorker(); assertThat(w.schedule(() -> {}, 100, TimeUnit.MILLISECONDS)) .describedAs("worker delayed scheduling") .isNotNull(); assertThat(w.schedulePeriodically(() -> {}, 100, 100, TimeUnit.MILLISECONDS)) .describedAs("worker periodic scheduling") .isNotNull(); }
@Test public void scheduledDoesntReject() { Scheduler s = scheduler(); assertThat(s.schedule(() -> {}, 100, TimeUnit.MILLISECONDS)) .describedAs("direct delayed scheduling") .isNotNull(); assertThat(s.schedulePeriodically(() -> {}, 100, 100, TimeUnit.MILLISECONDS)) .describedAs("direct periodic scheduling") .isNotNull(); Scheduler.Worker w = s.createWorker(); assertThat(w.schedule(() -> {}, 100, TimeUnit.MILLISECONDS)) .describedAs("worker delayed scheduling") .isNotNull(); assertThat(w.schedulePeriodically(() -> {}, 100, 100, TimeUnit.MILLISECONDS)) .describedAs("worker periodic scheduling") .isNotNull(); }
@Override public void subscribe(CoreSubscriber<? super Long> actual) { Worker w = timedScheduler.createWorker(); IntervalRunnable r = new IntervalRunnable(actual, w); actual.onSubscribe(r); try { w.schedulePeriodically(r, initialDelay, period, unit); } catch (RejectedExecutionException ree) { if (!r.cancelled) { actual.onError(Operators.onRejectedExecution(ree, r, null, null, actual.currentContext())); } } }
@Override public Disposable schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit) { return actual.schedulePeriodically(decorate(task), initialDelay, period, unit); } }
@Override public Disposable schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit) { return actual.schedulePeriodically(decorate(task), initialDelay, period, unit); } }
@Override public Disposable schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit) { return main.schedulePeriodically(task, initialDelay, period, unit); }
Disposable newPeriod() { try { return worker.schedulePeriodically(new ConsumerIndexHolder(producerIndex, this), timespan, timespan, TimeUnit.MILLISECONDS); } catch (Exception e) { actual.onError(Operators.onRejectedExecution(e, s, null, null, actual.currentContext())); return Disposables.disposed(); } }
Disposable c = w.schedulePeriodically(() -> { try { latch.countDown();
@Test public void directAndWorkerTimeSchedulingRejected() { Scheduler scheduler = scheduler(); Worker worker = scheduler.createWorker(); try { assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> scheduler.schedule(() -> { }, 100, TimeUnit.MILLISECONDS)) .isSameAs(Exceptions.failWithRejectedNotTimeCapable()); assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> scheduler.schedulePeriodically(() -> { }, 100, 100, TimeUnit.MILLISECONDS)) .isSameAs(Exceptions.failWithRejectedNotTimeCapable()); assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> worker.schedule(() -> { }, 100, TimeUnit.MILLISECONDS)) .isSameAs(Exceptions.failWithRejectedNotTimeCapable()); assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> worker.schedulePeriodically(() -> { }, 100, 100, TimeUnit.MILLISECONDS)) .isSameAs(Exceptions.failWithRejectedNotTimeCapable()); } finally { worker.dispose(); } }
@Test public void directAndWorkerTimeSchedulingRejected() { Scheduler scheduler = scheduler(); Scheduler.Worker worker = scheduler.createWorker(); try { assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> scheduler.schedule(() -> { }, 100, TimeUnit.MILLISECONDS)) .isSameAs(Exceptions.failWithRejectedNotTimeCapable()); assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> scheduler.schedulePeriodically(() -> { }, 100, 100, TimeUnit.MILLISECONDS)) .isSameAs(Exceptions.failWithRejectedNotTimeCapable()); assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> worker.schedule(() -> { }, 100, TimeUnit.MILLISECONDS)) .isSameAs(Exceptions.failWithRejectedNotTimeCapable()); assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> worker.schedulePeriodically(() -> { }, 100, 100, TimeUnit.MILLISECONDS)) .isSameAs(Exceptions.failWithRejectedNotTimeCapable()); } finally { worker.dispose(); } }
@Test public void notScheduledRejects() { Scheduler s = Schedulers.fromExecutorService(Executors.newSingleThreadExecutor()); assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> s.schedule(() -> {}, 100, TimeUnit.MILLISECONDS)) .describedAs("direct delayed scheduling") .isSameAs(Exceptions.failWithRejectedNotTimeCapable()); assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> s.schedulePeriodically(() -> {}, 100, 100, TimeUnit.MILLISECONDS)) .describedAs("direct periodic scheduling") .isSameAs(Exceptions.failWithRejectedNotTimeCapable()); Worker w = s.createWorker(); assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> w.schedule(() -> {}, 100, TimeUnit.MILLISECONDS)) .describedAs("worker delayed scheduling") .isSameAs(Exceptions.failWithRejectedNotTimeCapable()); assertThatExceptionOfType(RejectedExecutionException.class) .isThrownBy(() -> w.schedulePeriodically(() -> {}, 100, 100, TimeUnit.MILLISECONDS)) .describedAs("worker periodic scheduling") .isSameAs(Exceptions.failWithRejectedNotTimeCapable()); }
@Test public void massCancel() throws InterruptedException { Scheduler timer = Schedulers.newSingle("test-timer"); try { Worker w1 = timer.createWorker(); AtomicInteger counter = new AtomicInteger(); Runnable task = counter::getAndIncrement; int tasks = 10; Disposable[] c = new Disposable[tasks]; for (int i = 0; i < tasks; i++) { c[i] = w1.schedulePeriodically(task, 500, 500, TimeUnit.MILLISECONDS); } w1.dispose(); for (int i = 0; i < tasks; i++) { assertThat(c[i].isDisposed()).isTrue(); } Assert.assertEquals(0, counter.get()); } finally { timer.dispose(); } }
@Override public void subscribe(CoreSubscriber<? super Long> actual) { Worker w = timedScheduler.createWorker(); IntervalRunnable r = new IntervalRunnable(actual, w); actual.onSubscribe(r); try { w.schedulePeriodically(r, initialDelay, period, unit); } catch (RejectedExecutionException ree) { if (!r.cancelled) { actual.onError(Operators.onRejectedExecution(ree, r, null, null, actual.currentContext())); } } }
@Override public Disposable schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit) { return main.schedulePeriodically(task, initialDelay, period, unit); }
Disposable newPeriod() { try { return worker.schedulePeriodically(new ConsumerIndexHolder(producerIndex, this), timespan, timespan, TimeUnit.MILLISECONDS); } catch (Exception e) { actual.onError(Operators.onRejectedExecution(e, s, null, null, actual.currentContext())); return Disposables.disposed(); } }