Search in sources :

Example 11 with Cancellable

use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.

the class SequentialCancellableTest method testWithIgnoreCancel.

@Test
void testWithIgnoreCancel() {
    SequentialCancellable sc = new SequentialCancellable();
    sc.nextCancellable(Cancellable.IGNORE_CANCEL);
    Cancellable next = mock(Cancellable.class);
    sc.nextCancellable(next);
    verifyNoInteractions(next);
    sc.cancel();
    verify(next).cancel();
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) Test(org.junit.jupiter.api.Test)

Example 12 with Cancellable

use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.

the class WriteStreamSubscriberOutOfEventloopTest method testTerminalOrder.

@Test
void testTerminalOrder() throws Exception {
    Processor subject = newCompletableProcessor();
    CompletableSource.Subscriber subscriber = new CompletableSource.Subscriber() {

        @Override
        public void onSubscribe(Cancellable cancellable) {
        // noop
        }

        @Override
        public void onComplete() {
            subject.onComplete();
        }

        @Override
        public void onError(Throwable t) {
            if (pendingFlush.contains(1)) {
                subject.onError(t);
            } else {
                subject.onError(new IllegalStateException("The expected object wasn't written before termination!", t));
            }
        }
    };
    WriteDemandEstimator demandEstimator = mock(WriteDemandEstimator.class);
    this.subscriber = new WriteStreamSubscriber(channel, demandEstimator, subscriber, UNSUPPORTED_PROTOCOL_CLOSE_HANDLER, NoopWriteObserver.INSTANCE, identity(), false, __ -> false);
    this.subscriber.onNext(1);
    this.subscriber.onError(DELIBERATE_EXCEPTION);
    try {
        fromSource(subject).toFuture().get();
        fail();
    } catch (ExecutionException cause) {
        assertSame(DELIBERATE_EXCEPTION, cause.getCause());
    }
}
Also used : UNSUPPORTED_PROTOCOL_CLOSE_HANDLER(io.servicetalk.transport.netty.internal.CloseHandler.UNSUPPORTED_PROTOCOL_CLOSE_HANDLER) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Matchers.empty(org.hamcrest.Matchers.empty) UnaryOperator.identity(java.util.function.UnaryOperator.identity) Cancellable(io.servicetalk.concurrent.Cancellable) EventLoop(io.netty.channel.EventLoop) CompletableSource(io.servicetalk.concurrent.CompletableSource) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) NoopWriteObserver(io.servicetalk.transport.netty.internal.NoopTransportObserver.NoopWriteObserver) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) Processor(io.servicetalk.concurrent.CompletableSource.Processor) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) Processors.newCompletableProcessor(io.servicetalk.concurrent.api.Processors.newCompletableProcessor) Mockito.mock(org.mockito.Mockito.mock) Processor(io.servicetalk.concurrent.CompletableSource.Processor) Processors.newCompletableProcessor(io.servicetalk.concurrent.api.Processors.newCompletableProcessor) Cancellable(io.servicetalk.concurrent.Cancellable) CompletableSource(io.servicetalk.concurrent.CompletableSource) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 13 with Cancellable

use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.

the class DefaultNettyConnectionTest method testUpdateFlushStrategy.

@Test
void testUpdateFlushStrategy() {
    toSource(conn.write(from(newBuffer("Hello"), TRAILER.duplicate()))).subscribe(writeListener);
    writeListener.awaitOnComplete();
    // Flush on each (default)
    pollChannelAndVerifyWrites("Hello", TRAILER_MSG);
    writeListener = new TestCompletableSubscriber();
    Cancellable c = conn.updateFlushStrategy((old, __) -> batchFlush(3, never()));
    toSource(conn.write(publisher)).subscribe(writeListener);
    publisher.onNext(newBuffer("Hello1"));
    // No flush
    pollChannelAndVerifyWrites();
    publisher.onNext(newBuffer("Hello2"));
    publisher.onNext(TRAILER.duplicate());
    // Batch flush of 2
    pollChannelAndVerifyWrites("Hello1", "Hello2", TRAILER_MSG);
    publisher.onComplete();
    writeListener.awaitOnComplete();
    c.cancel();
    writeListener = new TestCompletableSubscriber();
    toSource(conn.write(from(newBuffer("Hello3"), TRAILER.duplicate()))).subscribe(writeListener);
    writeListener.awaitOnComplete();
    // Reverted to flush on each
    pollChannelAndVerifyWrites("Hello3", TRAILER_MSG);
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) TestCompletableSubscriber(io.servicetalk.concurrent.test.internal.TestCompletableSubscriber) Test(org.junit.jupiter.api.Test)

Example 14 with Cancellable

use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.

the class FlushStrategyHolderTest method updateWhenOriginal.

@Test
void updateWhenOriginal() {
    FlushStrategy prev = holder.currentStrategy();
    FlushStrategy next = mock(FlushStrategy.class);
    Cancellable revert = holder.updateFlushStrategy((current, isCurrentOriginal) -> isCurrentOriginal ? next : current);
    assertThat("Unexpected strategy post update.", holder.currentStrategy(), equalTo(next));
    revert.cancel();
    assertThat("Unexpected strategy post revert.", holder.currentStrategy(), equalTo(prev));
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) Test(org.junit.jupiter.api.Test)

Example 15 with Cancellable

use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.

the class FlushStrategyHolderTest method revertNoOpOnMismatch.

@Test
void revertNoOpOnMismatch() {
    FlushStrategy prev = holder.currentStrategy();
    FlushStrategy next1 = mock(FlushStrategy.class);
    Cancellable revert1 = holder.updateFlushStrategy((current, isCurrentOriginal) -> isCurrentOriginal ? next1 : current);
    assertThat("Unexpected strategy post update.", holder.currentStrategy(), equalTo(next1));
    FlushStrategy next2 = mock(FlushStrategy.class);
    Cancellable revert2 = holder.updateFlushStrategy((current, isCurrentOriginal) -> next2);
    revert1.cancel();
    // Cancel should be a noop since we have already updated
    assertThat("Unexpected strategy post revert.", holder.currentStrategy(), equalTo(next2));
    revert2.cancel();
    // Cancel should revert to original
    assertThat("Unexpected strategy post revert.", holder.currentStrategy(), equalTo(prev));
}
Also used : Cancellable(io.servicetalk.concurrent.Cancellable) Test(org.junit.jupiter.api.Test)

Aggregations

Cancellable (io.servicetalk.concurrent.Cancellable)74 Test (org.junit.jupiter.api.Test)47 CountDownLatch (java.util.concurrent.CountDownLatch)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 SingleSource (io.servicetalk.concurrent.SingleSource)11 Matchers.is (org.hamcrest.Matchers.is)10 CompletableSource (io.servicetalk.concurrent.CompletableSource)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Publisher.from (io.servicetalk.concurrent.api.Publisher.from)7 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)7 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)7 Subscriber (io.servicetalk.concurrent.CompletableSource.Subscriber)6 Executor (io.servicetalk.concurrent.api.Executor)6 Single (io.servicetalk.concurrent.api.Single)6 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)6 TestCancellable (io.servicetalk.concurrent.api.TestCancellable)6 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)6 TimeUnit (java.util.concurrent.TimeUnit)6 PublisherSource (io.servicetalk.concurrent.PublisherSource)5