fix: ensure buffered events are sent in order

Before this commit, an event sent in the "connect" handler could be
sent before the events that were buffered while disconnected.

Related: https://github.com/socketio/socket.io-client/issues/1458
This commit is contained in:
Damien Arrachequesne
2021-04-26 11:22:38 +02:00
parent 48fec45740
commit 4885e7d59f
2 changed files with 31 additions and 1 deletions

View File

@@ -367,8 +367,8 @@ public class Socket extends Emitter {
private void onconnect(String id) {
this.connected = true;
this.id = id;
super.emit(EVENT_CONNECT);
this.emitBuffered();
super.emit(EVENT_CONNECT);
}
private void emitBuffered() {

View File

@@ -257,4 +257,34 @@ public class SocketTest extends Connection {
socket.disconnect();
}
@Test(timeout = TIMEOUT)
public void shouldEmitEventsInOrder() throws InterruptedException {
final BlockingQueue<String> values = new LinkedBlockingQueue<>();
socket = client();
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.emit("ack", "second", new Ack() {
@Override
public void call(Object... args) {
values.offer((String) args[0]);
}
});
}
});
socket.emit("ack", "first", new Ack() {
@Override
public void call(Object... args) {
values.offer((String) args[0]);
}
});
socket.connect();
assertThat(values.take(), is("first"));
assertThat(values.take(), is("second"));
}
}