diff --git a/itests/kit/rpc.go b/itests/kit/rpc.go index 7328bf261..9d90c846f 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,21 @@ 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) + }() + + select { + case <-ch: + case <-time.After(waitTime): + fmt.Println(errMsg) + return + } +} + func fullRpc(t *testing.T, f *TestFullNode) *TestFullNode { handler, err := node.FullNodeHandler(f.FullNode, false) require.NoError(t, err)