init defines the default parallelism for tests, capping it to MaxTestParallelism. Any user-provided value for -test.parallel will override this.
()
| 16 | // init defines the default parallelism for tests, capping it to MaxTestParallelism. |
| 17 | // Any user-provided value for -test.parallel will override this. |
| 18 | func init() { |
| 19 | // Setup the test flags. |
| 20 | testing.Init() |
| 21 | |
| 22 | // info is used for debugging panics in this init function. |
| 23 | info := "Resolve the issue in the file initflags.go" |
| 24 | _, file, line, ok := runtime.Caller(0) |
| 25 | if ok { |
| 26 | info = fmt.Sprintf("Resolve the issue in the file %s:%d", file, line) |
| 27 | } |
| 28 | |
| 29 | // Lookup the test.parallel flag's value, and cap it to MaxTestParallelism. This |
| 30 | // all happens before `flag.Parse()`, so any user-provided value will overwrite |
| 31 | // whatever we set here. |
| 32 | par := flag.CommandLine.Lookup("test.parallel") |
| 33 | if par == nil { |
| 34 | // This should never happen. If you are reading this message because of a panic, |
| 35 | // just comment out the panic and add a `return` statement instead. |
| 36 | msg := "no 'test.parallel' flag found, unable to set default parallelism" |
| 37 | panic(msg + "\n" + info) |
| 38 | } |
| 39 | |
| 40 | parValue, err := strconv.ParseInt(par.Value.String(), 0, 64) |
| 41 | if err != nil { |
| 42 | // This should never happen, but if it does, panic with a useful message. If you |
| 43 | // are reading this message because of a panic, that means the default value for |
| 44 | // -test.parallel is not an integer. A safe fix is to comment out the panic. This |
| 45 | // will assume the default value of '0', and replace it with MaxTestParallelism. |
| 46 | // Which is not ideal, but at least tests will run. |
| 47 | msg := fmt.Sprintf("failed to parse test.parallel: %v", err) |
| 48 | |
| 49 | panic(msg + "\n" + info) |
| 50 | } |
| 51 | |
| 52 | if parValue > MaxTestParallelism { |
| 53 | _ = par.Value.Set(fmt.Sprintf("%d", MaxTestParallelism)) |
| 54 | } |
| 55 | } |