Search in sources :

Example 1 with Subscriber

use of io.servicetalk.concurrent.CompletableSource.Subscriber in project servicetalk by apple.

the class CompletableDoOnUtils method doOnSubscribeSupplier.

static Supplier<Subscriber> doOnSubscribeSupplier(Consumer<Cancellable> onSubscribe) {
    requireNonNull(onSubscribe);
    Subscriber subscriber = new Subscriber() {

        @Override
        public void onSubscribe(Cancellable cancellable) {
            onSubscribe.accept(cancellable);
        }

        @Override
        public void onComplete() {
        // NOOP
        }

        @Override
        public void onError(Throwable t) {
        // NOOP
        }
    };
    return () -> subscriber;
}
Also used : Subscriber(io.servicetalk.concurrent.CompletableSource.Subscriber) Cancellable(io.servicetalk.concurrent.Cancellable)

Example 2 with Subscriber

use of io.servicetalk.concurrent.CompletableSource.Subscriber in project servicetalk by apple.

the class DefaultNettyConnection method write.

@Override
public Completable write(final Publisher<Write> write, final Supplier<FlushStrategy> flushStrategySupplier, final Supplier<WriteDemandEstimator> demandEstimatorSupplier) {
    return cleanupStateWhenDone(new SubscribableCompletable() {

        @Override
        protected void handleSubscribe(Subscriber completableSubscriber) {
            final WriteObserver writeObserver = DefaultNettyConnection.this.dataObserver.onNewWrite();
            WriteStreamSubscriber subscriber = new WriteStreamSubscriber(channel(), demandEstimatorSupplier.get(), completableSubscriber, closeHandler, writeObserver, enrichProtocolError, isClient, shouldWait);
            if (failIfWriteActive(subscriber, completableSubscriber)) {
                toSource(composeFlushes(channel(), write, flushStrategySupplier.get(), writeObserver)).subscribe(subscriber);
            }
        }
    }).onErrorMap(this::enrichError);
}
Also used : Subscriber(io.servicetalk.concurrent.CompletableSource.Subscriber) SubscribableCompletable(io.servicetalk.concurrent.api.internal.SubscribableCompletable) WriteObserver(io.servicetalk.transport.api.ConnectionObserver.WriteObserver)

Example 3 with Subscriber

use of io.servicetalk.concurrent.CompletableSource.Subscriber in project servicetalk by apple.

the class TimeoutCompletableTest method cancelDoesOnError.

@Test
void cancelDoesOnError() throws Exception {
    DelayedOnSubscribeCompletable delayedCompletable = new DelayedOnSubscribeCompletable();
    init(delayedCompletable, false);
    CountDownLatch cancelLatch = new CountDownLatch(1);
    CompletableSource.Subscriber subscriber = delayedCompletable.subscriber;
    assertNotNull(subscriber);
    subscriber.onSubscribe(() -> {
        subscriber.onError(DELIBERATE_EXCEPTION);
        cancelLatch.countDown();
    });
    testExecutor.advanceTimeBy(1, NANOSECONDS);
    assertThat(testExecutor.scheduledTasksPending(), is(0));
    assertThat(testExecutor.scheduledTasksExecuted(), is(1));
    cancelLatch.await();
    Throwable error = listener.awaitOnError();
    assertThat(error, instanceOf(TimeoutException.class));
}
Also used : Subscriber(io.servicetalk.concurrent.CompletableSource.Subscriber) CompletableSource(io.servicetalk.concurrent.CompletableSource) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 4 with Subscriber

use of io.servicetalk.concurrent.CompletableSource.Subscriber in project servicetalk by apple.

the class TimeoutCompletableTest method defaultExecutorSubscribeTimeout.

@Test
@Disabled("Requires the notion of globally mocked time source using a TestExecutor")
void defaultExecutorSubscribeTimeout() {
    DelayedOnSubscribeCompletable delayedCompletable = new DelayedOnSubscribeCompletable();
    // Assume the timeout call is not under the user's control, e.g. comes from a library of Completable providers
    final Completable operationThatInternallyTimesOut = delayedCompletable.timeout(Duration.ofDays(1));
    TestExecutor testExecutor = new TestExecutor();
    // TODO(dariusz): Replace all executors created with the test instance
    // Executors.setFactory(AllExecutorFactory.create(() -> testExecutor));
    toSource(operationThatInternallyTimesOut).subscribe(listener);
    testExecutor.advanceTimeBy(1, DAYS);
    CompletableSource.Subscriber subscriber = delayedCompletable.subscriber;
    assertNotNull(subscriber);
    assertThat(listener.awaitOnError(), instanceOf(TimeoutException.class));
}
Also used : TestExecutor(io.servicetalk.concurrent.api.TestExecutor) Subscriber(io.servicetalk.concurrent.CompletableSource.Subscriber) Completable(io.servicetalk.concurrent.api.Completable) CompletableSource(io.servicetalk.concurrent.CompletableSource) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 5 with Subscriber

use of io.servicetalk.concurrent.CompletableSource.Subscriber in project servicetalk by apple.

the class BeforeSubscribeTest method testCallbackThrowsError.

@Test
void testCallbackThrowsError() {
    List<Throwable> failures = new ArrayList<>();
    toSource(doSubscribe(Completable.completed(), s -> {
        throw DELIBERATE_EXCEPTION;
    })).subscribe(new Subscriber() {

        @Override
        public void onSubscribe(final Cancellable c) {
            failures.add(new AssertionError("onSubscribe invoked unexpectedly."));
        }

        @Override
        public void onError(final Throwable t) {
            failures.add(t);
        }

        @Override
        public void onComplete() {
            failures.add(new AssertionError("onComplete invoked unexpectedly."));
        }
    });
    assertThat("Unexpected errors: " + failures, failures, contains(DELIBERATE_EXCEPTION));
}
Also used : Subscriber(io.servicetalk.concurrent.CompletableSource.Subscriber) Cancellable(io.servicetalk.concurrent.Cancellable) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

Subscriber (io.servicetalk.concurrent.CompletableSource.Subscriber)15 Cancellable (io.servicetalk.concurrent.Cancellable)6 Test (org.junit.jupiter.api.Test)4 CompletableSource (io.servicetalk.concurrent.CompletableSource)3 TimeoutException (java.util.concurrent.TimeoutException)3 TestCompletableSubscriber (io.servicetalk.concurrent.test.internal.TestCompletableSubscriber)2 ArrayList (java.util.ArrayList)2 ChannelPipeline (io.netty.channel.ChannelPipeline)1 Completable (io.servicetalk.concurrent.api.Completable)1 TestCancellable (io.servicetalk.concurrent.api.TestCancellable)1 TestExecutor (io.servicetalk.concurrent.api.TestExecutor)1 SubscribableCompletable (io.servicetalk.concurrent.api.internal.SubscribableCompletable)1 SubscribableSingle (io.servicetalk.concurrent.api.internal.SubscribableSingle)1 DelayedCancellable (io.servicetalk.concurrent.internal.DelayedCancellable)1 WriteObserver (io.servicetalk.transport.api.ConnectionObserver.WriteObserver)1 DefaultExecutionContext (io.servicetalk.transport.api.DefaultExecutionContext)1 WeakReference (java.lang.ref.WeakReference)1 Collection (java.util.Collection)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1