|
| 1 | +package io.vertx.tests; |
| 2 | + |
| 3 | +import io.vertx.core.buffer.Buffer; |
| 4 | +import io.vertx.core.net.SocketAddress; |
| 5 | +import io.vertx.ext.unit.Async; |
| 6 | +import io.vertx.ext.unit.TestContext; |
| 7 | +import io.vertx.ext.unit.junit.VertxUnitRunner; |
| 8 | +import io.vertx.httpproxy.ProxyOptions; |
| 9 | +import io.vertx.httpproxy.cache.CacheOptions; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | + |
| 13 | +@RunWith(VertxUnitRunner.class) |
| 14 | +public class WebSocketCacheTest extends ProxyTestBase { |
| 15 | + |
| 16 | + public WebSocketCacheTest() { |
| 17 | + super(new ProxyOptions().setCacheOptions(new CacheOptions())); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testWsWithCache(TestContext ctx) { |
| 22 | + Async latch = ctx.async(); |
| 23 | + SocketAddress backend = startHttpBackend(ctx, 8081, req -> { |
| 24 | + req.toWebSocket().onSuccess(ws -> { |
| 25 | + ws.handler(ws::write); |
| 26 | + }); |
| 27 | + }); |
| 28 | + startProxy(backend); |
| 29 | + vertx.createWebSocketClient().connect(8080, "localhost", "/").onComplete(ctx.asyncAssertSuccess(ws1 -> { |
| 30 | + ws1.handler(buffer -> { |
| 31 | + ctx.assertEquals(buffer.toString(), "v1"); |
| 32 | + ws1.close().onComplete(ctx.asyncAssertSuccess(v -> { |
| 33 | + vertx.createWebSocketClient().connect(8080, "localhost", "/").onComplete(ctx.asyncAssertSuccess(ws2 -> { |
| 34 | + ws2.handler(buffer2 -> { |
| 35 | + ctx.assertEquals(buffer2.toString(), "v2"); |
| 36 | + ws2.close().onComplete(ctx.asyncAssertSuccess(v2 -> { |
| 37 | + latch.complete(); |
| 38 | + })); |
| 39 | + }); |
| 40 | + ws2.write(Buffer.buffer("v2")); |
| 41 | + })); |
| 42 | + })); |
| 43 | + }); |
| 44 | + ws1.write(Buffer.buffer("v1")); |
| 45 | + })); |
| 46 | + } |
| 47 | +} |
0 commit comments