From 5b30d15f8e1e7de48c158b317f2dd9964a4bb07d Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 1 Apr 2024 18:06:08 +0300 Subject: [PATCH] rpcsrv: properly cleanup network server in tests Port the idea of https://github.com/nspcc-dev/neo-go/pull/3380. Signed-off-by: Anna Shaleva --- pkg/services/rpcsrv/subscription_test.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/services/rpcsrv/subscription_test.go b/pkg/services/rpcsrv/subscription_test.go index 0d2c5071d..fbc35bac1 100644 --- a/pkg/services/rpcsrv/subscription_test.go +++ b/pkg/services/rpcsrv/subscription_test.go @@ -57,7 +57,7 @@ func getNotification(t *testing.T, respCh <-chan []byte) *neorpc.Notification { return resp } -func initCleanServerAndWSClient(t *testing.T) (*core.Blockchain, *Server, *websocket.Conn, chan []byte) { +func initCleanServerAndWSClient(t *testing.T, startNetworkServer ...bool) (*core.Blockchain, *Server, *websocket.Conn, chan []byte) { chain, rpcSrv, httpSrv := initClearServerWithInMemoryChain(t) dialer := websocket.Dialer{HandshakeTimeout: 5 * time.Second} @@ -72,10 +72,16 @@ func initCleanServerAndWSClient(t *testing.T) (*core.Blockchain, *Server, *webso finishedFlag := &atomic.Bool{} readerToExitCh := make(chan struct{}) go wsReader(t, ws, respMsgs, finishedFlag, readerToExitCh) + if len(startNetworkServer) != 0 && startNetworkServer[0] { + rpcSrv.coreServer.Start() + } t.Cleanup(func() { finishedFlag.Store(true) <-readerToExitCh ws.Close() + if len(startNetworkServer) != 0 && startNetworkServer[0] { + rpcSrv.coreServer.Shutdown() + } }) return chain, rpcSrv, ws, respMsgs } @@ -102,9 +108,7 @@ func TestSubscriptions(t *testing.T) { var subIDs = make([]string, 0) var subFeeds = []string{"block_added", "transaction_added", "notification_from_execution", "transaction_executed", "notary_request_event", "header_of_added_block"} - chain, rpcSrv, c, respMsgs := initCleanServerAndWSClient(t) - rpcSrv.coreServer.Start() - defer rpcSrv.coreServer.Shutdown() + chain, rpcSrv, c, respMsgs := initCleanServerAndWSClient(t, true) for _, feed := range subFeeds { s := callSubscribe(t, c, respMsgs, fmt.Sprintf(`["%s"]`, feed)) @@ -391,8 +395,7 @@ func TestFilteredNotaryRequestSubscriptions(t *testing.T) { }, } - chain, rpcSrv, c, respMsgs := initCleanServerAndWSClient(t) - rpcSrv.coreServer.Start() + chain, rpcSrv, c, respMsgs := initCleanServerAndWSClient(t, true) // blocks are needed to make GAS deposit for priv0 blocks := getTestBlocks(t)