Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3df9d8e

Browse files
authored
test: set test flags from within an init to limit maximum test parallelism (#19575)
1 parent356604e commit3df9d8e

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

‎coderd/coderdtest/initflags.go‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package coderdtest
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"runtime"
7+
"strconv"
8+
"testing"
9+
)
10+
11+
const (
12+
// MaxTestParallelism is set to match our MakeFile's `make test` target.
13+
MaxTestParallelism=8
14+
)
15+
16+
// init defines the default parallelism for tests, capping it to MaxTestParallelism.
17+
// Any user-provided value for -test.parallel will override this.
18+
funcinit() {
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+
ifok {
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+
ifpar==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+
iferr!=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+
ifparValue>MaxTestParallelism {
53+
_=par.Value.Set(fmt.Sprintf("%d",MaxTestParallelism))
54+
}
55+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp