Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace time.After to time.NewTimer #181

Merged
merged 2 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion blockchain/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
4 changes: 3 additions & 1 deletion command/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion consensus/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
}()

Expand Down
8 changes: 5 additions & 3 deletions consensus/ibft/ibft.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions consensus/ibft/ibft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}()

Expand Down Expand Up @@ -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)
}()

Expand Down Expand Up @@ -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)
}()

Expand Down
4 changes: 3 additions & 1 deletion jsonrpc/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
})
Expand Down
25 changes: 16 additions & 9 deletions network/dial/dial_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions network/gossip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,13 @@ 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.Reset(10 * time.Second)

select {
case <-time.After(10 * time.Second):
case <-delay.C:
case <-s.closeCh:
return
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/sync_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion protocol/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
10 changes: 8 additions & 2 deletions txpool/event_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ func TestEventManager_SignalEvent(t *testing.T) {
supportedEventsProcessed := 0

completed := false
delay := time.NewTimer(5 * time.Second)

for !completed {
delay.Reset(5 * time.Second)

select {
case event := <-subscription.subscriptionChannel:
eventsProcessed++
Expand All @@ -124,7 +128,7 @@ func TestEventManager_SignalEvent(t *testing.T) {
supportedEventsProcessed == validEvents {
completed = true
}
case <-time.After(time.Second * 5):
case <-delay.C:
completed = true
}
}
Expand Down Expand Up @@ -157,6 +161,8 @@ func TestEventManager_SignalEventOrder(t *testing.T) {

wg.Add(totalEvents)

timeoutDelay := time.NewTimer(5 * time.Second)

go func() {
for {
select {
Expand All @@ -168,7 +174,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()
}
Expand Down