Commit 5c83e651 authored by Marcel van Lohuizen's avatar Marcel van Lohuizen

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/18893Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent 248c3a3c
...@@ -49,7 +49,7 @@ type B struct { ...@@ -49,7 +49,7 @@ type B struct {
N int N int
previousN int // number of iterations in the previous run previousN int // number of iterations in the previous run
previousDuration time.Duration // total duration of the previous run previousDuration time.Duration // total duration of the previous run
benchmark InternalBenchmark benchFunc func(b *B)
bytes int64 bytes int64
timerOn bool timerOn bool
showAllocResult bool showAllocResult bool
...@@ -132,7 +132,7 @@ func (b *B) runN(n int) { ...@@ -132,7 +132,7 @@ func (b *B) runN(n int) {
b.parallelism = 1 b.parallelism = 1
b.ResetTimer() b.ResetTimer()
b.StartTimer() b.StartTimer()
b.benchmark.F(b) b.benchFunc(b)
b.StopTimer() b.StopTimer()
b.previousN = n b.previousN = n
b.previousDuration = b.duration b.previousDuration = b.duration
...@@ -204,7 +204,7 @@ func (b *B) launch() { ...@@ -204,7 +204,7 @@ func (b *B) launch() {
// Signal that we're done whether we return normally // Signal that we're done whether we return normally
// or by FailNow's runtime.Goexit. // or by FailNow's runtime.Goexit.
defer func() { defer func() {
b.signal <- b b.signal <- true
}() }()
b.runN(n) b.runN(n)
...@@ -339,9 +339,10 @@ func runBenchmarksInternal(matchString func(pat, str string) (bool, error), benc ...@@ -339,9 +339,10 @@ func runBenchmarksInternal(matchString func(pat, str string) (bool, error), benc
runtime.GOMAXPROCS(procs) runtime.GOMAXPROCS(procs)
b := &B{ b := &B{
common: common{ common: common{
signal: make(chan interface{}), signal: make(chan bool),
name: Benchmark.Name,
}, },
benchmark: Benchmark, benchFunc: Benchmark.F,
} }
benchName := benchmarkName(Benchmark.Name, procs) benchName := benchmarkName(Benchmark.Name, procs)
fmt.Printf("%-*s\t", maxlen, benchName) fmt.Printf("%-*s\t", maxlen, benchName)
...@@ -476,9 +477,9 @@ func (b *B) SetParallelism(p int) { ...@@ -476,9 +477,9 @@ func (b *B) SetParallelism(p int) {
func Benchmark(f func(b *B)) BenchmarkResult { func Benchmark(f func(b *B)) BenchmarkResult {
b := &B{ b := &B{
common: common{ common: common{
signal: make(chan interface{}), signal: make(chan bool),
}, },
benchmark: InternalBenchmark{"", f}, benchFunc: f,
} }
return b.run() return b.run()
} }
// 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)
}
}
}
}
...@@ -147,6 +147,7 @@ import ( ...@@ -147,6 +147,7 @@ import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"io"
"os" "os"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
...@@ -197,14 +198,18 @@ var ( ...@@ -197,14 +198,18 @@ var (
type common struct { type common struct {
mu sync.RWMutex // guards output and failed mu sync.RWMutex // guards output and failed
output []byte // Output generated by test or benchmark. output []byte // Output generated by test or benchmark.
w io.Writer // For flushToParent.
failed bool // Test or benchmark has failed. failed bool // Test or benchmark has failed.
skipped bool // Test of benchmark has been skipped. skipped bool // Test of benchmark has been skipped.
finished bool finished bool
parent *common
name string // Name of test or benchmark.
start time.Time // Time test or benchmark started start time.Time // Time test or benchmark started
duration time.Duration duration time.Duration
self interface{} // To be sent on signal channel when done. barrier chan bool // To signal parallel subtests they may start.
signal chan interface{} // Output for serial tests. signal chan bool // To signal a test is done.
sub []*T // Queue of subtests to be run in parallel.
} }
// Short reports whether the -test.short flag is set. // Short reports whether the -test.short flag is set.
...@@ -251,6 +256,22 @@ func decorate(s string) string { ...@@ -251,6 +256,22 @@ func decorate(s string) string {
return buf.String() return buf.String()
} }
// flushToParent writes c.output to the parent after first writing the header
// with the given format and arguments.
func (c *common) flushToParent(format string, args ...interface{}) {
p := c.parent
p.mu.Lock()
defer p.mu.Unlock()
fmt.Fprintf(p.w, format, args...)
fmt.Fprintln(p.w)
c.mu.Lock()
defer c.mu.Unlock()
io.Copy(p.w, bytes.NewReader(c.output))
c.output = c.output[:0]
}
// fmtDuration returns a string representing d in the form "87.00s". // fmtDuration returns a string representing d in the form "87.00s".
func fmtDuration(d time.Duration) string { func fmtDuration(d time.Duration) string {
return fmt.Sprintf("%.2fs", d.Seconds()) return fmt.Sprintf("%.2fs", d.Seconds())
...@@ -293,15 +314,17 @@ var _ TB = (*B)(nil) ...@@ -293,15 +314,17 @@ var _ TB = (*B)(nil)
// may be called simultaneously from multiple goroutines. // may be called simultaneously from multiple goroutines.
type T struct { type T struct {
common common
name string // Name of test. isParallel bool
isParallel bool context *testContext // For running tests and subtests.
startParallel chan bool // Parallel tests will wait on this.
} }
func (c *common) private() {} func (c *common) private() {}
// Fail marks the function as having failed but continues execution. // Fail marks the function as having failed but continues execution.
func (c *common) Fail() { func (c *common) Fail() {
if c.parent != nil {
c.parent.Fail()
}
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
c.failed = true c.failed = true
...@@ -437,8 +460,13 @@ func (t *T) Parallel() { ...@@ -437,8 +460,13 @@ func (t *T) Parallel() {
// in the test duration. Record the elapsed time thus far and reset the // in the test duration. Record the elapsed time thus far and reset the
// timer afterwards. // timer afterwards.
t.duration += time.Since(t.start) t.duration += time.Since(t.start)
t.signal <- (*T)(nil) // Release main testing loop
<-t.startParallel // Wait for serial tests to finish // Add to the list of tests to be released by the parent.
t.parent.sub = append(t.parent.sub, t)
t.signal <- true // Release calling test.
<-t.parent.barrier // Wait for the parent test to complete.
t.context.waitParallel()
t.start = time.Now() t.start = time.Now()
} }
...@@ -449,8 +477,8 @@ type InternalTest struct { ...@@ -449,8 +477,8 @@ type InternalTest struct {
F func(*T) F func(*T)
} }
func tRunner(t *T, test *InternalTest) { func tRunner(t *T, fn func(t *T)) {
// When this goroutine is done, either because test.F(t) // When this goroutine is done, either because fn(t)
// returned normally or because a test failure triggered // returned normally or because a test failure triggered
// a call to runtime.Goexit, record the duration and send // a call to runtime.Goexit, record the duration and send
// a signal saying that the test is done. // a signal saying that the test is done.
...@@ -466,14 +494,87 @@ func tRunner(t *T, test *InternalTest) { ...@@ -466,14 +494,87 @@ func tRunner(t *T, test *InternalTest) {
t.report() t.report()
panic(err) panic(err)
} }
t.signal <- t
if len(t.sub) > 0 {
// Run parallel subtests.
// Decrease the running count for this test.
t.context.release()
// Release the parallel subtests.
close(t.barrier)
// Wait for subtests to complete.
for _, sub := range t.sub {
<-sub.signal
}
if !t.isParallel {
// Reacquire the count for sequential tests. See comment in Run.
t.context.waitParallel()
}
} else if t.isParallel {
// Only release the count for this test if it was run as a parallel
// test. See comment in Run method.
t.context.release()
}
t.report() // Report after all subtests have finished.
t.signal <- true
}() }()
t.start = time.Now() t.start = time.Now()
test.F(t) fn(t)
t.finished = true t.finished = true
} }
// testContext holds all fields that are common to all tests. This includes
// synchronization primitives to run at most *parallel tests.
type testContext struct {
mu sync.Mutex
// Channel used to signal tests that are ready to be run in parallel.
startParallel chan bool
// running is the number of tests currently running in parallel.
// This does not include tests that are waiting for subtests to complete.
running int
// numWaiting is the number tests waiting to be run in parallel.
numWaiting int
// maxParallel is a copy of the parallel flag.
maxParallel int
}
func newTestContext(maxParallel int) *testContext {
return &testContext{
startParallel: make(chan bool),
maxParallel: *parallel,
running: 1, // Set the count to 1 for the main (sequential) test.
}
}
func (c *testContext) waitParallel() {
c.mu.Lock()
if c.running < c.maxParallel {
c.running++
c.mu.Unlock()
return
}
c.numWaiting++
c.mu.Unlock()
<-c.startParallel
}
func (c *testContext) release() {
c.mu.Lock()
if c.numWaiting == 0 {
c.running--
c.mu.Unlock()
return
}
c.numWaiting--
c.mu.Unlock()
c.startParallel <- true // Pick a waiting test to be run.
}
// An internal function but exported because it is cross-package; part of the implementation // An internal function but exported because it is cross-package; part of the implementation
// of the "go test" command. // of the "go test" command.
func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) { func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) {
...@@ -526,15 +627,18 @@ func (m *M) Run() int { ...@@ -526,15 +627,18 @@ func (m *M) Run() int {
} }
func (t *T) report() { func (t *T) report() {
if t.parent == nil {
return
}
dstr := fmtDuration(t.duration) dstr := fmtDuration(t.duration)
format := "--- %s: %s (%s)\n%s" format := "--- %s: %s (%s)"
if t.Failed() { if t.Failed() {
fmt.Printf(format, "FAIL", t.name, dstr, t.output) t.flushToParent(format, "FAIL", t.name, dstr)
} else if *chatty { } else if *chatty {
if t.Skipped() { if t.Skipped() {
fmt.Printf(format, "SKIP", t.name, dstr, t.output) t.flushToParent(format, "SKIP", t.name, dstr)
} else { } else {
fmt.Printf(format, "PASS", t.name, dstr, t.output) t.flushToParent(format, "PASS", t.name, dstr)
} }
} }
} }
...@@ -547,63 +651,55 @@ func RunTests(matchString func(pat, str string) (bool, error), tests []InternalT ...@@ -547,63 +651,55 @@ func RunTests(matchString func(pat, str string) (bool, error), tests []InternalT
} }
for _, procs := range cpuList { for _, procs := range cpuList {
runtime.GOMAXPROCS(procs) runtime.GOMAXPROCS(procs)
// We build a new channel tree for each run of the loop. ctx := newTestContext(*parallel)
// collector merges in one channel all the upstream signals from parallel tests. t := &T{
// If all tests pump to the same channel, a bug can occur where a test common: common{
// kicks off a goroutine that Fails, yet the test still delivers a completion signal, signal: make(chan bool),
// which skews the counting. barrier: make(chan bool),
var collector = make(chan interface{}) w: os.Stdout,
},
numParallel := 0 context: ctx,
startParallel := make(chan bool)
for i := 0; i < len(tests); i++ {
matched, err := matchString(*match, tests[i].Name)
if err != nil {
fmt.Fprintf(os.Stderr, "testing: invalid regexp for -test.run: %s\n", err)
os.Exit(1)
}
if !matched {
continue
}
testName := tests[i].Name
t := &T{
common: common{
signal: make(chan interface{}),
},
name: testName,
startParallel: startParallel,
}
t.self = t
if *chatty {
fmt.Printf("=== RUN %s\n", t.name)
}
go tRunner(t, &tests[i])
out := (<-t.signal).(*T)
if out == nil { // Parallel run.
go func() {
collector <- <-t.signal
}()
numParallel++
continue
}
t.report()
ok = ok && !out.Failed()
} }
running := 0 tRunner(t, func(t *T) {
for numParallel+running > 0 { for i := 0; i < len(tests); i++ {
if running < *parallel && numParallel > 0 { // TODO: a version of this will be the Run method.
startParallel <- true matched, err := matchString(*match, tests[i].Name)
running++ if err != nil {
numParallel-- fmt.Fprintf(os.Stderr, "testing: invalid regexp for -test.run: %s\n", err)
continue os.Exit(1)
}
if !matched {
continue
}
testName := tests[i].Name
t := &T{
common: common{
barrier: make(chan bool),
signal: make(chan bool),
name: testName,
parent: &t.common,
},
context: t.context,
}
if *chatty {
fmt.Printf("=== RUN %s\n", t.name)
}
// Instead of reducing the running count of this test before calling the
// tRunner and increasing it afterwards, we rely on tRunner keeping the
// count correct. This ensures that a sequence of sequential tests runs
// without being preempted, even when their parent is a parallel test. This
// may especially reduce surprises if *parallel == 1.
go tRunner(t, tests[i].F)
<-t.signal
} }
t := (<-collector).(*T) // Run catching the signal rather than the tRunner as a separate
t.report() // goroutine to avoid adding a goroutine during the sequential
ok = ok && !t.Failed() // phase as this pollutes the stacktrace output when aborting.
running-- go func() { <-t.signal }()
} })
ok = ok && !t.Failed()
} }
return return
} }
......
...@@ -14,7 +14,7 @@ func main() { ...@@ -14,7 +14,7 @@ func main() {
// make sure error mentions that // make sure error mentions that
// name is unexported, not just "name not found". // name is unexported, not just "name not found".
t.name = nil // ERROR "unexported" t.common.name = nil // ERROR "unexported"
println(testing.anyLowercaseName("asdf")) // ERROR "unexported" "undefined: testing.anyLowercaseName" println(testing.anyLowercaseName("asdf")) // ERROR "unexported" "undefined: testing.anyLowercaseName"
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment