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

Fix race condition on 'now' when creating a timer #58

Merged
merged 1 commit into from
May 18, 2023
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
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

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...) }