Java Code Examples for io.reactivex.Scheduler#Worker

The following examples show how to use io.reactivex.Scheduler#Worker . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchedulerTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testPeriodicRescheduleAfterActionBlocking() {
  ContextScheduler scheduler2 = new ContextScheduler(vertx, true);
  Scheduler.Worker worker = scheduler2.createWorker();
  AtomicBoolean b = new AtomicBoolean();
  long time = System.nanoTime();
  worker.schedulePeriodically(() -> {
    if (b.compareAndSet(false, true)) {
      try {
        Thread.sleep(10);
      } catch (InterruptedException e) {
        fail();
      }
    } else {
      assertTrue(System.nanoTime() - time > NANOSECONDS.convert(20 + 10 + 20, MILLISECONDS));
      worker.dispose();
      testComplete();
    }
  }, 20, 20, MILLISECONDS);
  await();
}
 
Example 2
Source File: SchedulerTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
private void testSchedulePeriodic(Supplier<ContextScheduler> scheduler, Consumer<Thread> threadAssert) {
  disableThreadChecks();
  ContextScheduler scheduler2 = scheduler.get();
  Scheduler.Worker worker = scheduler2.createWorker();
  AtomicLong time = new AtomicLong(System.currentTimeMillis() - 40);
  AtomicInteger count = new AtomicInteger();
  AtomicReference<Disposable> sub = new AtomicReference<>();
  sub.set(worker.schedulePeriodically(() -> {
    threadAssert.accept(Thread.currentThread());
    if (count.incrementAndGet() > 2) {
      sub.get().dispose();
      testComplete();
    } else {
      long now = System.currentTimeMillis();
      long delta = now - time.get();
      assertTrue("" + delta, delta >= 40);
      time.set(now);
    }
  }, 0, 40, MILLISECONDS));
  await();
}
 
Example 3
Source File: SchedulerTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
private void testScheduleDelayed(Supplier<ContextScheduler> scheduler, Consumer<Thread> threadAssert) throws Exception {
  ContextScheduler scheduler2 = scheduler.get();
  Scheduler.Worker worker = scheduler2.createWorker();
  long time = System.currentTimeMillis();
  CountDownLatch latch = new CountDownLatch(1);
  AtomicReference<Thread> thread = new AtomicReference<>();
  AtomicLong execTime = new AtomicLong();
  worker.schedule(() -> {
    thread.set(Thread.currentThread());
    execTime.set(System.currentTimeMillis() - time);
    latch.countDown();
  }, 40, MILLISECONDS);
  awaitLatch(latch);
  threadAssert.accept(thread.get());
  assertTrue(execTime.get() >= 40);
}
 
Example 4
Source File: SchedulerTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
private void testUnsubscribeBetweenActions(Supplier<ContextScheduler> scheduler) throws Exception {
  ContextScheduler scheduler2 = scheduler.get();
  Scheduler.Worker worker = scheduler2.createWorker();
  AtomicInteger count = new AtomicInteger();
  CountDownLatch latch = new CountDownLatch(1);
  AtomicReference<Disposable> sub = new AtomicReference<>();
  sub.set(worker.schedulePeriodically(() -> {
    if (count.incrementAndGet() == 4) {
      latch.countDown();
    }
  }, 0, 20, MILLISECONDS));
  awaitLatch(latch);
  sub.get().dispose();
  Thread.sleep(60);
  assertEquals(4, count.get());
}
 
Example 5
Source File: AbstractObjectBoxTest.java    From Hentoid with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpRxSchedulers() {
    Scheduler immediate = new Scheduler() {
        @Override
        public Disposable scheduleDirect(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) {
            // this prevents StackOverflowErrors when scheduling with a delay
            return super.scheduleDirect(run, 0, unit);
        }

        @Override
        public Scheduler.Worker createWorker() {
            return new ExecutorScheduler.ExecutorWorker(Runnable::run, false);
        }
    };

    RxJavaPlugins.setInitIoSchedulerHandler(scheduler -> immediate);
    RxJavaPlugins.setInitComputationSchedulerHandler(scheduler -> immediate);
    RxJavaPlugins.setInitNewThreadSchedulerHandler(scheduler -> immediate);
    RxJavaPlugins.setInitSingleSchedulerHandler(scheduler -> immediate);
    RxAndroidPlugins.setInitMainThreadSchedulerHandler(scheduler -> immediate);
}
 
Example 6
Source File: Flowables.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
private static <T> void startScheduledResetAgain(final long duration, final TimeUnit unit,
        final Scheduler scheduler, final AtomicReference<CachedFlowable<T>> cacheRef,
        final AtomicReference<Optional<Scheduler.Worker>> workerRef) {

    Runnable action = new Runnable() {
        @Override
        public void run() {
            cacheRef.get().reset();
        }
    };
    // CAS loop to cancel the current worker and create a new one
    while (true) {
        Optional<Scheduler.Worker> wOld = workerRef.get();
        if (wOld == null) {
            // we are finished
            return;
        }
        Optional<Scheduler.Worker> w = Optional.of(scheduler.createWorker());
        if (workerRef.compareAndSet(wOld, w)) {
            if (wOld.isPresent())
                wOld.get().dispose();
            w.get().schedule(action, duration, unit);
            break;
        }
    }
}
 
Example 7
Source File: Observables.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
private static <T> void startScheduledResetAgain(final long duration, final TimeUnit unit,
                                                 final Scheduler scheduler, final AtomicReference<CachedObservable<T>> cacheRef,
                                                 final AtomicReference<Optional<Scheduler.Worker>> workerRef) {

    Runnable action = new Runnable() {
        @Override
        public void run() {
            cacheRef.get().reset();
        }
    };
    // CAS loop to cancel the current worker and create a new one
    while (true) {
        Optional<Scheduler.Worker> wOld = workerRef.get();
        if (wOld == null) {
            // we are finished
            return;
        }
        Optional<Scheduler.Worker> w = Optional.of(scheduler.createWorker());
        if (workerRef.compareAndSet(wOld, w)) {
            if (wOld.isPresent())
                wOld.get().dispose();
            w.get().schedule(action, duration, unit);
            break;
        }
    }
}
 
Example 8
Source File: SchedulerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
private void testUnsubscribeDuringExecute(Supplier<ContextScheduler> scheduler) throws Exception {
  ContextScheduler scheduler2 = scheduler.get();
  Scheduler.Worker worker = scheduler2.createWorker();
  AtomicInteger count = new AtomicInteger();
  AtomicReference<Disposable> sub = new AtomicReference<>();
  sub.set(worker.schedulePeriodically(() -> {
    if (count.getAndIncrement() == 0) {
      sub.get().dispose();
    }
  }, 0, 5, MILLISECONDS));
  Thread.sleep(60);
  assertEquals(1, count.get());
}
 
Example 9
Source File: SchedulerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
private void testScheduleImmediatly(Supplier<ContextScheduler> scheduler, Consumer<Thread> threadAssert) throws Exception {
  CountDownLatch latch = new CountDownLatch(1);
  ContextScheduler scheduler2 = scheduler.get();
  Scheduler.Worker worker = scheduler2.createWorker();
  AtomicReference<Thread> thread = new AtomicReference<>();
  worker.schedule(() -> {
    thread.set(Thread.currentThread());
    latch.countDown();
  }, 0, MILLISECONDS);
  awaitLatch(latch);
  threadAssert.accept(thread.get());
}
 
Example 10
Source File: DelegatingIdlingResourceSchedulerTest.java    From RxIdler with Apache License 2.0 5 votes vote down vote up
@Test public void runningWorkReportsBusy() {
  Scheduler.Worker worker = scheduler.createWorker();
  worker.schedule(new Runnable() {
    @Override public void run() {
      assertBusy();
    }
  });
  delegate.triggerActions();
}
 
Example 11
Source File: DelegatingIdlingResourceSchedulerTest.java    From RxIdler with Apache License 2.0 5 votes vote down vote up
@Test public void unsubscribingScheduledWorkWhileRunningWorkReportsBusy() {
  final Scheduler.Worker worker = scheduler.createWorker();
  worker.schedule(new Runnable() {
    @Override public void run() {
      worker.dispose();
      assertBusy();
    }
  });
  delegate.triggerActions();
}
 
Example 12
Source File: SchedulerTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
private void testWorkerUnsubscribe(Supplier<ContextScheduler> scheduler) throws Exception {
  ContextScheduler scheduler2 = scheduler.get();
  Scheduler.Worker worker = scheduler2.createWorker();
  CountDownLatch latch = new CountDownLatch(2);
  Disposable sub1 = worker.schedule(latch::countDown, 40, MILLISECONDS);
  Disposable sub2 = worker.schedule(latch::countDown, 40, MILLISECONDS);
  worker.dispose();
  assertTrue(sub1.isDisposed());
  assertTrue(sub2.isDisposed());
  assertFalse(latch.await(40, MILLISECONDS));
  assertEquals(2, latch.getCount());
}
 
Example 13
Source File: DelegatingIdlingResourceSchedulerTest.java    From RxIdler with Apache License 2.0 4 votes vote down vote up
@Test public void scheduleWorkAfterUnsubscribedReportsIdle() {
  Scheduler.Worker worker = scheduler.createWorker();
  worker.dispose();
  worker.schedule(new CountingRunnable());
  assertIdle(0);
}
 
Example 14
Source File: DelegatingIdlingResourceSchedulerTest.java    From RxIdler with Apache License 2.0 4 votes vote down vote up
@Test public void finishingWorkWithoutRegisteredCallbackDoesNotCrash() {
  IdlingResourceScheduler scheduler = Rx2Idler.wrap(delegate, "Bob");
  Scheduler.Worker worker = scheduler.createWorker();
  worker.schedule(new CountingRunnable());
  delegate.triggerActions();
}
 
Example 15
Source File: DelegatingIdlingResourceSchedulerTest.java    From RxIdler with Apache License 2.0 4 votes vote down vote up
@Test public void unsubscribingScheduledWorksReportsIdle() {
  Scheduler.Worker worker = scheduler.createWorker();
  worker.schedule(new CountingRunnable());
  worker.dispose();
  assertIdle(1);
}
 
Example 16
Source File: DelegatingIdlingResourceSchedulerTest.java    From RxIdler with Apache License 2.0 4 votes vote down vote up
@Test public void schedulePeriodicallyWithNonZeroDelayReportsIdle() {
  Scheduler.Worker worker = scheduler.createWorker();
  worker.schedulePeriodically(new CountingRunnable(), 1, 1, SECONDS);
  assertIdle(0);
}
 
Example 17
Source File: DelegatingIdlingResourceSchedulerTest.java    From RxIdler with Apache License 2.0 4 votes vote down vote up
@Test public void schedulePeriodicallyWithZeroDelayReportsBusy() {
  Scheduler.Worker worker = scheduler.createWorker();
  worker.schedulePeriodically(new CountingRunnable(), 0, 1, SECONDS);
  assertBusy();
}
 
Example 18
Source File: DelegatingIdlingResourceSchedulerTest.java    From RxIdler with Apache License 2.0 4 votes vote down vote up
@Test public void scheduleWithNonZeroDelayReportsIdle() {
  Scheduler.Worker worker = scheduler.createWorker();
  worker.schedule(new CountingRunnable(), 1, SECONDS);
  assertIdle(0);
}
 
Example 19
Source File: DelegatingIdlingResourceSchedulerTest.java    From RxIdler with Apache License 2.0 4 votes vote down vote up
@Test public void scheduleWithZeroDelayReportsBusy() {
  Scheduler.Worker worker = scheduler.createWorker();
  worker.schedule(new CountingRunnable(), 0, SECONDS);
  assertBusy();
}
 
Example 20
Source File: DelegatingIdlingResourceSchedulerTest.java    From RxIdler with Apache License 2.0 4 votes vote down vote up
@Test public void scheduledWorkUnsubscribedReportsIdle() {
  Scheduler.Worker worker = scheduler.createWorker();
  worker.schedule(new CountingRunnable()).dispose();
  assertIdle(1);
}