Skip to content

Commit 5c83e65

Browse files
committed
testing: prepare for the introduction of Run methods
The biggest change is that each test is now responsible for managing the starting and stopping of its parallel subtests. The "Main" test could be run as a tRunner as well. This shows that the introduction of subtests is merely a generalization of and consistent with the current semantics. Change-Id: Ibf8388c08f85d4b2c0df69c069326762ed36a72e Reviewed-on: https://go-review.googlesource.com/18893 Reviewed-by: Russ Cox <[email protected]>
1 parent 248c3a3 commit 5c83e65

File tree

4 files changed

+275
-77
lines changed

4 files changed

+275
-77
lines changed

src/testing/benchmark.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type B struct {
4949
N int
5050
previousN int // number of iterations in the previous run
5151
previousDuration time.Duration // total duration of the previous run
52-
benchmark InternalBenchmark
52+
benchFunc func(b *B)
5353
bytes int64
5454
timerOn bool
5555
showAllocResult bool
@@ -132,7 +132,7 @@ func (b *B) runN(n int) {
132132
b.parallelism = 1
133133
b.ResetTimer()
134134
b.StartTimer()
135-
b.benchmark.F(b)
135+
b.benchFunc(b)
136136
b.StopTimer()
137137
b.previousN = n
138138
b.previousDuration = b.duration
@@ -204,7 +204,7 @@ func (b *B) launch() {
204204
// Signal that we're done whether we return normally
205205
// or by FailNow's runtime.Goexit.
206206
defer func() {
207-
b.signal <- b
207+
b.signal <- true
208208
}()
209209

210210
b.runN(n)
@@ -339,9 +339,10 @@ func runBenchmarksInternal(matchString func(pat, str string) (bool, error), benc
339339
runtime.GOMAXPROCS(procs)
340340
b := &B{
341341
common: common{
342-
signal: make(chan interface{}),
342+
signal: make(chan bool),
343+
name: Benchmark.Name,
343344
},
344-
benchmark: Benchmark,
345+
benchFunc: Benchmark.F,
345346
}
346347
benchName := benchmarkName(Benchmark.Name, procs)
347348
fmt.Printf("%-*s\t", maxlen, benchName)
@@ -476,9 +477,9 @@ func (b *B) SetParallelism(p int) {
476477
func Benchmark(f func(b *B)) BenchmarkResult {
477478
b := &B{
478479
common: common{
479-
signal: make(chan interface{}),
480+
signal: make(chan bool),
480481
},
481-
benchmark: InternalBenchmark{"", f},
482+
benchFunc: f,
482483
}
483484
return b.run()
484485
}

src/testing/sub_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package testing
6+
7+
func TestTestContext(t *T) {
8+
const (
9+
add1 = 0
10+
done = 1
11+
)
12+
// After each of the calls are applied to the context, the
13+
type call struct {
14+
typ int // run or done
15+
// result from applying the call
16+
running int
17+
waiting int
18+
started bool
19+
}
20+
testCases := []struct {
21+
max int
22+
run []call
23+
}{{
24+
max: 1,
25+
run: []call{
26+
{typ: add1, running: 1, waiting: 0, started: true},
27+
{typ: done, running: 0, waiting: 0, started: false},
28+
},
29+
}, {
30+
max: 1,
31+
run: []call{
32+
{typ: add1, running: 1, waiting: 0, started: true},
33+
{typ: add1, running: 1, waiting: 1, started: false},
34+
{typ: done, running: 1, waiting: 0, started: true},
35+
{typ: done, running: 0, waiting: 0, started: false},
36+
{typ: add1, running: 1, waiting: 0, started: true},
37+
},
38+
}, {
39+
max: 3,
40+
run: []call{
41+
{typ: add1, running: 1, waiting: 0, started: true},
42+
{typ: add1, running: 2, waiting: 0, started: true},
43+
{typ: add1, running: 3, waiting: 0, started: true},
44+
{typ: add1, running: 3, waiting: 1, started: false},
45+
{typ: add1, running: 3, waiting: 2, started: false},
46+
{typ: add1, running: 3, waiting: 3, started: false},
47+
{typ: done, running: 3, waiting: 2, started: true},
48+
{typ: add1, running: 3, waiting: 3, started: false},
49+
{typ: done, running: 3, waiting: 2, started: true},
50+
{typ: done, running: 3, waiting: 1, started: true},
51+
{typ: done, running: 3, waiting: 0, started: true},
52+
{typ: done, running: 2, waiting: 0, started: false},
53+
{typ: done, running: 1, waiting: 0, started: false},
54+
{typ: done, running: 0, waiting: 0, started: false},
55+
},
56+
}}
57+
for i, tc := range testCases {
58+
ctx := &testContext{
59+
startParallel: make(chan bool),
60+
maxParallel: tc.max,
61+
}
62+
for j, call := range tc.run {
63+
doCall := func(f func()) chan bool {
64+
done := make(chan bool)
65+
go func() {
66+
f()
67+
done <- true
68+
}()
69+
return done
70+
}
71+
started := false
72+
switch call.typ {
73+
case add1:
74+
signal := doCall(ctx.waitParallel)
75+
select {
76+
case <-signal:
77+
started = true
78+
case ctx.startParallel <- true:
79+
<-signal
80+
}
81+
case done:
82+
signal := doCall(ctx.release)
83+
select {
84+
case <-signal:
85+
case <-ctx.startParallel:
86+
started = true
87+
<-signal
88+
}
89+
}
90+
if started != call.started {
91+
t.Errorf("%d:%d:started: got %v; want %v", i, j, started, call.started)
92+
}
93+
if ctx.running != call.running {
94+
t.Errorf("%d:%d:running: got %v; want %v", i, j, ctx.running, call.running)
95+
}
96+
if ctx.numWaiting != call.waiting {
97+
t.Errorf("%d:%d:waiting: got %v; want %v", i, j, ctx.numWaiting, call.waiting)
98+
}
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)