import (
"sync/atomic"
"testing"
+ "time"
)
func TestWork(t *testing.T) {
}
}
+func TestWorkParallel(t *testing.T) {
+ var w Work
+
+ for tries := 0; tries < 10; tries++ {
+ const N = 100
+ for i := 0; i < N; i++ {
+ w.Add(i)
+ }
+ start := time.Now()
+ var n int32
+ w.Do(N, func(x interface{}) {
+ time.Sleep(1 * time.Millisecond)
+ atomic.AddInt32(&n, +1)
+ })
+ if n != N {
+ t.Fatalf("par.Work.Do did not do all the work")
+ }
+ if time.Since(start) < N/2*time.Millisecond {
+ return
+ }
+ }
+ t.Fatalf("par.Work.Do does not seem to be parallel")
+}
+
func TestCache(t *testing.T) {
var cache Cache