diff --git a/tools/check/ut.go b/tools/check/ut.go index 6a3882a233a57..4e8c47a70bb5c 100644 --- a/tools/check/ut.go +++ b/tools/check/ut.go @@ -31,7 +31,7 @@ import ( _ "go.uber.org/automaxprocs" ) -func usage() { +func usage() bool { msg := `// run all tests ut @@ -59,6 +59,7 @@ ut build // build a test package ut build xxx` fmt.Println(msg) + return true } const modulePath = "github.com/pingcap/tidb" @@ -72,11 +73,11 @@ type task struct { var P int var workDir string -func cmdList(args ...string) { +func cmdList(args ...string) bool { pkgs, err := listPackages() if err != nil { fmt.Println("list package error", err) - return + return false } // list all packages @@ -84,7 +85,7 @@ func cmdList(args ...string) { for _, pkg := range pkgs { fmt.Println(pkg) } - return + return false } // list test case of a single package @@ -93,40 +94,41 @@ func cmdList(args ...string) { pkgs = filter(pkgs, func(s string) bool { return s == pkg }) if len(pkgs) != 1 { fmt.Println("package not exist", pkg) - return + return false } err := buildTestBinary(pkg) if err != nil { fmt.Println("build package error", pkg, err) - return + return false } exist, err := testBinaryExist(pkg) if err != nil { fmt.Println("check test binary existance error", err) - return + return false } if !exist { fmt.Println("no test case in ", pkg) - return + return false } res, err := listTestCases(pkg, nil) if err != nil { fmt.Println("list test cases for package error", err) - return + return false } for _, x := range res { fmt.Println(x.test) } } + return true } -func cmdBuild(args ...string) { +func cmdBuild(args ...string) bool { pkgs, err := listPackages() if err != nil { fmt.Println("list package error", err) - return + return false } // build all packages @@ -135,10 +137,10 @@ func cmdBuild(args ...string) { err := buildTestBinary(pkg) if err != nil { fmt.Println("build package error", pkg, err) - return + return false } } - return + return true } // build test binary of a single package @@ -147,17 +149,18 @@ func cmdBuild(args ...string) { err := buildTestBinary(pkg) if err != nil { fmt.Println("build package error", pkg, err) - return + return false } } + return true } -func cmdRun(args ...string) { +func cmdRun(args ...string) bool { var err error pkgs, err := listPackages() if err != nil { fmt.Println("list packages error", err) - return + return false } tasks := make([]task, 0, 5000) // run all tests @@ -167,13 +170,13 @@ func cmdRun(args ...string) { err := buildTestBinary(pkg) if err != nil { fmt.Println("build package error", pkg, err) - return + return false } exist, err := testBinaryExist(pkg) if err != nil { fmt.Println("check test binary existance error", err) - return + return false } if !exist { fmt.Println("no test case in ", pkg) @@ -183,7 +186,7 @@ func cmdRun(args ...string) { tasks, err = listTestCases(pkg, tasks) if err != nil { fmt.Println("list test cases error", err) - return + return false } } } @@ -194,22 +197,22 @@ func cmdRun(args ...string) { err := buildTestBinary(pkg) if err != nil { fmt.Println("build package error", pkg, err) - return + return false } exist, err := testBinaryExist(pkg) if err != nil { fmt.Println("check test binary existance error", err) - return + return false } if !exist { fmt.Println("no test case in ", pkg) - return + return false } tasks, err = listTestCases(pkg, tasks) if err != nil { fmt.Println("list test cases error", err) - return + return false } } @@ -219,22 +222,22 @@ func cmdRun(args ...string) { err := buildTestBinary(pkg) if err != nil { fmt.Println("build package error", pkg, err) - return + return false } exist, err := testBinaryExist(pkg) if err != nil { fmt.Println("check test binary existance error", err) - return + return false } if !exist { fmt.Println("no test case in ", pkg) - return + return false } tasks, err = listTestCases(pkg, tasks) if err != nil { fmt.Println("list test cases error", err) - return + return false } // filter the test case to run tmp := tasks[:0] @@ -249,11 +252,12 @@ func cmdRun(args ...string) { numactl := numactlExist() taskCh := make(chan task, 100) + works := make([]numa, P) var wg sync.WaitGroup for i := 0; i < P; i++ { - n := numa{fmt.Sprintf("%d", i), numactl} + works[i] = numa{fmt.Sprintf("%d", i), numactl, false} wg.Add(1) - go n.worker(&wg, taskCh) + go works[i].worker(&wg, taskCh) } shuffle(tasks) @@ -262,6 +266,12 @@ func cmdRun(args ...string) { } close(taskCh) wg.Wait() + for _, work := range works { + if work.Fail { + return false + } + } + return true } func main() { @@ -281,15 +291,19 @@ func main() { } if len(os.Args) >= 2 { + var isSucceed bool switch os.Args[1] { case "list": - cmdList(os.Args[2:]...) + isSucceed = cmdList(os.Args[2:]...) case "build": - cmdBuild(os.Args[2:]...) + isSucceed = cmdBuild(os.Args[2:]...) case "run": - cmdRun(os.Args[2:]...) + isSucceed = cmdRun(os.Args[2:]...) default: - usage() + isSucceed = usage() + } + if !isSucceed { + os.Exit(1) } } } @@ -339,6 +353,7 @@ func listPackages() ([]string, error) { type numa struct { cpu string numactl bool + Fail bool } func (n *numa) worker(wg *sync.WaitGroup, ch chan task) { @@ -349,6 +364,7 @@ func (n *numa) worker(wg *sync.WaitGroup, ch chan task) { if res.err != nil { fmt.Println("[FAIL] ", t.pkg, t.test, t.old, time.Since(start), res.err) io.Copy(os.Stderr, &res.output) + n.Fail = true } } } @@ -426,8 +442,11 @@ func buildTestBinary(pkg string) error { cmd.Dir = path.Join(workDir, pkg) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - err := cmd.Run() - return withTrace(err) + if err := cmd.Run(); err != nil { + return withTrace(err) + } + return nil + } func testBinaryExist(pkg string) (bool, error) {