Search in sources :

Example 16 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 17 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)

Example 18 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 19 with Cancellable

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

the class AutoOnSubscribeCompletableSubscriberFunction method apply.

@Override
public Subscriber apply(final Subscriber subscriber) {
    final DelayedCancellable subscription = new DelayedCancellable();
    subscriber.onSubscribe(subscription);
    return new DelegatingCompletableSubscriber(subscriber) {

        @Override
        public void onSubscribe(final Cancellable s) {
            subscription.delayedCancellable(s);
        }
    };
}
Also used : DelayedCancellable(io.servicetalk.concurrent.internal.DelayedCancellable) Cancellable(io.servicetalk.concurrent.Cancellable) DelayedCancellable(io.servicetalk.concurrent.internal.DelayedCancellable)

Example 20 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)

Aggregations

Cancellable (io.servicetalk.concurrent.Cancellable)68 Test (org.junit.jupiter.api.Test)46 CountDownLatch (java.util.concurrent.CountDownLatch)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)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 Completable (io.servicetalk.concurrent.api.Completable)5