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

Commit

Permalink
testing: prepare for the introduction of Run methods
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mpvl committed Mar 18, 2016
1 parent 248c3a3 commit 5c83e65
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 77 deletions.
15 changes: 8 additions & 7 deletions src/testing/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type B struct {
N int
previousN int // number of iterations in the previous run
previousDuration time.Duration // total duration of the previous run
benchmark InternalBenchmark
benchFunc func(b *B)
bytes int64
timerOn bool
showAllocResult bool
Expand Down Expand Up @@ -132,7 +132,7 @@ func (b *B) runN(n int) {
b.parallelism = 1
b.ResetTimer()
b.StartTimer()
b.benchmark.F(b)
b.benchFunc(b)
b.StopTimer()
b.previousN = n
b.previousDuration = b.duration
Expand Down Expand Up @@ -204,7 +204,7 @@ func (b *B) launch() {
// Signal that we're done whether we return normally
// or by FailNow's runtime.Goexit.
defer func() {
b.signal <- b
b.signal <- true
}()

b.runN(n)
Expand Down Expand Up @@ -339,9 +339,10 @@ func runBenchmarksInternal(matchString func(pat, str string) (bool, error), benc
runtime.GOMAXPROCS(procs)
b := &B{
common: common{
signal: make(chan interface{}),
signal: make(chan bool),
name: Benchmark.Name,
},
benchmark: Benchmark,
benchFunc: Benchmark.F,
}
benchName := benchmarkName(Benchmark.Name, procs)
fmt.Printf("%-*s\t", maxlen, benchName)
Expand Down Expand Up @@ -476,9 +477,9 @@ func (b *B) SetParallelism(p int) {
func Benchmark(f func(b *B)) BenchmarkResult {
b := &B{
common: common{
signal: make(chan interface{}),
signal: make(chan bool),
},
benchmark: InternalBenchmark{"", f},
benchFunc: f,
}
return b.run()
}
101 changes: 101 additions & 0 deletions src/testing/sub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package testing

func TestTestContext(t *T) {
const (
add1 = 0
done = 1
)
// After each of the calls are applied to the context, the
type call struct {
typ int // run or done
// result from applying the call
running int
waiting int
started bool
}
testCases := []struct {
max int
run []call
}{{
max: 1,
run: []call{
{typ: add1, running: 1, waiting: 0, started: true},
{typ: done, running: 0, waiting: 0, started: false},
},
}, {
max: 1,
run: []call{
{typ: add1, running: 1, waiting: 0, started: true},
{typ: add1, running: 1, waiting: 1, started: false},
{typ: done, running: 1, waiting: 0, started: true},
{typ: done, running: 0, waiting: 0, started: false},
{typ: add1, running: 1, waiting: 0, started: true},
},
}, {
max: 3,
run: []call{
{typ: add1, running: 1, waiting: 0, started: true},
{typ: add1, running: 2, waiting: 0, started: true},
{typ: add1, running: 3, waiting: 0, started: true},
{typ: add1, running: 3, waiting: 1, started: false},
{typ: add1, running: 3, waiting: 2, started: false},
{typ: add1, running: 3, waiting: 3, started: false},
{typ: done, running: 3, waiting: 2, started: true},
{typ: add1, running: 3, waiting: 3, started: false},
{typ: done, running: 3, waiting: 2, started: true},
{typ: done, running: 3, waiting: 1, started: true},
{typ: done, running: 3, waiting: 0, started: true},
{typ: done, running: 2, waiting: 0, started: false},
{typ: done, running: 1, waiting: 0, started: false},
{typ: done, running: 0, waiting: 0, started: false},
},
}}
for i, tc := range testCases {
ctx := &testContext{
startParallel: make(chan bool),
maxParallel: tc.max,
}
for j, call := range tc.run {
doCall := func(f func()) chan bool {
done := make(chan bool)
go func() {
f()
done <- true
}()
return done
}
started := false
switch call.typ {
case add1:
signal := doCall(ctx.waitParallel)
select {
case <-signal:
started = true
case ctx.startParallel <- true:
<-signal
}
case done:
signal := doCall(ctx.release)
select {
case <-signal:
case <-ctx.startParallel:
started = true
<-signal
}
}
if started != call.started {
t.Errorf("%d:%d:started: got %v; want %v", i, j, started, call.started)
}
if ctx.running != call.running {
t.Errorf("%d:%d:running: got %v; want %v", i, j, ctx.running, call.running)
}
if ctx.numWaiting != call.waiting {
t.Errorf("%d:%d:waiting: got %v; want %v", i, j, ctx.numWaiting, call.waiting)
}
}
}
}
Loading

0 comments on commit 5c83e65

Please sign in to comment.