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)));
}
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));
}
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()));
}
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()));
}
Aggregations