Skip to content

Commit

Permalink
Fix race condition in test
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Nov 5, 2018
1 parent f0a80fe commit 57847d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions vertx-web-client/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar!
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
import io.vertx.core.http.HttpTestBase;
import io.vertx.core.http.HttpVersion;
import io.vertx.core.json.JsonArray;
import io.vertx.ext.unit.junit.Repeat;
import io.vertx.ext.unit.junit.RepeatRule;
import io.vertx.ext.web.client.impl.ClientPhase;
import io.vertx.ext.web.client.impl.HttpContext;
import io.vertx.ext.web.client.impl.WebClientInternal;
import io.vertx.ext.web.codec.BodyCodec;

import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -59,14 +64,8 @@ private void handleMutateRequest(HttpContext context) {
context.next();
}

private void handleMutateCodec(HttpContext context) {
if (context.phase() == ClientPhase.RECEIVE_RESPONSE) {
if (context.clientResponse().statusCode() == 200) {
context.request().as(BodyCodec.none());
}
}
context.next();
}
@Rule
public RepeatRule rule = new RepeatRule();

@Test
public void testMutateRequestInterceptor() throws Exception {
Expand All @@ -78,21 +77,36 @@ public void testMutateRequestInterceptor() throws Exception {
await();
}

private void handleMutateCodec(HttpContext context) {
if (context.phase() == ClientPhase.RECEIVE_RESPONSE) {
if (context.clientResponse().statusCode() == 200) {
context.request().as(BodyCodec.none());
}
}
context.next();
}

@Repeat(1000)
@Test
public void testMutateCodecInterceptor() throws Exception {
server.requestHandler(req -> req.response().end("foo!"));
startServer();
AsyncFile foo = vertx.fileSystem().openBlocking("foo.txt", new OpenOptions().setSync(true).setTruncateExisting(true));
File f = Files.createTempFile("vertx", ".dat").toFile();
assertTrue(f.delete());
AsyncFile foo = vertx.fileSystem().openBlocking(f.getAbsolutePath(), new OpenOptions().setSync(true).setTruncateExisting(true));
client.addInterceptor(this::handleMutateCodec);
HttpRequest<Void> builder = client.get("/somepath").as(BodyCodec.pipe(foo));
builder.send(onSuccess(resp -> {
foo.write(Buffer.buffer("bar!"));
foo.close();
assertEquals("bar!", vertx.fileSystem().readFileBlocking("foo.txt").toString());
vertx.fileSystem().deleteBlocking("foo.txt");
complete();
foo.close(onSuccess(v -> {
assertEquals("bar!", vertx.fileSystem().readFileBlocking(f.getAbsolutePath()).toString());
testComplete();
}));
}));
await();
if (f.exists()) {
f.delete();
}
}

private void mutateResponseHandler(HttpContext context) {
Expand Down

0 comments on commit 57847d0

Please sign in to comment.