From d887d5cb7f8c63be431e01f3728d9e755321543b Mon Sep 17 00:00:00 2001 From: 0xcb9ff9 <0xcb9ff9@proton.me> Date: Wed, 21 Sep 2022 11:15:57 +0800 Subject: [PATCH 1/2] replace time.After to time.NewTimer --- blockchain/subscription_test.go | 4 +++- command/helper/helper.go | 4 +++- consensus/dev/dev.go | 4 +++- consensus/ibft/ibft.go | 8 +++++--- consensus/ibft/ibft_test.go | 12 ++++++------ jsonrpc/dispatcher_test.go | 4 +++- network/dial/dial_queue_test.go | 25 ++++++++++++++++--------- network/gossip_test.go | 9 +++++++-- network/server.go | 4 +++- protocol/sync_peer.go | 4 ++-- protocol/testing.go | 4 +++- txpool/event_manager_test.go | 8 ++++++-- 12 files changed, 60 insertions(+), 30 deletions(-) diff --git a/blockchain/subscription_test.go b/blockchain/subscription_test.go index c653e2f801..ee546cbeda 100644 --- a/blockchain/subscription_test.go +++ b/blockchain/subscription_test.go @@ -34,13 +34,15 @@ func TestSubscriptionLinear(t *testing.T) { evnt.AddNewHeader(&types.Header{Number: uint64(i)}) e.push(evnt) + timeoutDelay := time.NewTimer(1 * time.Second) + // it should fire updateCh select { case evnt := <-eventCh: if evnt.NewChain[0].Number != uint64(i) { t.Fatal("bad") } - case <-time.After(1 * time.Second): + case <-timeoutDelay.C: t.Fatal("timeout") } } diff --git a/command/helper/helper.go b/command/helper/helper.go index 16f914ca30..37ad3d3d00 100644 --- a/command/helper/helper.go +++ b/command/helper/helper.go @@ -68,10 +68,12 @@ func HandleSignals( close(gracefulCh) }() + timeoutDelay := time.NewTimer(5 * time.Second) + select { case <-signalCh: return errors.New("shutdown by signal channel") - case <-time.After(5 * time.Second): + case <-timeoutDelay.C: return errors.New("shutdown by timeout") case <-gracefulCh: return nil diff --git a/consensus/dev/dev.go b/consensus/dev/dev.go index dfd7d0dec2..546094a27d 100644 --- a/consensus/dev/dev.go +++ b/consensus/dev/dev.go @@ -74,8 +74,10 @@ func (d *Dev) nextNotify() chan struct{} { d.interval = 1 } + delay := time.NewTimer(time.Duration(d.interval) * time.Second) + go func() { - <-time.After(time.Duration(d.interval) * time.Second) + <-delay.C d.notifyCh <- struct{}{} }() diff --git a/consensus/ibft/ibft.go b/consensus/ibft/ibft.go index 81dac2ba70..7b65e944d5 100644 --- a/consensus/ibft/ibft.go +++ b/consensus/ibft/ibft.go @@ -825,8 +825,10 @@ func (i *Ibft) runAcceptState() { // start new round // calculate how much time do we have to wait to mine the block delay := time.Until(time.Unix(int64(i.state.block.Header.Timestamp), 0)) + delayTimer := time.NewTimer(delay) + select { - case <-time.After(delay): + case <-delayTimer.C: case <-i.closeCh: return } @@ -1435,7 +1437,7 @@ func (i *Ibft) Close() error { // getNextMessage reads a new message from the message queue func (i *Ibft) getNextMessage(timeout time.Duration) (*proto.MessageReq, bool) { - timeoutCh := time.After(timeout) + timeoutCh := time.NewTimer(timeout) for { msg := i.msgQueue.readMessage(i.getState(), i.state.view) @@ -1452,7 +1454,7 @@ func (i *Ibft) getNextMessage(timeout time.Duration) (*proto.MessageReq, bool) { // wait until there is a new message or // someone closes the stopCh (i.e. timeout for round change) select { - case <-timeoutCh: + case <-timeoutCh.C: i.logger.Info("unable to read new message from the message queue", "timeout expired", timeout) return nil, true diff --git a/consensus/ibft/ibft_test.go b/consensus/ibft/ibft_test.go index 6ef87f45af..d65de56dec 100644 --- a/consensus/ibft/ibft_test.go +++ b/consensus/ibft/ibft_test.go @@ -873,10 +873,10 @@ func TestRunSyncState_NewHeadReceivedFromPeer_CallsTxPoolResetWithHeaders(t *tes m.txpool = mockTxPool // we need to change state from Sync in order to break from the loop inside runSyncState - stateChangeDelay := time.After(100 * time.Millisecond) + stateChangeDelay := time.NewTimer(100 * time.Millisecond) go func() { - <-stateChangeDelay + <-stateChangeDelay.C m.setState(AcceptState) }() @@ -904,10 +904,10 @@ func TestRunSyncState_BulkSyncWithPeer_CallsTxPoolResetWithHeaders(t *testing.T) m.txpool = mockTxPool // we need to change state from Sync in order to break from the loop inside runSyncState - stateChangeDelay := time.After(100 * time.Millisecond) + stateChangeDelay := time.NewTimer(100 * time.Millisecond) go func() { - <-stateChangeDelay + <-stateChangeDelay.C m.setState(AcceptState) }() @@ -949,10 +949,10 @@ func TestRunSyncState_Unlock_After_Sync(t *testing.T) { m.txpool = &mockTxPool{} // we need to change state from Sync in order to break from the loop inside runSyncState - stateChangeDelay := time.After(100 * time.Millisecond) + stateChangeDelay := time.NewTimer(100 * time.Millisecond) go func() { - <-stateChangeDelay + <-stateChangeDelay.C m.setState(AcceptState) }() diff --git a/jsonrpc/dispatcher_test.go b/jsonrpc/dispatcher_test.go index 8d98d1030b..da253410fe 100644 --- a/jsonrpc/dispatcher_test.go +++ b/jsonrpc/dispatcher_test.go @@ -82,9 +82,11 @@ func TestDispatcher_HandleWebsocketConnection_EthSubscribe(t *testing.T) { }, }) + delayTimer := time.NewTimer(2 * time.Second) + select { case <-mockConnection.msgCh: - case <-time.After(2 * time.Second): + case <-delayTimer.C: t.Fatal("\"newHeads\" event not received in 2 seconds") } }) diff --git a/network/dial/dial_queue_test.go b/network/dial/dial_queue_test.go index 50cb9428d2..15c14ec5bf 100644 --- a/network/dial/dial_queue_test.go +++ b/network/dial/dial_queue_test.go @@ -36,19 +36,26 @@ func TestDialQueue(t *testing.T) { done <- struct{}{} }() - // we should not get any peer now - select { - case <-done: - t.Fatal("not expected") - case <-time.After(1 * time.Second): + { + delay := time.NewTimer(1 * time.Second) + // we should not get any peer now + select { + case <-done: + t.Fatal("not expected") + case <-delay.C: + } } q.AddTask(info0, 1) - select { - case <-done: - case <-time.After(1 * time.Second): - t.Fatal("timeout") + { + delay := time.NewTimer(1 * time.Second) + + select { + case <-done: + case <-delay.C: + t.Fatal("timeout") + } } } diff --git a/network/gossip_test.go b/network/gossip_test.go index bf3db2d4fe..7e745ec097 100644 --- a/network/gossip_test.go +++ b/network/gossip_test.go @@ -19,10 +19,13 @@ func WaitForSubscribers(ctx context.Context, srv *Server, topic string, expected if n := NumSubscribers(srv, topic); n >= expectedNumPeers { return nil } + + delay := time.NewTimer(100 * time.Millisecond) + select { case <-ctx.Done(): return errors.New("canceled") - case <-time.After(100 * time.Millisecond): + case <-delay.C: continue } } @@ -93,8 +96,10 @@ func TestSimpleGossip(t *testing.T) { messagesGossiped := 0 for { + delay := time.NewTimer(15 * time.Second) + select { - case <-time.After(time.Second * 15): + case <-delay.C: t.Fatalf("Gossip messages not received before timeout") case message := <-messageCh: if message.Message == sentMessage { diff --git a/network/server.go b/network/server.go index d43fa253b0..61e7179551 100644 --- a/network/server.go +++ b/network/server.go @@ -324,8 +324,10 @@ func (s *Server) setupBootnodes() error { // checkPeerCount will attempt to make new connections if the active peer count is lesser than the specified limit. func (s *Server) checkPeerConnections() { for { + delay := time.NewTimer(10 * time.Second) + select { - case <-time.After(10 * time.Second): + case <-delay.C: case <-s.closeCh: return } diff --git a/protocol/sync_peer.go b/protocol/sync_peer.go index 321abada1d..2d5985c9a3 100644 --- a/protocol/sync_peer.go +++ b/protocol/sync_peer.go @@ -117,7 +117,7 @@ func (s *SyncPeer) purgeBlocks(lastSeen types.Hash) uint64 { // popBlock pops a block from the block queue [BLOCKING] func (s *SyncPeer) popBlock(timeout time.Duration) (b *types.Block, err error) { - timeoutCh := time.After(timeout) + timeoutCh := time.NewTimer(timeout) for { if !s.IsClosed() { @@ -132,7 +132,7 @@ func (s *SyncPeer) popBlock(timeout time.Duration) (b *types.Block, err error) { s.enqueueLock.Unlock() select { case <-s.enqueueCh: - case <-timeoutCh: + case <-timeoutCh.C: return nil, ErrPopTimeout } } else { diff --git a/protocol/testing.go b/protocol/testing.go index a8dfa38f47..b98e6f8ae5 100644 --- a/protocol/testing.go +++ b/protocol/testing.go @@ -205,10 +205,12 @@ func TryPopBlock(t *testing.T, syncer *Syncer, peerID peer.ID, timeout time.Dura } }() + delay := time.NewTimer(timeout) + select { case block := <-blockCh: return block, true - case <-time.After(timeout): + case <-delay.C: return nil, false } } diff --git a/txpool/event_manager_test.go b/txpool/event_manager_test.go index e286f69a36..52bb7831fe 100644 --- a/txpool/event_manager_test.go +++ b/txpool/event_manager_test.go @@ -112,6 +112,8 @@ func TestEventManager_SignalEvent(t *testing.T) { completed := false for !completed { + delay := time.NewTimer(5 * time.Second) + select { case event := <-subscription.subscriptionChannel: eventsProcessed++ @@ -124,7 +126,7 @@ func TestEventManager_SignalEvent(t *testing.T) { supportedEventsProcessed == validEvents { completed = true } - case <-time.After(time.Second * 5): + case <-delay.C: completed = true } } @@ -157,6 +159,8 @@ func TestEventManager_SignalEventOrder(t *testing.T) { wg.Add(totalEvents) + timeoutDelay := time.NewTimer(5 * time.Second) + go func() { for { select { @@ -168,7 +172,7 @@ func TestEventManager_SignalEventOrder(t *testing.T) { wg.Done() } - case <-time.After(time.Second * 5): + case <-timeoutDelay.C: for i := 0; i < totalEvents-eventsProcessed; i++ { wg.Done() } From 085bf03389972ef85c3f79760875d1df452e130d Mon Sep 17 00:00:00 2001 From: 0xcb9ff9 <0xcb9ff9@proton.me> Date: Wed, 21 Sep 2022 12:23:56 +0800 Subject: [PATCH 2/2] using one timer --- network/server.go | 4 +++- txpool/event_manager_test.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/network/server.go b/network/server.go index 61e7179551..8a9f8a4867 100644 --- a/network/server.go +++ b/network/server.go @@ -323,8 +323,10 @@ func (s *Server) setupBootnodes() error { // checkPeerCount will attempt to make new connections if the active peer count is lesser than the specified limit. func (s *Server) checkPeerConnections() { + delay := time.NewTimer(10 * time.Second) + for { - delay := time.NewTimer(10 * time.Second) + delay.Reset(10 * time.Second) select { case <-delay.C: diff --git a/txpool/event_manager_test.go b/txpool/event_manager_test.go index 52bb7831fe..a3d2e711fb 100644 --- a/txpool/event_manager_test.go +++ b/txpool/event_manager_test.go @@ -111,8 +111,10 @@ func TestEventManager_SignalEvent(t *testing.T) { supportedEventsProcessed := 0 completed := false + delay := time.NewTimer(5 * time.Second) + for !completed { - delay := time.NewTimer(5 * time.Second) + delay.Reset(5 * time.Second) select { case event := <-subscription.subscriptionChannel: