rx.internal.util.RxRingBuffer Java Examples

The following examples show how to use rx.internal.util.RxRingBuffer. 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: 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 #2
Source File: BackPressureTest.java    From azure-cosmosdb-java with MIT License 5 votes vote down vote up
@Test(groups = { "long" }, timeOut = TIMEOUT)
public void query() throws Exception {
    FeedOptions options = new FeedOptions();
    options.setMaxItemCount(1);
    Observable<FeedResponse<Document>> queryObservable = client
            .queryDocuments(getCollectionLink(), "SELECT * from r", options);

    client.httpRequests.clear();

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

    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 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.getValueCount())
                .isLessThanOrEqualTo(RxRingBuffer.SIZE + THRESHOLD);

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

    subscriber.assertNoErrors();
    subscriber.assertCompleted();

    assertThat(subscriber.getOnNextEvents()).hasSize(createdDocuments.size());
}
 
Example #3
Source File: BackPressureCrossPartitionTest.java    From azure-cosmosdb-java with MIT License 5 votes vote down vote up
@DataProvider(name = "queryProvider")
public Object[][] queryProvider() {
    return new Object[][] {
            // query, maxItemCount, max expected back pressure buffered, total number of expected query results
            { "SELECT * FROM r", 1, 2 * RxRingBuffer.SIZE, numberOfDocs},
            { "SELECT * FROM r", 100, 2 * RxRingBuffer.SIZE, numberOfDocs},
            { "SELECT * FROM r ORDER BY r.prop", 100, 2 * RxRingBuffer.SIZE + 3 * numberOfPartitions, numberOfDocs},
            { "SELECT TOP 1000 * FROM r", 1, 2 * RxRingBuffer.SIZE, 1000},
            { "SELECT TOP 1000 * FROM r", 100, 2 * RxRingBuffer.SIZE, 1000},
            { "SELECT TOP 1000 * FROM r ORDER BY r.prop", 100, 2 * RxRingBuffer.SIZE + 3 * numberOfPartitions , 1000},
    };
}
 
Example #4
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public static <T, R> Observable<R> combineLatestDelayError(Iterable<? extends Observable<? extends T>> sources, FuncN<? extends R> combineFunction) {
    return create(new OnSubscribeCombineLatest(null, sources, combineFunction, RxRingBuffer.SIZE, true));
}
 
Example #5
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final <R> Observable<R> concatMapIterable(Func1<? super T, ? extends Iterable<? extends R>> collectionSelector) {
    return OnSubscribeFlattenIterable.createFrom(this, collectionSelector, RxRingBuffer.SIZE);
}
 
Example #6
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
@Experimental
public final <R> Observable<R> concatMapEager(Func1<? super T, ? extends Observable<? extends R>> mapper) {
    return concatMapEager(mapper, RxRingBuffer.SIZE);
}
 
Example #7
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final <R> Observable<R> flatMapIterable(Func1<? super T, ? extends Iterable<? extends R>> collectionSelector) {
    return flatMapIterable((Func1) collectionSelector, RxRingBuffer.SIZE);
}
 
Example #8
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final Observable<T> observeOn(Scheduler scheduler) {
    return observeOn(scheduler, RxRingBuffer.SIZE);
}
 
Example #9
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final Observable<T> observeOn(Scheduler scheduler, boolean delayError) {
    return observeOn(scheduler, delayError, RxRingBuffer.SIZE);
}
 
Example #10
Source File: OperatorParallelMerge.java    From RxJavaParallel with Apache License 2.0 4 votes vote down vote up
@Override
public void onStart() {
    // we request backpressure so we can handle long-running Observables that are enqueueing, such as flatMap use cases
    // we decouple the Producer chain while keeping the Subscription chain together (perf benefit) via super(actual)
    request(RxRingBuffer.SIZE);
}
 
Example #11
Source File: OperatorParallelMerge.java    From RxJavaParallel with Apache License 2.0 4 votes vote down vote up
private void initScalarValueQueueIfNeeded() {
    if (scalarValueQueue == null) {
        scalarValueQueue = RxRingBuffer.getSpmcInstance();
        add(scalarValueQueue);
    }
}
 
Example #12
Source File: SortedMerge.java    From hawkular-metrics with Apache License 2.0 4 votes vote down vote up
public SourceSubscriber(MergeProducer<T> parent) {
    queue = RxRingBuffer.getSpscInstance();
    this.parent = parent;
}
 
Example #13
Source File: SortedMerge.java    From hawkular-metrics with Apache License 2.0 4 votes vote down vote up
@Override
public void onStart() {
    add(queue);
    request(RxRingBuffer.SIZE);
}
 
Example #14
Source File: Transformers.java    From rxjava-extras with Apache License 2.0 3 votes vote down vote up
/**
 * <p>
 * Returns the source {@link Observable} merged with all of the other
 * observables using the given {@link Comparator} for order. A precondition
 * is that the source and other are already ordered. This transformer
 * supports backpressure and its inputs must also support backpressure.
 * 
 * <p>
 * <img src=
 * "https://github.com/davidmoten/rxjava-extras/blob/master/src/docs/orderedMerge.png?raw=true"
 * alt="marble diagram">
 * 
 * @param others
 *            a collection of already ordered observables to merge with
 * @param comparator
 *            the ordering to use
 * @param <T>
 *            the generic type of the objects being compared
 * @return merged and ordered observable
 */
public static final <T> Transformer<T, T> orderedMergeWith(
        final Collection<Observable<T>> others, final Comparator<? super T> comparator) {
    return new Transformer<T, T>() {

        @Override
        public Observable<T> call(Observable<T> source) {
            List<Observable<T>> collection = new ArrayList<Observable<T>>();
            collection.add(source);
            collection.addAll(others);
            return OrderedMerge.<T> create(collection, comparator, false, RxRingBuffer.SIZE);
        }
    };
}
 
Example #15
Source File: Transformers.java    From rxjava-extras with Apache License 2.0 2 votes vote down vote up
/**
 * Buffers the elements into continuous, non-overlapping Lists where the
 * boundary is determined by a predicate receiving each item, after being
 * buffered, and returns true to indicate a new buffer should start.
 * 
 * <p>
 * The operator won't return an empty first or last buffer.
 * 
 * <dl>
 * <dt><b>Backpressure Support:</b></dt>
 * <dd>This operator supports backpressure.</dd>
 * <dt><b>Scheduler:</b></dt>
 * <dd>This operator does not operate by default on a particular
 * {@link Scheduler}.</dd>
 * </dl>
 * 
 * @param <T>
 *            the input value type
 * @param predicate
 *            the Func1 that receives each item, after being buffered, and
 *            should return true to indicate a new buffer has to start.
 * @param capacityHint
 *            the expected number of items in each buffer
 * @return the new Observable instance
 * @see #bufferWhile(Func1)
 * @since (if this graduates from Experimental/Beta to supported, replace
 *        this parenthetical with the release number)
 */
public static final <T> Transformer<T, List<T>> bufferUntil(Func1<? super T, Boolean> predicate,
        int capacityHint) {
    return new OperatorBufferPredicateBoundary<T>(predicate, RxRingBuffer.SIZE, capacityHint,
            true);
}
 
Example #16
Source File: Transformers.java    From rxjava-extras with Apache License 2.0 2 votes vote down vote up
/**
 * Buffers the elements into continuous, non-overlapping Lists where the
 * boundary is determined by a predicate receiving each item, before being
 * buffered, and returns true to indicate a new buffer should start.
 * 
 * <p>
 * The operator won't return an empty first or last buffer.
 * 
 * <dl>
 * <dt><b>Backpressure Support:</b></dt>
 * <dd>This operator supports backpressure.</dd>
 * <dt><b>Scheduler:</b></dt>
 * <dd>This operator does not operate by default on a particular
 * {@link Scheduler}.</dd>
 * </dl>
 * 
 * @param <T>
 *            the input value type
 * @param predicate
 *            the Func1 that receives each item, before being buffered, and
 *            should return true to indicate a new buffer has to start.
 * @param capacityHint
 *            the expected number of items in each buffer
 * @return the new Observable instance
 * @see #bufferWhile(Func1)
 * @since (if this graduates from Experimental/Beta to supported, replace
 *        this parenthetical with the release number)
 */
public static final <T> Transformer<T, List<T>> bufferWhile(Func1<? super T, Boolean> predicate,
        int capacityHint) {
    return new OperatorBufferPredicateBoundary<T>(predicate, RxRingBuffer.SIZE, capacityHint,
            false);
}