Skip to content

Commit

Permalink
fix: ensure buffered events are sent in order
Browse files Browse the repository at this point in the history
Before this commit, an event sent in the "connect" handler could be
sent before the events that were buffered while disconnected.

Related: socketio/socket.io-client#1458

Backported from 4885e7d
  • Loading branch information
darrachequesne committed Jul 10, 2022
1 parent e57160a commit 8bd35da
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/io/socket/client/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ private void onack(Packet<JSONArray> packet) {

private void onconnect() {
this.connected = true;
this.emit(EVENT_CONNECT);
this.emitBuffered();
super.emit(EVENT_CONNECT);
}

private void emitBuffered() {
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/io/socket/client/SocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,34 @@ public void call(Object... args) {

socket.disconnect();
}

@Test(timeout = TIMEOUT)
public void shouldEmitEventsInOrder() throws InterruptedException, URISyntaxException {
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"));
}
}

0 comments on commit 8bd35da

Please sign in to comment.