當前位置: 首頁>>代碼示例>>Java>>正文


Java Schedulers.test方法代碼示例

本文整理匯總了Java中rx.schedulers.Schedulers.test方法的典型用法代碼示例。如果您正苦於以下問題:Java Schedulers.test方法的具體用法?Java Schedulers.test怎麽用?Java Schedulers.test使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在rx.schedulers.Schedulers的用法示例。


在下文中一共展示了Schedulers.test方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testUnsubscribeOnCancel

import rx.schedulers.Schedulers; //導入方法依賴的package包/類
@Test
public void testUnsubscribeOnCancel() throws Exception {
    final TestScheduler ticker = Schedulers.test();

    final Ctx ctx = Ctx.empty();

    final AtomicBoolean failed = new AtomicBoolean(false);
    final AtomicBoolean completed = new AtomicBoolean(false);
    final AtomicInteger counter = new AtomicInteger();

    final Observable os = Observable.interval(10, TimeUnit.MILLISECONDS, ticker);

    final Subscription s = os.subscribe((i) -> counter.getAndIncrement(),
                                        (e) -> failed.set(true),
                                        () -> completed.set(true));

    // unsubscribe when currentExchange is cancelled
    ctx.onCancel(s::unsubscribe);

    // receive first event
    ticker.advanceTimeBy(10, TimeUnit.MILLISECONDS);
    ticker.triggerActions();
    assertThat(counter.get()).isEqualTo(1);

    // receive second event
    ticker.advanceTimeBy(10, TimeUnit.MILLISECONDS);
    ticker.triggerActions();
    assertThat(counter.get()).isEqualTo(2);

    // should led to unsubscribe, not receive future elements
    ctx.cancel();

    // advance time enough to trigger third event, but we should not get it
    ticker.advanceTimeBy(10, TimeUnit.MILLISECONDS);
    ticker.triggerActions();
    assertThat(counter.get()).isEqualTo(2);

    // verify it was not completed or failed
    assertThat(completed.get()).isFalse();
    assertThat(failed.get()).isFalse();
}
 
開發者ID:groupon,項目名稱:jtier-ctx,代碼行數:42,代碼來源:RxJavaExample.java


注:本文中的rx.schedulers.Schedulers.test方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。