use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class TcpClientTests method doTestSharedNameResolver.
private void doTestSharedNameResolver(TcpClient client, boolean sharedClient) throws InterruptedException {
DisposableServer disposableServer = TcpServer.create().port(0).handle((req, res) -> res.sendString(Mono.just("testNoOpenedFileDescriptors"))).bindNow(Duration.ofSeconds(30));
LoopResources loop = LoopResources.create("doTestSharedNameResolver", 4, true);
AtomicReference<List<AddressResolverGroup<?>>> resolvers = new AtomicReference<>(new ArrayList<>());
try {
int count = 8;
CountDownLatch latch = new CountDownLatch(count);
TcpClient localClient = null;
if (sharedClient) {
localClient = client.runOn(loop).port(disposableServer.port()).doOnConnect(config -> resolvers.get().add(config.resolver())).doOnConnected(conn -> conn.onDispose(latch::countDown));
}
for (int i = 0; i < count; i++) {
if (!sharedClient) {
localClient = client.runOn(loop).port(disposableServer.port()).doOnConnect(config -> resolvers.get().add(config.resolver())).doOnConnected(conn -> conn.onDispose(latch::countDown));
}
localClient.handle((in, out) -> in.receive().then()).connect().subscribe();
}
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(resolvers.get().size()).isEqualTo(count);
AddressResolverGroup<?> resolver = resolvers.get().get(0);
assertThat(resolvers.get()).allMatch(addressResolverGroup -> addressResolverGroup == resolver);
} finally {
disposableServer.disposeNow();
loop.disposeLater().block();
}
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class TcpClientTests method noSystemProxySettings.
/* https://github.com/reactor/reactor-netty/issues/1765 */
@Test
void noSystemProxySettings() {
Properties props = System.getProperties();
assumeThat(!(props.containsKey("http.proxyHost") || props.containsKey("https.proxyHost") || props.containsKey("socksProxyHost"))).isTrue();
DisposableServer disposableServer = TcpServer.create().port(0).handle((req, res) -> res.sendString(Mono.just("noSystemProxySettings"))).bindNow();
AtomicReference<AddressResolverGroup<?>> resolver = new AtomicReference<>();
Connection conn = null;
try {
conn = TcpClient.create().host("localhost").port(disposableServer.port()).proxyWithSystemProperties().doOnConnect(conf -> resolver.set(conf.resolver())).connectNow();
} finally {
disposableServer.disposeNow();
if (conn != null) {
conn.disposeNow();
}
}
assertThat(resolver.get()).isNull();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class BlockingConnectionTest method getContextAddressAndHost.
@Test
void getContextAddressAndHost() {
DisposableServer c = new TcpServer() {
@Override
public TcpServerConfig configuration() {
return null;
}
@Override
protected TcpServer duplicate() {
return null;
}
@Override
public Mono<? extends DisposableServer> bind() {
return Mono.just(NEVER_STOP_SERVER);
}
}.bindNow();
assertThat(c).isSameAs(NEVER_STOP_SERVER);
assertThat(c.port()).isEqualTo(((InetSocketAddress) NEVER_STOP_CONTEXT.address()).getPort());
assertThat(c.host()).isEqualTo(((InetSocketAddress) NEVER_STOP_CONTEXT.address()).getHostString());
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class BlockingConnectionTest method simpleServerFromAsyncServer.
@Test
void simpleServerFromAsyncServer() {
DisposableServer simpleServer = TcpServer.create().handle((in, out) -> out.sendString(in.receive().asString().takeUntil(s -> s.endsWith("CONTROL")).map(s -> "ECHO: " + s.replaceAll("CONTROL", "")).concatWith(Mono.just("DONE"))).neverComplete()).wiretap(true).bindNow();
InetSocketAddress address = (InetSocketAddress) simpleServer.address();
AtomicReference<List<String>> data1 = new AtomicReference<>();
AtomicReference<List<String>> data2 = new AtomicReference<>();
Connection simpleClient1 = TcpClient.create().port(address.getPort()).handle((in, out) -> out.sendString(Flux.just("Hello", "World", "CONTROL")).then(in.receive().asString().takeUntil(s -> s.endsWith("DONE")).map(s -> s.replaceAll("DONE", "")).filter(s -> !s.isEmpty()).collectList().doOnNext(data1::set).doOnNext(System.err::println).then())).wiretap(true).connectNow();
Connection simpleClient2 = TcpClient.create().port(address.getPort()).handle((in, out) -> out.sendString(Flux.just("How", "Are", "You?", "CONTROL")).then(in.receive().asString().takeUntil(s -> s.endsWith("DONE")).map(s -> s.replaceAll("DONE", "")).filter(s -> !s.isEmpty()).collectList().doOnNext(data2::set).doOnNext(System.err::println).then())).wiretap(true).connectNow();
simpleClient1.onDispose().block(Duration.ofSeconds(30));
System.err.println("STOPPED 1");
simpleClient2.onDispose().block(Duration.ofSeconds(30));
System.err.println("STOPPED 2");
System.err.println("STOPPING SERVER");
simpleServer.disposeNow();
assertThat(data1.get()).allSatisfy(s -> assertThat(s).startsWith("ECHO: "));
assertThat(data2.get()).allSatisfy(s -> assertThat(s).startsWith("ECHO: "));
assertThat(data1.get().toString().replaceAll("ECHO: ", "").replaceAll(", ", "")).isEqualTo("[HelloWorld]");
assertThat(data2.get().toString().replaceAll("ECHO: ", "").replaceAll(", ", "")).isEqualTo("[HowAreYou?]");
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class ServerTransport method bind.
/**
* Binds the {@link ServerTransport} and returns a {@link Mono} of {@link DisposableServer}. If
* {@link Mono} is cancelled, the underlying binding will be aborted. Once the {@link
* DisposableServer} has been emitted and is not necessary anymore, disposing the main server
* loop must be done by the user via {@link DisposableServer#dispose()}.
*
* @return a {@link Mono} of {@link DisposableServer}
*/
public Mono<? extends DisposableServer> bind() {
CONF config = configuration();
Objects.requireNonNull(config.bindAddress(), "bindAddress");
Mono<? extends DisposableServer> mono = Mono.create(sink -> {
SocketAddress local = Objects.requireNonNull(config.bindAddress().get(), "Bind Address supplier returned null");
if (local instanceof InetSocketAddress) {
InetSocketAddress localInet = (InetSocketAddress) local;
if (localInet.isUnresolved()) {
local = AddressUtils.createResolved(localInet.getHostName(), localInet.getPort());
}
}
boolean isDomainSocket = false;
DisposableBind disposableServer;
if (local instanceof DomainSocketAddress) {
isDomainSocket = true;
disposableServer = new UdsDisposableBind(sink, config, local);
} else {
disposableServer = new InetDisposableBind(sink, config, local);
}
ConnectionObserver childObs = new ChildObserver(config.defaultChildObserver().then(config.childObserver()));
Acceptor acceptor = new Acceptor(config.childEventLoopGroup(), config.channelInitializer(childObs, null, true), config.childOptions, config.childAttrs, isDomainSocket);
TransportConnector.bind(config, new AcceptorInitializer(acceptor), local, isDomainSocket).subscribe(disposableServer);
});
if (config.doOnBind() != null) {
mono = mono.doOnSubscribe(s -> config.doOnBind().accept(config));
}
return mono;
}
Aggregations