Created
July 8, 2021 08:02
-
-
Save tcataldo/50dd5ed1b2c38b26b14e1714c1426ce5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.vertx.starter; | |
import java.util.Date; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.TimeoutException; | |
import org.junit.Test; | |
import io.vertx.core.AbstractVerticle; | |
import io.vertx.core.DeploymentOptions; | |
import io.vertx.core.Vertx; | |
import io.vertx.core.VertxOptions; | |
import io.vertx.core.http.HttpClientOptions; | |
import io.vertx.core.http.HttpServer; | |
import io.vertx.core.http.HttpServerOptions; | |
import io.vertx.core.http.WebSocket; | |
import io.vertx.core.json.JsonObject; | |
public class WsTest { | |
@Test | |
public void testWs() throws InterruptedException, ExecutionException, TimeoutException { | |
int count = 1_000_000; | |
Vertx vxSrv = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true)); | |
CompletableFuture<Void> srvListen = new CompletableFuture<>(); | |
vxSrv.deployVerticle(() -> new AbstractVerticle() { | |
public void start() throws Exception { | |
HttpServer srv = vxSrv.createHttpServer(new HttpServerOptions()); | |
srv.webSocketHandler(ws -> { | |
System.err.println("ws " + ws); | |
Thread t = new Thread(() -> { | |
for (int i = 0; i < count; i++) { | |
if (i % 1000 == 0) { | |
System.err.println("W: " + i); | |
} | |
JsonObject js = new JsonObject().put("log", Integer.toString(i)) | |
.put("ws-rid", ws.textHandlerID()).put("date", new Date().toString()); | |
ws.writeFinalTextFrame(js.encode()); | |
if (ws.writeQueueFull()) { | |
CompletableFuture<Void> writeBlock = new CompletableFuture<>(); | |
ws.drainHandler(writeBlock::complete); | |
writeBlock.join(); | |
} | |
} | |
}); | |
t.start(); | |
}); | |
srv.listen(44444, ar -> { | |
srvListen.complete(null); | |
}); | |
} | |
}, new DeploymentOptions().setInstances(4)); | |
System.err.println("Waiting for deploy..."); | |
srvListen.get(10, TimeUnit.SECONDS); | |
Vertx vxCli = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true)); | |
CompletableFuture<WebSocket> cliConn = new CompletableFuture<>(); | |
CountDownLatch cdl = new CountDownLatch(count); | |
vxCli.createHttpClient(new HttpClientOptions().setDefaultPort(44444).setDefaultHost("127.0.0.1")).webSocket("/", | |
wsRes -> { | |
if (wsRes.succeeded()) { | |
WebSocket ws = wsRes.result(); | |
cliConn.complete(ws); | |
ws.textMessageHandler(str -> { | |
ws.pause(); | |
System.err.println("S: " + str); | |
cdl.countDown(); | |
vxCli.setTimer(100, tid -> ws.resume()); | |
}); | |
} else { | |
cliConn.completeExceptionally(wsRes.cause()); | |
} | |
}); | |
cliConn.join(); | |
System.err.println("con ok, messages...."); | |
cdl.await(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment