add tests

This commit is contained in:
nkzawa
2016-02-01 01:50:09 +09:00
parent 48420a6c6f
commit ba723d0460
3 changed files with 71 additions and 6 deletions

View File

@@ -420,8 +420,8 @@ public class ConnectionTest extends Connection {
IO.Options opts = createOptions();
opts.reconnection = true;
opts.timeout = 0;
opts.reconnectionAttempts = 5;
opts.reconnectionDelay = 10;
opts.reconnectionAttempts = 3;
opts.reconnectionDelay = 100;
opts.randomizationFactor = 0.2;
final Manager manager = new Manager(new URI(uri()), opts);
socket = manager.socket("/timeout");
@@ -458,9 +458,8 @@ public class ConnectionTest extends Connection {
socket.connect();
values.take();
assertThat(reconnects[0], is(5));
// this fails sometimes
//assertThat(increasingDelay[0], is(true));
assertThat(reconnects[0], is(3));
assertThat(increasingDelay[0], is(true));
socket.close();
manager.close();
}
@@ -545,6 +544,32 @@ public class ConnectionTest extends Connection {
assertThat((Boolean) values.take(), is(true));
}
@Test(timeout = TIMEOUT)
public void reconnectAfterStoppingReconnection() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
IO.Options opts = createOptions();
opts.forceNew = true;
opts.timeout = 0;
opts.reconnectionDelay = 10;
socket = client("/invalid", opts);
socket.once(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.once(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer("done");
}
});
socket.disconnect();
socket.connect();
}
});
socket.connect();
values.take();
socket.disconnect();
}
@Test(timeout = TIMEOUT)
public void stopReconnectingOnASocketAndKeepToReconnectOnAnother() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();

View File

@@ -7,10 +7,13 @@ import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.net.URISyntaxException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertThat;
@@ -61,6 +64,33 @@ public class SocketTest extends Connection {
assertThat(id.isPresent(), is(false));
}
@Test(timeout = TIMEOUT)
public void doesNotFireConnectErrorIfWeForceDisconnectInOpeningState() throws URISyntaxException, InterruptedException {
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
IO.Options opts = new IO.Options();
opts.timeout = 100;
socket = client(opts);
socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer(Optional.of(new Error("Unexpected")));
}
});
socket.connect();
socket.disconnect();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
values.offer(Optional.empty());
}
}, 300);
@SuppressWarnings("unchecked")
Optional<Error> err = values.take();
if (err.isPresent()) throw err.get();
}
@Test(timeout = TIMEOUT)
public void pingAndPongWithLatency() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();

View File

@@ -59,4 +59,14 @@ public class UrlTest {
assertThat(id1, is(not(id3)));
assertThat(id2, is(not(id3)));
}
@Test
public void ipv6() throws URISyntaxException, MalformedURLException {
String url = "http://[::1]";
URL parsed = Url.parse(url);
assertThat(parsed.getProtocol(), is("http"));
assertThat(parsed.getHost(), is("[::1]"));
assertThat(parsed.getPort(), is(80));
assertThat(Url.extractId(url), is("http://[::1]:80"));
}
}