rpc : new timer when timer is nil and stop when not nil

This commit is contained in:
ucwong 2020-04-02 14:35:45 +00:00
parent 8a40a8f0c6
commit f4f075cd9b

View file

@ -230,9 +230,14 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <-
var ( var (
sendResponse <-chan time.Time sendResponse <-chan time.Time
wantPong string wantPong string
timer *Timer
) )
timer := time.NewTimer(200 * time.Millisecond) //timer := time.NewTimer(200 * time.Millisecond)
defer timer.Stop() defer func() {
if timer != nil {
timer.Stop()
}
}()
for { for {
select { select {
case _, open := <-sendPing: case _, open := <-sendPing:
@ -249,7 +254,11 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <-
t.Errorf("got pong with wrong data %q", data) t.Errorf("got pong with wrong data %q", data)
} }
wantPong = "" wantPong = ""
if timer == nil {
timer = time.NewTimer(200 * time.Millisecond)
} else {
timer.Reset(200 * time.Millisecond) timer.Reset(200 * time.Millisecond)
}
sendResponse = timer.C sendResponse = timer.C
case <-sendResponse: case <-sendResponse:
t.Logf("server sending response") t.Logf("server sending response")