@Test public void exampleEquivalence() { TestScheduler scheduler = Schedulers.test(); TestSubscriber<Object> testerJoin = new TestSubscriber<>(); TestSubscriber<Object> testerGroupJoin = new TestSubscriber<>(); Observable<Long> left = Observable.interval(100, TimeUnit.MILLISECONDS, scheduler); Observable<Long> right = Observable.interval(100, TimeUnit.MILLISECONDS, scheduler); left.join( right, i -> Observable.timer(150, TimeUnit.MILLISECONDS, scheduler), i -> Observable.timer(0, TimeUnit.MILLISECONDS, scheduler), (l, r) -> Tuple.create(l, r)) .take(10) .subscribe(testerJoin); left.groupJoin( right, i -> Observable.timer(150, TimeUnit.MILLISECONDS, scheduler), i -> Observable.timer(0, TimeUnit.MILLISECONDS, scheduler), (l, rs) -> rs.map(r -> Tuple.create(l, r))) .flatMap(i -> i) .take(10) .subscribe(testerGroupJoin); scheduler.advanceTimeTo(600, TimeUnit.MILLISECONDS); testerJoin.assertReceivedOnNext(testerGroupJoin.getOnNextEvents()); }
@Test public void test() { TestSubscriber<Object> tester = new TestSubscriber<>(); TestScheduler scheduler = Schedulers.test(); Observable<Long> left = Observable.interval(100, TimeUnit.MILLISECONDS, scheduler).take(6); Observable<Long> right = Observable.interval(200, TimeUnit.MILLISECONDS, scheduler).take(3); left.groupJoin( right, i -> Observable.never(), i -> Observable.timer(0, TimeUnit.MILLISECONDS, scheduler), (l, rs) -> rs.toList().map(rl -> Tuple.create(l, rl))) .flatMap(i -> i) .subscribe(tester); scheduler.advanceTimeTo(600, TimeUnit.MILLISECONDS); tester.assertReceivedOnNext( Arrays.asList( Tuple.create(0L, Arrays.asList(0L, 1L, 2L)), Tuple.create(1L, Arrays.asList(0L, 1L, 2L)), Tuple.create(2L, Arrays.asList(1L, 2L)), Tuple.create(3L, Arrays.asList(1L, 2L)), Tuple.create(4L, Arrays.asList(2L)), Tuple.create(5L, Arrays.asList(2L)))); }
/** * Make sure we get a MissingBackpressureException propagated through when we have a fast temporal * (hot) producer. */ @Test public void testHotOperatorBackpressure() { TestSubscriber<String> ts = new TestSubscriber<String>(); Observable.timer(0, 1, TimeUnit.MICROSECONDS) .observeOn(Schedulers.computation()) .map( new Func1<Long, String>() { @Override public String call(Long t1) { System.out.println(t1); try { Thread.sleep(100); } catch (InterruptedException e) { } return t1 + " slow value"; } }) .subscribe(ts); ts.awaitTerminalEvent(); System.out.println("Errors: " + ts.getOnErrorEvents()); assertEquals(1, ts.getOnErrorEvents().size()); assertEquals(MissingBackpressureException.class, ts.getOnErrorEvents().get(0).getClass()); }
private Observable<Void> handleHystrixRequest(final HttpServerResponse<O> response) { writeHeaders(response); final Subject<Void, Void> subject = PublishSubject.create(); final MultipleAssignmentSubscription subscription = new MultipleAssignmentSubscription(); Subscription actionSubscription = Observable.timer(0, interval, TimeUnit.MILLISECONDS, Schedulers.computation()) .subscribe( new Action1<Long>() { @Override public void call(Long tick) { if (!response.getChannel().isOpen()) { subscription.unsubscribe(); return; } try { writeMetric(JsonMapper.toJson(metrics), response); } catch (Exception e) { subject.onError(e); } } }); subscription.set(actionSubscription); return subject; }
public void deleteAfterSomeTime(List<QuranRow> remove) { if (mPendingRemoval != null) { // handle a new delete request when one is already happening by adding those items to delete // now and un-subscribing from the old request. if (mItemsToRemove != null) { remove.addAll(mItemsToRemove); } cancelDeletion(); } mItemsToRemove = remove; mPendingRemoval = Observable.timer(DELAY_DELETION_DURATION_IN_MS, TimeUnit.MILLISECONDS) .flatMap( new Func1<Long, Observable<BookmarkResult>>() { @Override public Observable<BookmarkResult> call(Long aLong) { return removeItemsObservable(); } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( new Action1<BookmarkResult>() { @Override public void call(BookmarkResult result) { mPendingRemoval = null; mCachedData = result; if (mFragment != null) { mFragment.onNewData(result); } } }); }
private void loadList(List<AppInfo> apps) { mRecyclerView.setVisibility(View.VISIBLE); Observable<AppInfo> appsSequence = Observable.interval(1000, TimeUnit.MILLISECONDS) .map( position -> { App.L.debug("Position: " + position); return apps.get(position.intValue()); }); Observable<Long> tictoc = Observable.interval(1000, TimeUnit.MILLISECONDS); appsSequence .join( tictoc, appInfo -> Observable.timer(2, TimeUnit.SECONDS), time -> Observable.timer(0, TimeUnit.SECONDS), this::updateTitle) .observeOn(AndroidSchedulers.mainThread()) .take(10) .subscribe( new Observer<AppInfo>() { @Override public void onCompleted() { Toast.makeText(getActivity(), "Here is the list!", Toast.LENGTH_LONG).show(); } @Override public void onError(Throwable e) { mSwipeRefreshLayout.setRefreshing(false); Toast.makeText(getActivity(), "Something went wrong!", Toast.LENGTH_SHORT).show(); } @Override public void onNext(AppInfo appInfo) { if (mSwipeRefreshLayout.isRefreshing()) { mSwipeRefreshLayout.setRefreshing(false); } mAddedApps.add(appInfo); int position = mAddedApps.size() - 1; mAdapter.addApplication(position, appInfo); mRecyclerView.smoothScrollToPosition(position); } }); }
private void delayShowAll(long time) { Observable.timer(time, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) .subscribe( new Subscriber<Long>() { public void onCompleted() { MainActivity.this.gotoNext(); } public void onError(Throwable e) {} public void onNext(Long aLong) {} }); }
private void showRedHeartLayout() { Observable.timer(400, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) .subscribe( new Subscriber<Long>() { public void onCompleted() { mHeartLayout.setVisibility(View.VISIBLE); MainActivity.this.delayDo2(); } public void onError(Throwable e) {} public void onNext(Long aLong) {} }); }
@Test public void testErrorPropagatesWhenNoOutstandingRequests() { Observable<Long> timer = Observable.timer(0, 1, TimeUnit.MICROSECONDS) .doOnEach( new Action1<Notification<? super Long>>() { @Override public void call(Notification<? super Long> n) { // System.out.println("BEFORE " + // n); } }) .observeOn(Schedulers.newThread()) .doOnEach( new Action1<Notification<? super Long>>() { @Override public void call(Notification<? super Long> n) { try { Thread.sleep(100); } catch (InterruptedException e) { } // System.out.println("AFTER " + // n); } }); TestSubscriber<Long> ts = new TestSubscriber<Long>(); Observable.combineLatest( timer, Observable.<Integer>never(), new Func2<Long, Integer, Long>() { @Override public Long call(Long t1, Integer t2) { return t1; } }) .take(RxRingBuffer.SIZE * 2) .subscribe(ts); ts.awaitTerminalEvent(); assertEquals(1, ts.getOnErrorEvents().size()); assertEquals(MissingBackpressureException.class, ts.getOnErrorEvents().get(0).getClass()); }
private void delayDo2() { Observable.timer( (long) this.mRandom2.nextInt(200), TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) .subscribe( new Subscriber<Long>() { public void onCompleted() { MainActivity.this.mHeartLayout.addHeart(MainActivity.this.randomColor()); MainActivity.this.delayDo2(); } public void onError(Throwable e) {} public void onNext(Long aLong) {} }); }
private void delayDo() { Observable.timer(0, TimeUnit.SECONDS, AndroidSchedulers.mainThread()) .subscribe( new Subscriber<Long>() { public void onCompleted() { MainActivity.this.mWhiteSnowView.setVisibility(View.GONE); MainActivity.this.mWebView.setVisibility(View.VISIBLE); MainActivity.this.delayShow(5100); // 延时显示显示打印机 MainActivity.this.mWebView.loadUrl(MainActivity.URL); } public void onError(Throwable e) {} public void onNext(Long aLong) {} }); }
public void example() { Observable<String> left = Observable.interval(100, TimeUnit.MILLISECONDS).map(i -> "L" + i).take(6); Observable<String> right = Observable.interval(200, TimeUnit.MILLISECONDS).map(i -> "R" + i).take(3); left.groupJoin( right, i -> Observable.never(), i -> Observable.timer(0, TimeUnit.MILLISECONDS), (l, rs) -> rs.toList().subscribe(list -> System.out.println(l + ": " + list))) .subscribe(); // L0: [R0, R1, R2] // L1: [R0, R1, R2] // L2: [R1, R2] // L3: [R1, R2] // L4: [R2] // L5: [R2] }
private void reSchedule(final Handler h) { System.out.println("reScheduling:" + backOff); logger.info("rescheduling in {} ms", backOff); Observable.timer(backOff, TimeUnit.MILLISECONDS) // .flatMap(new Func1<Long, Observable<Long>>(){ // // @Override // public Observable<Long> call(Long t) { // System.out.println("call from flatmap -> doWork -> just " + t); // doWork(h); // return Observable.just(t); // } // }) .subscribe( s -> { System.out.println("onENextCalled,round -> " + round); doWork(h); }); // // subscribeOn(Schedulers.computation()) // .subscribe((n)->{System.out.println("timerOnNext");}, // e -> { // logger.warn("failed to rescheduled.inc backoff",e); // backOff += waitOnEmtpyInMs; // reSchedule(h); // }); // Observable.timer(backOff, TimeUnit.MILLISECONDS) // .map(l -> {retunr }) // .subscribe(s -> { // doWork(h); // }, e -> { // logger.warn("failed to rescheduled.inc backoff",e); // backOff += waitOnEmtpyInMs; // reSchedule(h); // }); }