fix disconnection while in opening state

This commit is contained in:
nkzawa
2016-01-31 23:55:53 +09:00
parent f7810c19d3
commit 599eb98c6e
3 changed files with 40 additions and 5 deletions

View File

@@ -84,7 +84,7 @@ public class Manager extends Emitter {
private double _randomizationFactor; private double _randomizationFactor;
private Backoff backoff; private Backoff backoff;
private long _timeout; private long _timeout;
private Set<Socket> connected; private Set<Socket> connecting = new HashSet<Socket>();
private URI uri; private URI uri;
private List<Packet> packetBuffer; private List<Packet> packetBuffer;
private Queue<On.Handle> subs; private Queue<On.Handle> subs;
@@ -139,7 +139,6 @@ public class Manager extends Emitter {
this.timeout(opts.timeout); this.timeout(opts.timeout);
this.readyState = ReadyState.CLOSED; this.readyState = ReadyState.CLOSED;
this.uri = uri; this.uri = uri;
this.connected = new HashSet<Socket>();
this.encoding = false; this.encoding = false;
this.packetBuffer = new ArrayList<Packet>(); this.packetBuffer = new ArrayList<Packet>();
this.encoder = new Parser.Encoder(); this.encoder = new Parser.Encoder();
@@ -402,11 +401,16 @@ public class Manager extends Emitter {
} else { } else {
final Manager self = this; final Manager self = this;
final Socket s = socket; final Socket s = socket;
socket.on(Socket.EVENT_CONNECTING, new Listener() {
@Override
public void call(Object... args) {
self.connecting.add(s);
}
});
socket.on(Socket.EVENT_CONNECT, new Listener() { socket.on(Socket.EVENT_CONNECT, new Listener() {
@Override @Override
public void call(Object... objects) { public void call(Object... objects) {
s.id = self.engine.id(); s.id = self.engine.id();
self.connected.add(s);
} }
}); });
} }
@@ -415,8 +419,8 @@ public class Manager extends Emitter {
} }
/*package*/ void destroy(Socket socket) { /*package*/ void destroy(Socket socket) {
this.connected.remove(socket); this.connecting.remove(socket);
if (!this.connected.isEmpty()) return; if (!this.connecting.isEmpty()) return;
this.close(); this.close();
} }

View File

@@ -25,6 +25,8 @@ public class Socket extends Emitter {
*/ */
public static final String EVENT_CONNECT = "connect"; public static final String EVENT_CONNECT = "connect";
public static final String EVENT_CONNECTING = "connecting";
/** /**
* Called on a disconnection. * Called on a disconnection.
*/ */
@@ -60,6 +62,7 @@ public class Socket extends Emitter {
put(EVENT_CONNECT, 1); put(EVENT_CONNECT, 1);
put(EVENT_CONNECT_ERROR, 1); put(EVENT_CONNECT_ERROR, 1);
put(EVENT_CONNECT_TIMEOUT, 1); put(EVENT_CONNECT_TIMEOUT, 1);
put(EVENT_CONNECTING, 1);
put(EVENT_DISCONNECT, 1); put(EVENT_DISCONNECT, 1);
put(EVENT_ERROR, 1); put(EVENT_ERROR, 1);
put(EVENT_RECONNECT, 1); put(EVENT_RECONNECT, 1);
@@ -123,6 +126,7 @@ public class Socket extends Emitter {
Socket.this.subEvents(); Socket.this.subEvents();
Socket.this.io.open(); // ensure open Socket.this.io.open(); // ensure open
if (Manager.ReadyState.OPEN == Socket.this.io.readyState) Socket.this.onopen(); if (Manager.ReadyState.OPEN == Socket.this.io.readyState) Socket.this.onopen();
Socket.this.emit(EVENT_CONNECTING);
} }
}); });
return this; return this;

View File

@@ -591,6 +591,33 @@ public class ConnectionTest extends Connection {
assertThat((Boolean) values.take(), is(true)); assertThat((Boolean) values.take(), is(true));
} }
@Test(timeout = TIMEOUT)
public void connectWhileDisconnectingAnotherSocket() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
final Manager manager = new Manager(new URI(uri()));
final Socket socket1 = manager.socket("/foo");
socket1.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
final Socket socket2 = manager.socket("/asd");
socket2.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer("done");
socket2.disconnect();
}
});
socket2.open();
socket1.disconnect();
}
});
socket1.open();
values.take();
manager.close();
}
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void tryToReconnectTwiceAndFailWithIncorrectAddress() throws URISyntaxException, InterruptedException { public void tryToReconnectTwiceAndFailWithIncorrectAddress() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>(); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();