From 5f0489ace0b312aac3ecbce35fe453c630c5def0 Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Thu, 7 Apr 2022 12:20:08 +0200 Subject: [PATCH 1/2] fix: testkit: give up on waiting for the RPC server to shutdown after 1 second --- itests/kit/rpc.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/itests/kit/rpc.go b/itests/kit/rpc.go index 7328bf261..c4c160e23 100644 --- a/itests/kit/rpc.go +++ b/itests/kit/rpc.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/stretchr/testify/require" @@ -25,7 +26,9 @@ func CreateRPCServer(t *testing.T, handler http.Handler, listener net.Listener) } testServ.Start() - t.Cleanup(testServ.Close) + t.Cleanup(func() { + waitUpTo(testServ.Close, time.Second, "Gave up waiting for RPC server to close after 1s") + }) t.Cleanup(testServ.CloseClientConnections) addr := testServ.Listener.Addr() @@ -34,6 +37,23 @@ func CreateRPCServer(t *testing.T, handler http.Handler, listener net.Listener) return testServ, maddr } +func waitUpTo(fn func(), waitTime time.Duration, errMsg string) { + ch := make(chan struct{}) + go func() { + fn() + close(ch) + }() + + timer := time.NewTimer(waitTime) + defer timer.Stop() + select { + case <-ch: + case <-timer.C: + fmt.Println(errMsg) + return + } +} + func fullRpc(t *testing.T, f *TestFullNode) *TestFullNode { handler, err := node.FullNodeHandler(f.FullNode, false) require.NoError(t, err) From c34ade9ffb46a0d1e8d9ad0f401de587c412488e Mon Sep 17 00:00:00 2001 From: dirkmc Date: Thu, 7 Apr 2022 13:32:00 +0200 Subject: [PATCH 2/2] Update itests/kit/rpc.go Co-authored-by: Anton Evangelatov --- itests/kit/rpc.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/itests/kit/rpc.go b/itests/kit/rpc.go index c4c160e23..9d90c846f 100644 --- a/itests/kit/rpc.go +++ b/itests/kit/rpc.go @@ -44,11 +44,9 @@ func waitUpTo(fn func(), waitTime time.Duration, errMsg string) { close(ch) }() - timer := time.NewTimer(waitTime) - defer timer.Stop() select { case <-ch: - case <-timer.C: + case <-time.After(waitTime): fmt.Println(errMsg) return }