From 41f98798168bc14610cad077b505e7951b8e6070 Mon Sep 17 00:00:00 2001 From: Ekaterina Pavlova Date: Thu, 15 Feb 2024 17:43:36 +0300 Subject: [PATCH] services: fix Signed-off-by: Ekaterina Pavlova --- pkg/network/server.go | 5 +++- pkg/network/server_test.go | 52 +++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/pkg/network/server.go b/pkg/network/server.go index ba8e99edde..d657687f07 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -320,7 +320,10 @@ func (s *Server) Shutdown() { close(s.quit) <-s.relayFin - _ = s.log.Sync() + err := s.log.Sync() + if err != nil { + s.log.Info("shutting down logs") + } } // AddService allows to add a service to be started/stopped by Server. diff --git a/pkg/network/server_test.go b/pkg/network/server_test.go index 2ca95885fa..26b5ecdbcc 100644 --- a/pkg/network/server_test.go +++ b/pkg/network/server_test.go @@ -133,7 +133,8 @@ func TestServerRegisterPeer(t *testing.T) { ps[i].version = &payload.Version{Nonce: uint32(i), UserAgent: []byte("fake")} } - startWithCleanup(t, s) + go s.Start() + defer s.Shutdown() s.register <- ps[0] require.Eventually(t, func() bool { return 1 == s.PeerCount() }, time.Second, time.Millisecond*10) @@ -384,19 +385,13 @@ func startTestServer(t *testing.T, protocolCfg ...func(*config.Blockchain)) *Ser } else { s = newTestServer(t, srvCfg) } - startWithCleanup(t, s) return s } -func startWithCleanup(t *testing.T, s *Server) { - go s.Start() - t.Cleanup(func() { - s.Shutdown() - }) -} - func TestBlock(t *testing.T) { s := startTestServer(t) + go s.Start() + defer s.Shutdown() s.chain.(*fakechain.FakeChain).Blockheight.Store(12344) require.Equal(t, uint32(12344), s.chain.BlockHeight()) @@ -411,7 +406,8 @@ func TestConsensus(t *testing.T) { s := newTestServer(t, ServerConfig{}) cons := new(fakeConsensus) s.AddConsensusService(cons, cons.OnPayload, cons.OnTransaction) - startWithCleanup(t, s) + go s.Start() + defer s.Shutdown() s.chain.(*fakechain.FakeChain).Blockheight.Store(4) p := newLocalPeer(t, s) @@ -456,7 +452,8 @@ func TestTransaction(t *testing.T) { s := newTestServer(t, ServerConfig{}) cons := new(fakeConsensus) s.AddConsensusService(cons, cons.OnPayload, cons.OnTransaction) - startWithCleanup(t, s) + go s.Start() + defer s.Shutdown() t.Run("good", func(t *testing.T) { tx := newDummyTx() @@ -529,6 +526,8 @@ func (s *Server) testHandleGetData(t *testing.T, invType payload.InventoryType, func TestGetData(t *testing.T) { s := startTestServer(t) + go s.Start() + defer s.Shutdown() s.chain.(*fakechain.FakeChain).UtilityTokenBalance = big.NewInt(1000000) t.Run("block", func(t *testing.T) { @@ -598,6 +597,8 @@ func initGetBlocksTest(t *testing.T) (*Server, []*block.Block) { func TestGetBlocks(t *testing.T) { s, blocks := initGetBlocksTest(t) + go s.Start() + defer s.Shutdown() expected := make([]util.Uint256, len(blocks)) for i := range blocks { @@ -628,6 +629,8 @@ func TestGetBlocks(t *testing.T) { func TestGetBlockByIndex(t *testing.T) { s, blocks := initGetBlocksTest(t) + go s.Start() + defer s.Shutdown() var expected []*block.Block var actual []*block.Block @@ -662,6 +665,8 @@ func TestGetBlockByIndex(t *testing.T) { func TestGetHeaders(t *testing.T) { s, blocks := initGetBlocksTest(t) + go s.Start() + defer s.Shutdown() expected := make([]*block.Header, len(blocks)) for i := range blocks { @@ -704,6 +709,8 @@ func TestGetHeaders(t *testing.T) { func TestInv(t *testing.T) { s := startTestServer(t) + go s.Start() + defer s.Shutdown() s.chain.(*fakechain.FakeChain).UtilityTokenBalance = big.NewInt(10000000) var actual []util.Uint256 @@ -769,6 +776,8 @@ func TestInv(t *testing.T) { func TestHandleGetMPTData(t *testing.T) { t.Run("P2PStateExchange extensions off", func(t *testing.T) { s := startTestServer(t) + go s.Start() + defer s.Shutdown() p := newLocalPeer(t, s) p.handshaked = 1 msg := NewMessage(CMDGetMPTData, &payload.MPTInventory{ @@ -778,6 +787,8 @@ func TestHandleGetMPTData(t *testing.T) { }) check := func(t *testing.T, s *Server) { + go s.Start() + defer s.Shutdown() var recvResponse atomic.Bool r1 := random.Uint256() r2 := random.Uint256() @@ -826,6 +837,8 @@ func TestHandleGetMPTData(t *testing.T) { func TestHandleMPTData(t *testing.T) { t.Run("P2PStateExchange extensions off", func(t *testing.T) { s := startTestServer(t) + go s.Start() + defer s.Shutdown() p := newLocalPeer(t, s) p.handshaked = 1 msg := NewMessage(CMDMPTData, &payload.MPTData{ @@ -844,7 +857,8 @@ func TestHandleMPTData(t *testing.T) { return nil }, } - startWithCleanup(t, s) + go s.Start() + defer s.Shutdown() p := newLocalPeer(t, s) p.handshaked = 1 @@ -857,6 +871,8 @@ func TestHandleMPTData(t *testing.T) { func TestRequestMPTNodes(t *testing.T) { s := startTestServer(t) + go s.Start() + defer s.Shutdown() var actual []util.Uint256 p := newLocalPeer(t, s) @@ -902,6 +918,8 @@ func TestRequestMPTNodes(t *testing.T) { func TestRequestTx(t *testing.T) { s := startTestServer(t) + go s.Start() + defer s.Shutdown() var actual []util.Uint256 p := newLocalPeer(t, s) @@ -947,6 +965,8 @@ func TestRequestTx(t *testing.T) { func TestAddrs(t *testing.T) { s := startTestServer(t) + go s.Start() + defer s.Shutdown() ips := make([][16]byte, 4) copy(ips[0][:], net.IPv4(1, 2, 3, 4)) @@ -1004,6 +1024,8 @@ func (f feerStub) GetBaseExecFee() int64 { return interop func TestMemPool(t *testing.T) { s := startTestServer(t) + go s.Start() + defer s.Shutdown() var actual []util.Uint256 p := newLocalPeer(t, s) @@ -1081,20 +1103,25 @@ func TestVerifyNotaryRequest(t *testing.T) { func TestTryInitStateSync(t *testing.T) { t.Run("module inactive", func(t *testing.T) { s := startTestServer(t) + go s.Start() s.tryInitStateSync() + s.Shutdown() }) t.Run("module already initialized", func(t *testing.T) { s := startTestServer(t) + go s.Start() ss := &fakechain.FakeStateSync{} ss.IsActiveFlag.Store(true) ss.IsInitializedFlag.Store(true) s.stateSync = ss s.tryInitStateSync() + s.Shutdown() }) t.Run("good", func(t *testing.T) { s := startTestServer(t) + go s.Start() for _, h := range []uint32{10, 8, 7, 4, 11, 4} { p := newLocalPeer(t, s) p.handshaked = 1 @@ -1117,6 +1144,7 @@ func TestTryInitStateSync(t *testing.T) { ss.IsActiveFlag.Store(true) s.stateSync = ss s.tryInitStateSync() + s.Shutdown() }) }