Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #58 from mjte-riot/master
Browse files Browse the repository at this point in the history
Fix race condition on 'now' when creating a timer
  • Loading branch information
benbjohnson committed May 18, 2023
2 parents 4362d70 + b675fcb commit 96c602c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,11 @@ func (m *Mock) runNextTimer(max time.Time) bool {

// Move "now" forward and unlock clock.
m.now = t.Next()
now := m.now
m.mu.Unlock()

// Execute timer.
t.Tick(m.now)
t.Tick(now)
return true
}

Expand Down Expand Up @@ -258,8 +259,9 @@ func (m *Mock) Timer(d time.Duration) *Timer {
stopped: false,
}
m.timers = append(m.timers, (*internalTimer)(t))
now := m.now
m.mu.Unlock()
m.runNextTimer(m.now)
m.runNextTimer(now)
return t
}

Expand Down
19 changes: 19 additions & 0 deletions clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,5 +766,24 @@ func TestMock_AddAfterFuncRace(t *testing.T) {
wg.Wait() // and wait for them
}

func TestMock_AfterRace(t *testing.T) {
mock := NewMock()

const num = 10
var finished atomic.Int32

Check failure on line 773 in clock_test.go

View workflow job for this annotation

GitHub Actions / build (1.16.x)

undefined: atomic.Int32

for i := 0; i < num; i++ {
go func() {
<-mock.After(1 * time.Millisecond)
finished.Add(1)
}()
}

for finished.Load() < num {
mock.Add(time.Second)
gosched()
}
}

func warn(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) }
func warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", v...) }

0 comments on commit 96c602c

Please sign in to comment.