Skip to content

Commit

Permalink
Make TestProxyBase start synchronous and blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Sep 2, 2018
1 parent 29ec921 commit eb7a83f
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 126 deletions.
17 changes: 12 additions & 5 deletions src/test/java/io/vertx/test/core/HttpProxy.java
Expand Up @@ -13,6 +13,8 @@


import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Base64; import java.util.Base64;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;


import io.vertx.core.Handler; import io.vertx.core.Handler;
import io.vertx.core.MultiMap; import io.vertx.core.MultiMap;
Expand Down Expand Up @@ -69,11 +71,9 @@ public HttpProxy(String username) {
* *
* @param vertx * @param vertx
* Vertx instance to use for creating the server and client * Vertx instance to use for creating the server and client
* @param finishedHandler
* will be called when the server has started
*/ */
@Override @Override
public void start(Vertx vertx, Handler<Void> finishedHandler) { public HttpProxy start(Vertx vertx) throws Exception {
HttpServerOptions options = new HttpServerOptions(); HttpServerOptions options = new HttpServerOptions();
options.setHost("localhost").setPort(PORT); options.setHost("localhost").setPort(PORT);
server = vertx.createHttpServer(options); server = vertx.createHttpServer(options);
Expand Down Expand Up @@ -164,9 +164,16 @@ public void start(Vertx vertx, Handler<Void> finishedHandler) {
request.response().setStatusCode(405).end("method not supported"); request.response().setStatusCode(405).end("method not supported");
} }
}); });
server.listen(server -> { CompletableFuture<Void> fut = new CompletableFuture<>();
finishedHandler.handle(null); server.listen(ar -> {
if (ar.succeeded()) {
fut.complete(null);
} else {
fut.completeExceptionally(ar.cause());
}
}); });
fut.get(10, TimeUnit.SECONDS);
return this;
} }


/** /**
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/io/vertx/test/core/HttpTestBase.java
Expand Up @@ -92,14 +92,12 @@ protected void startServer(Context context, HttpServer server) throws Exception
awaitLatch(latch); awaitLatch(latch);
} }


protected void startProxy(String username, ProxyType proxyType) throws InterruptedException { protected void startProxy(String username, ProxyType proxyType) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
if (proxyType == ProxyType.HTTP) { if (proxyType == ProxyType.HTTP) {
proxy = new HttpProxy(username); proxy = new HttpProxy(username);
} else { } else {
proxy = new SocksProxy(username); proxy = new SocksProxy(username);
} }
proxy.start(vertx, v -> latch.countDown()); proxy.start(vertx);
awaitLatch(latch);
} }
} }
183 changes: 87 additions & 96 deletions src/test/java/io/vertx/test/core/NetTest.java
Expand Up @@ -2674,26 +2674,25 @@ private TestLoggerFactory testLogging() throws Exception {
* test socks5 proxy for accessing arbitrary server port. * test socks5 proxy for accessing arbitrary server port.
*/ */
@Test @Test
public void testWithSocks5Proxy() { public void testWithSocks5Proxy() throws Exception {
NetClientOptions clientOptions = new NetClientOptions() NetClientOptions clientOptions = new NetClientOptions()
.setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setPort(11080)); .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setPort(11080));
NetClient client = vertx.createNetClient(clientOptions); NetClient client = vertx.createNetClient(clientOptions);
server.connectHandler(sock -> { server.connectHandler(sock -> {


}); });
proxy = new SocksProxy(null); proxy = new SocksProxy(null);
proxy.start(vertx, v -> { proxy.start(vertx);
server.listen(1234, "localhost", ar -> { server.listen(1234, "localhost", ar -> {
assertTrue(ar.succeeded()); assertTrue(ar.succeeded());
client.connect(1234, "localhost", ar2 -> { client.connect(1234, "localhost", ar2 -> {
if (ar2.failed()) { if (ar2.failed()) {
log.warn("failed", ar2.cause()); log.warn("failed", ar2.cause());
} }
assertTrue(ar2.succeeded()); assertTrue(ar2.succeeded());
// make sure we have gone through the proxy // make sure we have gone through the proxy
assertEquals("localhost:1234", proxy.getLastUri()); assertEquals("localhost:1234", proxy.getLastUri());
testComplete(); testComplete();
});
}); });
}); });
await(); await();
Expand All @@ -2703,7 +2702,7 @@ public void testWithSocks5Proxy() {
* test socks5 proxy for accessing arbitrary server port with authentication. * test socks5 proxy for accessing arbitrary server port with authentication.
*/ */
@Test @Test
public void testWithSocks5ProxyAuth() { public void testWithSocks5ProxyAuth() throws Exception {
NetClientOptions clientOptions = new NetClientOptions() NetClientOptions clientOptions = new NetClientOptions()
.setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setPort(11080) .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setPort(11080)
.setUsername("username").setPassword("username")); .setUsername("username").setPassword("username"));
Expand All @@ -2712,13 +2711,12 @@ public void testWithSocks5ProxyAuth() {


}); });
proxy = new SocksProxy("username"); proxy = new SocksProxy("username");
proxy.start(vertx, v -> { proxy.start(vertx);
server.listen(1234, "localhost", ar -> { server.listen(1234, "localhost", ar -> {
assertTrue(ar.succeeded()); assertTrue(ar.succeeded());
client.connect(1234, "localhost", ar2 -> { client.connect(1234, "localhost", ar2 -> {
assertTrue(ar2.succeeded()); assertTrue(ar2.succeeded());
testComplete(); testComplete();
});
}); });
}); });
await(); await();
Expand All @@ -2728,7 +2726,7 @@ public void testWithSocks5ProxyAuth() {
* test socks5 proxy when accessing ssl server port with correct cert. * test socks5 proxy when accessing ssl server port with correct cert.
*/ */
@Test @Test
public void testConnectSSLWithSocks5Proxy() { public void testConnectSSLWithSocks5Proxy() throws Exception {
server.close(); server.close();
NetServerOptions options = new NetServerOptions() NetServerOptions options = new NetServerOptions()
.setPort(1234) .setPort(1234)
Expand All @@ -2747,13 +2745,12 @@ public void testConnectSSLWithSocks5Proxy() {


}); });
proxy = new SocksProxy(null); proxy = new SocksProxy(null);
proxy.start(vertx, v -> { proxy.start(vertx);
server.listen(ar -> { server.listen(ar -> {
assertTrue(ar.succeeded()); assertTrue(ar.succeeded());
client.connect(1234, "localhost", ar2 -> { client.connect(1234, "localhost", ar2 -> {
assertTrue(ar2.succeeded()); assertTrue(ar2.succeeded());
testComplete(); testComplete();
});
}); });
}); });
await(); await();
Expand All @@ -2764,7 +2761,7 @@ public void testConnectSSLWithSocks5Proxy() {
* https://github.com/eclipse/vert.x/issues/1602 * https://github.com/eclipse/vert.x/issues/1602
*/ */
@Test @Test
public void testUpgradeSSLWithSocks5Proxy() { public void testUpgradeSSLWithSocks5Proxy() throws Exception {
server.close(); server.close();
NetServerOptions options = new NetServerOptions() NetServerOptions options = new NetServerOptions()
.setPort(1234) .setPort(1234)
Expand All @@ -2782,19 +2779,18 @@ public void testUpgradeSSLWithSocks5Proxy() {


}); });
proxy = new SocksProxy(null); proxy = new SocksProxy(null);
proxy.start(vertx, v -> { proxy.start(vertx);
server.listen(ar -> { server.listen(ar -> {
assertTrue(ar.succeeded()); assertTrue(ar.succeeded());
client.connect(1234, "localhost", ar2 -> { client.connect(1234, "localhost", ar2 -> {
assertTrue(ar2.succeeded()); assertTrue(ar2.succeeded());
NetSocket ns = ar2.result(); NetSocket ns = ar2.result();
ns.exceptionHandler(th -> { ns.exceptionHandler(th -> {
fail(th); fail(th);
}); });
ns.upgradeToSsl(v2 -> { ns.upgradeToSsl(v2 -> {
// failure would occur before upgradeToSsl completes // failure would occur before upgradeToSsl completes
testComplete(); testComplete();
});
}); });
}); });
}); });
Expand All @@ -2807,26 +2803,25 @@ public void testUpgradeSSLWithSocks5Proxy() {
* that limit the target host and ports (e.g. connecting to localhost or to port 25 may not be allowed) * that limit the target host and ports (e.g. connecting to localhost or to port 25 may not be allowed)
*/ */
@Test @Test
public void testWithHttpConnectProxy() { public void testWithHttpConnectProxy() throws Exception {
NetClientOptions clientOptions = new NetClientOptions() NetClientOptions clientOptions = new NetClientOptions()
.setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setPort(13128)); .setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setPort(13128));
NetClient client = vertx.createNetClient(clientOptions); NetClient client = vertx.createNetClient(clientOptions);
server.connectHandler(sock -> { server.connectHandler(sock -> {


}); });
proxy = new HttpProxy(null); proxy = new HttpProxy(null);
proxy.start(vertx, v -> { proxy.start(vertx);
server.listen(1234, "localhost", ar -> { server.listen(1234, "localhost", ar -> {
assertTrue(ar.succeeded()); assertTrue(ar.succeeded());
client.connect(1234, "localhost", ar2 -> { client.connect(1234, "localhost", ar2 -> {
if (ar2.failed()) { if (ar2.failed()) {
log.warn("failed", ar2.cause()); log.warn("failed", ar2.cause());
} }
assertTrue(ar2.succeeded()); assertTrue(ar2.succeeded());
// make sure we have gone through the proxy // make sure we have gone through the proxy
assertEquals("localhost:1234", proxy.getLastUri()); assertEquals("localhost:1234", proxy.getLastUri());
testComplete(); testComplete();
});
}); });
}); });
await(); await();
Expand All @@ -2836,26 +2831,25 @@ public void testWithHttpConnectProxy() {
* test socks4a proxy for accessing arbitrary server port. * test socks4a proxy for accessing arbitrary server port.
*/ */
@Test @Test
public void testWithSocks4aProxy() { public void testWithSocks4aProxy() throws Exception {
NetClientOptions clientOptions = new NetClientOptions() NetClientOptions clientOptions = new NetClientOptions()
.setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080)); .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080));
NetClient client = vertx.createNetClient(clientOptions); NetClient client = vertx.createNetClient(clientOptions);
server.connectHandler(sock -> { server.connectHandler(sock -> {


}); });
proxy = new Socks4Proxy(null); proxy = new Socks4Proxy(null);
proxy.start(vertx, v -> { proxy.start(vertx);
server.listen(1234, "localhost", ar -> { server.listen(1234, "localhost", ar -> {
assertTrue(ar.succeeded()); assertTrue(ar.succeeded());
client.connect(1234, "localhost", ar2 -> { client.connect(1234, "localhost", ar2 -> {
if (ar2.failed()) { if (ar2.failed()) {
log.warn("failed", ar2.cause()); log.warn("failed", ar2.cause());
} }
assertTrue(ar2.succeeded()); assertTrue(ar2.succeeded());
// make sure we have gone through the proxy // make sure we have gone through the proxy
assertEquals("localhost:1234", proxy.getLastUri()); assertEquals("localhost:1234", proxy.getLastUri());
testComplete(); testComplete();
});
}); });
}); });
await(); await();
Expand All @@ -2865,7 +2859,7 @@ public void testWithSocks4aProxy() {
* test socks4a proxy for accessing arbitrary server port using username auth. * test socks4a proxy for accessing arbitrary server port using username auth.
*/ */
@Test @Test
public void testWithSocks4aProxyAuth() { public void testWithSocks4aProxyAuth() throws Exception {
NetClientOptions clientOptions = new NetClientOptions() NetClientOptions clientOptions = new NetClientOptions()
.setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080) .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080)
.setUsername("username")); .setUsername("username"));
Expand All @@ -2874,18 +2868,17 @@ public void testWithSocks4aProxyAuth() {


}); });
proxy = new Socks4Proxy("username"); proxy = new Socks4Proxy("username");
proxy.start(vertx, v -> { proxy.start(vertx);
server.listen(1234, "localhost", ar -> { server.listen(1234, "localhost", ar -> {
assertTrue(ar.succeeded()); assertTrue(ar.succeeded());
client.connect(1234, "localhost", ar2 -> { client.connect(1234, "localhost", ar2 -> {
if (ar2.failed()) { if (ar2.failed()) {
log.warn("failed", ar2.cause()); log.warn("failed", ar2.cause());
} }
assertTrue(ar2.succeeded()); assertTrue(ar2.succeeded());
// make sure we have gone through the proxy // make sure we have gone through the proxy
assertEquals("localhost:1234", proxy.getLastUri()); assertEquals("localhost:1234", proxy.getLastUri());
testComplete(); testComplete();
});
}); });
}); });
await(); await();
Expand All @@ -2895,26 +2888,24 @@ public void testWithSocks4aProxyAuth() {
* test socks4a proxy for accessing arbitrary server port using an already resolved address. * test socks4a proxy for accessing arbitrary server port using an already resolved address.
*/ */
@Test @Test
public void testWithSocks4LocalResolver() { public void testWithSocks4LocalResolver() throws Exception {
NetClientOptions clientOptions = new NetClientOptions() NetClientOptions clientOptions = new NetClientOptions()
.setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080)); .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080));
NetClient client = vertx.createNetClient(clientOptions); NetClient client = vertx.createNetClient(clientOptions);
server.connectHandler(sock -> { server.connectHandler(sock -> {


}); });
proxy = new Socks4Proxy(null); proxy = new Socks4Proxy(null).start(vertx);
proxy.start(vertx, v -> { server.listen(1234, "localhost", ar -> {
server.listen(1234, "localhost", ar -> { assertTrue(ar.succeeded());
assertTrue(ar.succeeded()); client.connect(1234, "127.0.0.1", ar2 -> {
client.connect(1234, "127.0.0.1", ar2 -> { if (ar2.failed()) {
if (ar2.failed()) { log.warn("failed", ar2.cause());
log.warn("failed", ar2.cause()); }
} assertTrue(ar2.succeeded());
assertTrue(ar2.succeeded()); // make sure we have gone through the proxy
// make sure we have gone through the proxy assertEquals("127.0.0.1:1234", proxy.getLastUri());
assertEquals("127.0.0.1:1234", proxy.getLastUri()); testComplete();
testComplete();
});
}); });
}); });
await(); await();
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/io/vertx/test/core/ProxyErrorTest.java
Expand Up @@ -71,12 +71,10 @@ protected VertxOptions getOptions() {


// we don't start http/https servers, due to the error, they will not be queried // we don't start http/https servers, due to the error, they will not be queried


private void startProxy(int error, String username) throws InterruptedException { private void startProxy(int error, String username) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
proxy = new HttpProxy(username); proxy = new HttpProxy(username);
proxy.setError(error); proxy.setError(error);
proxy.start(vertx, v -> latch.countDown()); proxy.start(vertx);
latch.await();
} }


@Test @Test
Expand Down

0 comments on commit eb7a83f

Please sign in to comment.