Skip to content

Commit

Permalink
test: ensure the wsclient waits for a close message from server
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqixu committed Mar 7, 2024
1 parent d5c6e3a commit eb60625
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions client/wsclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ func TestPerformsClosingHandshake(t *testing.T) {
var wsConn *websocket.Conn
connected := make(chan struct{})
closed := make(chan struct{})
acked := make(chan struct{})

srv.OnWSConnect = func(conn *websocket.Conn) {
wsConn = conn
Expand All @@ -299,6 +300,14 @@ func TestPerformsClosingHandshake(t *testing.T) {
return conn != nil
})

{
defhandler := client.conn.CloseHandler()
client.conn.SetCloseHandler(func(code int, msg string) error {
close(acked)
return defhandler(code, msg)
})
}

defHandler := wsConn.CloseHandler()

wsConn.SetCloseHandler(func(code int, _ string) error {
Expand All @@ -313,6 +322,11 @@ func TestPerformsClosingHandshake(t *testing.T) {

select {
case <-closed:
select {
case <-acked:
case <-time.After(2 * time.Second):
require.Fail(t, "Close connection without waiting for a close message from server")
}
case <-time.After(2 * time.Second):
require.Fail(t, "Connection never closed")
}
Expand Down

0 comments on commit eb60625

Please sign in to comment.