use of reactor.netty.Connection 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.Connection in project reactor-netty by reactor.
the class TcpClientTests method tcpClientHandlesLineFeedData.
private void tcpClientHandlesLineFeedData(TcpClient client) throws InterruptedException {
final int messages = 100;
final CountDownLatch latch = new CountDownLatch(messages);
final List<String> strings = new ArrayList<>();
Connection c = client.handle((in, out) -> out.sendString(Flux.range(1, messages).map(i -> "Hello World!" + i + "\n").subscribeOn(Schedulers.parallel())).then(in.receive().asString().take(100).flatMapIterable(s -> Arrays.asList(s.split("\\n"))).doOnNext(s -> {
strings.add(s);
latch.countDown();
}).then())).wiretap(true).connectNow(Duration.ofSeconds(30));
log.debug("Connected");
c.onDispose().log().block(Duration.ofSeconds(30));
assertThat(latch.await(15, TimeUnit.SECONDS)).as("Expected messages not received. Received " + strings.size() + " messages: " + strings).isTrue();
assertThat(strings).hasSize(messages);
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class TcpClientTests method consumerSpecAssignsEventHandlers.
@Test
void consumerSpecAssignsEventHandlers() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(2);
final CountDownLatch close = new CountDownLatch(1);
final AtomicLong totalDelay = new AtomicLong();
final long start = System.currentTimeMillis();
TcpClient client = TcpClient.create().host("localhost").port(timeoutServerPort);
Connection s = client.handle((in, out) -> {
in.withConnection(c -> c.onDispose(close::countDown));
out.withConnection(c -> c.onWriteIdle(200, () -> {
totalDelay.addAndGet(System.currentTimeMillis() - start);
latch.countDown();
}));
return Mono.delay(Duration.ofSeconds(1)).then().log();
}).wiretap(true).connectNow();
assertThat(latch.await(5, TimeUnit.SECONDS)).as("latch was counted down").isTrue();
assertThat(close.await(30, TimeUnit.SECONDS)).as("close was counted down").isTrue();
assertThat(totalDelay.get()).as("totalDelay was > 200ms").isGreaterThanOrEqualTo(200L);
s.disposeNow();
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class NewConnectionProviderTest method testDisposableConnectBindException.
@Test
@SuppressWarnings("unchecked")
void testDisposableConnectBindException() {
MonoSink<Connection> sink = Mockito.mock(MonoSink.class);
NewConnectionProvider.DisposableConnect connect = new NewConnectionProvider.DisposableConnect(sink, () -> new InetSocketAddress("test1", 6956));
connect.onError(new UnsupportedOperationException());
Mockito.verify(sink).error(Mockito.argThat(a -> a instanceof UnsupportedOperationException));
connect = new NewConnectionProvider.DisposableConnect(sink, () -> new InetSocketAddress("test2", 4956));
// Not possible to mock io.netty.channel.unix.Errors.NativeIoException or create a new instance because of Jni
// error:
// java.lang.UnsatisfiedLinkError: 'int io.netty.channel.unix.ErrorsStaticallyReferencedJniMethods.errnoENOENT()'
connect.onError(new IOException("bind(..) failed: Address already in use"));
Mockito.verify(sink).error(Mockito.argThat(a -> a instanceof ChannelBindException && ((ChannelBindException) a).localHost().equals("test2") && ((ChannelBindException) a).localPort() == 4956));
// Issue-1668
connect = new NewConnectionProvider.DisposableConnect(sink, () -> new InetSocketAddress("test3", 7956));
connect.onError(new IOException("bind(..) failed: Die Adresse wird bereits verwendet"));
Mockito.verify(sink).error(Mockito.argThat(a -> a instanceof ChannelBindException && ((ChannelBindException) a).localHost().equals("test3") && ((ChannelBindException) a).localPort() == 7956));
}
use of reactor.netty.Connection 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?]");
}
Aggregations