Skip to content

Commit

Permalink
Make the CheckingSender used in tests less GC aggressive
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Apr 15, 2019
1 parent 840644c commit 38b8b75
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
17 changes: 10 additions & 7 deletions src/test/java/io/vertx/core/http/Http1xTest.java
Expand Up @@ -22,6 +22,7 @@
import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.core.net.*; import io.vertx.core.net.*;
import io.vertx.core.net.impl.ConnectionBase;
import io.vertx.core.parsetools.RecordParser; import io.vertx.core.parsetools.RecordParser;
import io.vertx.core.streams.WriteStream; import io.vertx.core.streams.WriteStream;
import io.vertx.test.core.Repeat; import io.vertx.test.core.Repeat;
Expand Down Expand Up @@ -4515,7 +4516,7 @@ public void testSetChunkedToFalse() throws Exception {
} }


@Test @Test
public void testHttpServerRequestShouldExceptionHandlerWhenTheClosedHandlerIsCalled() { public void testHttpServerRequestShouldCallExceptionHandlerWhenTheClosedHandlerIsCalled() {
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> { server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
HttpServerResponse resp = req.response(); HttpServerResponse resp = req.response();
resp.setChunked(true); resp.setChunked(true);
Expand All @@ -4541,7 +4542,7 @@ public void testHttpServerRequestShouldExceptionHandlerWhenTheClosedHandlerIsCal
} }


@Test @Test
public void testHttpClientRequestShouldExceptionHandlerWhenTheClosedHandlerIsCalled() throws Exception { public void testHttpClientRequestShouldCallExceptionHandlerWhenTheClosedHandlerIsCalled() throws Exception {
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> { server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
vertx.setTimer(1000, id -> { vertx.setTimer(1000, id -> {
req.response().close(); req.response().close();
Expand All @@ -4550,19 +4551,21 @@ public void testHttpClientRequestShouldExceptionHandlerWhenTheClosedHandlerIsCal
startServer(); startServer();
HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", resp -> { HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", resp -> {
}).setChunked(true); }).setChunked(true);
CountDownLatch latch = new CountDownLatch(1);
req.sendHead(version -> latch.countDown());
awaitLatch(latch);
CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), req); CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), req);
AtomicBoolean connected = new AtomicBoolean();
req.exceptionHandler(err -> { req.exceptionHandler(err -> {
assertTrue(connected.get());
Throwable failure = sender.close(); Throwable failure = sender.close();
if (failure != null) { if (failure != null) {
fail(failure); fail(failure);
} else { } else if (err == ConnectionBase.CLOSED_EXCEPTION) {
testComplete(); testComplete();
} }
}); });
sender.send(); req.sendHead(v -> {
connected.set(true);
sender.send();
});
await(); await();
} }


Expand Down
19 changes: 12 additions & 7 deletions src/test/java/io/vertx/test/core/CheckingSender.java
@@ -1,6 +1,7 @@
package io.vertx.test.core; package io.vertx.test.core;


import io.vertx.core.Context; import io.vertx.core.Context;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer; import io.vertx.core.buffer.Buffer;
import io.vertx.core.streams.WriteStream; import io.vertx.core.streams.WriteStream;


Expand Down Expand Up @@ -30,15 +31,19 @@ public CheckingSender(Context context, int countDown, WriteStream<Buffer> stream
} }


public void send() { public void send() {
if (countDown > 0) { if (Vertx.currentContext() == context) {
try { if (countDown > 0) {
stream.write(data); try {
} catch (Exception e) { stream.write(data);
if (error == null) { } catch (Exception e) {
error = e; if (error == null) {
return; error = e;
return;
}
} }
context.owner().setTimer(1, id -> send());
} }
} else {
context.runOnContext(v -> send()); context.runOnContext(v -> send());
} }
} }
Expand Down

0 comments on commit 38b8b75

Please sign in to comment.