fix: testkit: give up on waiting for the RPC server to shutdown after 1 second

This commit is contained in:
Dirk McCormick 2022-04-07 12:20:08 +02:00
parent f652dd3436
commit 5f0489ace0

View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"time"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -25,7 +26,9 @@ func CreateRPCServer(t *testing.T, handler http.Handler, listener net.Listener)
} }
testServ.Start() 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) t.Cleanup(testServ.CloseClientConnections)
addr := testServ.Listener.Addr() addr := testServ.Listener.Addr()
@ -34,6 +37,23 @@ func CreateRPCServer(t *testing.T, handler http.Handler, listener net.Listener)
return testServ, maddr 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 { func fullRpc(t *testing.T, f *TestFullNode) *TestFullNode {
handler, err := node.FullNodeHandler(f.FullNode, false) handler, err := node.FullNodeHandler(f.FullNode, false)
require.NoError(t, err) require.NoError(t, err)