add ping and pong events
This commit is contained in:
@@ -11,6 +11,7 @@ import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
@@ -60,6 +61,43 @@ public class SocketTest extends Connection {
|
||||
assertThat(id.isPresent(), is(false));
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void pingAndPongWithLatency() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
socket = client();
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
final boolean[] pinged = new boolean[] { false };
|
||||
socket.once(Socket.EVENT_PING, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
pinged[0] = true;
|
||||
}
|
||||
});
|
||||
socket.once(Socket.EVENT_PONG, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
long ms = (long)args[0];
|
||||
values.offer(pinged[0]);
|
||||
values.offer(ms);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
boolean pinged = (boolean)values.take();
|
||||
assertThat(pinged, is(true));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
long ms = (long)values.take();
|
||||
assertThat(ms, greaterThan((long)0));
|
||||
|
||||
socket.disconnect();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void shouldChangeSocketIdUponReconnection() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
|
||||
|
||||
@@ -10,7 +10,7 @@ if (process.env.SSL) {
|
||||
server = require('http').createServer();
|
||||
}
|
||||
|
||||
var io = require('socket.io')(server);
|
||||
var io = require('socket.io')(server, { pingInterval: 2000 });
|
||||
var port = process.env.PORT || 3000;
|
||||
var nsp = process.argv[2] || '/';
|
||||
var slice = Array.prototype.slice;
|
||||
|
||||
Reference in New Issue
Block a user