Search in sources :

Example 6 with ContextMap

use of io.servicetalk.context.api.ContextMap in project servicetalk by apple.

the class ConnectionFactoryFilterTest method testAppend.

@Test
void testAppend() throws Exception {
    Deque<Integer> createOrder = new ArrayDeque<>();
    Deque<Integer> connectOrder = new ArrayDeque<>();
    class FactoryOrder implements ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> {

        final int order;

        ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> original;

        FactoryOrder(int order, ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> original) {
            this.order = order;
            this.original = original;
        }

        @Override
        public Single<ListenableAsyncCloseable> newConnection(final InetSocketAddress unused, @Nullable final ContextMap context, @Nullable final TransportObserver observer) {
            connectOrder.add(order);
            return original.newConnection(unused, context, observer);
        }

        @Override
        public Completable closeAsync() {
            return Completable.completed();
        }

        @Override
        public Completable onClose() {
            return Completable.completed();
        }
    }
    class FilterOrder implements ConnectionFactoryFilter<InetSocketAddress, ListenableAsyncCloseable> {

        final int order;

        FilterOrder(int order) {
            this.order = order;
        }

        @Override
        public ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> create(final ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> original) {
            createOrder.add(order);
            return new FactoryOrder(order, original);
        }
    }
    FilterOrder first = new FilterOrder(1);
    FilterOrder second = new FilterOrder(2);
    ConnectionFactoryFilter<InetSocketAddress, ListenableAsyncCloseable> combined = first.append(second);
    ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> root = new FactoryOrder(999, new ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable>() {

        @Override
        public Single<ListenableAsyncCloseable> newConnection(final InetSocketAddress unused, @Nullable final ContextMap context, @Nullable final TransportObserver observer) {
            return Single.succeeded(DUMMY_CLOSABLE);
        }

        @Override
        public Completable onClose() {
            return Completable.completed();
        }

        @Override
        public Completable closeAsync() {
            return Completable.completed();
        }
    });
    ConnectionFactory<InetSocketAddress, ListenableAsyncCloseable> factory = combined.create(root);
    ListenableAsyncCloseable connection = factory.newConnection(mock(InetSocketAddress.class), null, null).toFuture().get();
    assertThat(connection, is(sameInstance(DUMMY_CLOSABLE)));
    assertThat(createOrder, is(hasSize(2)));
    assertThat(createOrder, is(containsInRelativeOrder(2, 1)));
    assertThat(connectOrder, is(hasSize(3)));
    assertThat(connectOrder, is(containsInRelativeOrder(1, 2, 999)));
}
Also used : Completable(io.servicetalk.concurrent.api.Completable) InetSocketAddress(java.net.InetSocketAddress) TransportObserver(io.servicetalk.transport.api.TransportObserver) ArrayDeque(java.util.ArrayDeque) ContextMap(io.servicetalk.context.api.ContextMap) Single(io.servicetalk.concurrent.api.Single) ListenableAsyncCloseable(io.servicetalk.concurrent.api.ListenableAsyncCloseable) Nullable(javax.annotation.Nullable) Test(org.junit.jupiter.api.Test)

Example 7 with ContextMap

use of io.servicetalk.context.api.ContextMap in project servicetalk by apple.

the class ContextPreservingBiConsumer method slowPath.

private void slowPath(T t, U u) {
    ContextMap prev = CONTEXT_THREAD_LOCAL.get();
    try {
        CONTEXT_THREAD_LOCAL.set(saved);
        delegate.accept(t, u);
    } finally {
        CONTEXT_THREAD_LOCAL.set(prev);
    }
}
Also used : ContextMap(io.servicetalk.context.api.ContextMap)

Example 8 with ContextMap

use of io.servicetalk.context.api.ContextMap in project servicetalk by apple.

the class ContextPreservingCancellable method slowPath.

private void slowPath() {
    ContextMap prev = CONTEXT_THREAD_LOCAL.get();
    try {
        CONTEXT_THREAD_LOCAL.set(saved);
        delegate.cancel();
    } finally {
        CONTEXT_THREAD_LOCAL.set(prev);
    }
}
Also used : ContextMap(io.servicetalk.context.api.ContextMap)

Example 9 with ContextMap

use of io.servicetalk.context.api.ContextMap in project servicetalk by apple.

the class ContextPreservingCompletableSubscriber method onSubscribeSlowPath.

private void onSubscribeSlowPath(Cancellable cancellable) {
    ContextMap prev = CONTEXT_THREAD_LOCAL.get();
    try {
        CONTEXT_THREAD_LOCAL.set(saved);
        invokeOnSubscribe(cancellable);
    } finally {
        CONTEXT_THREAD_LOCAL.set(prev);
    }
}
Also used : ContextMap(io.servicetalk.context.api.ContextMap)

Example 10 with ContextMap

use of io.servicetalk.context.api.ContextMap in project servicetalk by apple.

the class ContextPreservingConsumer method slowPath.

private void slowPath(T t) {
    ContextMap prev = CONTEXT_THREAD_LOCAL.get();
    try {
        CONTEXT_THREAD_LOCAL.set(saved);
        delegate.accept(t);
    } finally {
        CONTEXT_THREAD_LOCAL.set(prev);
    }
}
Also used : ContextMap(io.servicetalk.context.api.ContextMap)

Aggregations

ContextMap (io.servicetalk.context.api.ContextMap)49 ContextMapHolder (io.servicetalk.context.api.ContextMapHolder)20 Single (io.servicetalk.concurrent.api.Single)5 AsyncContext (io.servicetalk.concurrent.api.AsyncContext)4 Completable (io.servicetalk.concurrent.api.Completable)4 Executor (io.servicetalk.concurrent.api.Executor)4 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 ListenableAsyncCloseable (io.servicetalk.concurrent.api.ListenableAsyncCloseable)3 TerminalSignalConsumer (io.servicetalk.concurrent.api.TerminalSignalConsumer)3 AbstractOffloadingTest (io.servicetalk.concurrent.api.internal.AbstractOffloadingTest)3 Cancellable (io.servicetalk.concurrent.Cancellable)2 Publisher (io.servicetalk.concurrent.api.Publisher)2 FilterableStreamingHttpConnection (io.servicetalk.http.api.FilterableStreamingHttpConnection)2 TransportObserver (io.servicetalk.transport.api.TransportObserver)2 ArrayList (java.util.ArrayList)2 EnumSet (java.util.EnumSet)2 BiFunction (java.util.function.BiFunction)2 Nullable (javax.annotation.Nullable)2 CoreMatchers.is (org.hamcrest.CoreMatchers.is)2