Source file src/testing/testing.go

Documentation: testing

     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  //     func TestXxx(*testing.T)
     9  // where Xxx does not start with a lowercase letter. The function name
    10  // serves to identify the test routine.
    11  //
    12  // Within these functions, use the Error, Fail or related methods to signal failure.
    13  //
    14  // To write a new test suite, create a file whose name ends _test.go that
    15  // contains the TestXxx functions as described here. Put the file in the same
    16  // package as the one being tested. The file will be excluded from regular
    17  // package builds but will be included when the ``go test'' command is run.
    18  // For more detail, run ``go help test'' and ``go help testflag''.
    19  //
    20  // A simple test function looks like this:
    21  //
    22  //     func TestAbs(t *testing.T) {
    23  //         got := Abs(-1)
    24  //         if got != 1 {
    25  //             t.Errorf("Abs(-1) = %d; want 1", got)
    26  //         }
    27  //     }
    28  //
    29  // Benchmarks
    30  //
    31  // Functions of the form
    32  //     func BenchmarkXxx(*testing.B)
    33  // are considered benchmarks, and are executed by the "go test" command when
    34  // its -bench flag is provided. Benchmarks are run sequentially.
    35  //
    36  // For a description of the testing flags, see
    37  // https://golang.org/cmd/go/#hdr-Testing_flags
    38  //
    39  // A sample benchmark function looks like this:
    40  //     func BenchmarkHello(b *testing.B) {
    41  //         for i := 0; i < b.N; i++ {
    42  //             fmt.Sprintf("hello")
    43  //         }
    44  //     }
    45  //
    46  // The benchmark function must run the target code b.N times.
    47  // During benchmark execution, b.N is adjusted until the benchmark function lasts
    48  // long enough to be timed reliably. The output
    49  //     BenchmarkHello    10000000    282 ns/op
    50  // means that the loop ran 10000000 times at a speed of 282 ns per loop.
    51  //
    52  // If a benchmark needs some expensive setup before running, the timer
    53  // may be reset:
    54  //
    55  //     func BenchmarkBigLen(b *testing.B) {
    56  //         big := NewBig()
    57  //         b.ResetTimer()
    58  //         for i := 0; i < b.N; i++ {
    59  //             big.Len()
    60  //         }
    61  //     }
    62  //
    63  // If a benchmark needs to test performance in a parallel setting, it may use
    64  // the RunParallel helper function; such benchmarks are intended to be used with
    65  // the go test -cpu flag:
    66  //
    67  //     func BenchmarkTemplateParallel(b *testing.B) {
    68  //         templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))
    69  //         b.RunParallel(func(pb *testing.PB) {
    70  //             var buf bytes.Buffer
    71  //             for pb.Next() {
    72  //                 buf.Reset()
    73  //                 templ.Execute(&buf, "World")
    74  //             }
    75  //         })
    76  //     }
    77  //
    78  // Examples
    79  //
    80  // The package also runs and verifies example code. Example functions may
    81  // include a concluding line comment that begins with "Output:" and is compared with
    82  // the standard output of the function when the tests are run. (The comparison
    83  // ignores leading and trailing space.) These are examples of an example:
    84  //
    85  //     func ExampleHello() {
    86  //         fmt.Println("hello")
    87  //         // Output: hello
    88  //     }
    89  //
    90  //     func ExampleSalutations() {
    91  //         fmt.Println("hello, and")
    92  //         fmt.Println("goodbye")
    93  //         // Output:
    94  //         // hello, and
    95  //         // goodbye
    96  //     }
    97  //
    98  // The comment prefix "Unordered output:" is like "Output:", but matches any
    99  // line order:
   100  //
   101  //     func ExamplePerm() {
   102  //         for _, value := range Perm(5) {
   103  //             fmt.Println(value)
   104  //         }
   105  //         // Unordered output: 4
   106  //         // 2
   107  //         // 1
   108  //         // 3
   109  //         // 0
   110  //     }
   111  //
   112  // Example functions without output comments are compiled but not executed.
   113  //
   114  // The naming convention to declare examples for the package, a function F, a type T and
   115  // method M on type T are:
   116  //
   117  //     func Example() { ... }
   118  //     func ExampleF() { ... }
   119  //     func ExampleT() { ... }
   120  //     func ExampleT_M() { ... }
   121  //
   122  // Multiple example functions for a package/type/function/method may be provided by
   123  // appending a distinct suffix to the name. The suffix must start with a
   124  // lower-case letter.
   125  //
   126  //     func Example_suffix() { ... }
   127  //     func ExampleF_suffix() { ... }
   128  //     func ExampleT_suffix() { ... }
   129  //     func ExampleT_M_suffix() { ... }
   130  //
   131  // The entire test file is presented as the example when it contains a single
   132  // example function, at least one other function, type, variable, or constant
   133  // declaration, and no test or benchmark functions.
   134  //
   135  // Skipping
   136  //
   137  // Tests or benchmarks may be skipped at run time with a call to
   138  // the Skip method of *T or *B:
   139  //
   140  //     func TestTimeConsuming(t *testing.T) {
   141  //         if testing.Short() {
   142  //             t.Skip("skipping test in short mode.")
   143  //         }
   144  //         ...
   145  //     }
   146  //
   147  // Subtests and Sub-benchmarks
   148  //
   149  // The Run methods of T and B allow defining subtests and sub-benchmarks,
   150  // without having to define separate functions for each. This enables uses
   151  // like table-driven benchmarks and creating hierarchical tests.
   152  // It also provides a way to share common setup and tear-down code:
   153  //
   154  //     func TestFoo(t *testing.T) {
   155  //         // <setup code>
   156  //         t.Run("A=1", func(t *testing.T) { ... })
   157  //         t.Run("A=2", func(t *testing.T) { ... })
   158  //         t.Run("B=1", func(t *testing.T) { ... })
   159  //         // <tear-down code>
   160  //     }
   161  //
   162  // Each subtest and sub-benchmark has a unique name: the combination of the name
   163  // of the top-level test and the sequence of names passed to Run, separated by
   164  // slashes, with an optional trailing sequence number for disambiguation.
   165  //
   166  // The argument to the -run and -bench command-line flags is an unanchored regular
   167  // expression that matches the test's name. For tests with multiple slash-separated
   168  // elements, such as subtests, the argument is itself slash-separated, with
   169  // expressions matching each name element in turn. Because it is unanchored, an
   170  // empty expression matches any string.
   171  // For example, using "matching" to mean "whose name contains":
   172  //
   173  //     go test -run ''      # Run all tests.
   174  //     go test -run Foo     # Run top-level tests matching "Foo", such as "TestFooBar".
   175  //     go test -run Foo/A=  # For top-level tests matching "Foo", run subtests matching "A=".
   176  //     go test -run /A=1    # For all top-level tests, run subtests matching "A=1".
   177  //
   178  // Subtests can also be used to control parallelism. A parent test will only
   179  // complete once all of its subtests complete. In this example, all tests are
   180  // run in parallel with each other, and only with each other, regardless of
   181  // other top-level tests that may be defined:
   182  //
   183  //     func TestGroupedParallel(t *testing.T) {
   184  //         for _, tc := range tests {
   185  //             tc := tc // capture range variable
   186  //             t.Run(tc.Name, func(t *testing.T) {
   187  //                 t.Parallel()
   188  //                 ...
   189  //             })
   190  //         }
   191  //     }
   192  //
   193  // The race detector kills the program if it exceeds 8192 concurrent goroutines,
   194  // so use care when running parallel tests with the -race flag set.
   195  //
   196  // Run does not return until parallel subtests have completed, providing a way
   197  // to clean up after a group of parallel tests:
   198  //
   199  //     func TestTeardownParallel(t *testing.T) {
   200  //         // This Run will not return until the parallel tests finish.
   201  //         t.Run("group", func(t *testing.T) {
   202  //             t.Run("Test1", parallelTest1)
   203  //             t.Run("Test2", parallelTest2)
   204  //             t.Run("Test3", parallelTest3)
   205  //         })
   206  //         // <tear-down code>
   207  //     }
   208  //
   209  // Main
   210  //
   211  // It is sometimes necessary for a test program to do extra setup or teardown
   212  // before or after testing. It is also sometimes necessary for a test to control
   213  // which code runs on the main thread. To support these and other cases,
   214  // if a test file contains a function:
   215  //
   216  //	func TestMain(m *testing.M)
   217  //
   218  // then the generated test will call TestMain(m) instead of running the tests
   219  // directly. TestMain runs in the main goroutine and can do whatever setup
   220  // and teardown is necessary around a call to m.Run. It should then call
   221  // os.Exit with the result of m.Run. When TestMain is called, flag.Parse has
   222  // not been run. If TestMain depends on command-line flags, including those
   223  // of the testing package, it should call flag.Parse explicitly.
   224  //
   225  // A simple implementation of TestMain is:
   226  //
   227  //	func TestMain(m *testing.M) {
   228  //		// call flag.Parse() here if TestMain uses flags
   229  //		os.Exit(m.Run())
   230  //	}
   231  //
   232  package testing
   233  
   234  import (
   235  	"bytes"
   236  	"errors"
   237  	"flag"
   238  	"fmt"
   239  	"internal/race"
   240  	"io"
   241  	"os"
   242  	"runtime"
   243  	"runtime/debug"
   244  	"runtime/trace"
   245  	"strconv"
   246  	"strings"
   247  	"sync"
   248  	"sync/atomic"
   249  	"time"
   250  )
   251  
   252  var initRan bool
   253  
   254  // Init registers testing flags. These flags are automatically registered by
   255  // the "go test" command before running test functions, so Init is only needed
   256  // when calling functions such as Benchmark without using "go test".
   257  //
   258  // Init has no effect if it was already called.
   259  func Init() {
   260  	if initRan {
   261  		return
   262  	}
   263  	initRan = true
   264  	// The short flag requests that tests run more quickly, but its functionality
   265  	// is provided by test writers themselves. The testing package is just its
   266  	// home. The all.bash installation script sets it to make installation more
   267  	// efficient, but by default the flag is off so a plain "go test" will do a
   268  	// full test of the package.
   269  	short = flag.Bool("test.short", false, "run smaller test suite to save time")
   270  
   271  	// The failfast flag requests that test execution stop after the first test failure.
   272  	failFast = flag.Bool("test.failfast", false, "do not start new tests after the first test failure")
   273  
   274  	// The directory in which to create profile files and the like. When run from
   275  	// "go test", the binary always runs in the source directory for the package;
   276  	// this flag lets "go test" tell the binary to write the files in the directory where
   277  	// the "go test" command is run.
   278  	outputDir = flag.String("test.outputdir", "", "write profiles to `dir`")
   279  	// Report as tests are run; default is silent for success.
   280  	chatty = flag.Bool("test.v", false, "verbose: print additional output")
   281  	count = flag.Uint("test.count", 1, "run tests and benchmarks `n` times")
   282  	coverProfile = flag.String("test.coverprofile", "", "write a coverage profile to `file`")
   283  	matchList = flag.String("test.list", "", "list tests, examples, and benchmarks matching `regexp` then exit")
   284  	match = flag.String("test.run", "", "run only tests and examples matching `regexp`")
   285  	memProfile = flag.String("test.memprofile", "", "write an allocation profile to `file`")
   286  	memProfileRate = flag.Int("test.memprofilerate", 0, "set memory allocation profiling `rate` (see runtime.MemProfileRate)")
   287  	cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profile to `file`")
   288  	blockProfile = flag.String("test.blockprofile", "", "write a goroutine blocking profile to `file`")
   289  	blockProfileRate = flag.Int("test.blockprofilerate", 1, "set blocking profile `rate` (see runtime.SetBlockProfileRate)")
   290  	mutexProfile = flag.String("test.mutexprofile", "", "write a mutex contention profile to the named file after execution")
   291  	mutexProfileFraction = flag.Int("test.mutexprofilefraction", 1, "if >= 0, calls runtime.SetMutexProfileFraction()")
   292  	traceFile = flag.String("test.trace", "", "write an execution trace to `file`")
   293  	timeout = flag.Duration("test.timeout", 0, "panic test binary after duration `d` (default 0, timeout disabled)")
   294  	cpuListStr = flag.String("test.cpu", "", "comma-separated `list` of cpu counts to run each test with")
   295  	parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "run at most `n` tests in parallel")
   296  	testlog = flag.String("test.testlogfile", "", "write test action log to `file` (for use only by cmd/go)")
   297  
   298  	initBenchmarkFlags()
   299  }
   300  
   301  var (
   302  	// Flags, registered during Init.
   303  	short                *bool
   304  	failFast             *bool
   305  	outputDir            *string
   306  	chatty               *bool
   307  	count                *uint
   308  	coverProfile         *string
   309  	matchList            *string
   310  	match                *string
   311  	memProfile           *string
   312  	memProfileRate       *int
   313  	cpuProfile           *string
   314  	blockProfile         *string
   315  	blockProfileRate     *int
   316  	mutexProfile         *string
   317  	mutexProfileFraction *int
   318  	traceFile            *string
   319  	timeout              *time.Duration
   320  	cpuListStr           *string
   321  	parallel             *int
   322  	testlog              *string
   323  
   324  	haveExamples bool // are there examples?
   325  
   326  	cpuList     []int
   327  	testlogFile *os.File
   328  
   329  	numFailed uint32 // number of test failures
   330  )
   331  
   332  // The maximum number of stack frames to go through when skipping helper functions for
   333  // the purpose of decorating log messages.
   334  const maxStackLen = 50
   335  
   336  // common holds the elements common between T and B and
   337  // captures common methods such as Errorf.
   338  type common struct {
   339  	mu      sync.RWMutex        // guards this group of fields
   340  	output  []byte              // Output generated by test or benchmark.
   341  	w       io.Writer           // For flushToParent.
   342  	ran     bool                // Test or benchmark (or one of its subtests) was executed.
   343  	failed  bool                // Test or benchmark has failed.
   344  	skipped bool                // Test of benchmark has been skipped.
   345  	done    bool                // Test is finished and all subtests have completed.
   346  	helpers map[string]struct{} // functions to be skipped when writing file/line info
   347  	cleanup func()              // optional function to be called at the end of the test
   348  
   349  	chatty     bool   // A copy of the chatty flag.
   350  	finished   bool   // Test function has completed.
   351  	hasSub     int32  // written atomically
   352  	raceErrors int    // number of races detected during test
   353  	runner     string // function name of tRunner running the test
   354  
   355  	parent   *common
   356  	level    int       // Nesting depth of test or benchmark.
   357  	creator  []uintptr // If level > 0, the stack trace at the point where the parent called t.Run.
   358  	name     string    // Name of test or benchmark.
   359  	start    time.Time // Time test or benchmark started
   360  	duration time.Duration
   361  	barrier  chan bool // To signal parallel subtests they may start.
   362  	signal   chan bool // To signal a test is done.
   363  	sub      []*T      // Queue of subtests to be run in parallel.
   364  }
   365  
   366  // Short reports whether the -test.short flag is set.
   367  func Short() bool {
   368  	if short == nil {
   369  		panic("testing: Short called before Init")
   370  	}
   371  	// Catch code that calls this from TestMain without first calling flag.Parse.
   372  	if !flag.Parsed() {
   373  		panic("testing: Short called before Parse")
   374  	}
   375  
   376  	return *short
   377  }
   378  
   379  // CoverMode reports what the test coverage mode is set to. The
   380  // values are "set", "count", or "atomic". The return value will be
   381  // empty if test coverage is not enabled.
   382  func CoverMode() string {
   383  	return cover.Mode
   384  }
   385  
   386  // Verbose reports whether the -test.v flag is set.
   387  func Verbose() bool {
   388  	// Same as in Short.
   389  	if chatty == nil {
   390  		panic("testing: Verbose called before Init")
   391  	}
   392  	if !flag.Parsed() {
   393  		panic("testing: Verbose called before Parse")
   394  	}
   395  	return *chatty
   396  }
   397  
   398  // frameSkip searches, starting after skip frames, for the first caller frame
   399  // in a function not marked as a helper and returns that frame.
   400  // The search stops if it finds a tRunner function that
   401  // was the entry point into the test and the test is not a subtest.
   402  // This function must be called with c.mu held.
   403  func (c *common) frameSkip(skip int) runtime.Frame {
   404  	// If the search continues into the parent test, we'll have to hold
   405  	// its mu temporarily. If we then return, we need to unlock it.
   406  	shouldUnlock := false
   407  	defer func() {
   408  		if shouldUnlock {
   409  			c.mu.Unlock()
   410  		}
   411  	}()
   412  	var pc [maxStackLen]uintptr
   413  	// Skip two extra frames to account for this function
   414  	// and runtime.Callers itself.
   415  	n := runtime.Callers(skip+2, pc[:])
   416  	if n == 0 {
   417  		panic("testing: zero callers found")
   418  	}
   419  	frames := runtime.CallersFrames(pc[:n])
   420  	var firstFrame, prevFrame, frame runtime.Frame
   421  	for more := true; more; prevFrame = frame {
   422  		frame, more = frames.Next()
   423  		if firstFrame.PC == 0 {
   424  			firstFrame = frame
   425  		}
   426  		if frame.Function == c.runner {
   427  			// We've gone up all the way to the tRunner calling
   428  			// the test function (so the user must have
   429  			// called tb.Helper from inside that test function).
   430  			// If this is a top-level test, only skip up to the test function itself.
   431  			// If we're in a subtest, continue searching in the parent test,
   432  			// starting from the point of the call to Run which created this subtest.
   433  			if c.level > 1 {
   434  				frames = runtime.CallersFrames(c.creator)
   435  				parent := c.parent
   436  				// We're no longer looking at the current c after this point,
   437  				// so we should unlock its mu, unless it's the original receiver,
   438  				// in which case our caller doesn't expect us to do that.
   439  				if shouldUnlock {
   440  					c.mu.Unlock()
   441  				}
   442  				c = parent
   443  				// Remember to unlock c.mu when we no longer need it, either
   444  				// because we went up another nesting level, or because we
   445  				// returned.
   446  				shouldUnlock = true
   447  				c.mu.Lock()
   448  				continue
   449  			}
   450  			return prevFrame
   451  		}
   452  		if _, ok := c.helpers[frame.Function]; !ok {
   453  			// Found a frame that wasn't inside a helper function.
   454  			return frame
   455  		}
   456  	}
   457  	return firstFrame
   458  }
   459  
   460  // decorate prefixes the string with the file and line of the call site
   461  // and inserts the final newline if needed and indentation spaces for formatting.
   462  // This function must be called with c.mu held.
   463  func (c *common) decorate(s string, skip int) string {
   464  	frame := c.frameSkip(skip)
   465  	file := frame.File
   466  	line := frame.Line
   467  	if file != "" {
   468  		// Truncate file name at last file name separator.
   469  		if index := strings.LastIndex(file, "/"); index >= 0 {
   470  			file = file[index+1:]
   471  		} else if index = strings.LastIndex(file, "\\"); index >= 0 {
   472  			file = file[index+1:]
   473  		}
   474  	} else {
   475  		file = "???"
   476  	}
   477  	if line == 0 {
   478  		line = 1
   479  	}
   480  	buf := new(strings.Builder)
   481  	// Every line is indented at least 4 spaces.
   482  	buf.WriteString("    ")
   483  	if c.chatty {
   484  		fmt.Fprintf(buf, "%s: ", c.name)
   485  	}
   486  	fmt.Fprintf(buf, "%s:%d: ", file, line)
   487  	lines := strings.Split(s, "\n")
   488  	if l := len(lines); l > 1 && lines[l-1] == "" {
   489  		lines = lines[:l-1]
   490  	}
   491  	for i, line := range lines {
   492  		if i > 0 {
   493  			// Second and subsequent lines are indented an additional 4 spaces.
   494  			buf.WriteString("\n        ")
   495  		}
   496  		buf.WriteString(line)
   497  	}
   498  	buf.WriteByte('\n')
   499  	return buf.String()
   500  }
   501  
   502  // flushToParent writes c.output to the parent after first writing the header
   503  // with the given format and arguments.
   504  func (c *common) flushToParent(format string, args ...interface{}) {
   505  	p := c.parent
   506  	p.mu.Lock()
   507  	defer p.mu.Unlock()
   508  
   509  	fmt.Fprintf(p.w, format, args...)
   510  
   511  	c.mu.Lock()
   512  	defer c.mu.Unlock()
   513  	io.Copy(p.w, bytes.NewReader(c.output))
   514  	c.output = c.output[:0]
   515  }
   516  
   517  type indenter struct {
   518  	c *common
   519  }
   520  
   521  func (w indenter) Write(b []byte) (n int, err error) {
   522  	n = len(b)
   523  	for len(b) > 0 {
   524  		end := bytes.IndexByte(b, '\n')
   525  		if end == -1 {
   526  			end = len(b)
   527  		} else {
   528  			end++
   529  		}
   530  		// An indent of 4 spaces will neatly align the dashes with the status
   531  		// indicator of the parent.
   532  		const indent = "    "
   533  		w.c.output = append(w.c.output, indent...)
   534  		w.c.output = append(w.c.output, b[:end]...)
   535  		b = b[end:]
   536  	}
   537  	return
   538  }
   539  
   540  // fmtDuration returns a string representing d in the form "87.00s".
   541  func fmtDuration(d time.Duration) string {
   542  	return fmt.Sprintf("%.2fs", d.Seconds())
   543  }
   544  
   545  // TB is the interface common to T and B.
   546  type TB interface {
   547  	Cleanup(func())
   548  	Error(args ...interface{})
   549  	Errorf(format string, args ...interface{})
   550  	Fail()
   551  	FailNow()
   552  	Failed() bool
   553  	Fatal(args ...interface{})
   554  	Fatalf(format string, args ...interface{})
   555  	Helper()
   556  	Log(args ...interface{})
   557  	Logf(format string, args ...interface{})
   558  	Name() string
   559  	Skip(args ...interface{})
   560  	SkipNow()
   561  	Skipf(format string, args ...interface{})
   562  	Skipped() bool
   563  
   564  	// A private method to prevent users implementing the
   565  	// interface and so future additions to it will not
   566  	// violate Go 1 compatibility.
   567  	private()
   568  }
   569  
   570  var _ TB = (*T)(nil)
   571  var _ TB = (*B)(nil)
   572  
   573  // T is a type passed to Test functions to manage test state and support formatted test logs.
   574  //
   575  // A test ends when its Test function returns or calls any of the methods
   576  // FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods, as well as
   577  // the Parallel method, must be called only from the goroutine running the
   578  // Test function.
   579  //
   580  // The other reporting methods, such as the variations of Log and Error,
   581  // may be called simultaneously from multiple goroutines.
   582  type T struct {
   583  	common
   584  	isParallel bool
   585  	context    *testContext // For running tests and subtests.
   586  }
   587  
   588  func (c *common) private() {}
   589  
   590  // Name returns the name of the running test or benchmark.
   591  func (c *common) Name() string {
   592  	return c.name
   593  }
   594  
   595  func (c *common) setRan() {
   596  	if c.parent != nil {
   597  		c.parent.setRan()
   598  	}
   599  	c.mu.Lock()
   600  	defer c.mu.Unlock()
   601  	c.ran = true
   602  }
   603  
   604  // Fail marks the function as having failed but continues execution.
   605  func (c *common) Fail() {
   606  	if c.parent != nil {
   607  		c.parent.Fail()
   608  	}
   609  	c.mu.Lock()
   610  	defer c.mu.Unlock()
   611  	// c.done needs to be locked to synchronize checks to c.done in parent tests.
   612  	if c.done {
   613  		panic("Fail in goroutine after " + c.name + " has completed")
   614  	}
   615  	c.failed = true
   616  }
   617  
   618  // Failed reports whether the function has failed.
   619  func (c *common) Failed() bool {
   620  	c.mu.RLock()
   621  	failed := c.failed
   622  	c.mu.RUnlock()
   623  	return failed || c.raceErrors+race.Errors() > 0
   624  }
   625  
   626  // FailNow marks the function as having failed and stops its execution
   627  // by calling runtime.Goexit (which then runs all deferred calls in the
   628  // current goroutine).
   629  // Execution will continue at the next test or benchmark.
   630  // FailNow must be called from the goroutine running the
   631  // test or benchmark function, not from other goroutines
   632  // created during the test. Calling FailNow does not stop
   633  // those other goroutines.
   634  func (c *common) FailNow() {
   635  	c.Fail()
   636  
   637  	// Calling runtime.Goexit will exit the goroutine, which
   638  	// will run the deferred functions in this goroutine,
   639  	// which will eventually run the deferred lines in tRunner,
   640  	// which will signal to the test loop that this test is done.
   641  	//
   642  	// A previous version of this code said:
   643  	//
   644  	//	c.duration = ...
   645  	//	c.signal <- c.self
   646  	//	runtime.Goexit()
   647  	//
   648  	// This previous version duplicated code (those lines are in
   649  	// tRunner no matter what), but worse the goroutine teardown
   650  	// implicit in runtime.Goexit was not guaranteed to complete
   651  	// before the test exited. If a test deferred an important cleanup
   652  	// function (like removing temporary files), there was no guarantee
   653  	// it would run on a test failure. Because we send on c.signal during
   654  	// a top-of-stack deferred function now, we know that the send
   655  	// only happens after any other stacked defers have completed.
   656  	c.finished = true
   657  	runtime.Goexit()
   658  }
   659  
   660  // log generates the output. It's always at the same stack depth.
   661  func (c *common) log(s string) {
   662  	c.logDepth(s, 3) // logDepth + log + public function
   663  }
   664  
   665  // logDepth generates the output at an arbitrary stack depth.
   666  func (c *common) logDepth(s string, depth int) {
   667  	c.mu.Lock()
   668  	defer c.mu.Unlock()
   669  	if c.done {
   670  		// This test has already finished. Try and log this message
   671  		// with our parent. If we don't have a parent, panic.
   672  		for parent := c.parent; parent != nil; parent = parent.parent {
   673  			parent.mu.Lock()
   674  			defer parent.mu.Unlock()
   675  			if !parent.done {
   676  				parent.output = append(parent.output, parent.decorate(s, depth+1)...)
   677  				return
   678  			}
   679  		}
   680  		panic("Log in goroutine after " + c.name + " has completed")
   681  	} else {
   682  		if c.chatty {
   683  			fmt.Print(c.decorate(s, depth+1))
   684  			return
   685  		}
   686  		c.output = append(c.output, c.decorate(s, depth+1)...)
   687  	}
   688  }
   689  
   690  // Log formats its arguments using default formatting, analogous to Println,
   691  // and records the text in the error log. For tests, the text will be printed only if
   692  // the test fails or the -test.v flag is set. For benchmarks, the text is always
   693  // printed to avoid having performance depend on the value of the -test.v flag.
   694  func (c *common) Log(args ...interface{}) { c.log(fmt.Sprintln(args...)) }
   695  
   696  // Logf formats its arguments according to the format, analogous to Printf, and
   697  // records the text in the error log. A final newline is added if not provided. For
   698  // tests, the text will be printed only if the test fails or the -test.v flag is
   699  // set. For benchmarks, the text is always printed to avoid having performance
   700  // depend on the value of the -test.v flag.
   701  func (c *common) Logf(format string, args ...interface{}) { c.log(fmt.Sprintf(format, args...)) }
   702  
   703  // Error is equivalent to Log followed by Fail.
   704  func (c *common) Error(args ...interface{}) {
   705  	c.log(fmt.Sprintln(args...))
   706  	c.Fail()
   707  }
   708  
   709  // Errorf is equivalent to Logf followed by Fail.
   710  func (c *common) Errorf(format string, args ...interface{}) {
   711  	c.log(fmt.Sprintf(format, args...))
   712  	c.Fail()
   713  }
   714  
   715  // Fatal is equivalent to Log followed by FailNow.
   716  func (c *common) Fatal(args ...interface{}) {
   717  	c.log(fmt.Sprintln(args...))
   718  	c.FailNow()
   719  }
   720  
   721  // Fatalf is equivalent to Logf followed by FailNow.
   722  func (c *common) Fatalf(format string, args ...interface{}) {
   723  	c.log(fmt.Sprintf(format, args...))
   724  	c.FailNow()
   725  }
   726  
   727  // Skip is equivalent to Log followed by SkipNow.
   728  func (c *common) Skip(args ...interface{}) {
   729  	c.log(fmt.Sprintln(args...))
   730  	c.SkipNow()
   731  }
   732  
   733  // Skipf is equivalent to Logf followed by SkipNow.
   734  func (c *common) Skipf(format string, args ...interface{}) {
   735  	c.log(fmt.Sprintf(format, args...))
   736  	c.SkipNow()
   737  }
   738  
   739  // SkipNow marks the test as having been skipped and stops its execution
   740  // by calling runtime.Goexit.
   741  // If a test fails (see Error, Errorf, Fail) and is then skipped,
   742  // it is still considered to have failed.
   743  // Execution will continue at the next test or benchmark. See also FailNow.
   744  // SkipNow must be called from the goroutine running the test, not from
   745  // other goroutines created during the test. Calling SkipNow does not stop
   746  // those other goroutines.
   747  func (c *common) SkipNow() {
   748  	c.skip()
   749  	c.finished = true
   750  	runtime.Goexit()
   751  }
   752  
   753  func (c *common) skip() {
   754  	c.mu.Lock()
   755  	defer c.mu.Unlock()
   756  	c.skipped = true
   757  }
   758  
   759  // Skipped reports whether the test was skipped.
   760  func (c *common) Skipped() bool {
   761  	c.mu.RLock()
   762  	defer c.mu.RUnlock()
   763  	return c.skipped
   764  }
   765  
   766  // Helper marks the calling function as a test helper function.
   767  // When printing file and line information, that function will be skipped.
   768  // Helper may be called simultaneously from multiple goroutines.
   769  func (c *common) Helper() {
   770  	c.mu.Lock()
   771  	defer c.mu.Unlock()
   772  	if c.helpers == nil {
   773  		c.helpers = make(map[string]struct{})
   774  	}
   775  	c.helpers[callerName(1)] = struct{}{}
   776  }
   777  
   778  // Cleanup registers a function to be called when the test and all its
   779  // subtests complete. Cleanup functions will be called in last added,
   780  // first called order.
   781  func (c *common) Cleanup(f func()) {
   782  	c.mu.Lock()
   783  	defer c.mu.Unlock()
   784  	oldCleanup := c.cleanup
   785  	c.cleanup = func() {
   786  		if oldCleanup != nil {
   787  			defer oldCleanup()
   788  		}
   789  		f()
   790  	}
   791  }
   792  
   793  // panicHanding is an argument to runCleanup.
   794  type panicHandling int
   795  
   796  const (
   797  	normalPanic panicHandling = iota
   798  	recoverAndReturnPanic
   799  )
   800  
   801  // runCleanup is called at the end of the test.
   802  // If catchPanic is true, this will catch panics, and return the recovered
   803  // value if any.
   804  func (c *common) runCleanup(ph panicHandling) (panicVal interface{}) {
   805  	c.mu.Lock()
   806  	cleanup := c.cleanup
   807  	c.cleanup = nil
   808  	c.mu.Unlock()
   809  	if cleanup == nil {
   810  		return nil
   811  	}
   812  
   813  	if ph == recoverAndReturnPanic {
   814  		defer func() {
   815  			panicVal = recover()
   816  		}()
   817  	}
   818  
   819  	cleanup()
   820  	return nil
   821  }
   822  
   823  // callerName gives the function name (qualified with a package path)
   824  // for the caller after skip frames (where 0 means the current function).
   825  func callerName(skip int) string {
   826  	// Make room for the skip PC.
   827  	var pc [1]uintptr
   828  	n := runtime.Callers(skip+2, pc[:]) // skip + runtime.Callers + callerName
   829  	if n == 0 {
   830  		panic("testing: zero callers found")
   831  	}
   832  	frames := runtime.CallersFrames(pc[:n])
   833  	frame, _ := frames.Next()
   834  	return frame.Function
   835  }
   836  
   837  // Parallel signals that this test is to be run in parallel with (and only with)
   838  // other parallel tests. When a test is run multiple times due to use of
   839  // -test.count or -test.cpu, multiple instances of a single test never run in
   840  // parallel with each other.
   841  func (t *T) Parallel() {
   842  	if t.isParallel {
   843  		panic("testing: t.Parallel called multiple times")
   844  	}
   845  	t.isParallel = true
   846  
   847  	// We don't want to include the time we spend waiting for serial tests
   848  	// in the test duration. Record the elapsed time thus far and reset the
   849  	// timer afterwards.
   850  	t.duration += time.Since(t.start)
   851  
   852  	// Add to the list of tests to be released by the parent.
   853  	t.parent.sub = append(t.parent.sub, t)
   854  	t.raceErrors += race.Errors()
   855  
   856  	if t.chatty {
   857  		// Print directly to root's io.Writer so there is no delay.
   858  		root := t.parent
   859  		for ; root.parent != nil; root = root.parent {
   860  		}
   861  		root.mu.Lock()
   862  		fmt.Fprintf(root.w, "=== PAUSE %s\n", t.name)
   863  		root.mu.Unlock()
   864  	}
   865  
   866  	t.signal <- true   // Release calling test.
   867  	<-t.parent.barrier // Wait for the parent test to complete.
   868  	t.context.waitParallel()
   869  
   870  	if t.chatty {
   871  		// Print directly to root's io.Writer so there is no delay.
   872  		root := t.parent
   873  		for ; root.parent != nil; root = root.parent {
   874  		}
   875  		root.mu.Lock()
   876  		fmt.Fprintf(root.w, "=== CONT  %s\n", t.name)
   877  		root.mu.Unlock()
   878  	}
   879  
   880  	t.start = time.Now()
   881  	t.raceErrors += -race.Errors()
   882  }
   883  
   884  // InternalTest is an internal type but exported because it is cross-package;
   885  // it is part of the implementation of the "go test" command.
   886  type InternalTest struct {
   887  	Name string
   888  	F    func(*T)
   889  }
   890  
   891  var errNilPanicOrGoexit = errors.New("test executed panic(nil) or runtime.Goexit")
   892  
   893  func tRunner(t *T, fn func(t *T)) {
   894  	t.runner = callerName(0)
   895  
   896  	// When this goroutine is done, either because fn(t)
   897  	// returned normally or because a test failure triggered
   898  	// a call to runtime.Goexit, record the duration and send
   899  	// a signal saying that the test is done.
   900  	defer func() {
   901  		if t.Failed() {
   902  			atomic.AddUint32(&numFailed, 1)
   903  		}
   904  
   905  		if t.raceErrors+race.Errors() > 0 {
   906  			t.Errorf("race detected during execution of test")
   907  		}
   908  
   909  		// If the test panicked, print any test output before dying.
   910  		err := recover()
   911  		signal := true
   912  		if !t.finished && err == nil {
   913  			err = errNilPanicOrGoexit
   914  			for p := t.parent; p != nil; p = p.parent {
   915  				if p.finished {
   916  					t.Errorf("%v: subtest may have called FailNow on a parent test", err)
   917  					err = nil
   918  					signal = false
   919  					break
   920  				}
   921  			}
   922  		}
   923  
   924  		doPanic := func(err interface{}) {
   925  			t.Fail()
   926  			if r := t.runCleanup(recoverAndReturnPanic); r != nil {
   927  				t.Logf("cleanup panicked with %v", r)
   928  			}
   929  			// Flush the output log up to the root before dying.
   930  			for root := &t.common; root.parent != nil; root = root.parent {
   931  				root.mu.Lock()
   932  				root.duration += time.Since(root.start)
   933  				d := root.duration
   934  				root.mu.Unlock()
   935  				root.flushToParent("--- FAIL: %s (%s)\n", root.name, fmtDuration(d))
   936  				if r := root.parent.runCleanup(recoverAndReturnPanic); r != nil {
   937  					fmt.Fprintf(root.parent.w, "cleanup panicked with %v", r)
   938  				}
   939  			}
   940  			panic(err)
   941  		}
   942  		if err != nil {
   943  			doPanic(err)
   944  		}
   945  
   946  		t.duration += time.Since(t.start)
   947  
   948  		if len(t.sub) > 0 {
   949  			// Run parallel subtests.
   950  			// Decrease the running count for this test.
   951  			t.context.release()
   952  			// Release the parallel subtests.
   953  			close(t.barrier)
   954  			// Wait for subtests to complete.
   955  			for _, sub := range t.sub {
   956  				<-sub.signal
   957  			}
   958  			cleanupStart := time.Now()
   959  			err := t.runCleanup(recoverAndReturnPanic)
   960  			t.duration += time.Since(cleanupStart)
   961  			if err != nil {
   962  				doPanic(err)
   963  			}
   964  			if !t.isParallel {
   965  				// Reacquire the count for sequential tests. See comment in Run.
   966  				t.context.waitParallel()
   967  			}
   968  		} else if t.isParallel {
   969  			// Only release the count for this test if it was run as a parallel
   970  			// test. See comment in Run method.
   971  			t.context.release()
   972  		}
   973  		t.report() // Report after all subtests have finished.
   974  
   975  		// Do not lock t.done to allow race detector to detect race in case
   976  		// the user does not appropriately synchronizes a goroutine.
   977  		t.done = true
   978  		if t.parent != nil && atomic.LoadInt32(&t.hasSub) == 0 {
   979  			t.setRan()
   980  		}
   981  		t.signal <- signal
   982  	}()
   983  	defer func() {
   984  		if len(t.sub) == 0 {
   985  			t.runCleanup(normalPanic)
   986  		}
   987  	}()
   988  
   989  	t.start = time.Now()
   990  	t.raceErrors = -race.Errors()
   991  	fn(t)
   992  
   993  	// code beyond here will not be executed when FailNow is invoked
   994  	t.finished = true
   995  }
   996  
   997  // Run runs f as a subtest of t called name. It runs f in a separate goroutine
   998  // and blocks until f returns or calls t.Parallel to become a parallel test.
   999  // Run reports whether f succeeded (or at least did not fail before calling t.Parallel).
  1000  //
  1001  // Run may be called simultaneously from multiple goroutines, but all such calls
  1002  // must return before the outer test function for t returns.
  1003  func (t *T) Run(name string, f func(t *T)) bool {
  1004  	atomic.StoreInt32(&t.hasSub, 1)
  1005  	testName, ok, _ := t.context.match.fullName(&t.common, name)
  1006  	if !ok || shouldFailFast() {
  1007  		return true
  1008  	}
  1009  	// Record the stack trace at the point of this call so that if the subtest
  1010  	// function - which runs in a separate stack - is marked as a helper, we can
  1011  	// continue walking the stack into the parent test.
  1012  	var pc [maxStackLen]uintptr
  1013  	n := runtime.Callers(2, pc[:])
  1014  	t = &T{
  1015  		common: common{
  1016  			barrier: make(chan bool),
  1017  			signal:  make(chan bool),
  1018  			name:    testName,
  1019  			parent:  &t.common,
  1020  			level:   t.level + 1,
  1021  			creator: pc[:n],
  1022  			chatty:  t.chatty,
  1023  		},
  1024  		context: t.context,
  1025  	}
  1026  	t.w = indenter{&t.common}
  1027  
  1028  	if t.chatty {
  1029  		// Print directly to root's io.Writer so there is no delay.
  1030  		root := t.parent
  1031  		for ; root.parent != nil; root = root.parent {
  1032  		}
  1033  		root.mu.Lock()
  1034  		fmt.Fprintf(root.w, "=== RUN   %s\n", t.name)
  1035  		root.mu.Unlock()
  1036  	}
  1037  	// Instead of reducing the running count of this test before calling the
  1038  	// tRunner and increasing it afterwards, we rely on tRunner keeping the
  1039  	// count correct. This ensures that a sequence of sequential tests runs
  1040  	// without being preempted, even when their parent is a parallel test. This
  1041  	// may especially reduce surprises if *parallel == 1.
  1042  	go tRunner(t, f)
  1043  	if !<-t.signal {
  1044  		// At this point, it is likely that FailNow was called on one of the
  1045  		// parent tests by one of the subtests. Continue aborting up the chain.
  1046  		runtime.Goexit()
  1047  	}
  1048  	return !t.failed
  1049  }
  1050  
  1051  // testContext holds all fields that are common to all tests. This includes
  1052  // synchronization primitives to run at most *parallel tests.
  1053  type testContext struct {
  1054  	match *matcher
  1055  
  1056  	mu sync.Mutex
  1057  
  1058  	// Channel used to signal tests that are ready to be run in parallel.
  1059  	startParallel chan bool
  1060  
  1061  	// running is the number of tests currently running in parallel.
  1062  	// This does not include tests that are waiting for subtests to complete.
  1063  	running int
  1064  
  1065  	// numWaiting is the number tests waiting to be run in parallel.
  1066  	numWaiting int
  1067  
  1068  	// maxParallel is a copy of the parallel flag.
  1069  	maxParallel int
  1070  }
  1071  
  1072  func newTestContext(maxParallel int, m *matcher) *testContext {
  1073  	return &testContext{
  1074  		match:         m,
  1075  		startParallel: make(chan bool),
  1076  		maxParallel:   maxParallel,
  1077  		running:       1, // Set the count to 1 for the main (sequential) test.
  1078  	}
  1079  }
  1080  
  1081  func (c *testContext) waitParallel() {
  1082  	c.mu.Lock()
  1083  	if c.running < c.maxParallel {
  1084  		c.running++
  1085  		c.mu.Unlock()
  1086  		return
  1087  	}
  1088  	c.numWaiting++
  1089  	c.mu.Unlock()
  1090  	<-c.startParallel
  1091  }
  1092  
  1093  func (c *testContext) release() {
  1094  	c.mu.Lock()
  1095  	if c.numWaiting == 0 {
  1096  		c.running--
  1097  		c.mu.Unlock()
  1098  		return
  1099  	}
  1100  	c.numWaiting--
  1101  	c.mu.Unlock()
  1102  	c.startParallel <- true // Pick a waiting test to be run.
  1103  }
  1104  
  1105  // No one should be using func Main anymore.
  1106  // See the doc comment on func Main and use MainStart instead.
  1107  var errMain = errors.New("testing: unexpected use of func Main")
  1108  
  1109  type matchStringOnly func(pat, str string) (bool, error)
  1110  
  1111  func (f matchStringOnly) MatchString(pat, str string) (bool, error)   { return f(pat, str) }
  1112  func (f matchStringOnly) StartCPUProfile(w io.Writer) error           { return errMain }
  1113  func (f matchStringOnly) StopCPUProfile()                             {}
  1114  func (f matchStringOnly) WriteProfileTo(string, io.Writer, int) error { return errMain }
  1115  func (f matchStringOnly) ImportPath() string                          { return "" }
  1116  func (f matchStringOnly) StartTestLog(io.Writer)                      {}
  1117  func (f matchStringOnly) StopTestLog() error                          { return errMain }
  1118  
  1119  // Main is an internal function, part of the implementation of the "go test" command.
  1120  // It was exported because it is cross-package and predates "internal" packages.
  1121  // It is no longer used by "go test" but preserved, as much as possible, for other
  1122  // systems that simulate "go test" using Main, but Main sometimes cannot be updated as
  1123  // new functionality is added to the testing package.
  1124  // Systems simulating "go test" should be updated to use MainStart.
  1125  func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) {
  1126  	os.Exit(MainStart(matchStringOnly(matchString), tests, benchmarks, examples).Run())
  1127  }
  1128  
  1129  // M is a type passed to a TestMain function to run the actual tests.
  1130  type M struct {
  1131  	deps       testDeps
  1132  	tests      []InternalTest
  1133  	benchmarks []InternalBenchmark
  1134  	examples   []InternalExample
  1135  
  1136  	timer     *time.Timer
  1137  	afterOnce sync.Once
  1138  
  1139  	numRun int
  1140  }
  1141  
  1142  // testDeps is an internal interface of functionality that is
  1143  // passed into this package by a test's generated main package.
  1144  // The canonical implementation of this interface is
  1145  // testing/internal/testdeps's TestDeps.
  1146  type testDeps interface {
  1147  	ImportPath() string
  1148  	MatchString(pat, str string) (bool, error)
  1149  	StartCPUProfile(io.Writer) error
  1150  	StopCPUProfile()
  1151  	StartTestLog(io.Writer)
  1152  	StopTestLog() error
  1153  	WriteProfileTo(string, io.Writer, int) error
  1154  }
  1155  
  1156  // MainStart is meant for use by tests generated by 'go test'.
  1157  // It is not meant to be called directly and is not subject to the Go 1 compatibility document.
  1158  // It may change signature from release to release.
  1159  func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) *M {
  1160  	Init()
  1161  	return &M{
  1162  		deps:       deps,
  1163  		tests:      tests,
  1164  		benchmarks: benchmarks,
  1165  		examples:   examples,
  1166  	}
  1167  }
  1168  
  1169  // Run runs the tests. It returns an exit code to pass to os.Exit.
  1170  func (m *M) Run() int {
  1171  	// Count the number of calls to m.Run.
  1172  	// We only ever expected 1, but we didn't enforce that,
  1173  	// and now there are tests in the wild that call m.Run multiple times.
  1174  	// Sigh. golang.org/issue/23129.
  1175  	m.numRun++
  1176  
  1177  	// TestMain may have already called flag.Parse.
  1178  	if !flag.Parsed() {
  1179  		flag.Parse()
  1180  	}
  1181  
  1182  	if *parallel < 1 {
  1183  		fmt.Fprintln(os.Stderr, "testing: -parallel can only be given a positive integer")
  1184  		flag.Usage()
  1185  		return 2
  1186  	}
  1187  
  1188  	if len(*matchList) != 0 {
  1189  		listTests(m.deps.MatchString, m.tests, m.benchmarks, m.examples)
  1190  		return 0
  1191  	}
  1192  
  1193  	parseCpuList()
  1194  
  1195  	m.before()
  1196  	defer m.after()
  1197  	m.startAlarm()
  1198  	haveExamples = len(m.examples) > 0
  1199  	testRan, testOk := runTests(m.deps.MatchString, m.tests)
  1200  	exampleRan, exampleOk := runExamples(m.deps.MatchString, m.examples)
  1201  	m.stopAlarm()
  1202  	if !testRan && !exampleRan && *matchBenchmarks == "" {
  1203  		fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
  1204  	}
  1205  	if !testOk || !exampleOk || !runBenchmarks(m.deps.ImportPath(), m.deps.MatchString, m.benchmarks) || race.Errors() > 0 {
  1206  		fmt.Println("FAIL")
  1207  		return 1
  1208  	}
  1209  
  1210  	fmt.Println("PASS")
  1211  	return 0
  1212  }
  1213  
  1214  func (t *T) report() {
  1215  	if t.parent == nil {
  1216  		return
  1217  	}
  1218  	dstr := fmtDuration(t.duration)
  1219  	format := "--- %s: %s (%s)\n"
  1220  	if t.Failed() {
  1221  		t.flushToParent(format, "FAIL", t.name, dstr)
  1222  	} else if t.chatty {
  1223  		if t.Skipped() {
  1224  			t.flushToParent(format, "SKIP", t.name, dstr)
  1225  		} else {
  1226  			t.flushToParent(format, "PASS", t.name, dstr)
  1227  		}
  1228  	}
  1229  }
  1230  
  1231  func listTests(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) {
  1232  	if _, err := matchString(*matchList, "non-empty"); err != nil {
  1233  		fmt.Fprintf(os.Stderr, "testing: invalid regexp in -test.list (%q): %s\n", *matchList, err)
  1234  		os.Exit(1)
  1235  	}
  1236  
  1237  	for _, test := range tests {
  1238  		if ok, _ := matchString(*matchList, test.Name); ok {
  1239  			fmt.Println(test.Name)
  1240  		}
  1241  	}
  1242  	for _, bench := range benchmarks {
  1243  		if ok, _ := matchString(*matchList, bench.Name); ok {
  1244  			fmt.Println(bench.Name)
  1245  		}
  1246  	}
  1247  	for _, example := range examples {
  1248  		if ok, _ := matchString(*matchList, example.Name); ok {
  1249  			fmt.Println(example.Name)
  1250  		}
  1251  	}
  1252  }
  1253  
  1254  // RunTests is an internal function but exported because it is cross-package;
  1255  // it is part of the implementation of the "go test" command.
  1256  func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool) {
  1257  	ran, ok := runTests(matchString, tests)
  1258  	if !ran && !haveExamples {
  1259  		fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
  1260  	}
  1261  	return ok
  1262  }
  1263  
  1264  func runTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ran, ok bool) {
  1265  	ok = true
  1266  	for _, procs := range cpuList {
  1267  		runtime.GOMAXPROCS(procs)
  1268  		for i := uint(0); i < *count; i++ {
  1269  			if shouldFailFast() {
  1270  				break
  1271  			}
  1272  			ctx := newTestContext(*parallel, newMatcher(matchString, *match, "-test.run"))
  1273  			t := &T{
  1274  				common: common{
  1275  					signal:  make(chan bool),
  1276  					barrier: make(chan bool),
  1277  					w:       os.Stdout,
  1278  					chatty:  *chatty,
  1279  				},
  1280  				context: ctx,
  1281  			}
  1282  			tRunner(t, func(t *T) {
  1283  				for _, test := range tests {
  1284  					t.Run(test.Name, test.F)
  1285  				}
  1286  				// Run catching the signal rather than the tRunner as a separate
  1287  				// goroutine to avoid adding a goroutine during the sequential
  1288  				// phase as this pollutes the stacktrace output when aborting.
  1289  				go func() { <-t.signal }()
  1290  			})
  1291  			ok = ok && !t.Failed()
  1292  			ran = ran || t.ran
  1293  		}
  1294  	}
  1295  	return ran, ok
  1296  }
  1297  
  1298  // before runs before all testing.
  1299  func (m *M) before() {
  1300  	if *memProfileRate > 0 {
  1301  		runtime.MemProfileRate = *memProfileRate
  1302  	}
  1303  	if *cpuProfile != "" {
  1304  		f, err := os.Create(toOutputDir(*cpuProfile))
  1305  		if err != nil {
  1306  			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
  1307  			return
  1308  		}
  1309  		if err := m.deps.StartCPUProfile(f); err != nil {
  1310  			fmt.Fprintf(os.Stderr, "testing: can't start cpu profile: %s\n", err)
  1311  			f.Close()
  1312  			return
  1313  		}
  1314  		// Could save f so after can call f.Close; not worth the effort.
  1315  	}
  1316  	if *traceFile != "" {
  1317  		f, err := os.Create(toOutputDir(*traceFile))
  1318  		if err != nil {
  1319  			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
  1320  			return
  1321  		}
  1322  		if err := trace.Start(f); err != nil {
  1323  			fmt.Fprintf(os.Stderr, "testing: can't start tracing: %s\n", err)
  1324  			f.Close()
  1325  			return
  1326  		}
  1327  		// Could save f so after can call f.Close; not worth the effort.
  1328  	}
  1329  	if *blockProfile != "" && *blockProfileRate >= 0 {
  1330  		runtime.SetBlockProfileRate(*blockProfileRate)
  1331  	}
  1332  	if *mutexProfile != "" && *mutexProfileFraction >= 0 {
  1333  		runtime.SetMutexProfileFraction(*mutexProfileFraction)
  1334  	}
  1335  	if *coverProfile != "" && cover.Mode == "" {
  1336  		fmt.Fprintf(os.Stderr, "testing: cannot use -test.coverprofile because test binary was not built with coverage enabled\n")
  1337  		os.Exit(2)
  1338  	}
  1339  	if *testlog != "" {
  1340  		// Note: Not using toOutputDir.
  1341  		// This file is for use by cmd/go, not users.
  1342  		var f *os.File
  1343  		var err error
  1344  		if m.numRun == 1 {
  1345  			f, err = os.Create(*testlog)
  1346  		} else {
  1347  			f, err = os.OpenFile(*testlog, os.O_WRONLY, 0)
  1348  			if err == nil {
  1349  				f.Seek(0, io.SeekEnd)
  1350  			}
  1351  		}
  1352  		if err != nil {
  1353  			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
  1354  			os.Exit(2)
  1355  		}
  1356  		m.deps.StartTestLog(f)
  1357  		testlogFile = f
  1358  	}
  1359  }
  1360  
  1361  // after runs after all testing.
  1362  func (m *M) after() {
  1363  	m.afterOnce.Do(func() {
  1364  		m.writeProfiles()
  1365  	})
  1366  }
  1367  
  1368  func (m *M) writeProfiles() {
  1369  	if *testlog != "" {
  1370  		if err := m.deps.StopTestLog(); err != nil {
  1371  			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *testlog, err)
  1372  			os.Exit(2)
  1373  		}
  1374  		if err := testlogFile.Close(); err != nil {
  1375  			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *testlog, err)
  1376  			os.Exit(2)
  1377  		}
  1378  	}
  1379  	if *cpuProfile != "" {
  1380  		m.deps.StopCPUProfile() // flushes profile to disk
  1381  	}
  1382  	if *traceFile != "" {
  1383  		trace.Stop() // flushes trace to disk
  1384  	}
  1385  	if *memProfile != "" {
  1386  		f, err := os.Create(toOutputDir(*memProfile))
  1387  		if err != nil {
  1388  			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
  1389  			os.Exit(2)
  1390  		}
  1391  		runtime.GC() // materialize all statistics
  1392  		if err = m.deps.WriteProfileTo("allocs", f, 0); err != nil {
  1393  			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *memProfile, err)
  1394  			os.Exit(2)
  1395  		}
  1396  		f.Close()
  1397  	}
  1398  	if *blockProfile != "" && *blockProfileRate >= 0 {
  1399  		f, err := os.Create(toOutputDir(*blockProfile))
  1400  		if err != nil {
  1401  			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
  1402  			os.Exit(2)
  1403  		}
  1404  		if err = m.deps.WriteProfileTo("block", f, 0); err != nil {
  1405  			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *blockProfile, err)
  1406  			os.Exit(2)
  1407  		}
  1408  		f.Close()
  1409  	}
  1410  	if *mutexProfile != "" && *mutexProfileFraction >= 0 {
  1411  		f, err := os.Create(toOutputDir(*mutexProfile))
  1412  		if err != nil {
  1413  			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
  1414  			os.Exit(2)
  1415  		}
  1416  		if err = m.deps.WriteProfileTo("mutex", f, 0); err != nil {
  1417  			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *mutexProfile, err)
  1418  			os.Exit(2)
  1419  		}
  1420  		f.Close()
  1421  	}
  1422  	if cover.Mode != "" {
  1423  		coverReport()
  1424  	}
  1425  }
  1426  
  1427  // toOutputDir returns the file name relocated, if required, to outputDir.
  1428  // Simple implementation to avoid pulling in path/filepath.
  1429  func toOutputDir(path string) string {
  1430  	if *outputDir == "" || path == "" {
  1431  		return path
  1432  	}
  1433  	// On Windows, it's clumsy, but we can be almost always correct
  1434  	// by just looking for a drive letter and a colon.
  1435  	// Absolute paths always have a drive letter (ignoring UNC).
  1436  	// Problem: if path == "C:A" and outputdir == "C:\Go" it's unclear
  1437  	// what to do, but even then path/filepath doesn't help.
  1438  	// TODO: Worth doing better? Probably not, because we're here only
  1439  	// under the management of go test.
  1440  	if runtime.GOOS == "windows" && len(path) >= 2 {
  1441  		letter, colon := path[0], path[1]
  1442  		if ('a' <= letter && letter <= 'z' || 'A' <= letter && letter <= 'Z') && colon == ':' {
  1443  			// If path starts with a drive letter we're stuck with it regardless.
  1444  			return path
  1445  		}
  1446  	}
  1447  	if os.IsPathSeparator(path[0]) {
  1448  		return path
  1449  	}
  1450  	return fmt.Sprintf("%s%c%s", *outputDir, os.PathSeparator, path)
  1451  }
  1452  
  1453  // startAlarm starts an alarm if requested.
  1454  func (m *M) startAlarm() {
  1455  	if *timeout > 0 {
  1456  		m.timer = time.AfterFunc(*timeout, func() {
  1457  			m.after()
  1458  			debug.SetTraceback("all")
  1459  			panic(fmt.Sprintf("test timed out after %v", *timeout))
  1460  		})
  1461  	}
  1462  }
  1463  
  1464  // stopAlarm turns off the alarm.
  1465  func (m *M) stopAlarm() {
  1466  	if *timeout > 0 {
  1467  		m.timer.Stop()
  1468  	}
  1469  }
  1470  
  1471  func parseCpuList() {
  1472  	for _, val := range strings.Split(*cpuListStr, ",") {
  1473  		val = strings.TrimSpace(val)
  1474  		if val == "" {
  1475  			continue
  1476  		}
  1477  		cpu, err := strconv.Atoi(val)
  1478  		if err != nil || cpu <= 0 {
  1479  			fmt.Fprintf(os.Stderr, "testing: invalid value %q for -test.cpu\n", val)
  1480  			os.Exit(1)
  1481  		}
  1482  		cpuList = append(cpuList, cpu)
  1483  	}
  1484  	if cpuList == nil {
  1485  		cpuList = append(cpuList, runtime.GOMAXPROCS(-1))
  1486  	}
  1487  }
  1488  
  1489  func shouldFailFast() bool {
  1490  	return *failFast && atomic.LoadUint32(&numFailed) > 0
  1491  }
  1492  

View as plain text