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.
19 // Make benchmark tests run 10x faster.
20 benchTime.d = 100 * time.Millisecond
23 func TestTestContext(t *T) {
28 // After each of the calls are applied to the context, the
30 typ int // run or done
31 // result from applying the call
36 testCases := []struct {
42 {typ: add1, running: 1, waiting: 0, started: true},
43 {typ: done, running: 0, waiting: 0, started: false},
48 {typ: add1, running: 1, waiting: 0, started: true},
49 {typ: add1, running: 1, waiting: 1, started: false},
50 {typ: done, running: 1, waiting: 0, started: true},
51 {typ: done, running: 0, waiting: 0, started: false},
52 {typ: add1, running: 1, waiting: 0, started: true},
57 {typ: add1, running: 1, waiting: 0, started: true},
58 {typ: add1, running: 2, waiting: 0, started: true},
59 {typ: add1, running: 3, waiting: 0, started: true},
60 {typ: add1, running: 3, waiting: 1, started: false},
61 {typ: add1, running: 3, waiting: 2, started: false},
62 {typ: add1, running: 3, waiting: 3, started: false},
63 {typ: done, running: 3, waiting: 2, started: true},
64 {typ: add1, running: 3, waiting: 3, started: false},
65 {typ: done, running: 3, waiting: 2, started: true},
66 {typ: done, running: 3, waiting: 1, started: true},
67 {typ: done, running: 3, waiting: 0, started: true},
68 {typ: done, running: 2, waiting: 0, started: false},
69 {typ: done, running: 1, waiting: 0, started: false},
70 {typ: done, running: 0, waiting: 0, started: false},
73 for i, tc := range testCases {
75 startParallel: make(chan bool),
78 for j, call := range tc.run {
79 doCall := func(f func()) chan bool {
80 done := make(chan bool)
90 signal := doCall(ctx.waitParallel)
94 case ctx.startParallel <- true:
98 signal := doCall(ctx.release)
101 case <-ctx.startParallel:
106 if started != call.started {
107 t.Errorf("%d:%d:started: got %v; want %v", i, j, started, call.started)
109 if ctx.running != call.running {
110 t.Errorf("%d:%d:running: got %v; want %v", i, j, ctx.running, call.running)
112 if ctx.numWaiting != call.waiting {
113 t.Errorf("%d:%d:waiting: got %v; want %v", i, j, ctx.numWaiting, call.waiting)
119 func TestTRun(t *T) {
121 testCases := []struct {
129 desc: "failnow skips future sequential and parallel tests at same level",
133 --- FAIL: failnow skips future sequential and parallel tests at same level (N.NNs)
134 --- FAIL: failnow skips future sequential and parallel tests at same level/#00 (N.NNs)
139 t.Run("", func(t *T) {
140 t.Run("par", func(t *T) {
144 t.Run("seq", func(t *T) {
148 t.Run("seq", func(t *T) {
149 realTest.Error("test must be skipped")
151 t.Run("par", func(t *T) {
153 realTest.Error("test must be skipped.")
157 realTest.Error("parallel test was not run")
160 realTest.Error("sequential test was not run")
164 desc: "failure in parallel test propagates upwards",
168 --- FAIL: failure in parallel test propagates upwards (N.NNs)
169 --- FAIL: failure in parallel test propagates upwards/#00 (N.NNs)
170 --- FAIL: failure in parallel test propagates upwards/#00/par (N.NNs)
173 t.Run("", func(t *T) {
175 t.Run("par", func(t *T) {
182 desc: "skipping without message, chatty",
186 === RUN skipping without message, chatty
187 --- SKIP: skipping without message, chatty (N.NNs)`,
188 f: func(t *T) { t.SkipNow() },
190 desc: "chatty with recursion",
194 === RUN chatty with recursion
195 === RUN chatty with recursion/#00
196 === RUN chatty with recursion/#00/#00
197 --- PASS: chatty with recursion (N.NNs)
198 --- PASS: chatty with recursion/#00 (N.NNs)
199 --- PASS: chatty with recursion/#00/#00 (N.NNs)`,
201 t.Run("", func(t *T) {
202 t.Run("", func(t *T) {})
206 desc: "skipping without message, not chatty",
208 f: func(t *T) { t.SkipNow() },
210 desc: "skipping after error",
212 --- FAIL: skipping after error (N.NNs)
213 sub_test.go:NNN: an error
214 sub_test.go:NNN: skipped`,
220 desc: "use Run to locally synchronize parallelism",
225 t.Run("waitGroup", func(t *T) {
226 for i := 0; i < 4; i++ {
227 t.Run("par", func(t *T) {
229 atomic.AddUint32(&count, 1)
234 t.Errorf("count was %d; want 4", count)
238 desc: "alternate sequential and parallel",
239 // Sequential tests should partake in the counting of running threads.
240 // Otherwise, if one runs parallel subtests in sequential tests that are
241 // itself subtests of parallel tests, the counts can get askew.
245 t.Run("a", func(t *T) {
247 t.Run("b", func(t *T) {
248 // Sequential: ensure running count is decremented.
249 t.Run("c", func(t *T) {
257 desc: "alternate sequential and parallel 2",
258 // Sequential tests should partake in the counting of running threads.
259 // Otherwise, if one runs parallel subtests in sequential tests that are
260 // itself subtests of parallel tests, the counts can get askew.
264 for i := 0; i < 2; i++ {
265 t.Run("a", func(t *T) {
267 time.Sleep(time.Nanosecond)
268 for i := 0; i < 2; i++ {
269 t.Run("b", func(t *T) {
270 time.Sleep(time.Nanosecond)
271 for i := 0; i < 2; i++ {
272 t.Run("c", func(t *T) {
274 time.Sleep(time.Nanosecond)
289 for i := 0; i < 12; i++ {
290 t.Run("a", func(t *T) {
292 time.Sleep(time.Nanosecond)
293 for i := 0; i < 12; i++ {
294 t.Run("b", func(t *T) {
295 time.Sleep(time.Nanosecond)
296 for i := 0; i < 12; i++ {
297 t.Run("c", func(t *T) {
299 time.Sleep(time.Nanosecond)
300 t.Run("d1", func(t *T) {})
301 t.Run("d2", func(t *T) {})
302 t.Run("d3", func(t *T) {})
303 t.Run("d4", func(t *T) {})
319 desc: "subtest calls error on parent",
322 --- FAIL: subtest calls error on parent (N.NNs)
323 sub_test.go:NNN: first this
324 sub_test.go:NNN: and now this!
325 sub_test.go:NNN: oh, and this too`,
328 t.Errorf("first this")
330 t.Run("", func(t *T) {
331 outer.Errorf("and now this!")
333 t.Errorf("oh, and this too")
336 desc: "subtest calls fatal on parent",
339 --- FAIL: subtest calls fatal on parent (N.NNs)
340 sub_test.go:NNN: first this
341 sub_test.go:NNN: and now this!
342 --- FAIL: subtest calls fatal on parent/#00 (N.NNs)
343 testing.go:NNN: test executed panic(nil) or runtime.Goexit: subtest may have called FailNow on a parent test`,
347 t.Errorf("first this")
348 t.Run("", func(t *T) {
349 outer.Fatalf("and now this!")
351 t.Errorf("Should not reach here.")
354 desc: "subtest calls error on ancestor",
357 --- FAIL: subtest calls error on ancestor (N.NNs)
358 sub_test.go:NNN: Report to ancestor
359 --- FAIL: subtest calls error on ancestor/#00 (N.NNs)
360 sub_test.go:NNN: Still do this
361 sub_test.go:NNN: Also do this`,
365 t.Run("", func(t *T) {
366 t.Run("", func(t *T) {
367 outer.Errorf("Report to ancestor")
369 t.Errorf("Still do this")
371 t.Errorf("Also do this")
374 desc: "subtest calls fatal on ancestor",
377 --- FAIL: subtest calls fatal on ancestor (N.NNs)
378 sub_test.go:NNN: Nope`,
382 t.Run("", func(t *T) {
383 for i := 0; i < 4; i++ {
384 t.Run("", func(t *T) {
387 t.Errorf("Don't do this")
389 t.Errorf("And neither do this")
394 desc: "panic on goroutine fail after test exit",
398 ch := make(chan bool)
399 t.Run("", func(t *T) {
403 if r := recover(); r == nil {
404 realTest.Errorf("expected panic")
408 t.Errorf("failed after success")
415 desc: "log in finished sub test logs to parent",
418 --- FAIL: log in finished sub test logs to parent (N.NNs)
419 sub_test.go:NNN: message2
420 sub_test.go:NNN: message1
421 sub_test.go:NNN: error`,
424 ch := make(chan bool)
425 t.Run("sub", func(t2 *T) {
438 for _, tc := range testCases {
439 ctx := newTestContext(tc.maxPar, newMatcher(regexp.MatchString, "", ""))
440 buf := &bytes.Buffer{}
443 signal: make(chan bool),
450 ok := root.Run(tc.desc, tc.f)
454 t.Errorf("%s:ok: got %v; want %v", tc.desc, ok, tc.ok)
456 if ok != !root.Failed() {
457 t.Errorf("%s:root failed: got %v; want %v", tc.desc, !ok, root.Failed())
459 if ctx.running != 0 || ctx.numWaiting != 0 {
460 t.Errorf("%s:running and waiting non-zero: got %d and %d", tc.desc, ctx.running, ctx.numWaiting)
462 got := strings.TrimSpace(buf.String())
463 want := strings.TrimSpace(tc.output)
464 re := makeRegexp(want)
465 if ok, err := regexp.MatchString(re, got); !ok || err != nil {
466 t.Errorf("%s:output:\ngot:\n%s\nwant:\n%s", tc.desc, got, want)
471 func TestBRun(t *T) {
473 for i := 0; i < b.N; i++ {
474 time.Sleep(time.Nanosecond)
477 testCases := []struct {
484 desc: "simulate sequential run of subbenchmarks.",
486 b.Run("", func(b *B) { work(b) })
487 time1 := b.result.NsPerOp()
488 b.Run("", func(b *B) { work(b) })
489 time2 := b.result.NsPerOp()
491 t.Errorf("no time spent in benchmark t1 >= t2 (%d >= %d)", time1, time2)
495 desc: "bytes set by all benchmarks",
497 b.Run("", func(b *B) { b.SetBytes(10); work(b) })
498 b.Run("", func(b *B) { b.SetBytes(10); work(b) })
499 if b.result.Bytes != 20 {
500 t.Errorf("bytes: got: %d; want 20", b.result.Bytes)
504 desc: "bytes set by some benchmarks",
505 // In this case the bytes result is meaningless, so it must be 0.
507 b.Run("", func(b *B) { b.SetBytes(10); work(b) })
508 b.Run("", func(b *B) { work(b) })
509 b.Run("", func(b *B) { b.SetBytes(10); work(b) })
510 if b.result.Bytes != 0 {
511 t.Errorf("bytes: got: %d; want 0", b.result.Bytes)
515 desc: "failure carried over to root",
517 output: "--- FAIL: root",
518 f: func(b *B) { b.Fail() },
520 desc: "skipping without message, chatty",
522 output: "--- SKIP: root",
523 f: func(b *B) { b.SkipNow() },
525 desc: "skipping with message, chatty",
529 sub_test.go:NNN: skipping`,
530 f: func(b *B) { b.Skip("skipping") },
532 desc: "chatty with recursion",
535 b.Run("", func(b *B) {
536 b.Run("", func(b *B) {})
540 desc: "skipping without message, not chatty",
541 f: func(b *B) { b.SkipNow() },
543 desc: "skipping after error",
547 sub_test.go:NNN: an error
548 sub_test.go:NNN: skipped`,
554 desc: "memory allocation",
557 alloc := func(b *B) {
558 var buf [bufSize]byte
559 for i := 0; i < b.N; i++ {
560 _ = append([]byte(nil), buf[:]...)
563 b.Run("", func(b *B) {
567 b.Run("", func(b *B) {
571 // runtime.MemStats sometimes reports more allocations than the
572 // benchmark is responsible for. Luckily the point of this test is
573 // to ensure that the results are not underreported, so we can
574 // simply verify the lower bound.
575 if got := b.result.MemAllocs; got < 2 {
576 t.Errorf("MemAllocs was %v; want 2", got)
578 if got := b.result.MemBytes; got < 2*bufSize {
579 t.Errorf("MemBytes was %v; want %v", got, 2*bufSize)
583 for _, tc := range testCases {
585 buf := &bytes.Buffer{}
586 // This is almost like the Benchmark function, except that we override
587 // the benchtime and catch the failure result of the subbenchmark.
590 signal: make(chan bool),
595 benchFunc: func(b *B) { ok = b.Run("test", tc.f) }, // Use Run to catch failure.
596 benchTime: benchTimeFlag{d: 1 * time.Microsecond},
599 if ok != !tc.failed {
600 t.Errorf("%s:ok: got %v; want %v", tc.desc, ok, !tc.failed)
602 if !ok != root.Failed() {
603 t.Errorf("%s:root failed: got %v; want %v", tc.desc, !ok, root.Failed())
605 // All tests are run as subtests
606 if root.result.N != 1 {
607 t.Errorf("%s: N for parent benchmark was %d; want 1", tc.desc, root.result.N)
609 got := strings.TrimSpace(buf.String())
610 want := strings.TrimSpace(tc.output)
611 re := makeRegexp(want)
612 if ok, err := regexp.MatchString(re, got); !ok || err != nil {
613 t.Errorf("%s:output:\ngot:\n%s\nwant:\n%s", tc.desc, got, want)
618 func makeRegexp(s string) string {
619 s = regexp.QuoteMeta(s)
620 s = strings.ReplaceAll(s, ":NNN:", `:\d\d\d:`)
621 s = strings.ReplaceAll(s, "N\\.NNs", `\d*\.\d*s`)
625 func TestBenchmarkOutput(t *T) {
626 // Ensure Benchmark initialized common.w by invoking it with an error and
628 Benchmark(func(b *B) { b.Error("do not print this output") })
629 Benchmark(func(b *B) {})
632 func TestBenchmarkStartsFrom1(t *T) {
634 Benchmark(func(b *B) {
635 if first && b.N != 1 {
636 panic(fmt.Sprintf("Benchmark() first N=%v; want 1", b.N))
642 func TestBenchmarkReadMemStatsBeforeFirstRun(t *T) {
644 Benchmark(func(b *B) {
645 if first && (b.startAllocs == 0 || b.startBytes == 0) {
646 panic(fmt.Sprintf("ReadMemStats not called before first run"))
652 func TestParallelSub(t *T) {
654 block := make(chan int)
655 for i := 0; i < 10; i++ {
658 t.Run(fmt.Sprint(i), func(t *T) {})
663 for i := 0; i < 10; i++ {
668 type funcWriter func([]byte) (int, error)
670 func (fw funcWriter) Write(b []byte) (int, error) { return fw(b) }
672 func TestRacyOutput(t *T) {
673 var runs int32 // The number of running Writes
674 var races int32 // Incremented for each race detected
675 raceDetector := func(b []byte) (int, error) {
676 // Check if some other goroutine is concurrently calling Write.
677 if atomic.LoadInt32(&runs) > 0 {
678 atomic.AddInt32(&races, 1) // Race detected!
680 atomic.AddInt32(&runs, 1)
681 defer atomic.AddInt32(&runs, -1)
682 runtime.Gosched() // Increase probability of a race
686 var wg sync.WaitGroup
688 common: common{w: funcWriter(raceDetector), chatty: true},
689 context: newTestContext(1, newMatcher(regexp.MatchString, "", "")),
691 root.Run("", func(t *T) {
692 for i := 0; i < 100; i++ {
696 t.Run(fmt.Sprint(i), func(t *T) {
697 t.Logf("testing run %d", i)
705 t.Errorf("detected %d racy Writes", races)
709 // The late log message did not include the test name. Issue 29388.
710 func TestLogAfterComplete(t *T) {
711 ctx := newTestContext(1, newMatcher(regexp.MatchString, "", ""))
715 // Use a buffered channel so that tRunner can write
716 // to it although nothing is reading from it.
717 signal: make(chan bool, 1),
723 c1 := make(chan bool)
724 c2 := make(chan string)
725 tRunner(t1, func(t *T) {
726 t.Run("TestLateLog", func(t *T) {
732 c2 <- "subtest did not panic"
737 c2 <- fmt.Sprintf("subtest panic with unexpected value %v", p)
740 const want = "Log in goroutine after TestLateLog has completed"
741 if !strings.Contains(s, want) {
742 c2 <- fmt.Sprintf("subtest panic %q does not contain %q", s, want)
747 t.Log("log after test")
753 if s := <-c2; s != "" {
758 func TestBenchmark(t *T) {
760 t.Skip("skipping in short mode")
762 res := Benchmark(func(b *B) {
763 for i := 0; i < 5; i++ {
764 b.Run("", func(b *B) {
765 for i := 0; i < b.N; i++ {
766 time.Sleep(time.Millisecond)
771 if res.NsPerOp() < 4000000 {
772 t.Errorf("want >5ms; got %v", time.Duration(res.NsPerOp()))