Skip to content

Commit

Permalink
Replace future.andThen(onSuccess(A)) by future.expecting(that(P)) in …
Browse files Browse the repository at this point in the history
…vertx tests.
  • Loading branch information
vietj committed May 21, 2024
1 parent d138ff3 commit 79c06b4
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 121 deletions.
2 changes: 1 addition & 1 deletion src/test/java/io/vertx/core/file/FileResolverTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public void testSendFileFromClasspath() {
.request(HttpMethod.GET, HttpTestBase.DEFAULT_HTTP_PORT, "localhost", "/")
.compose(req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(HttpResponseExpectation.SC_OK)
.compose(HttpClientResponse::body))
.onComplete(onSuccess(body -> {
assertTrue(body.toString().startsWith("<html><body>blah</body></html>"));
Expand Down
9 changes: 5 additions & 4 deletions src/test/java/io/vertx/core/file/FileSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;

import static io.vertx.test.core.AssertExpectations.that;
import static io.vertx.test.core.TestUtils.*;
import static org.hamcrest.CoreMatchers.instanceOf;

Expand Down Expand Up @@ -1980,7 +1981,7 @@ public void testCopyFileAttributes() throws Exception {
.props(from)
.compose(expected -> fs
.props(from)
.andThen(onSuccess(actual -> {
.expecting(that(actual -> {
assertEquals(expected.creationTime(), actual.creationTime());
assertEquals(expected.lastModifiedTime(), actual.lastModifiedTime());
}))
Expand Down Expand Up @@ -2015,12 +2016,12 @@ public void testCopyLinks() throws Exception {
fs.copy(from, to, options)
.compose(v -> fs
.lprops(to)
.andThen(onSuccess(props -> assertTrue(props.isSymbolicLink())))
.expecting(that(props -> assertTrue(props.isSymbolicLink())))
.compose(v2 -> fs
.readFile(from)
.compose(expected -> fs
.readFile(to)
.andThen(onSuccess(actual -> assertEquals(expected, actual)))))).onComplete(onSuccess(v -> complete()));
.expecting(that(actual -> assertEquals(expected, actual)))))).onComplete(onSuccess(v -> complete()));
await();
}

Expand Down Expand Up @@ -2169,7 +2170,7 @@ public void testFileSize() throws Exception {
createFileWithJunk(fileName, expected);
AsyncFile file = vertx.fileSystem().openBlocking(testDir + pathSep + fileName, new OpenOptions());
file.size()
.andThen(onSuccess(size -> assertEquals(expected, size.longValue())))
.expecting(that(size -> assertEquals(expected, size.longValue())))
.compose(v -> file.close())
.onComplete(onSuccess(v -> testComplete()));
await();
Expand Down
28 changes: 15 additions & 13 deletions src/test/java/io/vertx/core/http/Http1xTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.vertx.core.net.*;
import io.vertx.core.parsetools.RecordParser;
import io.vertx.core.streams.WriteStream;
import io.vertx.test.core.AssertExpectations;
import io.vertx.test.core.CheckingSender;
import io.vertx.test.core.Repeat;
import io.vertx.test.core.TestUtils;
Expand All @@ -50,6 +51,7 @@
import java.util.stream.Stream;

import static io.vertx.core.http.HttpMethod.PUT;
import static io.vertx.test.core.AssertExpectations.that;
import static io.vertx.test.core.TestUtils.*;

/**
Expand Down Expand Up @@ -1038,10 +1040,10 @@ public void testPipeliningOrder() throws Exception {
.putHeader("count", String.valueOf(theCount)))
.compose(req -> req
.send(Buffer.buffer("This is content " + theCount))
.andThen(onSuccess(resp -> assertEquals(theCount, Integer.parseInt(resp.headers().get("count")))))
.expecting(that(resp -> assertEquals(theCount, Integer.parseInt(resp.headers().get("count")))))
.compose(resp -> resp
.body()
.andThen(onSuccess(buff -> assertEquals("This is content " + theCount, buff.toString())))))
.expecting(that(buff -> assertEquals("This is content " + theCount, buff.toString())))))
.onComplete(onSuccess(v -> complete()));
}
});
Expand Down Expand Up @@ -1500,7 +1502,7 @@ private void testPooling(boolean keepAlive, boolean pipelining) throws Exception
client.request(new RequestOptions(requestOptions).setURI(path))
.compose(req -> req.putHeader("count", String.valueOf(theCount))
.send()
.andThen(onSuccess(resp -> {
.expecting(that(resp -> {
resp.exceptionHandler(this::fail);
assertEquals(200, resp.statusCode());
assertEquals(theCount, Integer.parseInt(resp.headers().get("count")));
Expand Down Expand Up @@ -1861,7 +1863,7 @@ public void testHttp10ResponseNonKeepAliveConnectionClosed() throws Exception {
for (int i = 0;i < 3;i++) {
client.request(requestOptions).compose(req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(that(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::end))
.onComplete(onSuccess(v -> complete()));
}
Expand Down Expand Up @@ -2255,7 +2257,7 @@ public void testEndServerRequestResumeTheConnection() throws Exception {
client.request(new RequestOptions(requestOptions).setMethod(PUT))
.compose(req -> req
.send(Buffer.buffer("1"))
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(that(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::end))
.onComplete(onSuccess(v -> complete()));
}
Expand Down Expand Up @@ -2813,7 +2815,7 @@ public void testRecyclePipelinedConnection() throws Exception {
.request(new RequestOptions(requestOptions).setURI(uri))
.compose(req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(that(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::end))
.onComplete(onSuccess(v -> respLatch.countDown()));
}
Expand Down Expand Up @@ -3211,7 +3213,7 @@ private void testCloseTheConnectionAfterResetPersistentClientRequest(boolean pip
client.request(new RequestOptions(requestOptions).setURI("/somepath"))
.compose(req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(that(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::body))
.onComplete(onSuccess(body -> {
assertEquals("Hello world", body.toString());
Expand All @@ -3228,7 +3230,7 @@ private void testCloseTheConnectionAfterResetPersistentClientRequest(boolean pip
client.request(new RequestOptions(requestOptions).setURI("/somepath"))
.compose(req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(that(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::body))
.onComplete(onSuccess(body -> {
assertEquals("Hello world", body.toString());
Expand Down Expand Up @@ -4236,7 +4238,7 @@ public void testDeferredRequestEnd() throws Exception {
client.request(new RequestOptions(requestOptions).setMethod(PUT))
.compose(req -> req
.send(expected)
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(that(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::body))
.onComplete(onSuccess(body -> {
assertEquals(expected, body);
Expand Down Expand Up @@ -4276,7 +4278,7 @@ public void testPipelinedWithResponseSent() throws Exception {
client.request(new RequestOptions(requestOptions).setMethod(PUT))
.compose(req -> req
.send(TestUtils.randomAlphaString(1024))
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(that(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::body))
.onComplete(onSuccess(body -> {
assertEquals(expected, body.toString());
Expand Down Expand Up @@ -4311,7 +4313,7 @@ public void testPipelinedWithPendingResponse() throws Exception {
client.request(new RequestOptions(requestOptions).setMethod(PUT))
.compose(req -> req
.send(TestUtils.randomAlphaString(1024))
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(that(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::body))
.onComplete(onSuccess(body -> {
assertEquals(expected, body.toString());
Expand Down Expand Up @@ -5134,7 +5136,7 @@ public void testServerConnectionGracefulShutdown() throws Exception {
startServer(testAddress);
client.request(requestOptions).compose(req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(that(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::body)
)
.onComplete(onSuccess(body -> complete()));
Expand Down Expand Up @@ -5178,7 +5180,7 @@ public void testServerConnectionGracefulShutdownWithPipelinedRequest() throws Ex
for (int i = 0;i < 2;i++) {
client.request(requestOptions).compose(req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(that(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::body)
)
.onComplete(onSuccess(body -> complete()));
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/io/vertx/core/http/Http2ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import java.util.function.BiFunction;
import java.util.zip.GZIPOutputStream;

import static io.vertx.test.core.AssertExpectations.that;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
Expand Down Expand Up @@ -1070,10 +1072,8 @@ public void testReceivingGoAwayDiscardsTheConnection() throws Exception {
vertx.runOnContext(v -> {
client.request(new RequestOptions(requestOptions).setTimeout(5000))
.compose(HttpClientRequest::send)
.onComplete(onSuccess(resp2 -> {
assertEquals(2, connections.size());
testComplete();
}));
.expecting(that(v2 -> assertEquals(2, connections.size())))
.onComplete(onSuccess(resp2 -> testComplete()));
});
});
req.send().onComplete(onFailure(resp -> {
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/io/vertx/core/http/Http2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.vertx.test.core.AssertExpectations.that;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
Expand Down Expand Up @@ -72,7 +74,7 @@ public void testServerResponseWriteBufferFromOtherThread() throws Exception {
startServer(testAddress);
client.request(requestOptions).compose(req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(that(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::body)).
onComplete(onSuccess(body -> {
assertEquals(Buffer.buffer("hello world"), body);
Expand All @@ -91,7 +93,7 @@ public void testServerResponseEndFromOtherThread() throws Exception {
startServer(testAddress);
client.request(requestOptions).compose(req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(HttpResponseExpectation.SC_OK)
.compose(HttpClientResponse::end))
.onComplete(onSuccess(v -> testComplete()));
await();
Expand All @@ -107,8 +109,8 @@ public void testServerResponseEndWithTrailersFromOtherThread() throws Exception
startServer(testAddress);
client.request(requestOptions).compose(req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.compose(resp -> resp.end().andThen(onSuccess(v -> {
.expecting(HttpResponseExpectation.SC_OK)
.compose(resp -> resp.end().expecting(that(v -> {
assertEquals(1, resp.trailers().size());
assertEquals("trailer", resp.trailers().get("some"));
}))))
Expand Down
14 changes: 8 additions & 6 deletions src/test/java/io/vertx/core/http/HttpBandwidthLimitingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public void sendFileIsThrottled() throws Exception {
long expectedLength = Files.size(Path.of(sampleF.getAbsolutePath()));
testClient.request(HttpMethod.GET, testServer.actualPort(), DEFAULT_HTTP_HOST,"/get-file")
.compose(req -> req.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::body))
.expecting(HttpResponseExpectation.SC_OK)
.compose(HttpClientResponse::body))
.onComplete(onSuccess(body -> {
receivedLength.set(body.getBytes().length);
Assert.assertEquals(expectedLength, receivedLength.get());
Expand Down Expand Up @@ -188,8 +188,8 @@ public void start(Promise<Void> startPromise) {
for (int i=0; i<2; i++) {
testClient.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST,"/get-file")
.compose(req -> req.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::body))
.expecting(HttpResponseExpectation.SC_OK)
.compose(HttpClientResponse::body))
.onComplete(onSuccess(body -> {
long receivedBytes = body.getBytes().length;
totalReceivedLength.addAndGet(receivedBytes);
Expand Down Expand Up @@ -278,8 +278,10 @@ private long expectedUpperBoundTimeMillis(long size, int rate) {

private void read(Buffer expected, HttpServer server, HttpClient client) {
client.request(HttpMethod.GET, server.actualPort(), DEFAULT_HTTP_HOST,"/buffer-read")
.compose(req -> req.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode()))).compose(HttpClientResponse::body))
.compose(req -> req
.send()
.expecting(HttpResponseExpectation.SC_OK)
.compose(HttpClientResponse::body))
.onComplete(onSuccess(body -> {
assertEquals(expected.getBytes().length, body.getBytes().length);
testComplete();
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/io/vertx/core/http/HttpClientConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static io.vertx.test.core.AssertExpectations.that;

public abstract class HttpClientConnectionTest extends HttpTestBase {

protected HttpClientInternal client;
Expand All @@ -39,7 +41,7 @@ public void testGet() throws Exception {
.compose(HttpClientConnection::request)
.compose(request -> request
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(HttpResponseExpectation.SC_OK)
.compose(HttpClientResponse::body))
.onComplete(onSuccess(body -> {
assertEquals("Hello World", body.toString());
Expand All @@ -57,9 +59,10 @@ public void testStreamGet() throws Exception {
HttpConnectOptions connect = new HttpConnectOptions().setServer(testAddress).setHost(requestOptions.getHost()).setPort(requestOptions.getPort());
client.connect(connect).compose(conn -> conn.request().compose(req -> req
.send()
.compose(HttpClientResponse::body)))
.andThen(onSuccess(body -> {
assertEquals("Hello World", body.toString());
.compose(HttpClientResponse::body)
.expecting(that(body -> assertEquals("Hello World", body.toString())))
))
.onComplete(onSuccess(v -> {
testComplete();
}));
await();
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/io/vertx/core/http/HttpClientTimeoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void testTimedOutWaiterDoesNotConnect() throws Exception {
client.request(requestOptions)
.compose(req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(HttpResponseExpectation.SC_OK)
.compose(HttpClientResponse::body))
.onComplete(onSuccess(buff -> {
assertEquals("OK", buff.toString());
Expand Down Expand Up @@ -335,7 +335,7 @@ public void testRequestTimeoutExtendedWhenResponseChunksReceived() throws Except
client.request(new RequestOptions(requestOptions).setIdleTimeout(timeout))
.compose(req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.expecting(HttpResponseExpectation.SC_OK)
.compose(HttpClientResponse::end))
.onComplete(onSuccess(v -> testComplete()));

Expand Down
Loading

0 comments on commit 79c06b4

Please sign in to comment.