Search in sources :

Example 1 with ConnectionAcceptorFactory

use of io.servicetalk.transport.api.ConnectionAcceptorFactory in project servicetalk by apple.

the class ConnectionAcceptorFilterTest method testAppend.

@Test
void testAppend() throws Exception {
    Deque<Integer> createOrder = new ArrayDeque<>();
    Deque<Integer> connectOrder = new ArrayDeque<>();
    class AcceptorOrder implements ConnectionAcceptor {

        final int order;

        ConnectionAcceptor original;

        AcceptorOrder(int order, ConnectionAcceptor original) {
            this.order = order;
            this.original = original;
        }

        @Override
        public Completable accept(ConnectionContext context) {
            connectOrder.add(order);
            return original.accept(context);
        }

        @Override
        public Completable closeAsync() {
            return Completable.completed();
        }
    }
    class FilterOrder implements ConnectionAcceptorFactory {

        final int order;

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

        @Override
        public ConnectionAcceptor create(ConnectionAcceptor original) {
            createOrder.add(order);
            return new AcceptorOrder(order, original);
        }
    }
    FilterOrder first = new FilterOrder(1);
    FilterOrder second = new FilterOrder(2);
    ConnectionAcceptorFactory combined = first.append(second);
    ConnectionAcceptor acceptor = combined.create(ConnectionAcceptor.ACCEPT_ALL);
    @SuppressWarnings("unused") Void nothing = acceptor.accept(mock(ConnectionContext.class)).toFuture().get();
    assertThat(nothing, is(nullValue()));
    assertThat(createOrder, is(hasSize(2)));
    assertThat(createOrder, is(containsInRelativeOrder(2, 1)));
    assertThat(connectOrder, is(hasSize(2)));
    assertThat(connectOrder, is(containsInRelativeOrder(1, 2)));
}
Also used : ConnectionAcceptor(io.servicetalk.transport.api.ConnectionAcceptor) ConnectionContext(io.servicetalk.transport.api.ConnectionContext) ConnectionAcceptorFactory(io.servicetalk.transport.api.ConnectionAcceptorFactory) ArrayDeque(java.util.ArrayDeque) Test(org.junit.jupiter.api.Test)

Example 2 with ConnectionAcceptorFactory

use of io.servicetalk.transport.api.ConnectionAcceptorFactory in project servicetalk by apple.

the class ConnectionAcceptorOffloadingTest method testAcceptorOffloading.

@ParameterizedTest
@ValueSource(booleans = { false, true })
void testAcceptorOffloading(boolean offload) throws Exception {
    AtomicReference<Boolean> offloaded = new AtomicReference<>();
    ConnectionAcceptorFactory factory = ConnectionAcceptorFactory.withStrategy(original -> context -> {
        boolean isIoThread = IoThreadFactory.IoThread.currentThreadIsIoThread();
        offloaded.set(!isIoThread);
        return original.accept(context);
    }, offload ? ConnectExecutionStrategy.offloadAll() : ConnectExecutionStrategy.offloadNone());
    try (ServerContext server = HttpServers.forPort(0).appendConnectionAcceptorFilter(factory).listenAndAwait(this::helloWorld)) {
        SocketAddress serverAddress = server.listenAddress();
        try (BlockingHttpClient client = HttpClients.forResolvedAddress(serverAddress).buildBlocking()) {
            HttpResponse response = client.request(client.get("/sayHello"));
            assertThat("unexpected status", response.status(), is(HttpResponseStatus.OK));
        }
    }
    assertThat("factory was not invoked", offloaded.get(), is(notNullValue()));
    assertThat("incorrect offloading", offloaded.get(), is(offload));
}
Also used : ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) SocketAddress(java.net.SocketAddress) ConnectionAcceptorFactory(io.servicetalk.transport.api.ConnectionAcceptorFactory) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with ConnectionAcceptorFactory

use of io.servicetalk.transport.api.ConnectionAcceptorFactory in project servicetalk by apple.

the class InfluencerConnectionAcceptorTest method withStrategyRequiredOffloads.

@Test
void withStrategyRequiredOffloads() {
    ConnectionAcceptorFactory factory = withStrategy(original -> original, ConnectExecutionStrategy.offloadAll());
    assertThat("unexpected strategy", factory.requiredOffloads(), is(ConnectExecutionStrategy.offloadAll()));
    InfluencerConnectionAcceptor acceptor = InfluencerConnectionAcceptor.withStrategy(factory.create(ACCEPT_ALL), factory.requiredOffloads());
    assertThat("acceptor should have same strategy", acceptor.requiredOffloads(), is(factory.requiredOffloads()));
}
Also used : ConnectionAcceptorFactory(io.servicetalk.transport.api.ConnectionAcceptorFactory) Test(org.junit.jupiter.api.Test)

Example 4 with ConnectionAcceptorFactory

use of io.servicetalk.transport.api.ConnectionAcceptorFactory in project servicetalk by apple.

the class InfluencerConnectionAcceptorTest method defaultRequiredOffloads.

@Test
void defaultRequiredOffloads() {
    ConnectionAcceptorFactory factory = ConnectionAcceptorFactory.identity();
    assertThat("Default should be safe", factory.requiredOffloads().hasOffloads());
    InfluencerConnectionAcceptor acceptor = InfluencerConnectionAcceptor.withStrategy(factory.create(ACCEPT_ALL), factory.requiredOffloads());
    assertThat("acceptor should have same strategy", acceptor.requiredOffloads(), is(factory.requiredOffloads()));
}
Also used : ConnectionAcceptorFactory(io.servicetalk.transport.api.ConnectionAcceptorFactory) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionAcceptorFactory (io.servicetalk.transport.api.ConnectionAcceptorFactory)4 Test (org.junit.jupiter.api.Test)3 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)1 HttpResponse (io.servicetalk.http.api.HttpResponse)1 ConnectionAcceptor (io.servicetalk.transport.api.ConnectionAcceptor)1 ConnectionContext (io.servicetalk.transport.api.ConnectionContext)1 ServerContext (io.servicetalk.transport.api.ServerContext)1 SocketAddress (java.net.SocketAddress)1 ArrayDeque (java.util.ArrayDeque)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1