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
  • Loading branch information
darrachequesne committed Apr 26, 2021
1 parent 48fec45 commit 4885e7d
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 @@ -367,8 +367,8 @@ private void onack(Packet<JSONArray> packet) {
private void onconnect(String id) {
this.connected = true;
this.id = id;
super.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 @@ -257,4 +257,34 @@ public void shouldThrowOnReservedEvent() {

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"));
}
}

0 comments on commit 4885e7d

Please sign in to comment.