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 #5 from bboreham/fix-multi-cpu
Browse files Browse the repository at this point in the history
Fix unit test failures when running on multiple cpus
  • Loading branch information
benbjohnson committed Jun 22, 2015
2 parents 52ab871 + c1ed37d commit ae77645
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
3 changes: 1 addition & 2 deletions clock.go
@@ -1,7 +1,6 @@
package clock

import (
"runtime"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -294,4 +293,4 @@ func (t *internalTicker) Tick(now time.Time) {
}

// Sleep momentarily so that other goroutines can process.
func gosched() { runtime.Gosched() }
func gosched() { time.Sleep(1 * time.Millisecond) }
20 changes: 13 additions & 7 deletions clock_test.go
Expand Up @@ -3,7 +3,6 @@ package clock
import (
"fmt"
"os"
"runtime"
"sync"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -426,12 +425,15 @@ func ExampleMock_After() {
clock := NewMock()
count := 0

ready := make(chan struct{})
// Create a channel to execute after 10 mock seconds.
go func() {
<-clock.After(10 * time.Second)
ch := clock.After(10 * time.Second)
close(ready)
<-ch
count = 100
}()
runtime.Gosched()
<-ready

// Print the starting value.
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)
Expand Down Expand Up @@ -459,7 +461,7 @@ func ExampleMock_AfterFunc() {
clock.AfterFunc(10*time.Second, func() {
count = 100
})
runtime.Gosched()
gosched()

// Print the starting value.
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)
Expand All @@ -483,7 +485,7 @@ func ExampleMock_Sleep() {
clock.Sleep(10 * time.Second)
count = 100
}()
runtime.Gosched()
gosched()

// Print the starting value.
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)
Expand All @@ -502,15 +504,17 @@ func ExampleMock_Ticker() {
clock := NewMock()
count := 0

ready := make(chan struct{})
// Increment count every mock second.
go func() {
ticker := clock.Ticker(1 * time.Second)
close(ready)
for {
<-ticker.C
count++
}
}()
runtime.Gosched()
<-ready

// Move the clock forward 10 seconds and print the new value.
clock.Add(10 * time.Second)
Expand All @@ -530,13 +534,15 @@ func ExampleMock_Timer() {
clock := NewMock()
count := 0

ready := make(chan struct{})
// Increment count after a mock second.
go func() {
timer := clock.Timer(1 * time.Second)
close(ready)
<-timer.C
count++
}()
runtime.Gosched()
<-ready

// Move the clock forward 10 seconds and print the new value.
clock.Add(10 * time.Second)
Expand Down

0 comments on commit ae77645

Please sign in to comment.