compatible with socket.io 1.3.2
This commit is contained in:
22
src/test/java/com/github/nkzawa/backo/BackoffTest.java
Normal file
22
src/test/java/com/github/nkzawa/backo/BackoffTest.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.github.nkzawa.backo;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class BackoffTest {
|
||||
|
||||
@Test
|
||||
public void durationShouldIncreaseTheBackoff() {
|
||||
Backoff b = new Backoff();
|
||||
|
||||
assertTrue(100 == b.duration());
|
||||
assertTrue(200 == b.duration());
|
||||
assertTrue(400 == b.duration());
|
||||
assertTrue(800 == b.duration());
|
||||
|
||||
b.reset();
|
||||
assertTrue(100 == b.duration());
|
||||
assertTrue(200 == b.duration());
|
||||
}
|
||||
}
|
||||
@@ -298,6 +298,93 @@ public class ConnectionTest extends Connection {
|
||||
values.take();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void attemptReconnectsAfterAFailedReconnect() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
IO.Options opts = createOptions();
|
||||
opts.reconnection = true;
|
||||
opts.timeout = 0;
|
||||
opts.reconnectionAttempts = 2;
|
||||
opts.reconnectionDelay = 10;
|
||||
final Manager manager = new Manager(new URI(uri()), opts);
|
||||
socket = manager.socket("/timeout");
|
||||
socket.once(Socket.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
final int[] reconnects = new int[] {0};
|
||||
Emitter.Listener reconnectCb = new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
reconnects[0]++;
|
||||
}
|
||||
};
|
||||
|
||||
manager.on(Manager.EVENT_RECONNECT_ATTEMPT, reconnectCb);
|
||||
manager.on(Manager.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
values.offer(reconnects[0]);
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
assertThat((Integer)values.take(), is(2));
|
||||
socket.close();
|
||||
manager.close();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void reconnectDelayShouldIncreaseEveryTime() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
IO.Options opts = createOptions();
|
||||
opts.reconnection = true;
|
||||
opts.timeout = 0;
|
||||
opts.reconnectionAttempts = 5;
|
||||
opts.reconnectionDelay = 10;
|
||||
opts.randomizationFactor = 0.2;
|
||||
final Manager manager = new Manager(new URI(uri()), opts);
|
||||
socket = manager.socket("/timeout");
|
||||
|
||||
final int[] reconnects = new int[] {0};
|
||||
final boolean[] increasingDelay = new boolean[] {true};
|
||||
final long[] startTime = new long[] {0};
|
||||
final long[] prevDelay = new long[] {0};
|
||||
|
||||
socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
startTime[0] = new Date().getTime();
|
||||
}
|
||||
});
|
||||
socket.on(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
reconnects[0]++;
|
||||
long currentTime = new Date().getTime();
|
||||
long delay = currentTime - startTime[0];
|
||||
if (delay <= prevDelay[0]) {
|
||||
increasingDelay[0] = false;
|
||||
}
|
||||
prevDelay[0] = delay;
|
||||
}
|
||||
});
|
||||
socket.on(Socket.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
values.offer(true);
|
||||
}
|
||||
});
|
||||
|
||||
socket.connect();
|
||||
values.take();
|
||||
assertThat(reconnects[0], is(5));
|
||||
assertThat(increasingDelay[0], is(true));
|
||||
socket.close();
|
||||
manager.close();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void reconnectEventFireInSocket() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
@@ -446,14 +533,14 @@ public class ConnectionTest extends Connection {
|
||||
manager.on(Manager.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
socket.close();
|
||||
manager.close();
|
||||
values.offer(reconnects[0]);
|
||||
}
|
||||
});
|
||||
|
||||
socket.open();
|
||||
assertThat((Integer)values.take(), is(2));
|
||||
socket.close();
|
||||
manager.close();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
|
||||
103
src/test/java/com/github/nkzawa/socketio/client/SocketTest.java
Normal file
103
src/test/java/com/github/nkzawa/socketio/client/SocketTest.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package com.github.nkzawa.socketio.client;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import com.github.nkzawa.util.Optional;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class SocketTest extends Connection {
|
||||
|
||||
private Socket socket;
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void shouldHaveAnAccessibleSocketIdEqualToTheEngineIOSocketId() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
|
||||
socket = client();
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
values.offer(Optional.ofNullable(socket.id()));
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Optional<String> id = values.take();
|
||||
assertThat(id.isPresent(), is(true));
|
||||
assertThat(id.get(), is(socket.io().engine.id()));
|
||||
socket.disconnect();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void clearsSocketIdUponDisconnection() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
|
||||
socket = client();
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
values.offer(Optional.ofNullable(socket.id()));
|
||||
}
|
||||
});
|
||||
|
||||
socket.disconnect();
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
@SuppressWarnings("unchecked")
|
||||
Optional<String> id = values.take();
|
||||
assertThat(id.isPresent(), is(false));
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void shouldChangeSocketIdUponReconnection() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
|
||||
socket = client();
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
values.offer(Optional.ofNullable(socket.id()));
|
||||
|
||||
socket.on(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
values.offer(Optional.ofNullable(socket.id()));
|
||||
}
|
||||
});
|
||||
|
||||
socket.on(Socket.EVENT_RECONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
values.offer(Optional.ofNullable(socket.id()));
|
||||
}
|
||||
});
|
||||
|
||||
socket.io().engine.close();
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
@SuppressWarnings("unchecked")
|
||||
Optional<String> id1 = values.take();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Optional<String> id2 = values.take();
|
||||
assertThat(id2.isPresent(), is(false));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Optional<String> id3 = values.take();
|
||||
assertThat(id3.get(), is(not(id1.get())));
|
||||
|
||||
socket.disconnect();
|
||||
}
|
||||
}
|
||||
44
src/test/java/com/github/nkzawa/util/Optional.java
Normal file
44
src/test/java/com/github/nkzawa/util/Optional.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package com.github.nkzawa.util;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class Optional<T> {
|
||||
|
||||
static final Optional EMPTY = Optional.ofNullable(null);
|
||||
|
||||
private T value;
|
||||
|
||||
public static <T> Optional<T> of(T value) {
|
||||
if (value == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
return new Optional<T>(value);
|
||||
}
|
||||
|
||||
public static <T> Optional<T> ofNullable(T value) {
|
||||
return new Optional<T>(value);
|
||||
}
|
||||
|
||||
public static <T> Optional<T> empty() {
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
private Optional(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean isPresent() {
|
||||
return this.value != null;
|
||||
}
|
||||
|
||||
public T get() {
|
||||
if (this.value == null) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public T orElse(T other) {
|
||||
return this.value != null ? this.value : other;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user