Skip to content

Commit

Permalink
tests: Replace time.sleep with eventually
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
pjbgf committed Nov 7, 2022
1 parent 9490da0 commit a2c309d
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions remote_test.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a2c309d

Please sign in to comment.