Skip to content

Commit

Permalink
Merge pull request #76 from Jardaliao/unit-test-for-closing-ws-connec…
Browse files Browse the repository at this point in the history
…tion

Add: unit test for closing ws connection
  • Loading branch information
AlexStocks authored Nov 23, 2023
2 parents 28c6c73 + fdaf534 commit cc92b66
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions transport/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
)

import (
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -364,6 +365,26 @@ func TestNewWSClient(t *testing.T) {
err = conn.writePing()
assert.Nil(t, err)

done := make(chan int)
conn.conn.SetCloseHandler(func(code int, text string) error {
defer func() {
done <- code
close(done)
}()
message := websocket.FormatCloseMessage(code, "")
conn.conn.WriteControl(websocket.CloseMessage, message, time.Now().Add(1e9))
return nil
})
serverSession := serverMsgHandler.array[0]
serverSession.Close()
select {
case code := <-done:
assert.True(t, code == websocket.CloseNormalClosure)
case <-time.After(5e9):
assert.True(t, false)
}
assert.True(t, serverSession.IsClosed())

ss.SetReader(nil)
assert.Nil(t, ss.(*session).reader)
ss.SetWriter(nil)
Expand Down

0 comments on commit cc92b66

Please sign in to comment.