use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpCompressionClientServerTests method testServerCompressionPredicateTrue.
private void testServerCompressionPredicateTrue(HttpServer server, HttpClient client, boolean useScheduler) throws Exception {
disposableServer = server.compress((req, res) -> true).handle((in, out) -> useScheduler ? out.sendString(Mono.just("reply").subscribeOn(Schedulers.boundedElastic())) : out.sendString(Mono.just("reply"))).bindNow(Duration.ofSeconds(10));
// don't activate compression on the client options to avoid auto-handling (which removes the header)
Tuple2<HttpHeaders, byte[]> resp = // edit the header manually to attempt to trigger compression on server side
client.port(disposableServer.port()).headers(h -> h.add("Accept-Encoding", "gzip")).get().uri("/test").responseSingle((res, byteBufMono) -> Mono.just(res.responseHeaders()).zipWith(byteBufMono.asByteArray())).block(Duration.ofSeconds(10));
assertThat(resp).isNotNull();
assertThat(resp.getT1().get("content-encoding")).isEqualTo("gzip");
byte[] replyBuffer = resp.getT2();
assertThat(new String(replyBuffer, Charset.defaultCharset())).isNotEqualTo("reply");
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(replyBuffer));
byte[] deflatedBuf = new byte[1024];
int readable = gis.read(deflatedBuf);
gis.close();
assertThat(readable).isGreaterThan(0);
String deflated = new String(deflatedBuf, 0, readable, Charset.defaultCharset());
assertThat(deflated).isEqualTo("reply");
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpCompressionClientServerTests method serverCompressionEnabledSmallResponse.
@ParameterizedCompressionTest
void serverCompressionEnabledSmallResponse(HttpServer server, HttpClient client) {
disposableServer = server.compress(25).handle((in, out) -> out.header("content-length", "5").sendString(Mono.just("reply"))).bindNow(Duration.ofSeconds(10));
// don't activate compression on the client options to avoid auto-handling (which removes the header)
Tuple2<String, HttpHeaders> resp = // edit the header manually to attempt to trigger compression on server side
client.port(disposableServer.port()).headers(h -> h.add("Accept-Encoding", "gzip")).get().uri("/test").response((res, byteBufFlux) -> byteBufFlux.asString().zipWith(Mono.just(res.responseHeaders()))).blockFirst(Duration.ofSeconds(10));
assertThat(resp).isNotNull();
// check the server didn't send the gzip header, only transfer-encoding
HttpHeaders headers = resp.getT2();
assertThat(headers.get("conTENT-encoding")).isNull();
// check the server sent plain text
String reply = resp.getT1();
assertThat(reply).isEqualTo("reply");
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class HttpCompressionClientServerTests method serverCompressionPredicateFalse.
@ParameterizedCompressionTest
void serverCompressionPredicateFalse(HttpServer server, HttpClient client) {
disposableServer = server.compress((req, res) -> false).handle((in, out) -> out.sendString(Flux.just("reply").hide())).bindNow(Duration.ofSeconds(10));
// don't activate compression on the client options to avoid auto-handling (which removes the header)
Tuple2<String, HttpHeaders> resp = // edit the header manually to attempt to trigger compression on server side
client.port(disposableServer.port()).headers(h -> h.add("Accept-Encoding", "gzip")).get().uri("/test").response((res, buf) -> buf.asString().zipWith(Mono.just(res.responseHeaders()))).blockFirst(Duration.ofSeconds(10));
assertThat(resp).isNotNull();
// check the server didn't send the gzip header, only transfer-encoding
HttpHeaders headers = resp.getT2();
assertThat(headers.get("transFER-encoding")).isEqualTo("chunked");
assertThat(headers.get("conTENT-encoding")).isNull();
// check the server sent plain text
assertThat(resp.getT1()).isEqualTo("reply");
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class FluxReceiveTest method testByteBufsReleasedWhenTimeout.
@Test
void testByteBufsReleasedWhenTimeout() {
byte[] content = new byte[1024 * 8];
Random rndm = new Random();
rndm.nextBytes(content);
DisposableServer server1 = createServer().route(routes -> routes.get("/target", (req, res) -> req.receive().thenMany(res.sendByteArray(Flux.just(content).delayElements(Duration.ofMillis(100)))))).bindNow();
DisposableServer server2 = createServer().route(routes -> routes.get("/forward", (req, res) -> createClient(server1.port()).get().uri("/target").responseContent().aggregate().asString().log().timeout(Duration.ofMillis(50)).then())).bindNow();
Flux.range(0, 50).flatMap(i -> createClient(server2.port()).get().uri("/forward").responseContent().log().onErrorResume(t -> Mono.empty())).blockLast(Duration.ofSeconds(15));
server1.disposeNow();
server2.disposeNow();
}
use of reactor.netty.DisposableServer in project reactor-netty by reactor.
the class DefaultPooledConnectionProviderTest method testIssue673_TimeoutException.
@Test
void testIssue673_TimeoutException() throws InterruptedException {
DisposableServer server = TcpServer.create().port(0).handle((in, out) -> out.sendString(Mono.just("test").delayElement(Duration.ofMillis(100)))).wiretap(true).bindNow();
DefaultPooledConnectionProvider provider = (DefaultPooledConnectionProvider) ConnectionProvider.builder("testIssue673_TimeoutException").maxConnections(1).pendingAcquireMaxCount(4).pendingAcquireTimeout(Duration.ofMillis(10)).build();
CountDownLatch latch = new CountDownLatch(2);
try {
AtomicReference<InstrumentedPool<PooledConnection>> pool = new AtomicReference<>();
List<? extends Signal<? extends Connection>> list = Flux.range(0, 5).flatMapDelayError(i -> TcpClient.create(provider).port(server.port()).doOnConnected(conn -> {
ConcurrentMap<PooledConnectionProvider.PoolKey, InstrumentedPool<PooledConnection>> pools = provider.channelPools;
pool.set(pools.get(pools.keySet().toArray()[0]));
}).doOnDisconnected(conn -> latch.countDown()).handle((in, out) -> in.receive().then()).wiretap(true).connect().materialize(), 256, 32).collectList().doFinally(fin -> latch.countDown()).block(Duration.ofSeconds(30));
assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch 30s").isTrue();
assertThat(list).isNotNull().hasSize(5);
int onNext = 0;
int onError = 0;
String msg = "Pool#acquire(Duration) has been pending for more than the configured timeout of 10ms";
for (int i = 0; i < 5; i++) {
Signal<? extends Connection> signal = list.get(i);
if (signal.isOnNext()) {
onNext++;
} else if (signal.getThrowable() instanceof TimeoutException && msg.equals(signal.getThrowable().getMessage())) {
onError++;
}
}
assertThat(onNext).isEqualTo(1);
assertThat(onError).isEqualTo(4);
assertThat(pool.get().metrics().acquiredSize()).as("currently acquired").isEqualTo(0);
assertThat(pool.get().metrics().idleSize()).as("currently idle").isEqualTo(0);
} finally {
server.disposeNow();
provider.dispose();
}
}
Aggregations