Esempio n. 1
0
  @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();
    }
  }
Esempio n. 2
0
  @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();
    }
  }
Esempio n. 3
0
  @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();
  }
Esempio n. 4
0
 void scheduleExact() {
   inner.schedulePeriodically(
       new Action0() {
         @Override
         public void call() {
           emit();
         }
       },
       timespan,
       timespan,
       unit);
 }
Esempio n. 5
0
 void scheduleChunk() {
   inner.schedulePeriodically(
       new Action0() {
         @Override
         public void call() {
           startNewChunk();
         }
       },
       timeshift,
       timeshift,
       unit);
 }
Esempio n. 6
0
 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);
 }
Esempio n. 7
0
 @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));
 }