From e0971d47db6722ad3826f9fdf996c8199fb6805d Mon Sep 17 00:00:00 2001 From: ucwong Date: Thu, 2 Apr 2020 09:33:05 +0000 Subject: [PATCH 1/8] rpc/websocket_test | Add missing timer.Stop calls if response timer is not nil --- rpc/websocket_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rpc/websocket_test.go b/rpc/websocket_test.go index f2a8438d7ce9..28a0f09d1b43 100644 --- a/rpc/websocket_test.go +++ b/rpc/websocket_test.go @@ -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(): @@ -247,6 +248,11 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <- } wantPong = "" sendResponse = time.NewTimer(200 * time.Millisecond).C + defer func() { + if sendResponse != nil { + sendResponse.Stop() + } + }() case <-sendResponse: t.Logf("server sending response") conn.WriteMessage(websocket.TextMessage, []byte(subNotify)) From 9759ac7816310e8c45a067c4286947dbfebf4817 Mon Sep 17 00:00:00 2001 From: ucwong Date: Thu, 2 Apr 2020 10:50:02 +0000 Subject: [PATCH 2/8] rpc/websocket_test : add timer var to make sure the timer finally stopped --- rpc/websocket_test.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/rpc/websocket_test.go b/rpc/websocket_test.go index 28a0f09d1b43..65dbab681c25 100644 --- a/rpc/websocket_test.go +++ b/rpc/websocket_test.go @@ -247,12 +247,9 @@ 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 - defer func() { - if sendResponse != nil { - sendResponse.Stop() - } - }() + timer := time.NewTimer(200 * time.Millisecond) + defer timer.Stop() + sendResponse = timer.C case <-sendResponse: t.Logf("server sending response") conn.WriteMessage(websocket.TextMessage, []byte(subNotify)) From e7354f6bc0c06a2b4793d519fb44096c486f6ced Mon Sep 17 00:00:00 2001 From: ucwong Date: Thu, 2 Apr 2020 14:04:20 +0000 Subject: [PATCH 3/8] rpc | put the timer out of the loop --- rpc/websocket_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpc/websocket_test.go b/rpc/websocket_test.go index 65dbab681c25..368098704b1d 100644 --- a/rpc/websocket_test.go +++ b/rpc/websocket_test.go @@ -231,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: @@ -247,8 +249,6 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <- t.Errorf("got pong with wrong data %q", data) } wantPong = "" - timer := time.NewTimer(200 * time.Millisecond) - defer timer.Stop() sendResponse = timer.C case <-sendResponse: t.Logf("server sending response") From 8a40a8f0c658702e51fc2be99a7312987aaaf39d Mon Sep 17 00:00:00 2001 From: ucwong Date: Thu, 2 Apr 2020 14:28:17 +0000 Subject: [PATCH 4/8] rpc : reset timer when receive a pong every time --- rpc/websocket_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/rpc/websocket_test.go b/rpc/websocket_test.go index 368098704b1d..fe41efe3ae47 100644 --- a/rpc/websocket_test.go +++ b/rpc/websocket_test.go @@ -249,6 +249,7 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <- t.Errorf("got pong with wrong data %q", data) } wantPong = "" + timer.Reset(200 * time.Millisecond) sendResponse = timer.C case <-sendResponse: t.Logf("server sending response") From f4f075cd9b24c14ba771785739e9863fbe422ad5 Mon Sep 17 00:00:00 2001 From: ucwong Date: Thu, 2 Apr 2020 14:35:45 +0000 Subject: [PATCH 5/8] rpc : new timer when timer is nil and stop when not nil --- rpc/websocket_test.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/rpc/websocket_test.go b/rpc/websocket_test.go index fe41efe3ae47..b79597cdf183 100644 --- a/rpc/websocket_test.go +++ b/rpc/websocket_test.go @@ -230,9 +230,14 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <- var ( sendResponse <-chan time.Time wantPong string + timer *Timer ) - timer := time.NewTimer(200 * time.Millisecond) - defer timer.Stop() + //timer := time.NewTimer(200 * time.Millisecond) + defer func() { + if timer != nil { + timer.Stop() + } + }() for { select { 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) } wantPong = "" - timer.Reset(200 * time.Millisecond) + if timer == nil { + timer = time.NewTimer(200 * time.Millisecond) + } else { + timer.Reset(200 * time.Millisecond) + } sendResponse = timer.C case <-sendResponse: t.Logf("server sending response") From eb616bd6186cdc949304b592519844efec2be1bc Mon Sep 17 00:00:00 2001 From: ucwong Date: Thu, 2 Apr 2020 14:47:39 +0000 Subject: [PATCH 6/8] rpc : init timer at first --- rpc/websocket_test.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/rpc/websocket_test.go b/rpc/websocket_test.go index b79597cdf183..602b96746ec8 100644 --- a/rpc/websocket_test.go +++ b/rpc/websocket_test.go @@ -232,12 +232,9 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <- wantPong string timer *Timer ) - //timer := time.NewTimer(200 * time.Millisecond) - defer func() { - if timer != nil { - timer.Stop() - } - }() + timer := time.NewTimer(0) + defer timer.Stop() + <-timer.C for { select { case _, open := <-sendPing: @@ -254,11 +251,7 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <- t.Errorf("got pong with wrong data %q", data) } wantPong = "" - if timer == nil { - timer = time.NewTimer(200 * time.Millisecond) - } else { - timer.Reset(200 * time.Millisecond) - } + timer.Reset(200 * time.Millisecond) sendResponse = timer.C case <-sendResponse: t.Logf("server sending response") From 792c7798921f88f4eb6bc1b7e3f0cb8f97e12118 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 2 Apr 2020 17:38:13 +0200 Subject: [PATCH 7/8] rpc: simplify wsPingTestHandler --- rpc/websocket_test.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/rpc/websocket_test.go b/rpc/websocket_test.go index 602b96746ec8..f53182256d21 100644 --- a/rpc/websocket_test.go +++ b/rpc/websocket_test.go @@ -228,11 +228,9 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <- // Write messages. var ( - sendResponse <-chan time.Time wantPong string - timer *Timer + timer = time.NewTimer(0) ) - timer := time.NewTimer(0) defer timer.Stop() <-timer.C for { @@ -252,11 +250,9 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <- } wantPong = "" timer.Reset(200 * time.Millisecond) - sendResponse = timer.C - case <-sendResponse: + case <-timer.C: t.Logf("server sending response") conn.WriteMessage(websocket.TextMessage, []byte(subNotify)) - sendResponse = nil case <-shutdown: conn.Close() return From 8659669381a9618ab996f3edfdf3b7d26e07a0cf Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 2 Apr 2020 21:25:59 +0200 Subject: [PATCH 8/8] rpc: fix lint --- rpc/websocket_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpc/websocket_test.go b/rpc/websocket_test.go index f53182256d21..f54fc3cd541b 100644 --- a/rpc/websocket_test.go +++ b/rpc/websocket_test.go @@ -228,8 +228,8 @@ func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <- // Write messages. var ( - wantPong string - timer = time.NewTimer(0) + wantPong string + timer = time.NewTimer(0) ) defer timer.Stop() <-timer.C