Java Code Examples for rx.observers.TestSubscriber#requestMore()

The following examples show how to use rx.observers.TestSubscriber#requestMore() . 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: SingleUntil.java    From akarnokd-misc with Apache License 2.0 6 votes vote down vote up
@Test
public void singleTakeUntil() {
    PublishSubject<String> controller = PublishSubject.create();

    TestSubscriber<String> testSubscriber = new TestSubscriber<>(0);

    Single.just("Hello")
        .toObservable()
        .takeUntil(controller.map(v -> { throw new CancellationException(); }))
        .subscribe(testSubscriber);

    controller.onNext("Stop flow");

    testSubscriber.requestMore(1);
    testSubscriber.assertNoValues();
    testSubscriber.assertError(CancellationException.class);
}
 
Example 2
Source File: OperatorFromTransformerTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testBackpressure() {
    TestSubscriber<Integer> ts = TestSubscriber.create(1);
    Observable.
            //
            range(1, 1000)
            // use toOperator
            .lift(toOperator(Functions.<Observable<Integer>> identity()))
            // block and get result
            .subscribe(ts);
    // make sure only one value has arrived
    ts.assertValueCount(1);
    ts.requestMore(2);
    ts.assertValueCount(3);
    ts.unsubscribe();
}
 
Example 3
Source File: OperatorFromTransformerTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testBackpressureWhereOperatorRequestsDontMatchOneToOne() {
    TestSubscriber<List<Integer>> ts = TestSubscriber.create(1);
    Observable.
            //
            range(1, 1000)
            // use toOperator
            .lift(toOperator(new Func1<Observable<Integer>, Observable<List<Integer>>>() {

                @Override
                public Observable<List<Integer>> call(Observable<Integer> o) {
                    return o.toList();
                }
            }))
            // block and get result
            .subscribe(ts);
    // make sure only one value has arrived
    ts.assertValueCount(1);
    ts.requestMore(1);
    ts.assertCompleted();
    ts.assertValueCount(1);
}
 
Example 4
Source File: DatabaseExampleTest.java    From rxjava-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void testParameterOperatorBackpressure() {
    // use composition to find the first person alphabetically with
    // a score less than the person with the last name alphabetically
    // whose name is not XAVIER. Two threads and connections will be used.

    Database db = DatabaseCreator.db();
    TestSubscriber<String> ts = TestSubscriber.create(0);
    Observable.just("FRED", "FRED").repeat()
            .compose(db.select("select name from person where name = ?").parameterTransformer()
                    .getAs(String.class))
            .subscribe(ts);
    ts.requestMore(1);
    ts.assertValueCount(1);
    ts.unsubscribe();
}
 
Example 5
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void bufferWhileBackpressureFullBuffer() {
    TestSubscriber<List<Integer>> ts = TestSubscriber.create(0);
    
    int count = RxRingBuffer.SIZE * 4;
    
    Observable.range(1, count)
    .compose(Transformers.bufferWhile(UtilityFunctions.<Integer>alwaysFalse()))
    .subscribe(ts);
    
    ts.assertNoValues();
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(1);
    
    ts.assertValueCount(1);
    ts.assertNoErrors();
    ts.assertCompleted();
    
    Assert.assertEquals(count, ts.getOnNextEvents().get(0).size());
}
 
Example 6
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void bufferUntilBackpressureFullBuffer() {
    TestSubscriber<List<Integer>> ts = TestSubscriber.create(0);
    
    int count = RxRingBuffer.SIZE * 4;
    
    Observable.range(1, count)
    .compose(Transformers.bufferUntil(UtilityFunctions.<Integer>alwaysFalse()))
    .subscribe(ts);
    
    ts.assertNoValues();
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(1);
    
    ts.assertValueCount(1);
    ts.assertNoErrors();
    ts.assertCompleted();
    
    Assert.assertEquals(count, ts.getOnNextEvents().get(0).size());
}
 
Example 7
Source File: OperatorBufferToFileTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void handlesEmptyUsingJavaIOSerialization() {
    System.out.println("handlesEmptyUsingJavaIOSerialization");
    Scheduler scheduler = createSingleThreadScheduler();
    for (int i = 0; i < loops(); i++) {
        TestSubscriber<String> ts = TestSubscriber.create(0);
        Observable.<String> empty().compose(Transformers
                .onBackpressureBufferToFile(DataSerializers.<String> javaIO(), scheduler))
                .subscribe(ts);
        ts.requestMore(1);
        ts.awaitTerminalEvent();
        ts.assertNoErrors();
        ts.assertNoValues();
        ts.assertCompleted();
        waitUntilWorkCompleted(scheduler);
    }
}
 
Example 8
Source File: StringSplitTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void testBackpressureOneByOneWithBufferEmissions() {
    Observable<String> o = Observable.just("boo:an", "d:you").compose(Transformers.split(":"));

    TestSubscriber<String> ts = TestSubscriber.create(0);
    o.subscribe(ts);
    ts.requestMore(1);
    ts.assertValues("boo");
    ts.requestMore(1);
    ts.assertValues("boo", "and");
    ts.requestMore(1);
    ts.assertValues("boo", "and", "you");
}
 
Example 9
Source File: BackPressureTest.java    From azure-cosmosdb-java with MIT License 5 votes vote down vote up
@Test(groups = { "long" }, timeOut = TIMEOUT)
public void readFeed() throws Exception {
    FeedOptions options = new FeedOptions();
    options.setMaxItemCount(1);
    Observable<FeedResponse<Document>> queryObservable = client
        .readDocuments(getCollectionLink(), options);

    client.httpRequests.clear();

    TestSubscriber subscriber = new TestSubscriber(1);
    queryObservable.observeOn(Schedulers.io(), 1).subscribe(subscriber);
    int sleepTimeInMillis = 10000; // 10 seconds

    int i = 0;
    // use a test subscriber and request for more result and sleep in between
    while (subscriber.getCompletions() == 0 && subscriber.getOnErrorEvents().isEmpty()) {
        TimeUnit.MILLISECONDS.sleep(sleepTimeInMillis);
        sleepTimeInMillis /= 2;

        if (sleepTimeInMillis > 1000) {
            // validate that only one item is returned to subscriber in each iteration
            assertThat(subscriber.getValueCount() - i).isEqualTo(1);
        }
        // validate that only one item is returned to subscriber in each iteration
        // validate that the difference between the number of requests to backend
        // and the number of returned results is always less than a fixed threshold
        assertThat(client.httpRequests.size() - subscriber.getOnNextEvents().size())
            .isLessThanOrEqualTo(RxRingBuffer.SIZE + THRESHOLD);

        subscriber.requestMore(1);
        i++;
    }

    subscriber.assertNoErrors();
    subscriber.assertCompleted();
    assertThat(subscriber.getOnNextEvents()).hasSize(createdDocuments.size());
}
 
Example 10
Source File: OperatorBufferToFileTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void handlesUnsubscriptionDuringDrainLoop() throws InterruptedException {
    System.out.println("handlesUnsubscriptionDuringDrainLoop");
    Scheduler scheduler = createSingleThreadScheduler();
    TestSubscriber<String> ts = TestSubscriber.create(0);
    Observable.just("abc", "def", "ghi")
            //
            .compose(Transformers.onBackpressureBufferToFile(DataSerializers.string(),
                    scheduler))
            .doOnNext(new Action1<Object>() {

                @Override
                public void call(Object t) {
                    try {
                        // pauses drain loop
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                }
            }).subscribe(ts);
    ts.requestMore(2);
    TimeUnit.MILLISECONDS.sleep(250);
    ts.unsubscribe();
    TimeUnit.MILLISECONDS.sleep(500);
    ts.assertValues("abc");
    waitUntilWorkCompleted(scheduler);
}
 
Example 11
Source File: SortedMergeTest.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testBackpressure() {
    Observable<Integer> o1 = Observable.just(1, 3, 5, 7);
    Observable<Integer> o2 = Observable.just(2, 4, 6, 8);

    TestSubscriber<Integer> ts = new TestSubscriber<>(0);
    SortedMerge.create(Arrays.asList(o1, o2)).subscribe(ts);

    ts.requestMore(2);

    ts.assertNoErrors();
    ts.assertNotCompleted();
    ts.assertValues(1, 2);

}
 
Example 12
Source File: TransformerStateMachineTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoesNotRequestMaxValueOfUpstreamIfBackpressureBufferOptionSelected() {
    final AtomicLong requested = new AtomicLong();
    Observable<Integer> o = Observable.range(1, 200) //
            .doOnRequest(new Action1<Long>() {
                @Override
                public void call(Long n) {
                    BackpressureUtils.getAndAddRequest(requested, n);
                }
            });
    TestSubscriber<Integer> ts = TestSubscriber.create(0);
    o //
            .compose(Transformers.stateMachine().initialState(null)
                    .transition(new Transition<Object, Integer, Integer>() {

                        @Override
                        public Object call(Object state, Integer value,
                                Subscriber<Integer> subscriber) {
                            // subscriber.onNext(value);
                            return state;
                        }
                    }).build())
            // get as list
            .subscribe(ts);
    ts.requestMore(4);
    assertEquals(201, requested.get());
    ts.assertCompleted();
    ts.assertNoValues();
}
 
Example 13
Source File: OrderedMergeTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testBackpressure() {
	Observable<Integer> o1 = Observable.just(1, 3, 5, 7);
	Observable<Integer> o2 = Observable.just(2, 4, 6, 8);

	TestSubscriber<Integer> ts = TestSubscriber.create(0);
	OrderedMerge.create(Arrays.asList(o1, o2)).subscribe(ts);

	ts.requestMore(2);

	ts.assertNoErrors();
	ts.assertNotCompleted();
	ts.assertValues(1, 2);
}
 
Example 14
Source File: OperatorBufferToFileTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void handlesErrorWhenDelayErrorIsFalse() throws InterruptedException {
    Scheduler scheduler = createSingleThreadScheduler();
    TestSubscriber<String> ts = TestSubscriber.create(0);
    Observable.just("abc", "def").concatWith(Observable.<String> error(new IOException("boo")))
            //
            .compose(Transformers.onBackpressureBufferToFile(DataSerializers.string(),
                    scheduler, Options.delayError(false).build()))
            .doOnNext(new Action1<String>() {
                boolean first = true;

                @Override
                public void call(String t) {
                    if (first) {
                        first = false;
                        try {
                            TimeUnit.MILLISECONDS.sleep(500);
                        } catch (InterruptedException e) {
                        }
                    }
                }
            }).subscribe(ts);
    ts.requestMore(2);
    ts.awaitTerminalEvent(5000, TimeUnit.SECONDS);
    ts.assertError(IOException.class);
    waitUntilWorkCompleted(scheduler);
}
 
Example 15
Source File: OnSubscribeDoOnEmptyTest.java    From rxjava-extras with Apache License 2.0 4 votes vote down vote up
@Test
public void testBackPressure() {

    final AtomicBoolean wasCalled = new AtomicBoolean(false);

    Observable<Integer> source = Observable.range(0, 1000)
            .compose(Transformers.<Integer> doOnEmpty(Actions.setToTrue0(wasCalled)));

    TestSubscriber<Integer> ts = new TestSubscriber<Integer>(0);

    source.subscribe(ts);

    ts.requestMore(1);

    assertTrue(ts.getOnNextEvents().size() == 1);
    ts.assertNotCompleted();
    assertTrue(ts.getOnErrorEvents().size() == 0);
    assertFalse(wasCalled.get());
}
 
Example 16
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void bufferUntilBackpressure() {
    TestSubscriber<List<Integer>> ts = TestSubscriber.create(0);
    
    Observable.range(1, RxRingBuffer.SIZE * 4)
    .compose(Transformers.bufferUntil(new Func1<Integer, Boolean>() {
        @Override
        public Boolean call(Integer v) {
            return (v % 3) == 0;
        }
    })).subscribe(ts);
    
    ts.assertNoValues();
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(1);
    
    ts.assertValue(Arrays.asList(1, 2, 3));

    ts.requestMore(1);
    ts.assertValues(
            Arrays.asList(1, 2, 3),
            Arrays.asList(4, 5, 6)
    );
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(1);
    ts.assertValues(
            Arrays.asList(1, 2, 3),
            Arrays.asList(4, 5, 6),
            Arrays.asList(7, 8, 9)
    );
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(Long.MAX_VALUE);
    
    ts.assertValueCount((RxRingBuffer.SIZE * 4) / 3 + 1);
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example 17
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void bufferWhileBackpressure() {
    TestSubscriber<List<Integer>> ts = TestSubscriber.create(0);
    
    Observable.range(1, RxRingBuffer.SIZE * 4)
    .compose(Transformers.bufferWhile(new Func1<Integer, Boolean>() {
        @Override
        public Boolean call(Integer v) {
            return (v % 3) == 0;
        }
    })).subscribe(ts);
    
    ts.assertNoValues();
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(1);
    
    ts.assertValue(Arrays.asList(1, 2));

    ts.requestMore(1);
    ts.assertValues(
            Arrays.asList(1, 2),
            Arrays.asList(3, 4, 5)
    );
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(1);
    ts.assertValues(
            Arrays.asList(1, 2),
            Arrays.asList(3, 4, 5),
            Arrays.asList(6, 7, 8)
    );
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(Long.MAX_VALUE);
    
    ts.assertValueCount((RxRingBuffer.SIZE * 4) / 3 + 1);
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example 18
Source File: ObserveOnTest.java    From akarnokd-misc with Apache License 2.0 4 votes vote down vote up
@Test
public void completeExactRequest() {
    TestSubscriber<Integer> ts = TestSubscriber.create(0);

    TestScheduler test = Schedulers.test();

    Observable.range(1, 10).observeOn(test).subscribe(ts);

    test.advanceTimeBy(1, TimeUnit.SECONDS);

    ts.assertNoValues();
    ts.assertNoErrors();
    ts.assertNotCompleted();

    ts.requestMore(10);

    test.advanceTimeBy(1, TimeUnit.SECONDS);

    ts.assertValueCount(10);
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example 19
Source File: TransformerOnTerminateResumeTest.java    From rxjava-extras with Apache License 2.0 3 votes vote down vote up
@Test
public void mainErrorsBackpressure() {
    
    Transformer<Integer, Integer> op = new TransformerOnTerminateResume<Integer>(new Func1<Throwable, Observable<Integer>>() {
        @Override
        public Observable<Integer> call(Throwable e) {
            return Observable.just(11);
        }
    }, Observable.just(12));
    
    TestSubscriber<Integer> ts = TestSubscriber.create(0);
    
    Observable.range(1, 10).concatWith(Observable.<Integer>error(new IOException()))
    .compose(op)
    .subscribe(ts);
    
    ts.assertNoValues();
    
    ts.requestMore(2);

    ts.assertValues(1, 2);

    ts.requestMore(5);

    ts.assertValues(1, 2, 3, 4, 5, 6, 7);

    ts.requestMore(3);

    ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

    ts.requestMore(1);

    ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example 20
Source File: TransformerOnTerminateResumeTest.java    From rxjava-extras with Apache License 2.0 3 votes vote down vote up
@Test
public void mainCompletesBackpressure() {
    
    Transformer<Integer, Integer> op = new TransformerOnTerminateResume<Integer>(new Func1<Throwable, Observable<Integer>>() {
        @Override
        public Observable<Integer> call(Throwable e) {
            return Observable.just(11);
        }
    }, Observable.just(12));
    
    TestSubscriber<Integer> ts = TestSubscriber.create(0);
    
    Observable.range(1, 10)
    .compose(op)
    .subscribe(ts);
    
    ts.assertNoValues();
    
    ts.requestMore(2);

    ts.assertValues(1, 2);

    ts.requestMore(5);

    ts.assertValues(1, 2, 3, 4, 5, 6, 7);

    ts.requestMore(3);

    ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

    ts.requestMore(1);

    ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12);
    ts.assertNoErrors();
    ts.assertCompleted();
}