aboutsummaryrefslogtreecommitdiffstats
path: root/remote_test.go
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2022-11-04 11:04:29 +0000
committerPaulo Gomes <pjbgf@linux.com>2022-11-07 14:50:08 +0000
commita2c309de872dc18053acb186b1ec125d1f723a90 (patch)
tree9e6ace8591fff4e95f982304842f0bf8512bd612 /remote_test.go
parent9490da0f86a12269abb2099e2ead1f20eec166d2 (diff)
downloadgo-git-a2c309de872dc18053acb186b1ec125d1f723a90.tar.gz
tests: Replace time.sleep with eventually
The previous approach was intermittently flake, leading to different results based on external results. The check for goroutines numbers now checks for less or equal, as the goal of the assertion is to confirm no goroutine is being leaked. Signed-off-by: Paulo Gomes <pjbgf@linux.com>
Diffstat (limited to 'remote_test.go')
-rw-r--r--remote_test.go27
1 files changed, 19 insertions, 8 deletions
diff --git a/remote_test.go b/remote_test.go
index d0c8fa8..751c89a 100644
--- a/remote_test.go
+++ b/remote_test.go
@@ -535,10 +535,22 @@ func (s *RemoteSuite) TestPushContext(c *C) {
})
c.Assert(err, IsNil)
- // let the goroutine from pushHashes finish and check that the number of
- // goroutines is the same as before
- time.Sleep(100 * time.Millisecond)
- c.Assert(runtime.NumGoroutine(), Equals, numGoroutines)
+ eventually(c, func() bool {
+ return runtime.NumGoroutine() <= numGoroutines
+ })
+}
+
+func eventually(c *C, condition func() bool) {
+ select {
+ case <-time.After(5 * time.Second):
+ default:
+ if condition() {
+ break
+ }
+ time.Sleep(100 * time.Millisecond)
+ }
+
+ c.Assert(condition(), Equals, true)
}
func (s *RemoteSuite) TestPushContextCanceled(c *C) {
@@ -566,10 +578,9 @@ func (s *RemoteSuite) TestPushContextCanceled(c *C) {
})
c.Assert(err, Equals, context.Canceled)
- // let the goroutine from pushHashes finish and check that the number of
- // goroutines is the same as before
- time.Sleep(100 * time.Millisecond)
- c.Assert(runtime.NumGoroutine(), Equals, numGoroutines)
+ eventually(c, func() bool {
+ return runtime.NumGoroutine() <= numGoroutines
+ })
}
func (s *RemoteSuite) TestPushTags(c *C) {