compatible with socket.io-client 1.0.6

This commit is contained in:
Naoyuki Kanezawa
2014-07-13 08:21:40 +09:00
parent d070baa1b6
commit 289b282a76
6 changed files with 239 additions and 77 deletions

View File

@@ -91,7 +91,6 @@ public abstract class Connection {
IO.Options createOptions() {
IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.reconnection = false;
return opts;
}

View File

@@ -196,21 +196,42 @@ public class ConnectionTest extends Connection {
@Test(timeout = TIMEOUT)
public void reconnectByDefault() throws URISyntaxException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
socket = IO.socket(uri());
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
socket = client();
socket.io.on(Manager.EVENT_RECONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.io.engine.close();
socket.io.on(Manager.EVENT_RECONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.close();
latch.countDown();
}
});
socket.close();
latch.countDown();
}
});
socket.open();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
socket.io.engine.close();
}
}, 500);
latch.await();
}
@Test(timeout = TIMEOUT)
public void reconnectEventFireInSocket() throws URISyntaxException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
socket = client();
socket.on(Socket.EVENT_RECONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.close();
latch.countDown();
}
});
socket.open();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
socket.io.engine.close();
}
}, 500);
latch.await();
}
@@ -312,6 +333,74 @@ public class ConnectionTest extends Connection {
latch.await();
}
@Test(timeout = TIMEOUT)
public void fireReconnectEventsOnSocket() throws URISyntaxException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
Manager.Options opts = new Manager.Options();
opts.reconnection = true;
opts.timeout = 0;
opts.reconnectionAttempts = 2;
opts.reconnectionDelay = 10;
Manager manager = new Manager(new URI(uri()), opts);
socket = manager.socket("/timeout_socket");
final int[] reconnects = new int[] {0};
Emitter.Listener reconnectCb = new Emitter.Listener() {
@Override
public void call(Object... args) {
reconnects[0]++;
assertThat((Integer)args[0], is(reconnects[0]));
}
};
socket.on(Socket.EVENT_RECONNECT_ATTEMPT, reconnectCb);
socket.on(Socket.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
@Override
public void call(Object... objects) {
assertThat(reconnects[0], is(2));
socket.close();
latch.countDown();
}
});
socket.open();
latch.await();
}
@Test(timeout = TIMEOUT)
public void fireReconnectingWithAttemptsNumberWhenReconnectingTwice() throws URISyntaxException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
Manager.Options opts = new Manager.Options();
opts.reconnection = true;
opts.timeout = 0;
opts.reconnectionAttempts = 2;
opts.reconnectionDelay = 10;
Manager manager = new Manager(new URI(uri()), opts);
socket = manager.socket("/timeout_socket");
final int[] reconnects = new int[] {0};
Emitter.Listener reconnectCb = new Emitter.Listener() {
@Override
public void call(Object... args) {
reconnects[0]++;
assertThat((Integer)args[0], is(reconnects[0]));
}
};
socket.on(Socket.EVENT_RECONNECTING, reconnectCb);
socket.on(Socket.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
@Override
public void call(Object... objects) {
assertThat(reconnects[0], is(2));
socket.close();
latch.countDown();
}
});
socket.open();
latch.await();
}
@Test(timeout = TIMEOUT)
public void emitDateAsString() throws URISyntaxException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);

View File

@@ -5,7 +5,6 @@ import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
@@ -18,7 +17,13 @@ public class UrlTest {
@Test
public void parse() throws MalformedURLException, URISyntaxException {
URL url = Url.parse(new URI("https://woot.com/test"));
assertThat(Url.parse("http://username:password@host:8080/directory/file?query#ref").toString(),
is("http://username:password@host:8080/directory/file?query#ref"));
}
@Test
public void parseRelativePath() throws MalformedURLException, URISyntaxException {
URL url = Url.parse("https://woot.com/test");
assertThat(url.getProtocol(), is("https"));
assertThat(url.getHost(), is("woot.com"));
assertThat(url.getPath(), is("/test"));
@@ -26,7 +31,7 @@ public class UrlTest {
@Test
public void parseNoProtocol() throws MalformedURLException, URISyntaxException {
URL url = Url.parse(new URI("//localhost:3000"));
URL url = Url.parse("//localhost:3000");
assertThat(url.getProtocol(), is("https"));
assertThat(url.getHost(), is("localhost"));
assertThat(url.getPort(), is(3000));
@@ -34,23 +39,24 @@ public class UrlTest {
@Test
public void parseNamespace() throws MalformedURLException, URISyntaxException {
assertThat(Url.parse(new URI("http://woot.com/woot")).getPath(), is("/woot"));
assertThat(Url.parse(new URI("http://google.com")).getPath(), is("/"));
assertThat(Url.parse(new URI("http://google.com/")).getPath(), is("/"));
assertThat(Url.parse("http://woot.com/woot").getPath(), is("/woot"));
assertThat(Url.parse("http://google.com").getPath(), is("/"));
assertThat(Url.parse("http://google.com/").getPath(), is("/"));
}
@Test
public void parseDefaultPort() throws MalformedURLException, URISyntaxException {
assertThat(Url.parse(new URI("http://google.com:80/")).toString(), is("http://google.com/"));
assertThat(Url.parse(new URI("https://google.com:443/")).toString(), is("https://google.com/"));
assertThat(Url.parse("http://google.com/").toString(), is("http://google.com:80/"));
assertThat(Url.parse("https://google.com/").toString(), is("https://google.com:443/"));
}
@Test
public void extractId() throws MalformedURLException, URISyntaxException {
String id1 = Url.extractId(new URL("http://google.com:80/"));
String id2 = Url.extractId(new URL("http://google.com/"));
String id3 = Url.extractId(new URL("https://google.com/"));
public void extractId() throws MalformedURLException {
String id1 = Url.extractId("http://google.com:80/");
String id2 = Url.extractId("http://google.com/");
String id3 = Url.extractId("https://google.com/");
assertThat(id1, is(id2));
assertThat(id1, is(not(id3)));
assertThat(id2, is(not(id3)));
}
}