diff --git a/internal/conv/string_test.go b/internal/conv/string_test.go index e6f24ec7b9..70f51924cc 100644 --- a/internal/conv/string_test.go +++ b/internal/conv/string_test.go @@ -25,7 +25,9 @@ func (s *StringSuite) TestUnsafeStrToBytes() { for range 5 { b := unsafeConvertStr() runtime.GC() - <-time.NewTimer(2 * time.Millisecond).C + timer := time.NewTimer(2 * time.Millisecond) + <-timer.C + timer.Stop() b2 := append(b, 'd') s.Equal("abc", string(b)) s.Equal("abcd", string(b2)) @@ -42,7 +44,9 @@ func (s *StringSuite) TestUnsafeBytesToStr() { for range 5 { str := unsafeConvertBytes() runtime.GC() - <-time.NewTimer(2 * time.Millisecond).C + timer := time.NewTimer(2 * time.Millisecond) + <-timer.C + timer.Stop() s.Equal("abc", str) } } diff --git a/store/internal/conv/string_test.go b/store/internal/conv/string_test.go index 3a14517531..4bdba63565 100644 --- a/store/internal/conv/string_test.go +++ b/store/internal/conv/string_test.go @@ -25,7 +25,9 @@ func (s *StringSuite) TestUnsafeStrToBytes() { for i := 0; i < 5; i++ { b := unsafeConvertStr() runtime.GC() - <-time.NewTimer(2 * time.Millisecond).C + timer := time.NewTimer(2 * time.Millisecond) + <-timer.C + timer.Stop() b2 := append(b, 'd') s.Equal("abc", string(b)) s.Equal("abcd", string(b2)) @@ -42,7 +44,9 @@ func (s *StringSuite) TestUnsafeBytesToStr() { for i := 0; i < 5; i++ { str := unsafeConvertBytes() runtime.GC() - <-time.NewTimer(2 * time.Millisecond).C + timer := time.NewTimer(2 * time.Millisecond) + <-timer.C + timer.Stop() s.Equal("abc", str) } } diff --git a/systemtests/system.go b/systemtests/system.go index dcb66a07c2..a921157147 100644 --- a/systemtests/system.go +++ b/systemtests/system.go @@ -314,11 +314,13 @@ func (s *SystemUnderTest) AwaitNodeUp(t *testing.T, rpcAddr string) { started <- struct{}{} } }() + timer := time.NewTimer(timeout) + defer timer.Stop() select { case <-started: case <-ctx.Done(): require.NoError(t, ctx.Err()) - case <-time.NewTimer(timeout).C: + case <-timer.C: t.Fatalf("timeout waiting for node start: %s", timeout) } } @@ -403,7 +405,9 @@ func (s *SystemUnderTest) AwaitBlockHeight(t *testing.T, targetHeight int64, tim } else { maxWaitTime = time.Duration(targetHeight-s.currentHeight.Load()+4) * s.blockTime } - abort := time.NewTimer(maxWaitTime).C + timer := time.NewTimer(maxWaitTime) + defer timer.Stop() + abort := timer.C for { select { case <-abort: @@ -433,10 +437,12 @@ func (s *SystemUnderTest) AwaitNextBlock(t *testing.T, timeout ...time.Duration) done <- s.currentHeight.Load() close(done) }() + timer := time.NewTimer(maxWaitTime) + defer timer.Stop() select { case v := <-done: return v - case <-time.NewTimer(maxWaitTime).C: + case <-timer.C: t.Fatalf("Timeout - no block within %s", maxWaitTime) return -1 } @@ -907,6 +913,7 @@ func TimeoutConsumer(t *testing.T, maxWaitTime time.Duration, next EventConsumer ctx, done := context.WithCancel(context.Background()) t.Cleanup(done) timeout := time.NewTimer(maxWaitTime) + t.Cleanup(func() { timeout.Stop() }) timedOut := make(chan struct{}, 1) go func() { select { @@ -922,6 +929,13 @@ func TimeoutConsumer(t *testing.T, maxWaitTime time.Duration, next EventConsumer t.Fatalf("Timeout waiting for new events %s", maxWaitTime) return false default: + if !timeout.Stop() { + // Drain the channel if the timer already fired + select { + case <-timeout.C: + default: + } + } timeout.Reset(maxWaitTime) result := next(e) if !result { diff --git a/x/nft/internal/conv/string_test.go b/x/nft/internal/conv/string_test.go index 3a14517531..4bdba63565 100644 --- a/x/nft/internal/conv/string_test.go +++ b/x/nft/internal/conv/string_test.go @@ -25,7 +25,9 @@ func (s *StringSuite) TestUnsafeStrToBytes() { for i := 0; i < 5; i++ { b := unsafeConvertStr() runtime.GC() - <-time.NewTimer(2 * time.Millisecond).C + timer := time.NewTimer(2 * time.Millisecond) + <-timer.C + timer.Stop() b2 := append(b, 'd') s.Equal("abc", string(b)) s.Equal("abcd", string(b2)) @@ -42,7 +44,9 @@ func (s *StringSuite) TestUnsafeBytesToStr() { for i := 0; i < 5; i++ { str := unsafeConvertBytes() runtime.GC() - <-time.NewTimer(2 * time.Millisecond).C + timer := time.NewTimer(2 * time.Millisecond) + <-timer.C + timer.Stop() s.Equal("abc", str) } } diff --git a/x/upgrade/internal/conv/string_test.go b/x/upgrade/internal/conv/string_test.go index dfaaa038cb..974b40fc72 100644 --- a/x/upgrade/internal/conv/string_test.go +++ b/x/upgrade/internal/conv/string_test.go @@ -25,7 +25,9 @@ func (s *StringSuite) TestUnsafeStrToBytes() { for i := 0; i < 5; i++ { b := unsafeConvertStr() runtime.GC() - <-time.NewTimer(2 * time.Millisecond).C + timer := time.NewTimer(2 * time.Millisecond) + <-timer.C + timer.Stop() b2 := append(b, 'd') s.Equal("abc", string(b)) s.Equal("abcd", string(b2))