Skip to content

Commit 440c9ee

Browse files
narqogopherbot
authored andcommitted
testing: rename testContext to testState
Following up to CL 603959, update internals of testing package to reduce the confusion around "context". The changes rename testContext/benchContext/fuzzContext to testState/benchState/fuzzState. Change-Id: Ib8855dab456d41ab343488fcf5fefff2431f7b72 Reviewed-on: https://go-review.googlesource.com/c/go/+/607555 Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Reviewed-by: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 31a9c13 commit 440c9ee

File tree

4 files changed

+103
-104
lines changed

4 files changed

+103
-104
lines changed

src/testing/benchmark.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ type InternalBenchmark struct {
9393
type B struct {
9494
common
9595
importPath string // import path of the package containing the benchmark
96-
context *benchContext
96+
bstate *benchState
9797
N int
9898
previousN int // number of iterations in the previous run
9999
previousDuration time.Duration // total duration of the previous run
@@ -199,10 +199,10 @@ func (b *B) runN(n int) {
199199
// run1 runs the first iteration of benchFunc. It reports whether more
200200
// iterations of this benchmarks should be run.
201201
func (b *B) run1() bool {
202-
if ctx := b.context; ctx != nil {
202+
if bstate := b.bstate; bstate != nil {
203203
// Extend maxLen, if needed.
204-
if n := len(b.name) + ctx.extLen + 1; n > ctx.maxLen {
205-
ctx.maxLen = n + 8 // Add additional slack to avoid too many jumps in size.
204+
if n := len(b.name) + bstate.extLen + 1; n > bstate.maxLen {
205+
bstate.maxLen = n + 8 // Add additional slack to avoid too many jumps in size.
206206
}
207207
}
208208
go func() {
@@ -253,9 +253,9 @@ func (b *B) run() {
253253
fmt.Fprintf(b.w, "cpu: %s\n", cpu)
254254
}
255255
})
256-
if b.context != nil {
256+
if b.bstate != nil {
257257
// Running go test --test.bench
258-
b.context.processBench(b) // Must call doBench.
258+
b.bstate.processBench(b) // Must call doBench.
259259
} else {
260260
// Running func Benchmark.
261261
b.doBench()
@@ -492,7 +492,7 @@ func benchmarkName(name string, n int) string {
492492
return name
493493
}
494494

495-
type benchContext struct {
495+
type benchState struct {
496496
match *matcher
497497

498498
maxLen int // The largest recorded benchmark name.
@@ -517,17 +517,17 @@ func runBenchmarks(importPath string, matchString func(pat, str string) (bool, e
517517
maxprocs = procs
518518
}
519519
}
520-
ctx := &benchContext{
520+
bstate := &benchState{
521521
match: newMatcher(matchString, *matchBenchmarks, "-test.bench", *skip),
522522
extLen: len(benchmarkName("", maxprocs)),
523523
}
524524
var bs []InternalBenchmark
525525
for _, Benchmark := range benchmarks {
526-
if _, matched, _ := ctx.match.fullName(nil, Benchmark.Name); matched {
526+
if _, matched, _ := bstate.match.fullName(nil, Benchmark.Name); matched {
527527
bs = append(bs, Benchmark)
528528
benchName := benchmarkName(Benchmark.Name, maxprocs)
529-
if l := len(benchName) + ctx.extLen + 1; l > ctx.maxLen {
530-
ctx.maxLen = l
529+
if l := len(benchName) + bstate.extLen + 1; l > bstate.maxLen {
530+
bstate.maxLen = l
531531
}
532532
}
533533
}
@@ -544,7 +544,7 @@ func runBenchmarks(importPath string, matchString func(pat, str string) (bool, e
544544
}
545545
},
546546
benchTime: benchTime,
547-
context: ctx,
547+
bstate: bstate,
548548
}
549549
if Verbose() {
550550
main.chatty = newChattyPrinter(main.w)
@@ -554,15 +554,15 @@ func runBenchmarks(importPath string, matchString func(pat, str string) (bool, e
554554
}
555555

556556
// processBench runs bench b for the configured CPU counts and prints the results.
557-
func (ctx *benchContext) processBench(b *B) {
557+
func (s *benchState) processBench(b *B) {
558558
for i, procs := range cpuList {
559559
for j := uint(0); j < *count; j++ {
560560
runtime.GOMAXPROCS(procs)
561561
benchName := benchmarkName(b.name, procs)
562562

563563
// If it's chatty, we've already printed this information.
564564
if b.chatty == nil {
565-
fmt.Fprintf(b.w, "%-*s\t", ctx.maxLen, benchName)
565+
fmt.Fprintf(b.w, "%-*s\t", s.maxLen, benchName)
566566
}
567567
// Recompute the running time for all but the first iteration.
568568
if i > 0 || j > 0 {
@@ -589,7 +589,7 @@ func (ctx *benchContext) processBench(b *B) {
589589
}
590590
results := r.String()
591591
if b.chatty != nil {
592-
fmt.Fprintf(b.w, "%-*s\t", ctx.maxLen, benchName)
592+
fmt.Fprintf(b.w, "%-*s\t", s.maxLen, benchName)
593593
}
594594
if *benchmarkMemory || b.showAllocResult {
595595
results += "\t" + r.MemString()
@@ -629,8 +629,8 @@ func (b *B) Run(name string, f func(b *B)) bool {
629629
defer benchmarkLock.Lock()
630630

631631
benchName, ok, partial := b.name, true, false
632-
if b.context != nil {
633-
benchName, ok, partial = b.context.match.fullName(&b.common, name)
632+
if b.bstate != nil {
633+
benchName, ok, partial = b.bstate.match.fullName(&b.common, name)
634634
}
635635
if !ok {
636636
return true
@@ -651,7 +651,7 @@ func (b *B) Run(name string, f func(b *B)) bool {
651651
importPath: b.importPath,
652652
benchFunc: f,
653653
benchTime: b.benchTime,
654-
context: b.context,
654+
bstate: b.bstate,
655655
}
656656
if partial {
657657
// Partial name match, like -bench=X/Y matching BenchmarkX.

src/testing/fuzz.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ type InternalFuzzTarget struct {
6767
// that are allowed in the (*F).Fuzz function are (*F).Failed and (*F).Name.
6868
type F struct {
6969
common
70-
fuzzContext *fuzzContext
71-
testContext *testContext
70+
fstate *fuzzState
71+
tstate *testState
7272

7373
// inFuzzFn is true when the fuzz function is running. Most F methods cannot
7474
// be called when inFuzzFn is true.
@@ -244,22 +244,22 @@ func (f *F) Fuzz(ff any) {
244244
// corpus and entries declared with F.Add.
245245
//
246246
// Don't load the seed corpus if this is a worker process; we won't use it.
247-
if f.fuzzContext.mode != fuzzWorker {
247+
if f.fstate.mode != fuzzWorker {
248248
for _, c := range f.corpus {
249-
if err := f.fuzzContext.deps.CheckCorpus(c.Values, types); err != nil {
249+
if err := f.fstate.deps.CheckCorpus(c.Values, types); err != nil {
250250
// TODO(#48302): Report the source location of the F.Add call.
251251
f.Fatal(err)
252252
}
253253
}
254254

255255
// Load seed corpus
256-
c, err := f.fuzzContext.deps.ReadCorpus(filepath.Join(corpusDir, f.name), types)
256+
c, err := f.fstate.deps.ReadCorpus(filepath.Join(corpusDir, f.name), types)
257257
if err != nil {
258258
f.Fatal(err)
259259
}
260260
for i := range c {
261261
c[i].IsSeed = true // these are all seed corpus values
262-
if f.fuzzContext.mode == fuzzCoordinator {
262+
if f.fstate.mode == fuzzCoordinator {
263263
// If this is the coordinator process, zero the values, since we don't need
264264
// to hold onto them.
265265
c[i].Values = nil
@@ -285,12 +285,12 @@ func (f *F) Fuzz(ff any) {
285285
if e.Path != "" {
286286
testName = fmt.Sprintf("%s/%s", testName, filepath.Base(e.Path))
287287
}
288-
if f.testContext.isFuzzing {
288+
if f.tstate.isFuzzing {
289289
// Don't preserve subtest names while fuzzing. If fn calls T.Run,
290290
// there will be a very large number of subtests with duplicate names,
291291
// which will use a large amount of memory. The subtest names aren't
292292
// useful since there's no way to re-run them deterministically.
293-
f.testContext.match.clearSubNames()
293+
f.tstate.match.clearSubNames()
294294
}
295295

296296
// Record the stack trace at the point of this call so that if the subtest
@@ -308,7 +308,7 @@ func (f *F) Fuzz(ff any) {
308308
creator: pc[:n],
309309
chatty: f.chatty,
310310
},
311-
context: f.testContext,
311+
tstate: f.tstate,
312312
}
313313
if captureOut != nil {
314314
// t.parent aliases f.common.
@@ -328,9 +328,9 @@ func (f *F) Fuzz(ff any) {
328328
// we make sure it is called right before the tRunner function
329329
// exits, regardless of whether it was executed cleanly, panicked,
330330
// or if the fuzzFn called t.Fatal.
331-
if f.testContext.isFuzzing {
332-
defer f.fuzzContext.deps.SnapshotCoverage()
333-
f.fuzzContext.deps.ResetCoverage()
331+
if f.tstate.isFuzzing {
332+
defer f.fstate.deps.SnapshotCoverage()
333+
f.fstate.deps.ResetCoverage()
334334
}
335335
fn.Call(args)
336336
})
@@ -342,14 +342,14 @@ func (f *F) Fuzz(ff any) {
342342
return !t.Failed()
343343
}
344344

345-
switch f.fuzzContext.mode {
345+
switch f.fstate.mode {
346346
case fuzzCoordinator:
347347
// Fuzzing is enabled, and this is the test process started by 'go test'.
348348
// Act as the coordinator process, and coordinate workers to perform the
349349
// actual fuzzing.
350350
corpusTargetDir := filepath.Join(corpusDir, f.name)
351351
cacheTargetDir := filepath.Join(*fuzzCacheDir, f.name)
352-
err := f.fuzzContext.deps.CoordinateFuzzing(
352+
err := f.fstate.deps.CoordinateFuzzing(
353353
fuzzDuration.d,
354354
int64(fuzzDuration.n),
355355
minimizeDuration.d,
@@ -376,7 +376,7 @@ func (f *F) Fuzz(ff any) {
376376
case fuzzWorker:
377377
// Fuzzing is enabled, and this is a worker process. Follow instructions
378378
// from the coordinator.
379-
if err := f.fuzzContext.deps.RunFuzzWorker(func(e corpusEntry) error {
379+
if err := f.fstate.deps.RunFuzzWorker(func(e corpusEntry) error {
380380
// Don't write to f.w (which points to Stdout) if running from a
381381
// fuzz worker. This would become very verbose, particularly during
382382
// minimization. Return the error instead, and let the caller deal
@@ -398,7 +398,7 @@ func (f *F) Fuzz(ff any) {
398398
// corpus now.
399399
for _, e := range f.corpus {
400400
name := fmt.Sprintf("%s/%s", f.name, filepath.Base(e.Path))
401-
if _, ok, _ := f.testContext.match.fullName(nil, name); ok {
401+
if _, ok, _ := f.tstate.match.fullName(nil, name); ok {
402402
run(f.w, e)
403403
}
404404
}
@@ -451,8 +451,8 @@ type fuzzCrashError interface {
451451
CrashPath() string
452452
}
453453

454-
// fuzzContext holds fields common to all fuzz tests.
455-
type fuzzContext struct {
454+
// fuzzState holds fields common to all fuzz tests.
455+
type fuzzState struct {
456456
deps testDeps
457457
mode fuzzMode
458458
}
@@ -486,9 +486,9 @@ func runFuzzTests(deps testDeps, fuzzTests []InternalFuzzTarget, deadline time.T
486486
break
487487
}
488488

489-
tctx := newTestContext(*parallel, m)
490-
tctx.deadline = deadline
491-
fctx := &fuzzContext{deps: deps, mode: seedCorpusOnly}
489+
tstate := newTestState(*parallel, m)
490+
tstate.deadline = deadline
491+
fstate := &fuzzState{deps: deps, mode: seedCorpusOnly}
492492
root := common{w: os.Stdout} // gather output in one place
493493
if Verbose() {
494494
root.chatty = newChattyPrinter(root.w)
@@ -497,7 +497,7 @@ func runFuzzTests(deps testDeps, fuzzTests []InternalFuzzTarget, deadline time.T
497497
if shouldFailFast() {
498498
break
499499
}
500-
testName, matched, _ := tctx.match.fullName(nil, ft.Name)
500+
testName, matched, _ := tstate.match.fullName(nil, ft.Name)
501501
if !matched {
502502
continue
503503
}
@@ -517,8 +517,8 @@ func runFuzzTests(deps testDeps, fuzzTests []InternalFuzzTarget, deadline time.T
517517
level: root.level + 1,
518518
chatty: root.chatty,
519519
},
520-
testContext: tctx,
521-
fuzzContext: fctx,
520+
tstate: tstate,
521+
fstate: fstate,
522522
}
523523
f.w = indenter{&f.common}
524524
if f.chatty != nil {
@@ -554,17 +554,17 @@ func runFuzzing(deps testDeps, fuzzTests []InternalFuzzTarget) (ok bool) {
554554
return true
555555
}
556556
m := newMatcher(deps.MatchString, *matchFuzz, "-test.fuzz", *skip)
557-
tctx := newTestContext(1, m)
558-
tctx.isFuzzing = true
559-
fctx := &fuzzContext{
557+
tstate := newTestState(1, m)
558+
tstate.isFuzzing = true
559+
fstate := &fuzzState{
560560
deps: deps,
561561
}
562562
root := common{w: os.Stdout}
563563
if *isFuzzWorker {
564564
root.w = io.Discard
565-
fctx.mode = fuzzWorker
565+
fstate.mode = fuzzWorker
566566
} else {
567-
fctx.mode = fuzzCoordinator
567+
fstate.mode = fuzzCoordinator
568568
}
569569
if Verbose() && !*isFuzzWorker {
570570
root.chatty = newChattyPrinter(root.w)
@@ -573,7 +573,7 @@ func runFuzzing(deps testDeps, fuzzTests []InternalFuzzTarget) (ok bool) {
573573
var testName string
574574
var matched []string
575575
for i := range fuzzTests {
576-
name, ok, _ := tctx.match.fullName(nil, fuzzTests[i].Name)
576+
name, ok, _ := tstate.match.fullName(nil, fuzzTests[i].Name)
577577
if !ok {
578578
continue
579579
}
@@ -599,8 +599,8 @@ func runFuzzing(deps testDeps, fuzzTests []InternalFuzzTarget) (ok bool) {
599599
level: root.level + 1,
600600
chatty: root.chatty,
601601
},
602-
fuzzContext: fctx,
603-
testContext: tctx,
602+
fstate: fstate,
603+
tstate: tstate,
604604
}
605605
f.w = indenter{&f.common}
606606
if f.chatty != nil {
@@ -694,7 +694,7 @@ func fRunner(f *F, fn func(*F)) {
694694
// This only affects fuzz tests run as normal tests.
695695
// While fuzzing, T.Parallel has no effect, so f.sub is empty, and this
696696
// branch is not taken. f.barrier is nil in that case.
697-
f.testContext.release()
697+
f.tstate.release()
698698
close(f.barrier)
699699
// Wait for the subtests to complete.
700700
for _, sub := range f.sub {

0 commit comments

Comments
 (0)