Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rpc/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func TestClientWebsocketPing(t *testing.T) {

// Wait for the subscription result.
timeout := time.NewTimer(5 * time.Second)
defer timeout.Stop()
for {
select {
case err := <-sub.Err():
Expand Down Expand Up @@ -230,6 +231,8 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <-
sendResponse <-chan time.Time
wantPong string
)
timer := time.NewTimer(200 * time.Millisecond)
defer timer.Stop()
for {
select {
case _, open := <-sendPing:
Expand All @@ -246,7 +249,7 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <-
t.Errorf("got pong with wrong data %q", data)
}
wantPong = ""
sendResponse = time.NewTimer(200 * time.Millisecond).C
sendResponse = timer.C
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct because the timer must be reset for every pong message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea in this test is: when a pong is received, the timer is started, and 200ms later a response is sent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, should be reset the timer every time when receive a pong

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is still not quite correct now because the timer should not fire without a receive from pongCh. So you need to initialize the timer like

timer := time.NewTimer(0)
defer timer.Stop()
<-timer.C

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your opinion, should new it once only pong is receive first time and defer stop when timer is not nil

case <-sendResponse:
t.Logf("server sending response")
conn.WriteMessage(websocket.TextMessage, []byte(subNotify))
Expand Down