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

Commit

Permalink
Fix races introduced in latest commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentbernat committed Sep 22, 2022
1 parent 4e77c34 commit 739bd11
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,24 +604,25 @@ func ExampleMock_After() {
func ExampleMock_AfterFunc() {
// Create a new mock clock.
clock := NewMock()
count := 0
var count counter
count.incr()

// Execute a function after 10 mock seconds.
clock.AfterFunc(10*time.Second, func() {
count = 100
count.incr()
})
gosched()

// Print the starting value.
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)
fmt.Printf("%s: %d\n", clock.Now().UTC(), count.get())

// Move the clock forward 10 seconds and print the new value.
clock.Add(10 * time.Second)
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)
fmt.Printf("%s: %d\n", clock.Now().UTC(), count.get())

// Output:
// 1970-01-01 00:00:00 +0000 UTC: 0
// 1970-01-01 00:00:10 +0000 UTC: 100
// 1970-01-01 00:00:00 +0000 UTC: 1
// 1970-01-01 00:00:10 +0000 UTC: 2
}

func ExampleMock_Sleep() {
Expand Down Expand Up @@ -728,9 +729,9 @@ func TestMock_AddAfterFuncRace(t *testing.T) {

mockedClock := NewMock()

called := false
var called counter
defer func() {
if !called {
if called.get() == 0 {
t.Errorf("AfterFunc did not call the function")
}
}()
Expand All @@ -741,7 +742,7 @@ func TestMock_AddAfterFuncRace(t *testing.T) {
<-start

mockedClock.AfterFunc(time.Millisecond, func() {
called = true
called.incr()
})
}()

Expand Down

0 comments on commit 739bd11

Please sign in to comment.