Source file src/testing/testing.go

     1  // Copyright 2009 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 provides support for automated testing of Go packages.
     6  // It is intended to be used in concert with the "go test" command, which automates
     7  // execution of any function of the form
     8  //
     9  //	func TestXxx(*testing.T)
    10  //
    11  // where Xxx does not start with a lowercase letter. The function name
    12  // serves to identify the test routine.
    13  //
    14  // Within these functions, use the Error, Fail or related methods to signal failure.
    15  //
    16  // To write a new test suite, create a file that
    17  // contains the TestXxx functions as described here,
    18  // and give that file a name ending in "_test.go".
    19  // The file will be excluded from regular
    20  // package builds but will be included when the "go test" command is run.
    21  //
    22  // The test file can be in the same package as the one being tested,
    23  // or in a corresponding package with the suffix "_test".
    24  //
    25  // If the test file is in the same package, it may refer to unexported
    26  // identifiers within the package, as in this example:
    27  //
    28  //	package abs
    29  //
    30  //	import "testing"
    31  //
    32  //	func TestAbs(t *testing.T) {
    33  //	    got := Abs(-1)
    34  //	    if got != 1 {
    35  //	        t.Errorf("Abs(-1) = %d; want 1", got)
    36  //	    }
    37  //	}
    38  //
    39  // If the file is in a separate "_test" package, the package being tested
    40  // must be imported explicitly and only its exported identifiers may be used.
    41  // This is known as "black box" testing.
    42  //
    43  //	package abs_test
    44  //
    45  //	import (
    46  //		"testing"
    47  //
    48  //		"path_to_pkg/abs"
    49  //	)
    50  //
    51  //	func TestAbs(t *testing.T) {
    52  //	    got := abs.Abs(-1)
    53  //	    if got != 1 {
    54  //	        t.Errorf("Abs(-1) = %d; want 1", got)
    55  //	    }
    56  //	}
    57  //
    58  // For more detail, run "go help test" and "go help testflag".
    59  //
    60  // # Benchmarks
    61  //
    62  // Functions of the form
    63  //
    64  //	func BenchmarkXxx(*testing.B)
    65  //
    66  // are considered benchmarks, and are executed by the "go test" command when
    67  // its -bench flag is provided. Benchmarks are run sequentially.
    68  //
    69  // For a description of the testing flags, see
    70  // https://golang.org/cmd/go/#hdr-Testing_flags.
    71  //
    72  // A sample benchmark function looks like this:
    73  //
    74  //	func BenchmarkRandInt(b *testing.B) {
    75  //	    for range b.N {
    76  //	        rand.Int()
    77  //	    }
    78  //	}
    79  //
    80  // The benchmark function must run the target code b.N times.
    81  // It is called multiple times with b.N adjusted until the
    82  // benchmark function lasts long enough to be timed reliably.
    83  // The output
    84  //
    85  //	BenchmarkRandInt-8   	68453040	        17.8 ns/op
    86  //
    87  // means that the loop ran 68453040 times at a speed of 17.8 ns per loop.
    88  //
    89  // If a benchmark needs some expensive setup before running, the timer
    90  // may be reset:
    91  //
    92  //	func BenchmarkBigLen(b *testing.B) {
    93  //	    big := NewBig()
    94  //	    b.ResetTimer()
    95  //	    for range b.N {
    96  //	        big.Len()
    97  //	    }
    98  //	}
    99  //
   100  // If a benchmark needs to test performance in a parallel setting, it may use
   101  // the RunParallel helper function; such benchmarks are intended to be used with
   102  // the go test -cpu flag:
   103  //
   104  //	func BenchmarkTemplateParallel(b *testing.B) {
   105  //	    templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))
   106  //	    b.RunParallel(func(pb *testing.PB) {
   107  //	        var buf bytes.Buffer
   108  //	        for pb.Next() {
   109  //	            buf.Reset()
   110  //	            templ.Execute(&buf, "World")
   111  //	        }
   112  //	    })
   113  //	}
   114  //
   115  // A detailed specification of the benchmark results format is given
   116  // in https://golang.org/design/14313-benchmark-format.
   117  //
   118  // There are standard tools for working with benchmark results at
   119  // https://golang.org/x/perf/cmd.
   120  // In particular, https://golang.org/x/perf/cmd/benchstat performs
   121  // statistically robust A/B comparisons.
   122  //
   123  // # Examples
   124  //
   125  // The package also runs and verifies example code. Example functions may
   126  // include a concluding line comment that begins with "Output:" and is compared with
   127  // the standard output of the function when the tests are run. (The comparison
   128  // ignores leading and trailing space.) These are examples of an example:
   129  //
   130  //	func ExampleHello() {
   131  //	    fmt.Println("hello")
   132  //	    // Output: hello
   133  //	}
   134  //
   135  //	func ExampleSalutations() {
   136  //	    fmt.Println("hello, and")
   137  //	    fmt.Println("goodbye")
   138  //	    // Output:
   139  //	    // hello, and
   140  //	    // goodbye
   141  //	}
   142  //
   143  // The comment prefix "Unordered output:" is like "Output:", but matches any
   144  // line order:
   145  //
   146  //	func ExamplePerm() {
   147  //	    for _, value := range Perm(5) {
   148  //	        fmt.Println(value)
   149  //	    }
   150  //	    // Unordered output: 4
   151  //	    // 2
   152  //	    // 1
   153  //	    // 3
   154  //	    // 0
   155  //	}
   156  //
   157  // Example functions without output comments are compiled but not executed.
   158  //
   159  // The naming convention to declare examples for the package, a function F, a type T and
   160  // method M on type T are:
   161  //
   162  //	func Example() { ... }
   163  //	func ExampleF() { ... }
   164  //	func ExampleT() { ... }
   165  //	func ExampleT_M() { ... }
   166  //
   167  // Multiple example functions for a package/type/function/method may be provided by
   168  // appending a distinct suffix to the name. The suffix must start with a
   169  // lower-case letter.
   170  //
   171  //	func Example_suffix() { ... }
   172  //	func ExampleF_suffix() { ... }
   173  //	func ExampleT_suffix() { ... }
   174  //	func ExampleT_M_suffix() { ... }
   175  //
   176  // The entire test file is presented as the example when it contains a single
   177  // example function, at least one other function, type, variable, or constant
   178  // declaration, and no test or benchmark functions.
   179  //
   180  // # Fuzzing
   181  //
   182  // 'go test' and the testing package support fuzzing, a testing technique where
   183  // a function is called with randomly generated inputs to find bugs not
   184  // anticipated by unit tests.
   185  //
   186  // Functions of the form
   187  //
   188  //	func FuzzXxx(*testing.F)
   189  //
   190  // are considered fuzz tests.
   191  //
   192  // For example:
   193  //
   194  //	func FuzzHex(f *testing.F) {
   195  //	  for _, seed := range [][]byte{{}, {0}, {9}, {0xa}, {0xf}, {1, 2, 3, 4}} {
   196  //	    f.Add(seed)
   197  //	  }
   198  //	  f.Fuzz(func(t *testing.T, in []byte) {
   199  //	    enc := hex.EncodeToString(in)
   200  //	    out, err := hex.DecodeString(enc)
   201  //	    if err != nil {
   202  //	      t.Fatalf("%v: decode: %v", in, err)
   203  //	    }
   204  //	    if !bytes.Equal(in, out) {
   205  //	      t.Fatalf("%v: not equal after round trip: %v", in, out)
   206  //	    }
   207  //	  })
   208  //	}
   209  //
   210  // A fuzz test maintains a seed corpus, or a set of inputs which are run by
   211  // default, and can seed input generation. Seed inputs may be registered by
   212  // calling (*F).Add or by storing files in the directory testdata/fuzz/<Name>
   213  // (where <Name> is the name of the fuzz test) within the package containing
   214  // the fuzz test. Seed inputs are optional, but the fuzzing engine may find
   215  // bugs more efficiently when provided with a set of small seed inputs with good
   216  // code coverage. These seed inputs can also serve as regression tests for bugs
   217  // identified through fuzzing.
   218  //
   219  // The function passed to (*F).Fuzz within the fuzz test is considered the fuzz
   220  // target. A fuzz target must accept a *T parameter, followed by one or more
   221  // parameters for random inputs. The types of arguments passed to (*F).Add must
   222  // be identical to the types of these parameters. The fuzz target may signal
   223  // that it's found a problem the same way tests do: by calling T.Fail (or any
   224  // method that calls it like T.Error or T.Fatal) or by panicking.
   225  //
   226  // When fuzzing is enabled (by setting the -fuzz flag to a regular expression
   227  // that matches a specific fuzz test), the fuzz target is called with arguments
   228  // generated by repeatedly making random changes to the seed inputs. On
   229  // supported platforms, 'go test' compiles the test executable with fuzzing
   230  // coverage instrumentation. The fuzzing engine uses that instrumentation to
   231  // find and cache inputs that expand coverage, increasing the likelihood of
   232  // finding bugs. If the fuzz target fails for a given input, the fuzzing engine
   233  // writes the inputs that caused the failure to a file in the directory
   234  // testdata/fuzz/<Name> within the package directory. This file later serves as
   235  // a seed input. If the file can't be written at that location (for example,
   236  // because the directory is read-only), the fuzzing engine writes the file to
   237  // the fuzz cache directory within the build cache instead.
   238  //
   239  // When fuzzing is disabled, the fuzz target is called with the seed inputs
   240  // registered with F.Add and seed inputs from testdata/fuzz/<Name>. In this
   241  // mode, the fuzz test acts much like a regular test, with subtests started
   242  // with F.Fuzz instead of T.Run.
   243  //
   244  // See https://go.dev/doc/fuzz for documentation about fuzzing.
   245  //
   246  // # Skipping
   247  //
   248  // Tests or benchmarks may be skipped at run time with a call to
   249  // the Skip method of *T or *B:
   250  //
   251  //	func TestTimeConsuming(t *testing.T) {
   252  //	    if testing.Short() {
   253  //	        t.Skip("skipping test in short mode.")
   254  //	    }
   255  //	    ...
   256  //	}
   257  //
   258  // The Skip method of *T can be used in a fuzz target if the input is invalid,
   259  // but should not be considered a failing input. For example:
   260  //
   261  //	func FuzzJSONMarshaling(f *testing.F) {
   262  //	    f.Fuzz(func(t *testing.T, b []byte) {
   263  //	        var v interface{}
   264  //	        if err := json.Unmarshal(b, &v); err != nil {
   265  //	            t.Skip()
   266  //	        }
   267  //	        if _, err := json.Marshal(v); err != nil {
   268  //	            t.Errorf("Marshal: %v", err)
   269  //	        }
   270  //	    })
   271  //	}
   272  //
   273  // # Subtests and Sub-benchmarks
   274  //
   275  // The Run methods of T and B allow defining subtests and sub-benchmarks,
   276  // without having to define separate functions for each. This enables uses
   277  // like table-driven benchmarks and creating hierarchical tests.
   278  // It also provides a way to share common setup and tear-down code:
   279  //
   280  //	func TestFoo(t *testing.T) {
   281  //	    // <setup code>
   282  //	    t.Run("A=1", func(t *testing.T) { ... })
   283  //	    t.Run("A=2", func(t *testing.T) { ... })
   284  //	    t.Run("B=1", func(t *testing.T) { ... })
   285  //	    // <tear-down code>
   286  //	}
   287  //
   288  // Each subtest and sub-benchmark has a unique name: the combination of the name
   289  // of the top-level test and the sequence of names passed to Run, separated by
   290  // slashes, with an optional trailing sequence number for disambiguation.
   291  //
   292  // The argument to the -run, -bench, and -fuzz command-line flags is an unanchored regular
   293  // expression that matches the test's name. For tests with multiple slash-separated
   294  // elements, such as subtests, the argument is itself slash-separated, with
   295  // expressions matching each name element in turn. Because it is unanchored, an
   296  // empty expression matches any string.
   297  // For example, using "matching" to mean "whose name contains":
   298  //
   299  //	go test -run ''        # Run all tests.
   300  //	go test -run Foo       # Run top-level tests matching "Foo", such as "TestFooBar".
   301  //	go test -run Foo/A=    # For top-level tests matching "Foo", run subtests matching "A=".
   302  //	go test -run /A=1      # For all top-level tests, run subtests matching "A=1".
   303  //	go test -fuzz FuzzFoo  # Fuzz the target matching "FuzzFoo"
   304  //
   305  // The -run argument can also be used to run a specific value in the seed
   306  // corpus, for debugging. For example:
   307  //
   308  //	go test -run=FuzzFoo/9ddb952d9814
   309  //
   310  // The -fuzz and -run flags can both be set, in order to fuzz a target but
   311  // skip the execution of all other tests.
   312  //
   313  // Subtests can also be used to control parallelism. A parent test will only
   314  // complete once all of its subtests complete. In this example, all tests are
   315  // run in parallel with each other, and only with each other, regardless of
   316  // other top-level tests that may be defined:
   317  //
   318  //	func TestGroupedParallel(t *testing.T) {
   319  //	    for _, tc := range tests {
   320  //	        tc := tc // capture range variable
   321  //	        t.Run(tc.Name, func(t *testing.T) {
   322  //	            t.Parallel()
   323  //	            ...
   324  //	        })
   325  //	    }
   326  //	}
   327  //
   328  // Run does not return until parallel subtests have completed, providing a way
   329  // to clean up after a group of parallel tests:
   330  //
   331  //	func TestTeardownParallel(t *testing.T) {
   332  //	    // This Run will not return until the parallel tests finish.
   333  //	    t.Run("group", func(t *testing.T) {
   334  //	        t.Run("Test1", parallelTest1)
   335  //	        t.Run("Test2", parallelTest2)
   336  //	        t.Run("Test3", parallelTest3)
   337  //	    })
   338  //	    // <tear-down code>
   339  //	}
   340  //
   341  // # Main
   342  //
   343  // It is sometimes necessary for a test or benchmark program to do extra setup or teardown
   344  // before or after it executes. It is also sometimes necessary to control
   345  // which code runs on the main thread. To support these and other cases,
   346  // if a test file contains a function:
   347  //
   348  //	func TestMain(m *testing.M)
   349  //
   350  // then the generated test will call TestMain(m) instead of running the tests or benchmarks
   351  // directly. TestMain runs in the main goroutine and can do whatever setup
   352  // and teardown is necessary around a call to m.Run. m.Run will return an exit
   353  // code that may be passed to os.Exit. If TestMain returns, the test wrapper
   354  // will pass the result of m.Run to os.Exit itself.
   355  //
   356  // When TestMain is called, flag.Parse has not been run. If TestMain depends on
   357  // command-line flags, including those of the testing package, it should call
   358  // flag.Parse explicitly. Command line flags are always parsed by the time test
   359  // or benchmark functions run.
   360  //
   361  // A simple implementation of TestMain is:
   362  //
   363  //	func TestMain(m *testing.M) {
   364  //		// call flag.Parse() here if TestMain uses flags
   365  //		os.Exit(m.Run())
   366  //	}
   367  //
   368  // TestMain is a low-level primitive and should not be necessary for casual
   369  // testing needs, where ordinary test functions suffice.
   370  package testing
   371  
   372  import (
   373  	"bytes"
   374  	"errors"
   375  	"flag"
   376  	"fmt"
   377  	"internal/goexperiment"
   378  	"internal/race"
   379  	"io"
   380  	"math/rand"
   381  	"os"
   382  	"reflect"
   383  	"runtime"
   384  	"runtime/debug"
   385  	"runtime/trace"
   386  	"sort"
   387  	"strconv"
   388  	"strings"
   389  	"sync"
   390  	"sync/atomic"
   391  	"time"
   392  	"unicode"
   393  	"unicode/utf8"
   394  )
   395  
   396  var initRan bool
   397  
   398  // Init registers testing flags. These flags are automatically registered by
   399  // the "go test" command before running test functions, so Init is only needed
   400  // when calling functions such as Benchmark without using "go test".
   401  //
   402  // Init is not safe to call concurrently. It has no effect if it was already called.
   403  func Init() {
   404  	if initRan {
   405  		return
   406  	}
   407  	initRan = true
   408  	// The short flag requests that tests run more quickly, but its functionality
   409  	// is provided by test writers themselves. The testing package is just its
   410  	// home. The all.bash installation script sets it to make installation more
   411  	// efficient, but by default the flag is off so a plain "go test" will do a
   412  	// full test of the package.
   413  	short = flag.Bool("test.short", false, "run smaller test suite to save time")
   414  
   415  	// The failfast flag requests that test execution stop after the first test failure.
   416  	failFast = flag.Bool("test.failfast", false, "do not start new tests after the first test failure")
   417  
   418  	// The directory in which to create profile files and the like. When run from
   419  	// "go test", the binary always runs in the source directory for the package;
   420  	// this flag lets "go test" tell the binary to write the files in the directory where
   421  	// the "go test" command is run.
   422  	outputDir = flag.String("test.outputdir", "", "write profiles to `dir`")
   423  	// Report as tests are run; default is silent for success.
   424  	flag.Var(&chatty, "test.v", "verbose: print additional output")
   425  	count = flag.Uint("test.count", 1, "run tests and benchmarks `n` times")
   426  	coverProfile = flag.String("test.coverprofile", "", "write a coverage profile to `file`")
   427  	gocoverdir = flag.String("test.gocoverdir", "", "write coverage intermediate files to this directory")
   428  	matchList = flag.String("test.list", "", "list tests, examples, and benchmarks matching `regexp` then exit")
   429  	match = flag.String("test.run", "", "run only tests and examples matching `regexp`")
   430  	skip = flag.String("test.skip", "", "do not list or run tests matching `regexp`")
   431  	memProfile = flag.String("test.memprofile", "", "write an allocation profile to `file`")
   432  	memProfileRate = flag.Int("test.memprofilerate", 0, "set memory allocation profiling `rate` (see runtime.MemProfileRate)")
   433  	cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profile to `file`")
   434  	blockProfile = flag.String("test.blockprofile", "", "write a goroutine blocking profile to `file`")
   435  	blockProfileRate = flag.Int("test.blockprofilerate", 1, "set blocking profile `rate` (see runtime.SetBlockProfileRate)")
   436  	mutexProfile = flag.String("test.mutexprofile", "", "write a mutex contention profile to the named file after execution")
   437  	mutexProfileFraction = flag.Int("test.mutexprofilefraction", 1, "if >= 0, calls runtime.SetMutexProfileFraction()")
   438  	panicOnExit0 = flag.Bool("test.paniconexit0", false, "panic on call to os.Exit(0)")
   439  	traceFile = flag.String("test.trace", "", "write an execution trace to `file`")
   440  	timeout = flag.Duration("test.timeout", 0, "panic test binary after duration `d` (default 0, timeout disabled)")
   441  	cpuListStr = flag.String("test.cpu", "", "comma-separated `list` of cpu counts to run each test with")
   442  	parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "run at most `n` tests in parallel")
   443  	testlog = flag.String("test.testlogfile", "", "write test action log to `file` (for use only by cmd/go)")
   444  	shuffle = flag.String("test.shuffle", "off", "randomize the execution order of tests and benchmarks")
   445  	fullPath = flag.Bool("test.fullpath", false, "show full file names in error messages")
   446  
   447  	initBenchmarkFlags()
   448  	initFuzzFlags()
   449  }
   450  
   451  var (
   452  	// Flags, registered during Init.
   453  	short                *bool
   454  	failFast             *bool
   455  	outputDir            *string
   456  	chatty               chattyFlag
   457  	count                *uint
   458  	coverProfile         *string
   459  	gocoverdir           *string
   460  	matchList            *string
   461  	match                *string
   462  	skip                 *string
   463  	memProfile           *string
   464  	memProfileRate       *int
   465  	cpuProfile           *string
   466  	blockProfile         *string
   467  	blockProfileRate     *int
   468  	mutexProfile         *string
   469  	mutexProfileFraction *int
   470  	panicOnExit0         *bool
   471  	traceFile            *string
   472  	timeout              *time.Duration
   473  	cpuListStr           *string
   474  	parallel             *int
   475  	shuffle              *string
   476  	testlog              *string
   477  	fullPath             *bool
   478  
   479  	haveExamples bool // are there examples?
   480  
   481  	cpuList     []int
   482  	testlogFile *os.File
   483  
   484  	numFailed atomic.Uint32 // number of test failures
   485  
   486  	running sync.Map // map[string]time.Time of running, unpaused tests
   487  )
   488  
   489  type chattyFlag struct {
   490  	on   bool // -v is set in some form
   491  	json bool // -v=test2json is set, to make output better for test2json
   492  }
   493  
   494  func (*chattyFlag) IsBoolFlag() bool { return true }
   495  
   496  func (f *chattyFlag) Set(arg string) error {
   497  	switch arg {
   498  	default:
   499  		return fmt.Errorf("invalid flag -test.v=%s", arg)
   500  	case "true", "test2json":
   501  		f.on = true
   502  		f.json = arg == "test2json"
   503  	case "false":
   504  		f.on = false
   505  		f.json = false
   506  	}
   507  	return nil
   508  }
   509  
   510  func (f *chattyFlag) String() string {
   511  	if f.json {
   512  		return "test2json"
   513  	}
   514  	if f.on {
   515  		return "true"
   516  	}
   517  	return "false"
   518  }
   519  
   520  func (f *chattyFlag) Get() any {
   521  	if f.json {
   522  		return "test2json"
   523  	}
   524  	return f.on
   525  }
   526  
   527  const marker = byte(0x16) // ^V for framing
   528  
   529  func (f *chattyFlag) prefix() string {
   530  	if f.json {
   531  		return string(marker)
   532  	}
   533  	return ""
   534  }
   535  
   536  type chattyPrinter struct {
   537  	w          io.Writer
   538  	lastNameMu sync.Mutex // guards lastName
   539  	lastName   string     // last printed test name in chatty mode
   540  	json       bool       // -v=json output mode
   541  }
   542  
   543  func newChattyPrinter(w io.Writer) *chattyPrinter {
   544  	return &chattyPrinter{w: w, json: chatty.json}
   545  }
   546  
   547  // prefix is like chatty.prefix but using p.json instead of chatty.json.
   548  // Using p.json allows tests to check the json behavior without modifying
   549  // the global variable. For convenience, we allow p == nil and treat
   550  // that as not in json mode (because it's not chatty at all).
   551  func (p *chattyPrinter) prefix() string {
   552  	if p != nil && p.json {
   553  		return string(marker)
   554  	}
   555  	return ""
   556  }
   557  
   558  // Updatef prints a message about the status of the named test to w.
   559  //
   560  // The formatted message must include the test name itself.
   561  func (p *chattyPrinter) Updatef(testName, format string, args ...any) {
   562  	p.lastNameMu.Lock()
   563  	defer p.lastNameMu.Unlock()
   564  
   565  	// Since the message already implies an association with a specific new test,
   566  	// we don't need to check what the old test name was or log an extra NAME line
   567  	// for it. (We're updating it anyway, and the current message already includes
   568  	// the test name.)
   569  	p.lastName = testName
   570  	fmt.Fprintf(p.w, p.prefix()+format, args...)
   571  }
   572  
   573  // Printf prints a message, generated by the named test, that does not
   574  // necessarily mention that tests's name itself.
   575  func (p *chattyPrinter) Printf(testName, format string, args ...any) {
   576  	p.lastNameMu.Lock()
   577  	defer p.lastNameMu.Unlock()
   578  
   579  	if p.lastName == "" {
   580  		p.lastName = testName
   581  	} else if p.lastName != testName {
   582  		fmt.Fprintf(p.w, "%s=== NAME  %s\n", p.prefix(), testName)
   583  		p.lastName = testName
   584  	}
   585  
   586  	fmt.Fprintf(p.w, format, args...)
   587  }
   588  
   589  // The maximum number of stack frames to go through when skipping helper functions for
   590  // the purpose of decorating log messages.
   591  const maxStackLen = 50
   592  
   593  // common holds the elements common between T and B and
   594  // captures common methods such as Errorf.
   595  type common struct {
   596  	mu          sync.RWMutex         // guards this group of fields
   597  	output      []byte               // Output generated by test or benchmark.
   598  	w           io.Writer            // For flushToParent.
   599  	ran         bool                 // Test or benchmark (or one of its subtests) was executed.
   600  	failed      bool                 // Test or benchmark has failed.
   601  	skipped     bool                 // Test or benchmark has been skipped.
   602  	done        bool                 // Test is finished and all subtests have completed.
   603  	helperPCs   map[uintptr]struct{} // functions to be skipped when writing file/line info
   604  	helperNames map[string]struct{}  // helperPCs converted to function names
   605  	cleanups    []func()             // optional functions to be called at the end of the test
   606  	cleanupName string               // Name of the cleanup function.
   607  	cleanupPc   []uintptr            // The stack trace at the point where Cleanup was called.
   608  	finished    bool                 // Test function has completed.
   609  	inFuzzFn    bool                 // Whether the fuzz target, if this is one, is running.
   610  
   611  	chatty         *chattyPrinter // A copy of chattyPrinter, if the chatty flag is set.
   612  	bench          bool           // Whether the current test is a benchmark.
   613  	hasSub         atomic.Bool    // whether there are sub-benchmarks.
   614  	cleanupStarted atomic.Bool    // Registered cleanup callbacks have started to execute
   615  	runner         string         // Function name of tRunner running the test.
   616  	isParallel     bool           // Whether the test is parallel.
   617  
   618  	parent   *common
   619  	level    int               // Nesting depth of test or benchmark.
   620  	creator  []uintptr         // If level > 0, the stack trace at the point where the parent called t.Run.
   621  	name     string            // Name of test or benchmark.
   622  	start    highPrecisionTime // Time test or benchmark started
   623  	duration time.Duration
   624  	barrier  chan bool // To signal parallel subtests they may start. Nil when T.Parallel is not present (B) or not usable (when fuzzing).
   625  	signal   chan bool // To signal a test is done.
   626  	sub      []*T      // Queue of subtests to be run in parallel.
   627  
   628  	lastRaceErrors  atomic.Int64 // Max value of race.Errors seen during the test or its subtests.
   629  	raceErrorLogged atomic.Bool
   630  
   631  	tempDirMu  sync.Mutex
   632  	tempDir    string
   633  	tempDirErr error
   634  	tempDirSeq int32
   635  }
   636  
   637  // Short reports whether the -test.short flag is set.
   638  func Short() bool {
   639  	if short == nil {
   640  		panic("testing: Short called before Init")
   641  	}
   642  	// Catch code that calls this from TestMain without first calling flag.Parse.
   643  	if !flag.Parsed() {
   644  		panic("testing: Short called before Parse")
   645  	}
   646  
   647  	return *short
   648  }
   649  
   650  // testBinary is set by cmd/go to "1" if this is a binary built by "go test".
   651  // The value is set to "1" by a -X option to cmd/link. We assume that
   652  // because this is possible, the compiler will not optimize testBinary
   653  // into a constant on the basis that it is an unexported package-scope
   654  // variable that is never changed. If the compiler ever starts implementing
   655  // such an optimization, we will need some technique to mark this variable
   656  // as "changed by a cmd/link -X option".
   657  var testBinary = "0"
   658  
   659  // Testing reports whether the current code is being run in a test.
   660  // This will report true in programs created by "go test",
   661  // false in programs created by "go build".
   662  func Testing() bool {
   663  	return testBinary == "1"
   664  }
   665  
   666  // CoverMode reports what the test coverage mode is set to. The
   667  // values are "set", "count", or "atomic". The return value will be
   668  // empty if test coverage is not enabled.
   669  func CoverMode() string {
   670  	if goexperiment.CoverageRedesign {
   671  		return cover2.mode
   672  	}
   673  	return cover.Mode
   674  }
   675  
   676  // Verbose reports whether the -test.v flag is set.
   677  func Verbose() bool {
   678  	// Same as in Short.
   679  	if !flag.Parsed() {
   680  		panic("testing: Verbose called before Parse")
   681  	}
   682  	return chatty.on
   683  }
   684  
   685  func (c *common) checkFuzzFn(name string) {
   686  	if c.inFuzzFn {
   687  		panic(fmt.Sprintf("testing: f.%s was called inside the fuzz target, use t.%s instead", name, name))
   688  	}
   689  }
   690  
   691  // frameSkip searches, starting after skip frames, for the first caller frame
   692  // in a function not marked as a helper and returns that frame.
   693  // The search stops if it finds a tRunner function that
   694  // was the entry point into the test and the test is not a subtest.
   695  // This function must be called with c.mu held.
   696  func (c *common) frameSkip(skip int) runtime.Frame {
   697  	// If the search continues into the parent test, we'll have to hold
   698  	// its mu temporarily. If we then return, we need to unlock it.
   699  	shouldUnlock := false
   700  	defer func() {
   701  		if shouldUnlock {
   702  			c.mu.Unlock()
   703  		}
   704  	}()
   705  	var pc [maxStackLen]uintptr
   706  	// Skip two extra frames to account for this function
   707  	// and runtime.Callers itself.
   708  	n := runtime.Callers(skip+2, pc[:])
   709  	if n == 0 {
   710  		panic("testing: zero callers found")
   711  	}
   712  	frames := runtime.CallersFrames(pc[:n])
   713  	var firstFrame, prevFrame, frame runtime.Frame
   714  	for more := true; more; prevFrame = frame {
   715  		frame, more = frames.Next()
   716  		if frame.Function == "runtime.gopanic" {
   717  			continue
   718  		}
   719  		if frame.Function == c.cleanupName {
   720  			frames = runtime.CallersFrames(c.cleanupPc)
   721  			continue
   722  		}
   723  		if firstFrame.PC == 0 {
   724  			firstFrame = frame
   725  		}
   726  		if frame.Function == c.runner {
   727  			// We've gone up all the way to the tRunner calling
   728  			// the test function (so the user must have
   729  			// called tb.Helper from inside that test function).
   730  			// If this is a top-level test, only skip up to the test function itself.
   731  			// If we're in a subtest, continue searching in the parent test,
   732  			// starting from the point of the call to Run which created this subtest.
   733  			if c.level > 1 {
   734  				frames = runtime.CallersFrames(c.creator)
   735  				parent := c.parent
   736  				// We're no longer looking at the current c after this point,
   737  				// so we should unlock its mu, unless it's the original receiver,
   738  				// in which case our caller doesn't expect us to do that.
   739  				if shouldUnlock {
   740  					c.mu.Unlock()
   741  				}
   742  				c = parent
   743  				// Remember to unlock c.mu when we no longer need it, either
   744  				// because we went up another nesting level, or because we
   745  				// returned.
   746  				shouldUnlock = true
   747  				c.mu.Lock()
   748  				continue
   749  			}
   750  			return prevFrame
   751  		}
   752  		// If more helper PCs have been added since we last did the conversion
   753  		if c.helperNames == nil {
   754  			c.helperNames = make(map[string]struct{})
   755  			for pc := range c.helperPCs {
   756  				c.helperNames[pcToName(pc)] = struct{}{}
   757  			}
   758  		}
   759  		if _, ok := c.helperNames[frame.Function]; !ok {
   760  			// Found a frame that wasn't inside a helper function.
   761  			return frame
   762  		}
   763  	}
   764  	return firstFrame
   765  }
   766  
   767  // decorate prefixes the string with the file and line of the call site
   768  // and inserts the final newline if needed and indentation spaces for formatting.
   769  // This function must be called with c.mu held.
   770  func (c *common) decorate(s string, skip int) string {
   771  	frame := c.frameSkip(skip)
   772  	file := frame.File
   773  	line := frame.Line
   774  	if file != "" {
   775  		if *fullPath {
   776  			// If relative path, truncate file name at last file name separator.
   777  		} else if index := strings.LastIndexAny(file, `/\`); index >= 0 {
   778  			file = file[index+1:]
   779  		}
   780  	} else {
   781  		file = "???"
   782  	}
   783  	if line == 0 {
   784  		line = 1
   785  	}
   786  	buf := new(strings.Builder)
   787  	// Every line is indented at least 4 spaces.
   788  	buf.WriteString("    ")
   789  	fmt.Fprintf(buf, "%s:%d: ", file, line)
   790  	lines := strings.Split(s, "\n")
   791  	if l := len(lines); l > 1 && lines[l-1] == "" {
   792  		lines = lines[:l-1]
   793  	}
   794  	for i, line := range lines {
   795  		if i > 0 {
   796  			// Second and subsequent lines are indented an additional 4 spaces.
   797  			buf.WriteString("\n        ")
   798  		}
   799  		buf.WriteString(line)
   800  	}
   801  	buf.WriteByte('\n')
   802  	return buf.String()
   803  }
   804  
   805  // flushToParent writes c.output to the parent after first writing the header
   806  // with the given format and arguments.
   807  func (c *common) flushToParent(testName, format string, args ...any) {
   808  	p := c.parent
   809  	p.mu.Lock()
   810  	defer p.mu.Unlock()
   811  
   812  	c.mu.Lock()
   813  	defer c.mu.Unlock()
   814  
   815  	if len(c.output) > 0 {
   816  		// Add the current c.output to the print,
   817  		// and then arrange for the print to replace c.output.
   818  		// (This displays the logged output after the --- FAIL line.)
   819  		format += "%s"
   820  		args = append(args[:len(args):len(args)], c.output)
   821  		c.output = c.output[:0]
   822  	}
   823  
   824  	if c.chatty != nil && (p.w == c.chatty.w || c.chatty.json) {
   825  		// We're flushing to the actual output, so track that this output is
   826  		// associated with a specific test (and, specifically, that the next output
   827  		// is *not* associated with that test).
   828  		//
   829  		// Moreover, if c.output is non-empty it is important that this write be
   830  		// atomic with respect to the output of other tests, so that we don't end up
   831  		// with confusing '=== NAME' lines in the middle of our '--- PASS' block.
   832  		// Neither humans nor cmd/test2json can parse those easily.
   833  		// (See https://go.dev/issue/40771.)
   834  		//
   835  		// If test2json is used, we never flush to parent tests,
   836  		// so that the json stream shows subtests as they finish.
   837  		// (See https://go.dev/issue/29811.)
   838  		c.chatty.Updatef(testName, format, args...)
   839  	} else {
   840  		// We're flushing to the output buffer of the parent test, which will
   841  		// itself follow a test-name header when it is finally flushed to stdout.
   842  		fmt.Fprintf(p.w, c.chatty.prefix()+format, args...)
   843  	}
   844  }
   845  
   846  type indenter struct {
   847  	c *common
   848  }
   849  
   850  func (w indenter) Write(b []byte) (n int, err error) {
   851  	n = len(b)
   852  	for len(b) > 0 {
   853  		end := bytes.IndexByte(b, '\n')
   854  		if end == -1 {
   855  			end = len(b)
   856  		} else {
   857  			end++
   858  		}
   859  		// An indent of 4 spaces will neatly align the dashes with the status
   860  		// indicator of the parent.
   861  		line := b[:end]
   862  		if line[0] == marker {
   863  			w.c.output = append(w.c.output, marker)
   864  			line = line[1:]
   865  		}
   866  		const indent = "    "
   867  		w.c.output = append(w.c.output, indent...)
   868  		w.c.output = append(w.c.output, line...)
   869  		b = b[end:]
   870  	}
   871  	return
   872  }
   873  
   874  // fmtDuration returns a string representing d in the form "87.00s".
   875  func fmtDuration(d time.Duration) string {
   876  	return fmt.Sprintf("%.2fs", d.Seconds())
   877  }
   878  
   879  // TB is the interface common to T, B, and F.
   880  type TB interface {
   881  	Cleanup(func())
   882  	Error(args ...any)
   883  	Errorf(format string, args ...any)
   884  	Fail()
   885  	FailNow()
   886  	Failed() bool
   887  	Fatal(args ...any)
   888  	Fatalf(format string, args ...any)
   889  	Helper()
   890  	Log(args ...any)
   891  	Logf(format string, args ...any)
   892  	Name() string
   893  	Setenv(key, value string)
   894  	Skip(args ...any)
   895  	SkipNow()
   896  	Skipf(format string, args ...any)
   897  	Skipped() bool
   898  	TempDir() string
   899  
   900  	// A private method to prevent users implementing the
   901  	// interface and so future additions to it will not
   902  	// violate Go 1 compatibility.
   903  	private()
   904  }
   905  
   906  var _ TB = (*T)(nil)
   907  var _ TB = (*B)(nil)
   908  
   909  // T is a type passed to Test functions to manage test state and support formatted test logs.
   910  //
   911  // A test ends when its Test function returns or calls any of the methods
   912  // FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods, as well as
   913  // the Parallel method, must be called only from the goroutine running the
   914  // Test function.
   915  //
   916  // The other reporting methods, such as the variations of Log and Error,
   917  // may be called simultaneously from multiple goroutines.
   918  type T struct {
   919  	common
   920  	isEnvSet bool
   921  	context  *testContext // For running tests and subtests.
   922  }
   923  
   924  func (c *common) private() {}
   925  
   926  // Name returns the name of the running (sub-) test or benchmark.
   927  //
   928  // The name will include the name of the test along with the names of
   929  // any nested sub-tests. If two sibling sub-tests have the same name,
   930  // Name will append a suffix to guarantee the returned name is unique.
   931  func (c *common) Name() string {
   932  	return c.name
   933  }
   934  
   935  func (c *common) setRan() {
   936  	if c.parent != nil {
   937  		c.parent.setRan()
   938  	}
   939  	c.mu.Lock()
   940  	defer c.mu.Unlock()
   941  	c.ran = true
   942  }
   943  
   944  // Fail marks the function as having failed but continues execution.
   945  func (c *common) Fail() {
   946  	if c.parent != nil {
   947  		c.parent.Fail()
   948  	}
   949  	c.mu.Lock()
   950  	defer c.mu.Unlock()
   951  	// c.done needs to be locked to synchronize checks to c.done in parent tests.
   952  	if c.done {
   953  		panic("Fail in goroutine after " + c.name + " has completed")
   954  	}
   955  	c.failed = true
   956  }
   957  
   958  // Failed reports whether the function has failed.
   959  func (c *common) Failed() bool {
   960  	c.mu.RLock()
   961  	defer c.mu.RUnlock()
   962  
   963  	if !c.done && int64(race.Errors()) > c.lastRaceErrors.Load() {
   964  		c.mu.RUnlock()
   965  		c.checkRaces()
   966  		c.mu.RLock()
   967  	}
   968  
   969  	return c.failed
   970  }
   971  
   972  // FailNow marks the function as having failed and stops its execution
   973  // by calling runtime.Goexit (which then runs all deferred calls in the
   974  // current goroutine).
   975  // Execution will continue at the next test or benchmark.
   976  // FailNow must be called from the goroutine running the
   977  // test or benchmark function, not from other goroutines
   978  // created during the test. Calling FailNow does not stop
   979  // those other goroutines.
   980  func (c *common) FailNow() {
   981  	c.checkFuzzFn("FailNow")
   982  	c.Fail()
   983  
   984  	// Calling runtime.Goexit will exit the goroutine, which
   985  	// will run the deferred functions in this goroutine,
   986  	// which will eventually run the deferred lines in tRunner,
   987  	// which will signal to the test loop that this test is done.
   988  	//
   989  	// A previous version of this code said:
   990  	//
   991  	//	c.duration = ...
   992  	//	c.signal <- c.self
   993  	//	runtime.Goexit()
   994  	//
   995  	// This previous version duplicated code (those lines are in
   996  	// tRunner no matter what), but worse the goroutine teardown
   997  	// implicit in runtime.Goexit was not guaranteed to complete
   998  	// before the test exited. If a test deferred an important cleanup
   999  	// function (like removing temporary files), there was no guarantee
  1000  	// it would run on a test failure. Because we send on c.signal during
  1001  	// a top-of-stack deferred function now, we know that the send
  1002  	// only happens after any other stacked defers have completed.
  1003  	c.mu.Lock()
  1004  	c.finished = true
  1005  	c.mu.Unlock()
  1006  	runtime.Goexit()
  1007  }
  1008  
  1009  // log generates the output. It's always at the same stack depth.
  1010  func (c *common) log(s string) {
  1011  	c.logDepth(s, 3) // logDepth + log + public function
  1012  }
  1013  
  1014  // logDepth generates the output at an arbitrary stack depth.
  1015  func (c *common) logDepth(s string, depth int) {
  1016  	c.mu.Lock()
  1017  	defer c.mu.Unlock()
  1018  	if c.done {
  1019  		// This test has already finished. Try and log this message
  1020  		// with our parent. If we don't have a parent, panic.
  1021  		for parent := c.parent; parent != nil; parent = parent.parent {
  1022  			parent.mu.Lock()
  1023  			defer parent.mu.Unlock()
  1024  			if !parent.done {
  1025  				parent.output = append(parent.output, parent.decorate(s, depth+1)...)
  1026  				return
  1027  			}
  1028  		}
  1029  		panic("Log in goroutine after " + c.name + " has completed: " + s)
  1030  	} else {
  1031  		if c.chatty != nil {
  1032  			if c.bench {
  1033  				// Benchmarks don't print === CONT, so we should skip the test
  1034  				// printer and just print straight to stdout.
  1035  				fmt.Print(c.decorate(s, depth+1))
  1036  			} else {
  1037  				c.chatty.Printf(c.name, "%s", c.decorate(s, depth+1))
  1038  			}
  1039  
  1040  			return
  1041  		}
  1042  		c.output = append(c.output, c.decorate(s, depth+1)...)
  1043  	}
  1044  }
  1045  
  1046  // Log formats its arguments using default formatting, analogous to Println,
  1047  // and records the text in the error log. For tests, the text will be printed only if
  1048  // the test fails or the -test.v flag is set. For benchmarks, the text is always
  1049  // printed to avoid having performance depend on the value of the -test.v flag.
  1050  func (c *common) Log(args ...any) {
  1051  	c.checkFuzzFn("Log")
  1052  	c.log(fmt.Sprintln(args...))
  1053  }
  1054  
  1055  // Logf formats its arguments according to the format, analogous to Printf, and
  1056  // records the text in the error log. A final newline is added if not provided. For
  1057  // tests, the text will be printed only if the test fails or the -test.v flag is
  1058  // set. For benchmarks, the text is always printed to avoid having performance
  1059  // depend on the value of the -test.v flag.
  1060  func (c *common) Logf(format string, args ...any) {
  1061  	c.checkFuzzFn("Logf")
  1062  	c.log(fmt.Sprintf(format, args...))
  1063  }
  1064  
  1065  // Error is equivalent to Log followed by Fail.
  1066  func (c *common) Error(args ...any) {
  1067  	c.checkFuzzFn("Error")
  1068  	c.log(fmt.Sprintln(args...))
  1069  	c.Fail()
  1070  }
  1071  
  1072  // Errorf is equivalent to Logf followed by Fail.
  1073  func (c *common) Errorf(format string, args ...any) {
  1074  	c.checkFuzzFn("Errorf")
  1075  	c.log(fmt.Sprintf(format, args...))
  1076  	c.Fail()
  1077  }
  1078  
  1079  // Fatal is equivalent to Log followed by FailNow.
  1080  func (c *common) Fatal(args ...any) {
  1081  	c.checkFuzzFn("Fatal")
  1082  	c.log(fmt.Sprintln(args...))
  1083  	c.FailNow()
  1084  }
  1085  
  1086  // Fatalf is equivalent to Logf followed by FailNow.
  1087  func (c *common) Fatalf(format string, args ...any) {
  1088  	c.checkFuzzFn("Fatalf")
  1089  	c.log(fmt.Sprintf(format, args...))
  1090  	c.FailNow()
  1091  }
  1092  
  1093  // Skip is equivalent to Log followed by SkipNow.
  1094  func (c *common) Skip(args ...any) {
  1095  	c.checkFuzzFn("Skip")
  1096  	c.log(fmt.Sprintln(args...))
  1097  	c.SkipNow()
  1098  }
  1099  
  1100  // Skipf is equivalent to Logf followed by SkipNow.
  1101  func (c *common) Skipf(format string, args ...any) {
  1102  	c.checkFuzzFn("Skipf")
  1103  	c.log(fmt.Sprintf(format, args...))
  1104  	c.SkipNow()
  1105  }
  1106  
  1107  // SkipNow marks the test as having been skipped and stops its execution
  1108  // by calling [runtime.Goexit].
  1109  // If a test fails (see Error, Errorf, Fail) and is then skipped,
  1110  // it is still considered to have failed.
  1111  // Execution will continue at the next test or benchmark. See also FailNow.
  1112  // SkipNow must be called from the goroutine running the test, not from
  1113  // other goroutines created during the test. Calling SkipNow does not stop
  1114  // those other goroutines.
  1115  func (c *common) SkipNow() {
  1116  	c.checkFuzzFn("SkipNow")
  1117  	c.mu.Lock()
  1118  	c.skipped = true
  1119  	c.finished = true
  1120  	c.mu.Unlock()
  1121  	runtime.Goexit()
  1122  }
  1123  
  1124  // Skipped reports whether the test was skipped.
  1125  func (c *common) Skipped() bool {
  1126  	c.mu.RLock()
  1127  	defer c.mu.RUnlock()
  1128  	return c.skipped
  1129  }
  1130  
  1131  // Helper marks the calling function as a test helper function.
  1132  // When printing file and line information, that function will be skipped.
  1133  // Helper may be called simultaneously from multiple goroutines.
  1134  func (c *common) Helper() {
  1135  	c.mu.Lock()
  1136  	defer c.mu.Unlock()
  1137  	if c.helperPCs == nil {
  1138  		c.helperPCs = make(map[uintptr]struct{})
  1139  	}
  1140  	// repeating code from callerName here to save walking a stack frame
  1141  	var pc [1]uintptr
  1142  	n := runtime.Callers(2, pc[:]) // skip runtime.Callers + Helper
  1143  	if n == 0 {
  1144  		panic("testing: zero callers found")
  1145  	}
  1146  	if _, found := c.helperPCs[pc[0]]; !found {
  1147  		c.helperPCs[pc[0]] = struct{}{}
  1148  		c.helperNames = nil // map will be recreated next time it is needed
  1149  	}
  1150  }
  1151  
  1152  // Cleanup registers a function to be called when the test (or subtest) and all its
  1153  // subtests complete. Cleanup functions will be called in last added,
  1154  // first called order.
  1155  func (c *common) Cleanup(f func()) {
  1156  	c.checkFuzzFn("Cleanup")
  1157  	var pc [maxStackLen]uintptr
  1158  	// Skip two extra frames to account for this function and runtime.Callers itself.
  1159  	n := runtime.Callers(2, pc[:])
  1160  	cleanupPc := pc[:n]
  1161  
  1162  	fn := func() {
  1163  		defer func() {
  1164  			c.mu.Lock()
  1165  			defer c.mu.Unlock()
  1166  			c.cleanupName = ""
  1167  			c.cleanupPc = nil
  1168  		}()
  1169  
  1170  		name := callerName(0)
  1171  		c.mu.Lock()
  1172  		c.cleanupName = name
  1173  		c.cleanupPc = cleanupPc
  1174  		c.mu.Unlock()
  1175  
  1176  		f()
  1177  	}
  1178  
  1179  	c.mu.Lock()
  1180  	defer c.mu.Unlock()
  1181  	c.cleanups = append(c.cleanups, fn)
  1182  }
  1183  
  1184  // TempDir returns a temporary directory for the test to use.
  1185  // The directory is automatically removed when the test and
  1186  // all its subtests complete.
  1187  // Each subsequent call to t.TempDir returns a unique directory;
  1188  // if the directory creation fails, TempDir terminates the test by calling Fatal.
  1189  func (c *common) TempDir() string {
  1190  	c.checkFuzzFn("TempDir")
  1191  	// Use a single parent directory for all the temporary directories
  1192  	// created by a test, each numbered sequentially.
  1193  	c.tempDirMu.Lock()
  1194  	var nonExistent bool
  1195  	if c.tempDir == "" { // Usually the case with js/wasm
  1196  		nonExistent = true
  1197  	} else {
  1198  		_, err := os.Stat(c.tempDir)
  1199  		nonExistent = os.IsNotExist(err)
  1200  		if err != nil && !nonExistent {
  1201  			c.Fatalf("TempDir: %v", err)
  1202  		}
  1203  	}
  1204  
  1205  	if nonExistent {
  1206  		c.Helper()
  1207  
  1208  		// Drop unusual characters (such as path separators or
  1209  		// characters interacting with globs) from the directory name to
  1210  		// avoid surprising os.MkdirTemp behavior.
  1211  		mapper := func(r rune) rune {
  1212  			if r < utf8.RuneSelf {
  1213  				const allowed = "!#$%&()+,-.=@^_{}~ "
  1214  				if '0' <= r && r <= '9' ||
  1215  					'a' <= r && r <= 'z' ||
  1216  					'A' <= r && r <= 'Z' {
  1217  					return r
  1218  				}
  1219  				if strings.ContainsRune(allowed, r) {
  1220  					return r
  1221  				}
  1222  			} else if unicode.IsLetter(r) || unicode.IsNumber(r) {
  1223  				return r
  1224  			}
  1225  			return -1
  1226  		}
  1227  		pattern := strings.Map(mapper, c.Name())
  1228  		c.tempDir, c.tempDirErr = os.MkdirTemp("", pattern)
  1229  		if c.tempDirErr == nil {
  1230  			c.Cleanup(func() {
  1231  				if err := removeAll(c.tempDir); err != nil {
  1232  					c.Errorf("TempDir RemoveAll cleanup: %v", err)
  1233  				}
  1234  			})
  1235  		}
  1236  	}
  1237  
  1238  	if c.tempDirErr == nil {
  1239  		c.tempDirSeq++
  1240  	}
  1241  	seq := c.tempDirSeq
  1242  	c.tempDirMu.Unlock()
  1243  
  1244  	if c.tempDirErr != nil {
  1245  		c.Fatalf("TempDir: %v", c.tempDirErr)
  1246  	}
  1247  
  1248  	dir := fmt.Sprintf("%s%c%03d", c.tempDir, os.PathSeparator, seq)
  1249  	if err := os.Mkdir(dir, 0777); err != nil {
  1250  		c.Fatalf("TempDir: %v", err)
  1251  	}
  1252  	return dir
  1253  }
  1254  
  1255  // removeAll is like os.RemoveAll, but retries Windows "Access is denied."
  1256  // errors up to an arbitrary timeout.
  1257  //
  1258  // Those errors have been known to occur spuriously on at least the
  1259  // windows-amd64-2012 builder (https://go.dev/issue/50051), and can only occur
  1260  // legitimately if the test leaves behind a temp file that either is still open
  1261  // or the test otherwise lacks permission to delete. In the case of legitimate
  1262  // failures, a failing test may take a bit longer to fail, but once the test is
  1263  // fixed the extra latency will go away.
  1264  func removeAll(path string) error {
  1265  	const arbitraryTimeout = 2 * time.Second
  1266  	var (
  1267  		start     time.Time
  1268  		nextSleep = 1 * time.Millisecond
  1269  	)
  1270  	for {
  1271  		err := os.RemoveAll(path)
  1272  		if !isWindowsRetryable(err) {
  1273  			return err
  1274  		}
  1275  		if start.IsZero() {
  1276  			start = time.Now()
  1277  		} else if d := time.Since(start) + nextSleep; d >= arbitraryTimeout {
  1278  			return err
  1279  		}
  1280  		time.Sleep(nextSleep)
  1281  		nextSleep += time.Duration(rand.Int63n(int64(nextSleep)))
  1282  	}
  1283  }
  1284  
  1285  // Setenv calls os.Setenv(key, value) and uses Cleanup to
  1286  // restore the environment variable to its original value
  1287  // after the test.
  1288  //
  1289  // Because Setenv affects the whole process, it cannot be used
  1290  // in parallel tests or tests with parallel ancestors.
  1291  func (c *common) Setenv(key, value string) {
  1292  	c.checkFuzzFn("Setenv")
  1293  	prevValue, ok := os.LookupEnv(key)
  1294  
  1295  	if err := os.Setenv(key, value); err != nil {
  1296  		c.Fatalf("cannot set environment variable: %v", err)
  1297  	}
  1298  
  1299  	if ok {
  1300  		c.Cleanup(func() {
  1301  			os.Setenv(key, prevValue)
  1302  		})
  1303  	} else {
  1304  		c.Cleanup(func() {
  1305  			os.Unsetenv(key)
  1306  		})
  1307  	}
  1308  }
  1309  
  1310  // panicHandling controls the panic handling used by runCleanup.
  1311  type panicHandling int
  1312  
  1313  const (
  1314  	normalPanic panicHandling = iota
  1315  	recoverAndReturnPanic
  1316  )
  1317  
  1318  // runCleanup is called at the end of the test.
  1319  // If ph is recoverAndReturnPanic, it will catch panics, and return the
  1320  // recovered value if any.
  1321  func (c *common) runCleanup(ph panicHandling) (panicVal any) {
  1322  	c.cleanupStarted.Store(true)
  1323  	defer c.cleanupStarted.Store(false)
  1324  
  1325  	if ph == recoverAndReturnPanic {
  1326  		defer func() {
  1327  			panicVal = recover()
  1328  		}()
  1329  	}
  1330  
  1331  	// Make sure that if a cleanup function panics,
  1332  	// we still run the remaining cleanup functions.
  1333  	defer func() {
  1334  		c.mu.Lock()
  1335  		recur := len(c.cleanups) > 0
  1336  		c.mu.Unlock()
  1337  		if recur {
  1338  			c.runCleanup(normalPanic)
  1339  		}
  1340  	}()
  1341  
  1342  	for {
  1343  		var cleanup func()
  1344  		c.mu.Lock()
  1345  		if len(c.cleanups) > 0 {
  1346  			last := len(c.cleanups) - 1
  1347  			cleanup = c.cleanups[last]
  1348  			c.cleanups = c.cleanups[:last]
  1349  		}
  1350  		c.mu.Unlock()
  1351  		if cleanup == nil {
  1352  			return nil
  1353  		}
  1354  		cleanup()
  1355  	}
  1356  }
  1357  
  1358  // resetRaces updates c.parent's count of data race errors (or the global count,
  1359  // if c has no parent), and updates c.lastRaceErrors to match.
  1360  //
  1361  // Any races that occurred prior to this call to resetRaces will
  1362  // not be attributed to c.
  1363  func (c *common) resetRaces() {
  1364  	if c.parent == nil {
  1365  		c.lastRaceErrors.Store(int64(race.Errors()))
  1366  	} else {
  1367  		c.lastRaceErrors.Store(c.parent.checkRaces())
  1368  	}
  1369  }
  1370  
  1371  // checkRaces checks whether the global count of data race errors has increased
  1372  // since c's count was last reset.
  1373  //
  1374  // If so, it marks c as having failed due to those races (logging an error for
  1375  // the first such race), and updates the race counts for the parents of c so
  1376  // that if they are currently suspended (such as in a call to T.Run) they will
  1377  // not log separate errors for the race(s).
  1378  //
  1379  // Note that multiple tests may be marked as failed due to the same race if they
  1380  // are executing in parallel.
  1381  func (c *common) checkRaces() (raceErrors int64) {
  1382  	raceErrors = int64(race.Errors())
  1383  	for {
  1384  		last := c.lastRaceErrors.Load()
  1385  		if raceErrors <= last {
  1386  			// All races have already been reported.
  1387  			return raceErrors
  1388  		}
  1389  		if c.lastRaceErrors.CompareAndSwap(last, raceErrors) {
  1390  			break
  1391  		}
  1392  	}
  1393  
  1394  	if c.raceErrorLogged.CompareAndSwap(false, true) {
  1395  		// This is the first race we've encountered for this test.
  1396  		// Mark the test as failed, and log the reason why only once.
  1397  		// (Note that the race detector itself will still write a goroutine
  1398  		// dump for any further races it detects.)
  1399  		c.Errorf("race detected during execution of test")
  1400  	}
  1401  
  1402  	// Update the parent(s) of this test so that they don't re-report the race.
  1403  	parent := c.parent
  1404  	for parent != nil {
  1405  		for {
  1406  			last := parent.lastRaceErrors.Load()
  1407  			if raceErrors <= last {
  1408  				// This race was already reported by another (likely parallel) subtest.
  1409  				return raceErrors
  1410  			}
  1411  			if parent.lastRaceErrors.CompareAndSwap(last, raceErrors) {
  1412  				break
  1413  			}
  1414  		}
  1415  		parent = parent.parent
  1416  	}
  1417  
  1418  	return raceErrors
  1419  }
  1420  
  1421  // callerName gives the function name (qualified with a package path)
  1422  // for the caller after skip frames (where 0 means the current function).
  1423  func callerName(skip int) string {
  1424  	var pc [1]uintptr
  1425  	n := runtime.Callers(skip+2, pc[:]) // skip + runtime.Callers + callerName
  1426  	if n == 0 {
  1427  		panic("testing: zero callers found")
  1428  	}
  1429  	return pcToName(pc[0])
  1430  }
  1431  
  1432  func pcToName(pc uintptr) string {
  1433  	pcs := []uintptr{pc}
  1434  	frames := runtime.CallersFrames(pcs)
  1435  	frame, _ := frames.Next()
  1436  	return frame.Function
  1437  }
  1438  
  1439  // Parallel signals that this test is to be run in parallel with (and only with)
  1440  // other parallel tests. When a test is run multiple times due to use of
  1441  // -test.count or -test.cpu, multiple instances of a single test never run in
  1442  // parallel with each other.
  1443  func (t *T) Parallel() {
  1444  	if t.isParallel {
  1445  		panic("testing: t.Parallel called multiple times")
  1446  	}
  1447  	if t.isEnvSet {
  1448  		panic("testing: t.Parallel called after t.Setenv; cannot set environment variables in parallel tests")
  1449  	}
  1450  	t.isParallel = true
  1451  	if t.parent.barrier == nil {
  1452  		// T.Parallel has no effect when fuzzing.
  1453  		// Multiple processes may run in parallel, but only one input can run at a
  1454  		// time per process so we can attribute crashes to specific inputs.
  1455  		return
  1456  	}
  1457  
  1458  	// We don't want to include the time we spend waiting for serial tests
  1459  	// in the test duration. Record the elapsed time thus far and reset the
  1460  	// timer afterwards.
  1461  	t.duration += highPrecisionTimeSince(t.start)
  1462  
  1463  	// Add to the list of tests to be released by the parent.
  1464  	t.parent.sub = append(t.parent.sub, t)
  1465  
  1466  	// Report any races during execution of this test up to this point.
  1467  	//
  1468  	// We will assume that any races that occur between here and the point where
  1469  	// we unblock are not caused by this subtest. That assumption usually holds,
  1470  	// although it can be wrong if the test spawns a goroutine that races in the
  1471  	// background while the rest of the test is blocked on the call to Parallel.
  1472  	// If that happens, we will misattribute the background race to some other
  1473  	// test, or to no test at all — but that false-negative is so unlikely that it
  1474  	// is not worth adding race-report noise for the common case where the test is
  1475  	// completely suspended during the call to Parallel.
  1476  	t.checkRaces()
  1477  
  1478  	if t.chatty != nil {
  1479  		t.chatty.Updatef(t.name, "=== PAUSE %s\n", t.name)
  1480  	}
  1481  	running.Delete(t.name)
  1482  
  1483  	t.signal <- true   // Release calling test.
  1484  	<-t.parent.barrier // Wait for the parent test to complete.
  1485  	t.context.waitParallel()
  1486  
  1487  	if t.chatty != nil {
  1488  		t.chatty.Updatef(t.name, "=== CONT  %s\n", t.name)
  1489  	}
  1490  	running.Store(t.name, highPrecisionTimeNow())
  1491  	t.start = highPrecisionTimeNow()
  1492  
  1493  	// Reset the local race counter to ignore any races that happened while this
  1494  	// goroutine was blocked, such as in the parent test or in other parallel
  1495  	// subtests.
  1496  	//
  1497  	// (Note that we don't call parent.checkRaces here:
  1498  	// if other parallel subtests have already introduced races, we want to
  1499  	// let them report those races instead of attributing them to the parent.)
  1500  	t.lastRaceErrors.Store(int64(race.Errors()))
  1501  }
  1502  
  1503  // Setenv calls os.Setenv(key, value) and uses Cleanup to
  1504  // restore the environment variable to its original value
  1505  // after the test.
  1506  //
  1507  // Because Setenv affects the whole process, it cannot be used
  1508  // in parallel tests or tests with parallel ancestors.
  1509  func (t *T) Setenv(key, value string) {
  1510  	// Non-parallel subtests that have parallel ancestors may still
  1511  	// run in parallel with other tests: they are only non-parallel
  1512  	// with respect to the other subtests of the same parent.
  1513  	// Since SetEnv affects the whole process, we need to disallow it
  1514  	// if the current test or any parent is parallel.
  1515  	isParallel := false
  1516  	for c := &t.common; c != nil; c = c.parent {
  1517  		if c.isParallel {
  1518  			isParallel = true
  1519  			break
  1520  		}
  1521  	}
  1522  	if isParallel {
  1523  		panic("testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests")
  1524  	}
  1525  
  1526  	t.isEnvSet = true
  1527  
  1528  	t.common.Setenv(key, value)
  1529  }
  1530  
  1531  // InternalTest is an internal type but exported because it is cross-package;
  1532  // it is part of the implementation of the "go test" command.
  1533  type InternalTest struct {
  1534  	Name string
  1535  	F    func(*T)
  1536  }
  1537  
  1538  var errNilPanicOrGoexit = errors.New("test executed panic(nil) or runtime.Goexit")
  1539  
  1540  func tRunner(t *T, fn func(t *T)) {
  1541  	t.runner = callerName(0)
  1542  
  1543  	// When this goroutine is done, either because fn(t)
  1544  	// returned normally or because a test failure triggered
  1545  	// a call to runtime.Goexit, record the duration and send
  1546  	// a signal saying that the test is done.
  1547  	defer func() {
  1548  		t.checkRaces()
  1549  
  1550  		// TODO(#61034): This is the wrong place for this check.
  1551  		if t.Failed() {
  1552  			numFailed.Add(1)
  1553  		}
  1554  
  1555  		// Check if the test panicked or Goexited inappropriately.
  1556  		//
  1557  		// If this happens in a normal test, print output but continue panicking.
  1558  		// tRunner is called in its own goroutine, so this terminates the process.
  1559  		//
  1560  		// If this happens while fuzzing, recover from the panic and treat it like a
  1561  		// normal failure. It's important that the process keeps running in order to
  1562  		// find short inputs that cause panics.
  1563  		err := recover()
  1564  		signal := true
  1565  
  1566  		t.mu.RLock()
  1567  		finished := t.finished
  1568  		t.mu.RUnlock()
  1569  		if !finished && err == nil {
  1570  			err = errNilPanicOrGoexit
  1571  			for p := t.parent; p != nil; p = p.parent {
  1572  				p.mu.RLock()
  1573  				finished = p.finished
  1574  				p.mu.RUnlock()
  1575  				if finished {
  1576  					if !t.isParallel {
  1577  						t.Errorf("%v: subtest may have called FailNow on a parent test", err)
  1578  						err = nil
  1579  					}
  1580  					signal = false
  1581  					break
  1582  				}
  1583  			}
  1584  		}
  1585  
  1586  		if err != nil && t.context.isFuzzing {
  1587  			prefix := "panic: "
  1588  			if err == errNilPanicOrGoexit {
  1589  				prefix = ""
  1590  			}
  1591  			t.Errorf("%s%s\n%s\n", prefix, err, string(debug.Stack()))
  1592  			t.mu.Lock()
  1593  			t.finished = true
  1594  			t.mu.Unlock()
  1595  			err = nil
  1596  		}
  1597  
  1598  		// Use a deferred call to ensure that we report that the test is
  1599  		// complete even if a cleanup function calls t.FailNow. See issue 41355.
  1600  		didPanic := false
  1601  		defer func() {
  1602  			// Only report that the test is complete if it doesn't panic,
  1603  			// as otherwise the test binary can exit before the panic is
  1604  			// reported to the user. See issue 41479.
  1605  			if didPanic {
  1606  				return
  1607  			}
  1608  			if err != nil {
  1609  				panic(err)
  1610  			}
  1611  			running.Delete(t.name)
  1612  			t.signal <- signal
  1613  		}()
  1614  
  1615  		doPanic := func(err any) {
  1616  			t.Fail()
  1617  			if r := t.runCleanup(recoverAndReturnPanic); r != nil {
  1618  				t.Logf("cleanup panicked with %v", r)
  1619  			}
  1620  			// Flush the output log up to the root before dying.
  1621  			for root := &t.common; root.parent != nil; root = root.parent {
  1622  				root.mu.Lock()
  1623  				root.duration += highPrecisionTimeSince(root.start)
  1624  				d := root.duration
  1625  				root.mu.Unlock()
  1626  				root.flushToParent(root.name, "--- FAIL: %s (%s)\n", root.name, fmtDuration(d))
  1627  				if r := root.parent.runCleanup(recoverAndReturnPanic); r != nil {
  1628  					fmt.Fprintf(root.parent.w, "cleanup panicked with %v", r)
  1629  				}
  1630  			}
  1631  			didPanic = true
  1632  			panic(err)
  1633  		}
  1634  		if err != nil {
  1635  			doPanic(err)
  1636  		}
  1637  
  1638  		t.duration += highPrecisionTimeSince(t.start)
  1639  
  1640  		if len(t.sub) > 0 {
  1641  			// Run parallel subtests.
  1642  
  1643  			// Decrease the running count for this test and mark it as no longer running.
  1644  			t.context.release()
  1645  			running.Delete(t.name)
  1646  
  1647  			// Release the parallel subtests.
  1648  			close(t.barrier)
  1649  			// Wait for subtests to complete.
  1650  			for _, sub := range t.sub {
  1651  				<-sub.signal
  1652  			}
  1653  
  1654  			// Run any cleanup callbacks, marking the test as running
  1655  			// in case the cleanup hangs.
  1656  			cleanupStart := highPrecisionTimeNow()
  1657  			running.Store(t.name, cleanupStart)
  1658  			err := t.runCleanup(recoverAndReturnPanic)
  1659  			t.duration += highPrecisionTimeSince(cleanupStart)
  1660  			if err != nil {
  1661  				doPanic(err)
  1662  			}
  1663  			t.checkRaces()
  1664  			if !t.isParallel {
  1665  				// Reacquire the count for sequential tests. See comment in Run.
  1666  				t.context.waitParallel()
  1667  			}
  1668  		} else if t.isParallel {
  1669  			// Only release the count for this test if it was run as a parallel
  1670  			// test. See comment in Run method.
  1671  			t.context.release()
  1672  		}
  1673  		t.report() // Report after all subtests have finished.
  1674  
  1675  		// Do not lock t.done to allow race detector to detect race in case
  1676  		// the user does not appropriately synchronize a goroutine.
  1677  		t.done = true
  1678  		if t.parent != nil && !t.hasSub.Load() {
  1679  			t.setRan()
  1680  		}
  1681  	}()
  1682  	defer func() {
  1683  		if len(t.sub) == 0 {
  1684  			t.runCleanup(normalPanic)
  1685  		}
  1686  	}()
  1687  
  1688  	t.start = highPrecisionTimeNow()
  1689  	t.resetRaces()
  1690  	fn(t)
  1691  
  1692  	// code beyond here will not be executed when FailNow is invoked
  1693  	t.mu.Lock()
  1694  	t.finished = true
  1695  	t.mu.Unlock()
  1696  }
  1697  
  1698  // Run runs f as a subtest of t called name. It runs f in a separate goroutine
  1699  // and blocks until f returns or calls t.Parallel to become a parallel test.
  1700  // Run reports whether f succeeded (or at least did not fail before calling t.Parallel).
  1701  //
  1702  // Run may be called simultaneously from multiple goroutines, but all such calls
  1703  // must return before the outer test function for t returns.
  1704  func (t *T) Run(name string, f func(t *T)) bool {
  1705  	if t.cleanupStarted.Load() {
  1706  		panic("testing: t.Run called during t.Cleanup")
  1707  	}
  1708  
  1709  	t.hasSub.Store(true)
  1710  	testName, ok, _ := t.context.match.fullName(&t.common, name)
  1711  	if !ok || shouldFailFast() {
  1712  		return true
  1713  	}
  1714  	// Record the stack trace at the point of this call so that if the subtest
  1715  	// function - which runs in a separate stack - is marked as a helper, we can
  1716  	// continue walking the stack into the parent test.
  1717  	var pc [maxStackLen]uintptr
  1718  	n := runtime.Callers(2, pc[:])
  1719  	t = &T{
  1720  		common: common{
  1721  			barrier: make(chan bool),
  1722  			signal:  make(chan bool, 1),
  1723  			name:    testName,
  1724  			parent:  &t.common,
  1725  			level:   t.level + 1,
  1726  			creator: pc[:n],
  1727  			chatty:  t.chatty,
  1728  		},
  1729  		context: t.context,
  1730  	}
  1731  	t.w = indenter{&t.common}
  1732  
  1733  	if t.chatty != nil {
  1734  		t.chatty.Updatef(t.name, "=== RUN   %s\n", t.name)
  1735  	}
  1736  	running.Store(t.name, highPrecisionTimeNow())
  1737  
  1738  	// Instead of reducing the running count of this test before calling the
  1739  	// tRunner and increasing it afterwards, we rely on tRunner keeping the
  1740  	// count correct. This ensures that a sequence of sequential tests runs
  1741  	// without being preempted, even when their parent is a parallel test. This
  1742  	// may especially reduce surprises if *parallel == 1.
  1743  	go tRunner(t, f)
  1744  
  1745  	// The parent goroutine will block until the subtest either finishes or calls
  1746  	// Parallel, but in general we don't know whether the parent goroutine is the
  1747  	// top-level test function or some other goroutine it has spawned.
  1748  	// To avoid confusing false-negatives, we leave the parent in the running map
  1749  	// even though in the typical case it is blocked.
  1750  
  1751  	if !<-t.signal {
  1752  		// At this point, it is likely that FailNow was called on one of the
  1753  		// parent tests by one of the subtests. Continue aborting up the chain.
  1754  		runtime.Goexit()
  1755  	}
  1756  
  1757  	if t.chatty != nil && t.chatty.json {
  1758  		t.chatty.Updatef(t.parent.name, "=== NAME  %s\n", t.parent.name)
  1759  	}
  1760  	return !t.failed
  1761  }
  1762  
  1763  // Deadline reports the time at which the test binary will have
  1764  // exceeded the timeout specified by the -timeout flag.
  1765  //
  1766  // The ok result is false if the -timeout flag indicates “no timeout” (0).
  1767  func (t *T) Deadline() (deadline time.Time, ok bool) {
  1768  	deadline = t.context.deadline
  1769  	return deadline, !deadline.IsZero()
  1770  }
  1771  
  1772  // testContext holds all fields that are common to all tests. This includes
  1773  // synchronization primitives to run at most *parallel tests.
  1774  type testContext struct {
  1775  	match    *matcher
  1776  	deadline time.Time
  1777  
  1778  	// isFuzzing is true in the context used when generating random inputs
  1779  	// for fuzz targets. isFuzzing is false when running normal tests and
  1780  	// when running fuzz tests as unit tests (without -fuzz or when -fuzz
  1781  	// does not match).
  1782  	isFuzzing bool
  1783  
  1784  	mu sync.Mutex
  1785  
  1786  	// Channel used to signal tests that are ready to be run in parallel.
  1787  	startParallel chan bool
  1788  
  1789  	// running is the number of tests currently running in parallel.
  1790  	// This does not include tests that are waiting for subtests to complete.
  1791  	running int
  1792  
  1793  	// numWaiting is the number tests waiting to be run in parallel.
  1794  	numWaiting int
  1795  
  1796  	// maxParallel is a copy of the parallel flag.
  1797  	maxParallel int
  1798  }
  1799  
  1800  func newTestContext(maxParallel int, m *matcher) *testContext {
  1801  	return &testContext{
  1802  		match:         m,
  1803  		startParallel: make(chan bool),
  1804  		maxParallel:   maxParallel,
  1805  		running:       1, // Set the count to 1 for the main (sequential) test.
  1806  	}
  1807  }
  1808  
  1809  func (c *testContext) waitParallel() {
  1810  	c.mu.Lock()
  1811  	if c.running < c.maxParallel {
  1812  		c.running++
  1813  		c.mu.Unlock()
  1814  		return
  1815  	}
  1816  	c.numWaiting++
  1817  	c.mu.Unlock()
  1818  	<-c.startParallel
  1819  }
  1820  
  1821  func (c *testContext) release() {
  1822  	c.mu.Lock()
  1823  	if c.numWaiting == 0 {
  1824  		c.running--
  1825  		c.mu.Unlock()
  1826  		return
  1827  	}
  1828  	c.numWaiting--
  1829  	c.mu.Unlock()
  1830  	c.startParallel <- true // Pick a waiting test to be run.
  1831  }
  1832  
  1833  // No one should be using func Main anymore.
  1834  // See the doc comment on func Main and use MainStart instead.
  1835  var errMain = errors.New("testing: unexpected use of func Main")
  1836  
  1837  type matchStringOnly func(pat, str string) (bool, error)
  1838  
  1839  func (f matchStringOnly) MatchString(pat, str string) (bool, error)   { return f(pat, str) }
  1840  func (f matchStringOnly) StartCPUProfile(w io.Writer) error           { return errMain }
  1841  func (f matchStringOnly) StopCPUProfile()                             {}
  1842  func (f matchStringOnly) WriteProfileTo(string, io.Writer, int) error { return errMain }
  1843  func (f matchStringOnly) ImportPath() string                          { return "" }
  1844  func (f matchStringOnly) StartTestLog(io.Writer)                      {}
  1845  func (f matchStringOnly) StopTestLog() error                          { return errMain }
  1846  func (f matchStringOnly) SetPanicOnExit0(bool)                        {}
  1847  func (f matchStringOnly) CoordinateFuzzing(time.Duration, int64, time.Duration, int64, int, []corpusEntry, []reflect.Type, string, string) error {
  1848  	return errMain
  1849  }
  1850  func (f matchStringOnly) RunFuzzWorker(func(corpusEntry) error) error { return errMain }
  1851  func (f matchStringOnly) ReadCorpus(string, []reflect.Type) ([]corpusEntry, error) {
  1852  	return nil, errMain
  1853  }
  1854  func (f matchStringOnly) CheckCorpus([]any, []reflect.Type) error { return nil }
  1855  func (f matchStringOnly) ResetCoverage()                          {}
  1856  func (f matchStringOnly) SnapshotCoverage()                       {}
  1857  
  1858  // Main is an internal function, part of the implementation of the "go test" command.
  1859  // It was exported because it is cross-package and predates "internal" packages.
  1860  // It is no longer used by "go test" but preserved, as much as possible, for other
  1861  // systems that simulate "go test" using Main, but Main sometimes cannot be updated as
  1862  // new functionality is added to the testing package.
  1863  // Systems simulating "go test" should be updated to use MainStart.
  1864  func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) {
  1865  	os.Exit(MainStart(matchStringOnly(matchString), tests, benchmarks, nil, examples).Run())
  1866  }
  1867  
  1868  // M is a type passed to a TestMain function to run the actual tests.
  1869  type M struct {
  1870  	deps        testDeps
  1871  	tests       []InternalTest
  1872  	benchmarks  []InternalBenchmark
  1873  	fuzzTargets []InternalFuzzTarget
  1874  	examples    []InternalExample
  1875  
  1876  	timer     *time.Timer
  1877  	afterOnce sync.Once
  1878  
  1879  	numRun int
  1880  
  1881  	// value to pass to os.Exit, the outer test func main
  1882  	// harness calls os.Exit with this code. See #34129.
  1883  	exitCode int
  1884  }
  1885  
  1886  // testDeps is an internal interface of functionality that is
  1887  // passed into this package by a test's generated main package.
  1888  // The canonical implementation of this interface is
  1889  // testing/internal/testdeps's TestDeps.
  1890  type testDeps interface {
  1891  	ImportPath() string
  1892  	MatchString(pat, str string) (bool, error)
  1893  	SetPanicOnExit0(bool)
  1894  	StartCPUProfile(io.Writer) error
  1895  	StopCPUProfile()
  1896  	StartTestLog(io.Writer)
  1897  	StopTestLog() error
  1898  	WriteProfileTo(string, io.Writer, int) error
  1899  	CoordinateFuzzing(time.Duration, int64, time.Duration, int64, int, []corpusEntry, []reflect.Type, string, string) error
  1900  	RunFuzzWorker(func(corpusEntry) error) error
  1901  	ReadCorpus(string, []reflect.Type) ([]corpusEntry, error)
  1902  	CheckCorpus([]any, []reflect.Type) error
  1903  	ResetCoverage()
  1904  	SnapshotCoverage()
  1905  }
  1906  
  1907  // MainStart is meant for use by tests generated by 'go test'.
  1908  // It is not meant to be called directly and is not subject to the Go 1 compatibility document.
  1909  // It may change signature from release to release.
  1910  func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, fuzzTargets []InternalFuzzTarget, examples []InternalExample) *M {
  1911  	Init()
  1912  	return &M{
  1913  		deps:        deps,
  1914  		tests:       tests,
  1915  		benchmarks:  benchmarks,
  1916  		fuzzTargets: fuzzTargets,
  1917  		examples:    examples,
  1918  	}
  1919  }
  1920  
  1921  var testingTesting bool
  1922  var realStderr *os.File
  1923  
  1924  // Run runs the tests. It returns an exit code to pass to os.Exit.
  1925  func (m *M) Run() (code int) {
  1926  	defer func() {
  1927  		code = m.exitCode
  1928  	}()
  1929  
  1930  	// Count the number of calls to m.Run.
  1931  	// We only ever expected 1, but we didn't enforce that,
  1932  	// and now there are tests in the wild that call m.Run multiple times.
  1933  	// Sigh. go.dev/issue/23129.
  1934  	m.numRun++
  1935  
  1936  	// TestMain may have already called flag.Parse.
  1937  	if !flag.Parsed() {
  1938  		flag.Parse()
  1939  	}
  1940  
  1941  	if chatty.json {
  1942  		// With -v=json, stdout and stderr are pointing to the same pipe,
  1943  		// which is leading into test2json. In general, operating systems
  1944  		// do a good job of ensuring that writes to the same pipe through
  1945  		// different file descriptors are delivered whole, so that writing
  1946  		// AAA to stdout and BBB to stderr simultaneously produces
  1947  		// AAABBB or BBBAAA on the pipe, not something like AABBBA.
  1948  		// However, the exception to this is when the pipe fills: in that
  1949  		// case, Go's use of non-blocking I/O means that writing AAA
  1950  		// or BBB might be split across multiple system calls, making it
  1951  		// entirely possible to get output like AABBBA. The same problem
  1952  		// happens inside the operating system kernel if we switch to
  1953  		// blocking I/O on the pipe. This interleaved output can do things
  1954  		// like print unrelated messages in the middle of a TestFoo line,
  1955  		// which confuses test2json. Setting os.Stderr = os.Stdout will make
  1956  		// them share a single pfd, which will hold a lock for each program
  1957  		// write, preventing any interleaving.
  1958  		//
  1959  		// It might be nice to set Stderr = Stdout always, or perhaps if
  1960  		// we can tell they are the same file, but for now -v=json is
  1961  		// a very clear signal. Making the two files the same may cause
  1962  		// surprises if programs close os.Stdout but expect to be able
  1963  		// to continue to write to os.Stderr, but it's hard to see why a
  1964  		// test would think it could take over global state that way.
  1965  		//
  1966  		// This fix only helps programs where the output is coming directly
  1967  		// from Go code. It does not help programs in which a subprocess is
  1968  		// writing to stderr or stdout at the same time that a Go test is writing output.
  1969  		// It also does not help when the output is coming from the runtime,
  1970  		// such as when using the print/println functions, since that code writes
  1971  		// directly to fd 2 without any locking.
  1972  		// We keep realStderr around to prevent fd 2 from being closed.
  1973  		//
  1974  		// See go.dev/issue/33419.
  1975  		realStderr = os.Stderr
  1976  		os.Stderr = os.Stdout
  1977  	}
  1978  
  1979  	if *parallel < 1 {
  1980  		fmt.Fprintln(os.Stderr, "testing: -parallel can only be given a positive integer")
  1981  		flag.Usage()
  1982  		m.exitCode = 2
  1983  		return
  1984  	}
  1985  	if *matchFuzz != "" && *fuzzCacheDir == "" {
  1986  		fmt.Fprintln(os.Stderr, "testing: -test.fuzzcachedir must be set if -test.fuzz is set")
  1987  		flag.Usage()
  1988  		m.exitCode = 2
  1989  		return
  1990  	}
  1991  
  1992  	if *matchList != "" {
  1993  		listTests(m.deps.MatchString, m.tests, m.benchmarks, m.fuzzTargets, m.examples)
  1994  		m.exitCode = 0
  1995  		return
  1996  	}
  1997  
  1998  	if *shuffle != "off" {
  1999  		var n int64
  2000  		var err error
  2001  		if *shuffle == "on" {
  2002  			n = time.Now().UnixNano()
  2003  		} else {
  2004  			n, err = strconv.ParseInt(*shuffle, 10, 64)
  2005  			if err != nil {
  2006  				fmt.Fprintln(os.Stderr, `testing: -shuffle should be "off", "on", or a valid integer:`, err)
  2007  				m.exitCode = 2
  2008  				return
  2009  			}
  2010  		}
  2011  		fmt.Println("-test.shuffle", n)
  2012  		rng := rand.New(rand.NewSource(n))
  2013  		rng.Shuffle(len(m.tests), func(i, j int) { m.tests[i], m.tests[j] = m.tests[j], m.tests[i] })
  2014  		rng.Shuffle(len(m.benchmarks), func(i, j int) { m.benchmarks[i], m.benchmarks[j] = m.benchmarks[j], m.benchmarks[i] })
  2015  	}
  2016  
  2017  	parseCpuList()
  2018  
  2019  	m.before()
  2020  	defer m.after()
  2021  
  2022  	// Run tests, examples, and benchmarks unless this is a fuzz worker process.
  2023  	// Workers start after this is done by their parent process, and they should
  2024  	// not repeat this work.
  2025  	if !*isFuzzWorker {
  2026  		deadline := m.startAlarm()
  2027  		haveExamples = len(m.examples) > 0
  2028  		testRan, testOk := runTests(m.deps.MatchString, m.tests, deadline)
  2029  		fuzzTargetsRan, fuzzTargetsOk := runFuzzTests(m.deps, m.fuzzTargets, deadline)
  2030  		exampleRan, exampleOk := runExamples(m.deps.MatchString, m.examples)
  2031  		m.stopAlarm()
  2032  		if !testRan && !exampleRan && !fuzzTargetsRan && *matchBenchmarks == "" && *matchFuzz == "" {
  2033  			fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
  2034  			if testingTesting && *match != "^$" {
  2035  				// If this happens during testing of package testing it could be that
  2036  				// package testing's own logic for when to run a test is broken,
  2037  				// in which case every test will run nothing and succeed,
  2038  				// with no obvious way to detect this problem (since no tests are running).
  2039  				// So make 'no tests to run' a hard failure when testing package testing itself.
  2040  				fmt.Print(chatty.prefix(), "FAIL: package testing must run tests\n")
  2041  				testOk = false
  2042  			}
  2043  		}
  2044  		anyFailed := !testOk || !exampleOk || !fuzzTargetsOk || !runBenchmarks(m.deps.ImportPath(), m.deps.MatchString, m.benchmarks)
  2045  		if !anyFailed && race.Errors() > 0 {
  2046  			fmt.Print(chatty.prefix(), "testing: race detected outside of test execution\n")
  2047  			anyFailed = true
  2048  		}
  2049  		if anyFailed {
  2050  			fmt.Print(chatty.prefix(), "FAIL\n")
  2051  			m.exitCode = 1
  2052  			return
  2053  		}
  2054  	}
  2055  
  2056  	fuzzingOk := runFuzzing(m.deps, m.fuzzTargets)
  2057  	if !fuzzingOk {
  2058  		fmt.Print(chatty.prefix(), "FAIL\n")
  2059  		if *isFuzzWorker {
  2060  			m.exitCode = fuzzWorkerExitCode
  2061  		} else {
  2062  			m.exitCode = 1
  2063  		}
  2064  		return
  2065  	}
  2066  
  2067  	m.exitCode = 0
  2068  	if !*isFuzzWorker {
  2069  		fmt.Print(chatty.prefix(), "PASS\n")
  2070  	}
  2071  	return
  2072  }
  2073  
  2074  func (t *T) report() {
  2075  	if t.parent == nil {
  2076  		return
  2077  	}
  2078  	dstr := fmtDuration(t.duration)
  2079  	format := "--- %s: %s (%s)\n"
  2080  	if t.Failed() {
  2081  		t.flushToParent(t.name, format, "FAIL", t.name, dstr)
  2082  	} else if t.chatty != nil {
  2083  		if t.Skipped() {
  2084  			t.flushToParent(t.name, format, "SKIP", t.name, dstr)
  2085  		} else {
  2086  			t.flushToParent(t.name, format, "PASS", t.name, dstr)
  2087  		}
  2088  	}
  2089  }
  2090  
  2091  func listTests(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, fuzzTargets []InternalFuzzTarget, examples []InternalExample) {
  2092  	if _, err := matchString(*matchList, "non-empty"); err != nil {
  2093  		fmt.Fprintf(os.Stderr, "testing: invalid regexp in -test.list (%q): %s\n", *matchList, err)
  2094  		os.Exit(1)
  2095  	}
  2096  
  2097  	for _, test := range tests {
  2098  		if ok, _ := matchString(*matchList, test.Name); ok {
  2099  			fmt.Println(test.Name)
  2100  		}
  2101  	}
  2102  	for _, bench := range benchmarks {
  2103  		if ok, _ := matchString(*matchList, bench.Name); ok {
  2104  			fmt.Println(bench.Name)
  2105  		}
  2106  	}
  2107  	for _, fuzzTarget := range fuzzTargets {
  2108  		if ok, _ := matchString(*matchList, fuzzTarget.Name); ok {
  2109  			fmt.Println(fuzzTarget.Name)
  2110  		}
  2111  	}
  2112  	for _, example := range examples {
  2113  		if ok, _ := matchString(*matchList, example.Name); ok {
  2114  			fmt.Println(example.Name)
  2115  		}
  2116  	}
  2117  }
  2118  
  2119  // RunTests is an internal function but exported because it is cross-package;
  2120  // it is part of the implementation of the "go test" command.
  2121  func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool) {
  2122  	var deadline time.Time
  2123  	if *timeout > 0 {
  2124  		deadline = time.Now().Add(*timeout)
  2125  	}
  2126  	ran, ok := runTests(matchString, tests, deadline)
  2127  	if !ran && !haveExamples {
  2128  		fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
  2129  	}
  2130  	return ok
  2131  }
  2132  
  2133  func runTests(matchString func(pat, str string) (bool, error), tests []InternalTest, deadline time.Time) (ran, ok bool) {
  2134  	ok = true
  2135  	for _, procs := range cpuList {
  2136  		runtime.GOMAXPROCS(procs)
  2137  		for i := uint(0); i < *count; i++ {
  2138  			if shouldFailFast() {
  2139  				break
  2140  			}
  2141  			if i > 0 && !ran {
  2142  				// There were no tests to run on the first
  2143  				// iteration. This won't change, so no reason
  2144  				// to keep trying.
  2145  				break
  2146  			}
  2147  			ctx := newTestContext(*parallel, newMatcher(matchString, *match, "-test.run", *skip))
  2148  			ctx.deadline = deadline
  2149  			t := &T{
  2150  				common: common{
  2151  					signal:  make(chan bool, 1),
  2152  					barrier: make(chan bool),
  2153  					w:       os.Stdout,
  2154  				},
  2155  				context: ctx,
  2156  			}
  2157  			if Verbose() {
  2158  				t.chatty = newChattyPrinter(t.w)
  2159  			}
  2160  			tRunner(t, func(t *T) {
  2161  				for _, test := range tests {
  2162  					t.Run(test.Name, test.F)
  2163  				}
  2164  			})
  2165  			select {
  2166  			case <-t.signal:
  2167  			default:
  2168  				panic("internal error: tRunner exited without sending on t.signal")
  2169  			}
  2170  			ok = ok && !t.Failed()
  2171  			ran = ran || t.ran
  2172  		}
  2173  	}
  2174  	return ran, ok
  2175  }
  2176  
  2177  // before runs before all testing.
  2178  func (m *M) before() {
  2179  	if *memProfileRate > 0 {
  2180  		runtime.MemProfileRate = *memProfileRate
  2181  	}
  2182  	if *cpuProfile != "" {
  2183  		f, err := os.Create(toOutputDir(*cpuProfile))
  2184  		if err != nil {
  2185  			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
  2186  			return
  2187  		}
  2188  		if err := m.deps.StartCPUProfile(f); err != nil {
  2189  			fmt.Fprintf(os.Stderr, "testing: can't start cpu profile: %s\n", err)
  2190  			f.Close()
  2191  			return
  2192  		}
  2193  		// Could save f so after can call f.Close; not worth the effort.
  2194  	}
  2195  	if *traceFile != "" {
  2196  		f, err := os.Create(toOutputDir(*traceFile))
  2197  		if err != nil {
  2198  			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
  2199  			return
  2200  		}
  2201  		if err := trace.Start(f); err != nil {
  2202  			fmt.Fprintf(os.Stderr, "testing: can't start tracing: %s\n", err)
  2203  			f.Close()
  2204  			return
  2205  		}
  2206  		// Could save f so after can call f.Close; not worth the effort.
  2207  	}
  2208  	if *blockProfile != "" && *blockProfileRate >= 0 {
  2209  		runtime.SetBlockProfileRate(*blockProfileRate)
  2210  	}
  2211  	if *mutexProfile != "" && *mutexProfileFraction >= 0 {
  2212  		runtime.SetMutexProfileFraction(*mutexProfileFraction)
  2213  	}
  2214  	if *coverProfile != "" && CoverMode() == "" {
  2215  		fmt.Fprintf(os.Stderr, "testing: cannot use -test.coverprofile because test binary was not built with coverage enabled\n")
  2216  		os.Exit(2)
  2217  	}
  2218  	if *gocoverdir != "" && CoverMode() == "" {
  2219  		fmt.Fprintf(os.Stderr, "testing: cannot use -test.gocoverdir because test binary was not built with coverage enabled\n")
  2220  		os.Exit(2)
  2221  	}
  2222  	if *testlog != "" {
  2223  		// Note: Not using toOutputDir.
  2224  		// This file is for use by cmd/go, not users.
  2225  		var f *os.File
  2226  		var err error
  2227  		if m.numRun == 1 {
  2228  			f, err = os.Create(*testlog)
  2229  		} else {
  2230  			f, err = os.OpenFile(*testlog, os.O_WRONLY, 0)
  2231  			if err == nil {
  2232  				f.Seek(0, io.SeekEnd)
  2233  			}
  2234  		}
  2235  		if err != nil {
  2236  			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
  2237  			os.Exit(2)
  2238  		}
  2239  		m.deps.StartTestLog(f)
  2240  		testlogFile = f
  2241  	}
  2242  	if *panicOnExit0 {
  2243  		m.deps.SetPanicOnExit0(true)
  2244  	}
  2245  }
  2246  
  2247  // after runs after all testing.
  2248  func (m *M) after() {
  2249  	m.afterOnce.Do(func() {
  2250  		m.writeProfiles()
  2251  	})
  2252  
  2253  	// Restore PanicOnExit0 after every run, because we set it to true before
  2254  	// every run. Otherwise, if m.Run is called multiple times the behavior of
  2255  	// os.Exit(0) will not be restored after the second run.
  2256  	if *panicOnExit0 {
  2257  		m.deps.SetPanicOnExit0(false)
  2258  	}
  2259  }
  2260  
  2261  func (m *M) writeProfiles() {
  2262  	if *testlog != "" {
  2263  		if err := m.deps.StopTestLog(); err != nil {
  2264  			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *testlog, err)
  2265  			os.Exit(2)
  2266  		}
  2267  		if err := testlogFile.Close(); err != nil {
  2268  			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *testlog, err)
  2269  			os.Exit(2)
  2270  		}
  2271  	}
  2272  	if *cpuProfile != "" {
  2273  		m.deps.StopCPUProfile() // flushes profile to disk
  2274  	}
  2275  	if *traceFile != "" {
  2276  		trace.Stop() // flushes trace to disk
  2277  	}
  2278  	if *memProfile != "" {
  2279  		f, err := os.Create(toOutputDir(*memProfile))
  2280  		if err != nil {
  2281  			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
  2282  			os.Exit(2)
  2283  		}
  2284  		runtime.GC() // materialize all statistics
  2285  		if err = m.deps.WriteProfileTo("allocs", f, 0); err != nil {
  2286  			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *memProfile, err)
  2287  			os.Exit(2)
  2288  		}
  2289  		f.Close()
  2290  	}
  2291  	if *blockProfile != "" && *blockProfileRate >= 0 {
  2292  		f, err := os.Create(toOutputDir(*blockProfile))
  2293  		if err != nil {
  2294  			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
  2295  			os.Exit(2)
  2296  		}
  2297  		if err = m.deps.WriteProfileTo("block", f, 0); err != nil {
  2298  			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *blockProfile, err)
  2299  			os.Exit(2)
  2300  		}
  2301  		f.Close()
  2302  	}
  2303  	if *mutexProfile != "" && *mutexProfileFraction >= 0 {
  2304  		f, err := os.Create(toOutputDir(*mutexProfile))
  2305  		if err != nil {
  2306  			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
  2307  			os.Exit(2)
  2308  		}
  2309  		if err = m.deps.WriteProfileTo("mutex", f, 0); err != nil {
  2310  			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *mutexProfile, err)
  2311  			os.Exit(2)
  2312  		}
  2313  		f.Close()
  2314  	}
  2315  	if CoverMode() != "" {
  2316  		coverReport()
  2317  	}
  2318  }
  2319  
  2320  // toOutputDir returns the file name relocated, if required, to outputDir.
  2321  // Simple implementation to avoid pulling in path/filepath.
  2322  func toOutputDir(path string) string {
  2323  	if *outputDir == "" || path == "" {
  2324  		return path
  2325  	}
  2326  	// On Windows, it's clumsy, but we can be almost always correct
  2327  	// by just looking for a drive letter and a colon.
  2328  	// Absolute paths always have a drive letter (ignoring UNC).
  2329  	// Problem: if path == "C:A" and outputdir == "C:\Go" it's unclear
  2330  	// what to do, but even then path/filepath doesn't help.
  2331  	// TODO: Worth doing better? Probably not, because we're here only
  2332  	// under the management of go test.
  2333  	if runtime.GOOS == "windows" && len(path) >= 2 {
  2334  		letter, colon := path[0], path[1]
  2335  		if ('a' <= letter && letter <= 'z' || 'A' <= letter && letter <= 'Z') && colon == ':' {
  2336  			// If path starts with a drive letter we're stuck with it regardless.
  2337  			return path
  2338  		}
  2339  	}
  2340  	if os.IsPathSeparator(path[0]) {
  2341  		return path
  2342  	}
  2343  	return fmt.Sprintf("%s%c%s", *outputDir, os.PathSeparator, path)
  2344  }
  2345  
  2346  // startAlarm starts an alarm if requested.
  2347  func (m *M) startAlarm() time.Time {
  2348  	if *timeout <= 0 {
  2349  		return time.Time{}
  2350  	}
  2351  
  2352  	deadline := time.Now().Add(*timeout)
  2353  	m.timer = time.AfterFunc(*timeout, func() {
  2354  		m.after()
  2355  		debug.SetTraceback("all")
  2356  		extra := ""
  2357  
  2358  		if list := runningList(); len(list) > 0 {
  2359  			var b strings.Builder
  2360  			b.WriteString("\nrunning tests:")
  2361  			for _, name := range list {
  2362  				b.WriteString("\n\t")
  2363  				b.WriteString(name)
  2364  			}
  2365  			extra = b.String()
  2366  		}
  2367  		panic(fmt.Sprintf("test timed out after %v%s", *timeout, extra))
  2368  	})
  2369  	return deadline
  2370  }
  2371  
  2372  // runningList returns the list of running tests.
  2373  func runningList() []string {
  2374  	var list []string
  2375  	running.Range(func(k, v any) bool {
  2376  		list = append(list, fmt.Sprintf("%s (%v)", k.(string), highPrecisionTimeSince(v.(highPrecisionTime)).Round(time.Second)))
  2377  		return true
  2378  	})
  2379  	sort.Strings(list)
  2380  	return list
  2381  }
  2382  
  2383  // stopAlarm turns off the alarm.
  2384  func (m *M) stopAlarm() {
  2385  	if *timeout > 0 {
  2386  		m.timer.Stop()
  2387  	}
  2388  }
  2389  
  2390  func parseCpuList() {
  2391  	for _, val := range strings.Split(*cpuListStr, ",") {
  2392  		val = strings.TrimSpace(val)
  2393  		if val == "" {
  2394  			continue
  2395  		}
  2396  		cpu, err := strconv.Atoi(val)
  2397  		if err != nil || cpu <= 0 {
  2398  			fmt.Fprintf(os.Stderr, "testing: invalid value %q for -test.cpu\n", val)
  2399  			os.Exit(1)
  2400  		}
  2401  		cpuList = append(cpuList, cpu)
  2402  	}
  2403  	if cpuList == nil {
  2404  		cpuList = append(cpuList, runtime.GOMAXPROCS(-1))
  2405  	}
  2406  }
  2407  
  2408  func shouldFailFast() bool {
  2409  	return *failFast && numFailed.Load() > 0
  2410  }
  2411  

View as plain text