Skip to content

Instantly share code, notes, and snippets.

@aliakhtar
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aliakhtar/602ee81f58046cdcfb9b to your computer and use it in GitHub Desktop.
Save aliakhtar/602ee81f58046cdcfb9b to your computer and use it in GitHub Desktop.
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.logging.Logger;
import static org.junit.Assert.*;
@RunWith(VertxUnitRunner.class)
public class WebSocketTest
{
private final Logger log = Logger.getLogger( this.toString() ) ;
private Vertx vertx;
private HttpClient client;
private boolean serverTimedOut = false;
private boolean clientTimedOut = false;
private long connOpenTime = 0;
@Before
public void setUp() throws Exception
{
vertx = Vertx.vertx( );
vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(100))
.websocketHandler(ws ->
{
connOpenTime = System.nanoTime();
ws.closeHandler(v -> serverTimedOut = true);
ws.frameHandler(f -> log.info(" Got text: " + f.textData()));
ws.handler(buffer -> log.info(" Got buffer: " + buffer.toString("UTF-8")));
ws.write(Buffer.buffer().appendString("pong"));
}).listen(8080);
client = vertx.createHttpClient( new HttpClientOptions()
.setKeepAlive(false)
.setTcpKeepAlive(false));
}
@Test
public void testTimeout(TestContext context) throws Exception
{
client.websocket(8080, "localhost", "/foo", ws ->
{
log.info("connected (client)");
ws.write(Buffer.buffer().appendString("ping"));
ws.handler(buffer -> log.info(" Got buffer (client): " + buffer.toString("UTF-8") ));
ws.closeHandler(v -> clientTimedOut = true);
});
Async async = context.async();
Thread.sleep(5 * 60 * 1000);
assertTrue(serverTimedOut);
assertTrue(clientTimedOut);
async.complete();
}
@After
public void tearDown() throws Exception
{
vertx.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment