mirror of
https://github.com/golang/go
synced 2024-11-23 16:40:03 -07:00
os: embed "sleep 1" within the test binary itself.
This is an alternative to http://golang.org/cl/4150, and is motivated by a review comment on that CL. testKillProcess() tries to build and run the Go equivalent for "sleep 1". This doesn't work for testing cross compilers since the Go compiler is not available on the targets. This change embeds the "sleep 1" functionality within the "os.test" binary itself. Change-Id: I6bad513deaa6c9e2704e70319098eb4983f1bb23 Reviewed-on: https://go-review.googlesource.com/4190 Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
6d06d4862d
commit
afac4f0a40
@ -21,7 +21,6 @@ import (
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -1328,39 +1327,9 @@ func testKillProcess(t *testing.T, processKiller func(p *Process)) {
|
||||
t.Skipf("skipping on %s", runtime.GOOS)
|
||||
}
|
||||
|
||||
dir, err := ioutil.TempDir("", "go-build")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp directory: %v", err)
|
||||
}
|
||||
defer RemoveAll(dir)
|
||||
|
||||
src := filepath.Join(dir, "main.go")
|
||||
f, err := Create(src)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create %v: %v", src, err)
|
||||
}
|
||||
st := template.Must(template.New("source").Parse(`
|
||||
package main
|
||||
import "time"
|
||||
func main() {
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
`))
|
||||
err = st.Execute(f, nil)
|
||||
if err != nil {
|
||||
f.Close()
|
||||
t.Fatalf("Failed to execute template: %v", err)
|
||||
}
|
||||
f.Close()
|
||||
|
||||
exe := filepath.Join(dir, "main.exe")
|
||||
output, err := osexec.Command("go", "build", "-o", exe, src).CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to build exe %v: %v %v", exe, err, string(output))
|
||||
}
|
||||
|
||||
cmd := osexec.Command(exe)
|
||||
err = cmd.Start()
|
||||
// Re-exec the test binary itself to emulate "sleep 1".
|
||||
cmd := osexec.Command(Args[0], "-test.run", "TestSleep")
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to start test process: %v", err)
|
||||
}
|
||||
@ -1374,6 +1343,15 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
// TestSleep emulates "sleep 1". It is a helper for testKillProcess, so we
|
||||
// don't have to rely on an external "sleep" command being available.
|
||||
func TestSleep(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping in short mode")
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
func TestKillStartProcess(t *testing.T) {
|
||||
testKillProcess(t, func(p *Process) {
|
||||
err := p.Kill()
|
||||
|
Loading…
Reference in New Issue
Block a user