Java Code Examples for rx.subjects.Subject#onCompleted()

The following examples show how to use rx.subjects.Subject#onCompleted() . 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: RateLimitedBatcherTest.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Test
public void onCompletedIsNotSentAfterOnError() {
    final RateLimitedBatcher<BatchableOperationMock, String> batcher = buildBatcher(minimumTimeInQueueMs);
    final Subject<BatchableOperationMock, BatchableOperationMock> updates = PublishSubject.<BatchableOperationMock>create().toSerialized();

    final AssertableSubscriber<Batch<BatchableOperationMock, String>> subscriber = updates.lift(batcher).test();
    testScheduler.triggerActions();
    subscriber.assertNoTerminalEvent().assertNoValues();

    updates.onError(new RuntimeException("some problem"));
    testScheduler.triggerActions(); // onError is forwarded right away (i.e.: don't wait for the next flush event)
    subscriber.assertNotCompleted().assertError(RuntimeException.class);

    updates.onCompleted();
    subscriber.assertNotCompleted();
}
 
Example 2
Source File: RateLimitedBatcherTest.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Test
public void onErrorIsNotSentAfterOnCompleted() {
    final RateLimitedBatcher<BatchableOperationMock, String> batcher = buildBatcher(minimumTimeInQueueMs);
    final Subject<BatchableOperationMock, BatchableOperationMock> updates = PublishSubject.<BatchableOperationMock>create().toSerialized();

    final AssertableSubscriber<Batch<BatchableOperationMock, String>> subscriber = updates.lift(batcher).test();
    testScheduler.triggerActions();
    subscriber.assertNoTerminalEvent().assertNoValues();

    updates.onCompleted();
    // onCompleted is sent after pending items are drained
    testScheduler.advanceTimeBy(minimumTimeInQueueMs, TimeUnit.MILLISECONDS);
    subscriber.assertNoErrors().assertCompleted();

    updates.onError(new RuntimeException("some problem"));
    subscriber.assertNoErrors();
}
 
Example 3
Source File: RateLimitedBatcherTest.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Test
public void ignoreErrorsFromDownstream() {
    final Instant now = Instant.ofEpochMilli(testScheduler.now());
    final RateLimitedBatcher<BatchableOperationMock, String> batcher = buildBatcher(minimumTimeInQueueMs);
    final Subject<BatchableOperationMock, BatchableOperationMock> updates = PublishSubject.<BatchableOperationMock>create().toSerialized();

    final AssertableSubscriber<?> subscriber = updates.lift(batcher)
            .lift(new ExceptionThrowingOperator("some error happened"))
            .test();
    testScheduler.triggerActions();
    subscriber.assertNoTerminalEvent().assertNoValues();

    for (int i = 0; i < 10; i++) {
        updates.onNext(new BatchableOperationMock(LOW, now, "resource2", "sub2", "create"));
        testScheduler.advanceTimeBy(minimumTimeInQueueMs, TimeUnit.MILLISECONDS);
        subscriber.assertNoTerminalEvent().assertNoValues();
    }

    updates.onCompleted();
    testScheduler.advanceTimeBy(minimumTimeInQueueMs, TimeUnit.MILLISECONDS);
    subscriber.assertNoValues().assertCompleted();
}
 
Example 4
Source File: AbstractGenericHandler.java    From couchbase-jvm-core with Apache License 2.0 6 votes vote down vote up
/**
 * Fulfill and complete the response observable.
 *
 * When called directly, this method completes on the event loop, but it can also be used in a callback (see
 * {@link #scheduleDirect(CoreScheduler, CouchbaseResponse, Subject)} for example.
 */
private void completeResponse(final CouchbaseResponse response,
    final Subject<CouchbaseResponse, CouchbaseResponse> observable) {
    // Noone is listening anymore, handle tracing and/or orphan reporting
    // depending on if enabled or not.
    CouchbaseRequest request = response.request();
    if (request != null && !request.isActive()) {
        if (env().operationTracingEnabled() && request.span() != null) {
            Scope scope = env().tracer().scopeManager()
                    .activate(request.span(), true);
            scope.span().setBaggageItem("couchbase.orphan", "true");
            scope.close();
        }
        if (env().orphanResponseReportingEnabled()) {
            env().orphanResponseReporter().report(response);
        }
    }

    try {
        observable.onNext(response);
        observable.onCompleted();
    } catch (Exception ex) {
        LOGGER.warn("Caught exception while onNext on observable", ex);
        observable.onError(ex);
    }
}
 
Example 5
Source File: TestCouchbaseTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private static CouchbaseConnector getConnector(Class responseClass) throws Exception {
  ENV = DefaultCouchbaseEnvironment.create();

  CouchbaseCore core = mock(CouchbaseCore.class);
  CouchbaseAsyncBucket asyncBucket = new CouchbaseAsyncBucket(core,
      ENV,
      BUCKET,
      USERNAME,
      PASSWORD,
      Collections.emptyList()
  );

  final CouchbaseRequest requestMock = mock(CouchbaseRequest.class);

  Subject<CouchbaseResponse, CouchbaseResponse> response = AsyncSubject.create();

  if(responseClass == SimpleSubdocResponse.class) {
    final BinarySubdocRequest subdocRequestMock = mock(BinarySubdocRequest.class);
    when(subdocRequestMock.span()).thenReturn(mock(Span.class));

    response.onNext(new SimpleSubdocResponse(ResponseStatus.SUCCESS,
        KeyValueStatus.SUCCESS.code(),
        BUCKET,
        Unpooled.EMPTY_BUFFER,
        subdocRequestMock,
        1234,
        null
    ));

    response.onCompleted();
  } else {

    Constructor con = responseClass.getConstructor(ResponseStatus.class,
        short.class,
        long.class,
        String.class,
        ByteBuf.class,
        MutationToken.class,
        CouchbaseRequest.class
    );

    response.onNext((CouchbaseResponse) con.newInstance(ResponseStatus.SUCCESS,
        KeyValueStatus.SUCCESS.code(),
        1234,
        BUCKET,
        Unpooled.EMPTY_BUFFER,
        null,
        requestMock
    ));
    response.onCompleted();
  }

  when(core.send(any(BinarySubdocRequest.class))).thenReturn(response);
  when(core.send(any())).thenReturn(response);
  when(requestMock.span()).thenReturn(mock(Span.class));

  CouchbaseConnector connector = mock(CouchbaseConnector.class);
  when(connector.getScheduler()).thenReturn(ENV.scheduler());
  when(connector.bucket()).thenReturn(asyncBucket);

  return connector;
}