- Notifications
You must be signed in to change notification settings - Fork1k
test: set test flags from within an init to limit maximum test parallelism#19575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
8117381
a9fc3c9
d53c1b5
f004c68
f3b28ea
0e6a0c1
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package coderdtest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Only applies to this pkg? What about others like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. We import If there are some pkgs that do not import this, they probably do not need the parallism limit | ||
import ( | ||
"flag" | ||
"fmt" | ||
"runtime" | ||
"strconv" | ||
"testing" | ||
) | ||
const ( | ||
// MaxTestParallelism is set to match our MakeFile's `make test` target. | ||
MaxTestParallelism = 8 | ||
) | ||
// init defines the default parallelism for tests, capping it to MaxTestParallelism. | ||
// Any user-provided value for -test.parallel will override this. | ||
func init() { | ||
// Setup the test flags. | ||
testing.Init() | ||
// info is used for debugging panics in this init function. | ||
info := "Resolve the issue in the file initflags.go" | ||
_, file, line, ok := runtime.Caller(0) | ||
if ok { | ||
info = fmt.Sprintf("Resolve the issue in the file %s:%d", file, line) | ||
} | ||
// Lookup the test.parallel flag's value, and cap it to MaxTestParallelism. This | ||
// all happens before `flag.Parse()`, so any user-provided value will overwrite | ||
// whatever we set here. | ||
par := flag.CommandLine.Lookup("test.parallel") | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if par == nil { | ||
// This should never happen. If you are reading this message because of a panic, | ||
// just comment out the panic and add a `return` statement instead. | ||
msg := "no 'test.parallel' flag found, unable to set default parallelism" | ||
panic(msg + "\n" + info) | ||
} | ||
parValue, err := strconv.ParseInt(par.Value.String(), 0, 64) | ||
if err != nil { | ||
// This should never happen, but if it does, panic with a useful message. If you | ||
// are reading this message because of a panic, that means the default value for | ||
// -test.parallel is not an integer. A safe fix is to comment out the panic. This | ||
// will assume the default value of '0', and replace it with MaxTestParallelism. | ||
// Which is not ideal, but at least tests will run. | ||
msg := fmt.Sprintf("failed to parse test.parallel: %v", err) | ||
panic(msg + "\n" + info) | ||
} | ||
if parValue > MaxTestParallelism { | ||
_ = par.Value.Set(fmt.Sprintf("%d", MaxTestParallelism)) | ||
} | ||
} |
Uh oh!
There was an error while loading.Please reload this page.