From 46f2da0fb180f399897f3c44e70a0d815a34c78b Mon Sep 17 00:00:00 2001 From: Ekaterina Pavlova Date: Tue, 5 Mar 2024 16:03:42 +0300 Subject: [PATCH 1/2] rpcsrv: add close of blockchain and rpc server in tests The logs race issue is a result of improper shutdown during testing. Close #3217 Signed-off-by: Ekaterina Pavlova --- pkg/services/rpcsrv/client_test.go | 124 ++++++---------------- pkg/services/rpcsrv/server_helper_test.go | 2 + pkg/services/rpcsrv/server_test.go | 34 ++---- pkg/services/rpcsrv/subscription_test.go | 43 ++------ 4 files changed, 47 insertions(+), 156 deletions(-) diff --git a/pkg/services/rpcsrv/client_test.go b/pkg/services/rpcsrv/client_test.go index 1795f8d3d..f4273a885 100644 --- a/pkg/services/rpcsrv/client_test.go +++ b/pkg/services/rpcsrv/client_test.go @@ -65,9 +65,7 @@ import ( ) func TestClient_NEP17(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + _, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -109,9 +107,7 @@ func TestClient_NEP17(t *testing.T) { } func TestClientRoleManagement(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -161,9 +157,7 @@ func TestClientRoleManagement(t *testing.T) { } func TestClientPolicyContract(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -252,9 +246,7 @@ func TestClientPolicyContract(t *testing.T) { } func TestClientManagementContract(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -338,9 +330,7 @@ func TestClientManagementContract(t *testing.T) { } func TestClientNEOContract(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -467,9 +457,7 @@ func TestClientNEOContract(t *testing.T) { } func TestClientNotary(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -544,9 +532,7 @@ func TestClientNotary(t *testing.T) { } func TestCalculateNetworkFee_Base(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) const extraFee = 10 var nonce uint32 @@ -734,9 +720,7 @@ func TestCalculateNetworkFee_Base(t *testing.T) { } func TestCalculateNetworkFee(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) const extraFee = 10 c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) @@ -862,9 +846,7 @@ func TestCalculateNetworkFee(t *testing.T) { } func TestNotaryActor(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChainAndServices(t, false, true, false) - defer chain.Close() - defer rpcSrv.Shutdown() + _, _, httpSrv := initServerWithInMemoryChainAndServices(t, false, true, false) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -898,9 +880,7 @@ func TestGetRawNotaryPoolAndTransaction(t *testing.T) { tx1, tx2 *transaction.Transaction ) - chain, rpcSrv, httpSrv := initServerWithInMemoryChainAndServices(t, false, true, false) - defer chain.Close() - defer rpcSrv.Shutdown() + _, _, httpSrv := initServerWithInMemoryChainAndServices(t, false, true, false) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -1024,8 +1004,7 @@ func TestGetRawNotaryPoolAndTransaction(t *testing.T) { } func TestPing(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() + _, rpcSrv, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -1038,9 +1017,7 @@ func TestPing(t *testing.T) { } func TestCreateNEP17TransferTx(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -1115,9 +1092,7 @@ func TestCreateNEP17TransferTx(t *testing.T) { } func TestInvokeVerify(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -1207,9 +1182,7 @@ func TestInvokeVerify(t *testing.T) { } func TestClient_GetNativeContracts(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -1221,9 +1194,7 @@ func TestClient_GetNativeContracts(t *testing.T) { } func TestClient_NEP11_ND(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -1306,9 +1277,7 @@ func TestClient_NEP11_ND(t *testing.T) { } func TestClient_NEP11_D(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + _, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -1390,9 +1359,7 @@ func TestClient_NEP11_D(t *testing.T) { } func TestClient_NNS(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + _, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -1456,9 +1423,7 @@ func TestClient_NNS(t *testing.T) { } func TestClient_IteratorSessions(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + _, rpcSrv, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -1600,9 +1565,7 @@ func TestClient_IteratorSessions(t *testing.T) { } func TestClient_States(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -1627,9 +1590,7 @@ func TestClient_States(t *testing.T) { } func TestClientOracle(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -1700,9 +1661,8 @@ func TestClient_Iterator_SessionConfigVariations(t *testing.T) { } } t.Run("default sessions enabled", func(t *testing.T) { - chain, rpcSrv, httpSrv := initClearServerWithServices(t, false, false, false) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initClearServerWithServices(t, false, false, false) + for _, b := range getTestBlocks(t) { require.NoError(t, chain.AddBlock(b)) } @@ -1741,7 +1701,6 @@ func TestClient_Iterator_SessionConfigVariations(t *testing.T) { rpcSrv.Start() handler := http.HandlerFunc(rpcSrv.handleHTTPRequest) httpSrv := httptest.NewServer(handler) - defer chain.Close() defer rpcSrv.Shutdown() for _, b := range getTestBlocks(t) { require.NoError(t, chain.AddBlock(b)) @@ -1755,8 +1714,7 @@ func TestClient_Iterator_SessionConfigVariations(t *testing.T) { }) t.Run("sessions disabled", func(t *testing.T) { chain, rpcSrv, httpSrv := initClearServerWithServices(t, false, false, true) - defer chain.Close() - defer rpcSrv.Shutdown() + for _, b := range getTestBlocks(t) { require.NoError(t, chain.AddBlock(b)) } @@ -1785,9 +1743,7 @@ func TestClient_Iterator_SessionConfigVariations(t *testing.T) { } func TestClient_Wait(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) run := func(t *testing.T, ws bool) { acc, err := wallet.NewAccount() @@ -1904,8 +1860,6 @@ func TestSubClientWait(t *testing.T) { func testSubClientWait(t *testing.T, local bool) { chain, rpcSrv, httpSrv := initClearServerWithServices(t, false, false, true) - defer chain.Close() - defer rpcSrv.Shutdown() c := mkSubsClient(t, rpcSrv, httpSrv, local) acc, err := wallet.NewAccount() @@ -2007,8 +1961,6 @@ func TestSubClientWaitWithLateSubscription(t *testing.T) { func testSubClientWaitWithLateSubscription(t *testing.T, local bool) { chain, rpcSrv, httpSrv := initClearServerWithServices(t, false, false, true) - defer chain.Close() - defer rpcSrv.Shutdown() c := mkSubsClient(t, rpcSrv, httpSrv, local) acc, err := wallet.NewAccount() @@ -2038,11 +1990,9 @@ func testSubClientWaitWithLateSubscription(t *testing.T, local bool) { } func TestWSClientHandshakeError(t *testing.T) { - chain, rpcSrv, httpSrv := initClearServerWithCustomConfig(t, func(cfg *config.Config) { + _, _, httpSrv := initClearServerWithCustomConfig(t, func(cfg *config.Config) { cfg.ApplicationConfiguration.RPC.MaxWebSocketClients = -1 }) - defer chain.Close() - defer rpcSrv.Shutdown() url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws" _, err := rpcclient.NewWS(context.Background(), url, rpcclient.WSOptions{}) @@ -2055,8 +2005,6 @@ func TestSubClientWaitWithMissedEvent(t *testing.T) { func testSubClientWaitWithMissedEvent(t *testing.T, local bool) { chain, rpcSrv, httpSrv := initClearServerWithServices(t, false, false, true) - defer chain.Close() - defer rpcSrv.Shutdown() c := mkSubsClient(t, rpcSrv, httpSrv, local) acc, err := wallet.NewAccount() @@ -2137,8 +2085,6 @@ waitloop: // user side. func TestWSClient_SubscriptionsCompat(t *testing.T) { chain, rpcSrv, httpSrv := initClearServerWithServices(t, false, false, true) - defer chain.Close() - defer rpcSrv.Shutdown() c := mkSubsClient(t, rpcSrv, httpSrv, false) blocks := getTestBlocks(t) @@ -2254,9 +2200,7 @@ func TestWSClient_SubscriptionsCompat(t *testing.T) { } func TestActor_CallWithNilParam(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -2285,9 +2229,7 @@ func TestActor_CallWithNilParam(t *testing.T) { } func TestClient_FindStorage(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + _, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -2347,9 +2289,7 @@ func TestClient_FindStorage(t *testing.T) { } func TestClient_FindStorageHistoric(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -2413,9 +2353,7 @@ func TestClient_FindStorageHistoric(t *testing.T) { } func TestClient_GetStorageHistoric(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) @@ -2446,9 +2384,7 @@ func TestClient_GetStorageHistoric(t *testing.T) { } func TestClient_GetVersion_Hardforks(t *testing.T) { - chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() + _, _, httpSrv := initServerWithInMemoryChain(t) c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{}) require.NoError(t, err) diff --git a/pkg/services/rpcsrv/server_helper_test.go b/pkg/services/rpcsrv/server_helper_test.go index 1d4115935..46de624a5 100644 --- a/pkg/services/rpcsrv/server_helper_test.go +++ b/pkg/services/rpcsrv/server_helper_test.go @@ -83,6 +83,7 @@ func getUnitTestChainWithCustomConfig(t testing.TB, enableOracle bool, enableNot } go chain.Run() + t.Cleanup(chain.Close) return chain, orc, cfg, logger } @@ -125,6 +126,7 @@ func wrapUnitTestChain(t testing.TB, chain *core.Blockchain, orc OracleHandler, errCh := make(chan error, 2) rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, orc, logger, errCh) rpcServer.Start() + t.Cleanup(rpcServer.Shutdown) handler := http.HandlerFunc(rpcServer.handleHTTPRequest) srv := httptest.NewServer(handler) diff --git a/pkg/services/rpcsrv/server_test.go b/pkg/services/rpcsrv/server_test.go index 75b62f7a9..23a6f52ec 100644 --- a/pkg/services/rpcsrv/server_test.go +++ b/pkg/services/rpcsrv/server_test.go @@ -2235,19 +2235,15 @@ func TestSubmitOracle(t *testing.T) { rpc := `{"jsonrpc": "2.0", "id": 1, "method": "submitoracleresponse", "params": %s}` t.Run("OracleDisabled", func(t *testing.T) { - chain, rpcSrv, httpSrv := initClearServerWithCustomConfig(t, func(c *config.Config) { + _, _, httpSrv := initClearServerWithCustomConfig(t, func(c *config.Config) { c.ApplicationConfiguration.Oracle.Enabled = false }) - defer chain.Close() - defer rpcSrv.Shutdown() req := fmt.Sprintf(rpc, "[]") body := doRPCCallOverHTTP(req, httpSrv.URL, t) checkErrGetResult(t, body, true, neorpc.ErrOracleDisabledCode) }) - chain, rpcSrv, httpSrv := initClearServerWithServices(t, true, false, false) - defer chain.Close() - defer rpcSrv.Shutdown() + _, _, httpSrv := initClearServerWithServices(t, true, false, false) runCase := func(t *testing.T, fail bool, errCode int64, params ...string) func(t *testing.T) { return func(t *testing.T) { @@ -2283,11 +2279,9 @@ func TestNotaryRequestRPC(t *testing.T) { rpcTx := `{"jsonrpc": "2.0", "id": 1, "method": "getrawnotarytransaction", "params": ["%s", %d]}` t.Run("disabled P2PSigExtensions", func(t *testing.T) { - chain, rpcSrv, httpSrv := initClearServerWithCustomConfig(t, func(c *config.Config) { + _, _, httpSrv := initClearServerWithCustomConfig(t, func(c *config.Config) { c.ProtocolConfiguration.P2PSigExtensions = false }) - defer chain.Close() - defer rpcSrv.Shutdown() t.Run("submitnotaryrequest", func(t *testing.T) { body := doRPCCallOverHTTP(fmt.Sprintf(rpcSubmit, "[]"), httpSrv.URL, t) checkErrGetResult(t, body, true, neorpc.InternalServerErrorCode) @@ -2302,9 +2296,7 @@ func TestNotaryRequestRPC(t *testing.T) { }) }) - chain, rpcSrv, httpSrv := initServerWithInMemoryChainAndServices(t, false, true, false) - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, httpSrv := initServerWithInMemoryChainAndServices(t, false, true, false) submitNotaryRequest := func(t *testing.T, fail bool, errCode int64, params ...string) func(t *testing.T) { return func(t *testing.T) { @@ -2548,9 +2540,6 @@ func runTestCasesWithExecutor(t *testing.T, e *executor, rpcCall string, method func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []byte) { chain, rpcSrv, httpSrv := initServerWithInMemoryChain(t) - defer chain.Close() - defer rpcSrv.Shutdown() - e := &executor{chain: chain, httpSrv: httpSrv} t.Run("single request", func(t *testing.T) { rpc := `{"jsonrpc": "2.0", "id": 1, "method": "%s", "params": %s}` @@ -3336,13 +3325,10 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) [] checkErrGetResult(t, body, true, neorpc.ErrInvalidSizeCode) }) t.Run("mempool OOM", func(t *testing.T) { - chain, rpcSrv, httpSrv := initClearServerWithCustomConfig(t, func(c *config.Config) { + chain, _, httpSrv := initClearServerWithCustomConfig(t, func(c *config.Config) { c.ProtocolConfiguration.MemPoolSize = 1 }) - defer chain.Close() - defer rpcSrv.Shutdown() - // create and push the first (prioritized) transaction with increased networkFee tx := newTxWithParams(t, chain, opcode.PUSH1, 10, 1, 2, false) rawTx := encodeBinaryToString(t, tx) @@ -3357,13 +3343,10 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) [] }) }) t.Run("test functions with unsupported states", func(t *testing.T) { - chain, rpcSrv, httpSrv := initClearServerWithCustomConfig(t, func(c *config.Config) { + chain, _, httpSrv := initClearServerWithCustomConfig(t, func(c *config.Config) { c.ApplicationConfiguration.Ledger.KeepOnlyLatestState = true }) - defer chain.Close() - defer rpcSrv.Shutdown() - e := &executor{chain: chain, httpSrv: httpSrv} rpc := `{"jsonrpc": "2.0", "id": 1, "method": "%s", "params": %s}` for method, cases := range rpcFunctionsWithUnsupportedStatesTestCases { @@ -4100,7 +4083,6 @@ func BenchmarkHandleIn(b *testing.B) { server, err := network.NewServer(serverConfig, chain, chain.GetStateSyncModule(), logger) require.NoError(b, err) rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, orc, logger, make(chan error)) - defer chain.Close() do := func(b *testing.B, req []byte) { b.ReportAllocs() @@ -4153,9 +4135,7 @@ func TestFailedPreconditionShutdown(t *testing.T) { } func TestErrorResponseContentType(t *testing.T) { - chain, rpcSrv, httpSrv := initClearServerWithServices(t, true, false, false) - defer chain.Close() - defer rpcSrv.Shutdown() + _, _, httpSrv := initClearServerWithServices(t, true, false, false) const ( expectedContentType = "application/json; charset=utf-8" diff --git a/pkg/services/rpcsrv/subscription_test.go b/pkg/services/rpcsrv/subscription_test.go index d5a291903..316699c23 100644 --- a/pkg/services/rpcsrv/subscription_test.go +++ b/pkg/services/rpcsrv/subscription_test.go @@ -96,8 +96,6 @@ func TestSubscriptions(t *testing.T) { var subFeeds = []string{"block_added", "transaction_added", "notification_from_execution", "transaction_executed", "notary_request_event", "header_of_added_block"} chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) - defer chain.Close() - defer rpcSrv.Shutdown() rpcSrv.coreServer.Start() defer rpcSrv.coreServer.Shutdown() @@ -295,10 +293,7 @@ func TestFilteredSubscriptions(t *testing.T) { for name, this := range cases { t.Run(name, func(t *testing.T) { - chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) - - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) // It's used as an end-of-event-stream, so it's always present. blockSubID := callSubscribe(t, c, respMsgs, `["block_added"]`) @@ -397,9 +392,6 @@ func TestFilteredNotaryRequestSubscriptions(t *testing.T) { chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) rpcSrv.coreServer.Start() - defer chain.Close() - defer rpcSrv.Shutdown() - // blocks are needed to make GAS deposit for priv0 blocks := getTestBlocks(t) for _, b := range blocks { @@ -437,10 +429,7 @@ func TestFilteredBlockSubscriptions(t *testing.T) { // We can't fit this into TestFilteredSubscriptions, because it uses // blocks as EOF events to wait for. const numBlocks = 10 - chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) - - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) blockSubID := callSubscribe(t, c, respMsgs, `["block_added", {"primary":3}]`) @@ -475,10 +464,7 @@ func TestFilteredBlockSubscriptions(t *testing.T) { func TestHeaderOfAddedBlockSubscriptions(t *testing.T) { const numBlocks = 10 - chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) - - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) headerSubID := callSubscribe(t, c, respMsgs, `["header_of_added_block", {"primary":3}]`) @@ -513,10 +499,7 @@ func TestHeaderOfAddedBlockSubscriptions(t *testing.T) { func TestMaxSubscriptions(t *testing.T) { var subIDs = make([]string, 0) - chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) - - defer chain.Close() - defer rpcSrv.Shutdown() + _, _, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) for i := 0; i < maxFeeds+1; i++ { var s string @@ -559,10 +542,7 @@ func TestBadSubUnsub(t *testing.T) { "bad id": `{"jsonrpc": "2.0", "method": "unsubscribe", "params": ["vasiliy"], "id": 1}`, "not subscribed id": `{"jsonrpc": "2.0", "method": "unsubscribe", "params": ["7"], "id": 1}`, } - chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) - - defer chain.Close() - defer rpcSrv.Shutdown() + _, _, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) testF := func(t *testing.T, cases map[string]string) func(t *testing.T) { return func(t *testing.T) { @@ -602,11 +582,9 @@ func TestWSClientsLimit(t *testing.T) { effectiveClients = 0 } t.Run(tname, func(t *testing.T) { - chain, rpcSrv, httpSrv := initClearServerWithCustomConfig(t, func(cfg *config.Config) { + _, _, httpSrv := initClearServerWithCustomConfig(t, func(cfg *config.Config) { cfg.ApplicationConfiguration.RPC.MaxWebSocketClients = limit }) - defer chain.Close() - defer rpcSrv.Shutdown() dialer := websocket.Dialer{HandshakeTimeout: time.Second} url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws" @@ -647,10 +625,7 @@ func TestSubscriptionOverflow(t *testing.T) { const blockCnt = notificationBufSize * 5 var receivedMiss bool - chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) - - defer chain.Close() - defer rpcSrv.Shutdown() + chain, _, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) resp := callWSGetRaw(t, c, `{"jsonrpc": "2.0","method": "subscribe","params": ["block_added"],"id": 1}`, respMsgs) require.Nil(t, resp.Error) @@ -688,9 +663,7 @@ func TestFilteredSubscriptions_InvalidFilter(t *testing.T) { params: `["transaction_executed", {"state":"NOTHALT"}]`, }, } - chain, rpcSrv, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) - defer chain.Close() - defer rpcSrv.Shutdown() + _, _, c, respMsgs, finishedFlag := initCleanServerAndWSClient(t) for name, this := range cases { t.Run(name, func(t *testing.T) { From ee39b5ca18c651ef8ea2c6ed94ab1c0266e3669e Mon Sep 17 00:00:00 2001 From: Ekaterina Pavlova Date: Tue, 5 Mar 2024 17:43:03 +0300 Subject: [PATCH 2/2] rpcsrv: add close of http server in tests The HTTP server should be closed at the end of the test. Signed-off-by: Ekaterina Pavlova --- pkg/rpcclient/wsclient_test.go | 3 +++ pkg/services/rpcsrv/client_test.go | 1 + pkg/services/rpcsrv/server_helper_test.go | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/rpcclient/wsclient_test.go b/pkg/rpcclient/wsclient_test.go index 693783e79..1c2eabebb 100644 --- a/pkg/rpcclient/wsclient_test.go +++ b/pkg/rpcclient/wsclient_test.go @@ -172,6 +172,7 @@ func TestWSClientEvents(t *testing.T) { return } })) + t.Cleanup(srv.Close) wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{}) require.NoError(t, err) wsc.getNextRequestID = getTestRequestID @@ -314,6 +315,7 @@ func TestWSClientNonBlockingEvents(t *testing.T) { return } })) + t.Cleanup(srv.Close) wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{CloseNotificationChannelIfFull: true}) require.NoError(t, err) wsc.getNextRequestID = getTestRequestID @@ -744,6 +746,7 @@ func TestWSFilteredSubscriptions(t *testing.T) { ws.Close() } })) + t.Cleanup(srv.Close) wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{}) require.NoError(t, err) wsc.getNextRequestID = getTestRequestID diff --git a/pkg/services/rpcsrv/client_test.go b/pkg/services/rpcsrv/client_test.go index f4273a885..cdc2f80da 100644 --- a/pkg/services/rpcsrv/client_test.go +++ b/pkg/services/rpcsrv/client_test.go @@ -1701,6 +1701,7 @@ func TestClient_Iterator_SessionConfigVariations(t *testing.T) { rpcSrv.Start() handler := http.HandlerFunc(rpcSrv.handleHTTPRequest) httpSrv := httptest.NewServer(handler) + t.Cleanup(httpSrv.Close) defer rpcSrv.Shutdown() for _, b := range getTestBlocks(t) { require.NoError(t, chain.AddBlock(b)) diff --git a/pkg/services/rpcsrv/server_helper_test.go b/pkg/services/rpcsrv/server_helper_test.go index 46de624a5..3595bacd6 100644 --- a/pkg/services/rpcsrv/server_helper_test.go +++ b/pkg/services/rpcsrv/server_helper_test.go @@ -130,7 +130,7 @@ func wrapUnitTestChain(t testing.TB, chain *core.Blockchain, orc OracleHandler, handler := http.HandlerFunc(rpcServer.handleHTTPRequest) srv := httptest.NewServer(handler) - + t.Cleanup(srv.Close) return chain, &rpcServer, srv }