Skip to content

Commit ba723d0

Browse files
committed
add tests
1 parent 48420a6 commit ba723d0

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

src/test/java/io/socket/client/ConnectionTest.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ public void reconnectDelayShouldIncreaseEveryTime() throws URISyntaxException, I
420420
IO.Options opts = createOptions();
421421
opts.reconnection = true;
422422
opts.timeout = 0;
423-
opts.reconnectionAttempts = 5;
424-
opts.reconnectionDelay = 10;
423+
opts.reconnectionAttempts = 3;
424+
opts.reconnectionDelay = 100;
425425
opts.randomizationFactor = 0.2;
426426
final Manager manager = new Manager(new URI(uri()), opts);
427427
socket = manager.socket("/timeout");
@@ -458,9 +458,8 @@ public void call(Object... args) {
458458

459459
socket.connect();
460460
values.take();
461-
assertThat(reconnects[0], is(5));
462-
// this fails sometimes
463-
//assertThat(increasingDelay[0], is(true));
461+
assertThat(reconnects[0], is(3));
462+
assertThat(increasingDelay[0], is(true));
464463
socket.close();
465464
manager.close();
466465
}
@@ -545,6 +544,32 @@ public void run() {
545544
assertThat((Boolean) values.take(), is(true));
546545
}
547546

547+
@Test(timeout = TIMEOUT)
548+
public void reconnectAfterStoppingReconnection() throws URISyntaxException, InterruptedException {
549+
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
550+
IO.Options opts = createOptions();
551+
opts.forceNew = true;
552+
opts.timeout = 0;
553+
opts.reconnectionDelay = 10;
554+
socket = client("/invalid", opts);
555+
socket.once(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
556+
@Override
557+
public void call(Object... args) {
558+
socket.once(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
559+
@Override
560+
public void call(Object... args) {
561+
values.offer("done");
562+
}
563+
});
564+
socket.disconnect();
565+
socket.connect();
566+
}
567+
});
568+
socket.connect();
569+
values.take();
570+
socket.disconnect();
571+
}
572+
548573
@Test(timeout = TIMEOUT)
549574
public void stopReconnectingOnASocketAndKeepToReconnectOnAnother() throws URISyntaxException, InterruptedException {
550575
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();

src/test/java/io/socket/client/SocketTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
import org.junit.runners.JUnit4;
88

99
import java.net.URISyntaxException;
10+
import java.util.Timer;
11+
import java.util.TimerTask;
1012
import java.util.concurrent.BlockingQueue;
1113
import java.util.concurrent.LinkedBlockingQueue;
1214

13-
import static org.hamcrest.CoreMatchers.*;
15+
import static org.hamcrest.CoreMatchers.is;
16+
import static org.hamcrest.CoreMatchers.not;
1417
import static org.hamcrest.Matchers.greaterThan;
1518
import static org.junit.Assert.assertThat;
1619

@@ -61,6 +64,33 @@ public void call(Object... args) {
6164
assertThat(id.isPresent(), is(false));
6265
}
6366

67+
@Test(timeout = TIMEOUT)
68+
public void doesNotFireConnectErrorIfWeForceDisconnectInOpeningState() throws URISyntaxException, InterruptedException {
69+
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
70+
IO.Options opts = new IO.Options();
71+
opts.timeout = 100;
72+
socket = client(opts);
73+
socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
74+
@Override
75+
public void call(Object... args) {
76+
values.offer(Optional.of(new Error("Unexpected")));
77+
}
78+
});
79+
socket.connect();
80+
socket.disconnect();
81+
82+
new Timer().schedule(new TimerTask() {
83+
@Override
84+
public void run() {
85+
values.offer(Optional.empty());
86+
}
87+
}, 300);
88+
89+
@SuppressWarnings("unchecked")
90+
Optional<Error> err = values.take();
91+
if (err.isPresent()) throw err.get();
92+
}
93+
6494
@Test(timeout = TIMEOUT)
6595
public void pingAndPongWithLatency() throws URISyntaxException, InterruptedException {
6696
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();

src/test/java/io/socket/client/UrlTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,14 @@ public void extractId() throws MalformedURLException {
5959
assertThat(id1, is(not(id3)));
6060
assertThat(id2, is(not(id3)));
6161
}
62+
63+
@Test
64+
public void ipv6() throws URISyntaxException, MalformedURLException {
65+
String url = "http://[::1]";
66+
URL parsed = Url.parse(url);
67+
assertThat(parsed.getProtocol(), is("http"));
68+
assertThat(parsed.getHost(), is("[::1]"));
69+
assertThat(parsed.getPort(), is(80));
70+
assertThat(Url.extractId(url), is("http://[::1]:80"));
71+
}
6272
}

0 commit comments

Comments
 (0)