cmd/go/internal/par: actually make par.Work run things in parallel

This was an unfortunate debugging print introduced
while working on the unfortunately large CL 123576.
At least now we're done with that awfulness.

Change-Id: Ib83db59382a799f649832d22d3c6f039d2ef9d2c
Reviewed-on: https://go-review.googlesource.com/125015
Reviewed-by: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
Russ Cox 2018-07-19 13:23:32 -04:00
parent ef45945718
commit dada467fc6
2 changed files with 25 additions and 1 deletions

View File

@ -55,7 +55,6 @@ func (w *Work) Do(n int, f func(item interface{})) {
if n < 1 {
panic("par.Work.Do: n < 1")
}
n = 1
if w.running >= 1 {
panic("par.Work.Do: already called Do")
}

View File

@ -7,6 +7,7 @@ package par
import (
"sync/atomic"
"testing"
"time"
)
func TestWork(t *testing.T) {
@ -30,6 +31,30 @@ 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