Skip to content

Commit

Permalink
services: fix
Browse files Browse the repository at this point in the history
Signed-off-by: Ekaterina Pavlova <[email protected]>
  • Loading branch information
AliceInHunterland committed Feb 15, 2024
1 parent ea23772 commit 41f9879
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
5 changes: 4 additions & 1 deletion pkg/network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Check warning on line 326 in pkg/network/server.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/server.go#L325-L326

Added lines #L325 - L326 were not covered by tests
}

// AddService allows to add a service to be started/stopped by Server.
Expand Down
52 changes: 40 additions & 12 deletions pkg/network/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand All @@ -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()
Expand Down Expand Up @@ -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{
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -1117,6 +1144,7 @@ func TestTryInitStateSync(t *testing.T) {
ss.IsActiveFlag.Store(true)
s.stateSync = ss
s.tryInitStateSync()
s.Shutdown()
})
}

Expand Down

0 comments on commit 41f9879

Please sign in to comment.