1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
21 func TestCgoCrashHandler(t
*testing
.T
) {
23 testCrashHandler(t
, true)
26 func TestCgoSignalDeadlock(t
*testing
.T
) {
27 // Don't call t.Parallel, since too much work going on at the
28 // same time can cause the testprogcgo code to overrun its
29 // timeouts (issue #18598).
31 if testing
.Short() && runtime
.GOOS
== "windows" {
32 t
.Skip("Skipping in short mode") // takes up to 64 seconds
34 got
:= runTestProg(t
, "testprogcgo", "CgoSignalDeadlock")
37 t
.Fatalf("expected %q, but got:\n%s", want
, got
)
41 func TestCgoTraceback(t
*testing
.T
) {
43 got
:= runTestProg(t
, "testprogcgo", "CgoTraceback")
46 t
.Fatalf("expected %q, but got:\n%s", want
, got
)
50 func TestCgoCallbackGC(t
*testing
.T
) {
53 case "plan9", "windows":
54 t
.Skipf("no pthreads on %s", runtime
.GOOS
)
58 case runtime
.GOOS
== "dragonfly":
59 t
.Skip("see golang.org/issue/11990")
60 case runtime
.GOOS
== "linux" && runtime
.GOARCH
== "arm":
61 t
.Skip("too slow for arm builders")
62 case runtime
.GOOS
== "linux" && (runtime
.GOARCH
== "mips64" || runtime
.GOARCH
== "mips64le"):
63 t
.Skip("too slow for mips64x builders")
66 got
:= runTestProg(t
, "testprogcgo", "CgoCallbackGC")
69 t
.Fatalf("expected %q, but got:\n%s", want
, got
)
73 func TestCgoExternalThreadPanic(t
*testing
.T
) {
75 if runtime
.GOOS
== "plan9" {
76 t
.Skipf("no pthreads on %s", runtime
.GOOS
)
78 got
:= runTestProg(t
, "testprogcgo", "CgoExternalThreadPanic")
80 if !strings
.Contains(got
, want
) {
81 t
.Fatalf("want failure containing %q. output:\n%s\n", want
, got
)
85 func TestCgoExternalThreadSIGPROF(t
*testing
.T
) {
89 case "plan9", "windows":
90 t
.Skipf("no pthreads on %s", runtime
.GOOS
)
92 if runtime
.GOARCH
!= "arm" && runtime
.GOARCH
!= "arm64" {
93 // static constructor needs external linking, but we don't support
94 // external linking on OS X 10.6.
95 out
, err
:= exec
.Command("uname", "-r").Output()
97 t
.Fatalf("uname -r failed: %v", err
)
99 // OS X 10.6 == Darwin 10.x
100 if strings
.HasPrefix(string(out
), "10.") {
101 t
.Skipf("no external linking on OS X 10.6")
105 if runtime
.GOARCH
== "ppc64" {
106 // TODO(austin) External linking not implemented on
107 // ppc64 (issue #8912)
108 t
.Skipf("no external linking on ppc64")
111 exe
, err
:= buildTestProg(t
, "testprogcgo", "-tags=threadprof")
116 got
, err
:= testEnv(exec
.Command(exe
, "CgoExternalThreadSIGPROF")).CombinedOutput()
118 t
.Fatalf("exit status: %v\n%s", err
, got
)
121 if want
:= "OK\n"; string(got
) != want
{
122 t
.Fatalf("expected %q, but got:\n%s", want
, got
)
126 func TestCgoExternalThreadSignal(t
*testing
.T
) {
129 switch runtime
.GOOS
{
130 case "plan9", "windows":
131 t
.Skipf("no pthreads on %s", runtime
.GOOS
)
134 exe
, err
:= buildTestProg(t
, "testprogcgo", "-tags=threadprof")
139 got
, err
:= testEnv(exec
.Command(exe
, "CgoExternalThreadSIGPROF")).CombinedOutput()
141 t
.Fatalf("exit status: %v\n%s", err
, got
)
144 want
:= []byte("OK\n")
145 if !bytes
.Equal(got
, want
) {
146 t
.Fatalf("expected %q, but got:\n%s", want
, got
)
150 func TestCgoDLLImports(t
*testing
.T
) {
152 if runtime
.GOOS
!= "windows" {
153 t
.Skip("skipping windows specific test")
155 got
:= runTestProg(t
, "testprogcgo", "CgoDLLImportsMain")
158 t
.Fatalf("expected %q, but got %v", want
, got
)
162 func TestCgoExecSignalMask(t
*testing
.T
) {
165 switch runtime
.GOOS
{
166 case "windows", "plan9":
167 t
.Skipf("skipping signal mask test on %s", runtime
.GOOS
)
169 got
:= runTestProg(t
, "testprogcgo", "CgoExecSignalMask")
172 t
.Errorf("expected %q, got %v", want
, got
)
176 func TestEnsureDropM(t
*testing
.T
) {
178 // Test for issue 13881.
179 switch runtime
.GOOS
{
180 case "windows", "plan9":
181 t
.Skipf("skipping dropm test on %s", runtime
.GOOS
)
183 got
:= runTestProg(t
, "testprogcgo", "EnsureDropM")
186 t
.Errorf("expected %q, got %v", want
, got
)
190 // Test for issue 14387.
191 // Test that the program that doesn't need any cgo pointer checking
192 // takes about the same amount of time with it as without it.
193 func TestCgoCheckBytes(t
*testing
.T
) {
195 // Make sure we don't count the build time as part of the run time.
196 testenv
.MustHaveGoBuild(t
)
197 exe
, err
:= buildTestProg(t
, "testprogcgo")
202 // Try it 10 times to avoid flakiness.
204 var tot1
, tot2 time
.Duration
205 for i
:= 0; i
< tries
; i
++ {
206 cmd
:= testEnv(exec
.Command(exe
, "CgoCheckBytes"))
207 cmd
.Env
= append(cmd
.Env
, "GODEBUG=cgocheck=0", fmt
.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i
))
211 d1
:= time
.Since(start
)
213 cmd
= testEnv(exec
.Command(exe
, "CgoCheckBytes"))
214 cmd
.Env
= append(cmd
.Env
, fmt
.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i
))
218 d2
:= time
.Since(start
)
221 // The slow version (d2) was less than 20 times
222 // slower than the fast version (d1), so OK.
230 t
.Errorf("cgo check too slow: got %v, expected at most %v", tot2
/tries
, (tot1
/tries
)*20)
233 func TestCgoPanicDeadlock(t
*testing
.T
) {
236 got
:= runTestProg(t
, "testprogcgo", "CgoPanicDeadlock")
237 want
:= "panic: cgo error\n\n"
238 if !strings
.HasPrefix(got
, want
) {
239 t
.Fatalf("output does not start with %q:\n%s", want
, got
)
243 func TestCgoCCodeSIGPROF(t
*testing
.T
) {
245 got
:= runTestProg(t
, "testprogcgo", "CgoCCodeSIGPROF")
248 t
.Errorf("expected %q got %v", want
, got
)
252 func TestCgoCrashTraceback(t
*testing
.T
) {
254 if runtime
.GOOS
!= "linux" || runtime
.GOARCH
!= "amd64" {
255 t
.Skipf("not yet supported on %s/%s", runtime
.GOOS
, runtime
.GOARCH
)
257 if runtime
.Compiler
== "gccgo" {
258 t
.Skip("gccgo does not have SetCgoTraceback")
260 got
:= runTestProg(t
, "testprogcgo", "CrashTraceback")
261 for i
:= 1; i
<= 3; i
++ {
262 if !strings
.Contains(got
, fmt
.Sprintf("cgo symbolizer:%d", i
)) {
263 t
.Errorf("missing cgo symbolizer:%d", i
)
268 func TestCgoTracebackContext(t
*testing
.T
) {
270 if runtime
.Compiler
== "gccgo" {
271 t
.Skip("gccgo does not have SetCgoTraceback")
273 got
:= runTestProg(t
, "testprogcgo", "TracebackContext")
276 t
.Errorf("expected %q got %v", want
, got
)
280 func testCgoPprof(t
*testing
.T
, buildArg
, runArg
string) {
282 if runtime
.GOOS
!= "linux" || runtime
.GOARCH
!= "amd64" {
283 t
.Skipf("not yet supported on %s/%s", runtime
.GOOS
, runtime
.GOARCH
)
285 if runtime
.Compiler
== "gccgo" {
286 t
.Skip("gccgo does not have SetCgoTraceback")
288 testenv
.MustHaveGoRun(t
)
290 exe
, err
:= buildTestProg(t
, "testprogcgo", buildArg
)
295 got
, err
:= testEnv(exec
.Command(exe
, runArg
)).CombinedOutput()
297 if testenv
.Builder() == "linux-amd64-alpine" {
298 // See Issue 18243 and Issue 19938.
299 t
.Skipf("Skipping failing test on Alpine (golang.org/issue/18243). Ignoring error: %v", err
)
303 fn
:= strings
.TrimSpace(string(got
))
306 for try
:= 0; try
< 2; try
++ {
307 cmd
:= testEnv(exec
.Command(testenv
.GoToolPath(t
), "tool", "pprof", "-top", "-nodecount=1"))
308 // Check that pprof works both with and without explicit executable on command line.
310 cmd
.Args
= append(cmd
.Args
, exe
, fn
)
312 cmd
.Args
= append(cmd
.Args
, fn
)
316 for i
, e
:= range cmd
.Env
{
317 if strings
.HasPrefix(e
, "PPROF_TMPDIR=") {
318 cmd
.Env
[i
] = "PPROF_TMPDIR=" + os
.TempDir()
324 cmd
.Env
= append(cmd
.Env
, "PPROF_TMPDIR="+os
.TempDir())
327 top
, err
:= cmd
.CombinedOutput()
328 t
.Logf("%s:\n%s", cmd
.Args
, top
)
331 } else if !bytes
.Contains(top
, []byte("cpuHog")) {
332 t
.Error("missing cpuHog in pprof output")
337 func TestCgoPprof(t
*testing
.T
) {
338 testCgoPprof(t
, "", "CgoPprof")
341 func TestCgoPprofPIE(t
*testing
.T
) {
342 testCgoPprof(t
, "-ldflags=-extldflags=-pie", "CgoPprof")
345 func TestCgoPprofThread(t
*testing
.T
) {
346 testCgoPprof(t
, "", "CgoPprofThread")
349 func TestCgoPprofThreadNoTraceback(t
*testing
.T
) {
350 testCgoPprof(t
, "", "CgoPprofThreadNoTraceback")
353 func TestRaceProf(t
*testing
.T
) {
354 if runtime
.GOOS
!= "linux" || runtime
.GOARCH
!= "amd64" {
355 t
.Skipf("not yet supported on %s/%s", runtime
.GOOS
, runtime
.GOARCH
)
357 if runtime
.Compiler
== "gccgo" {
358 t
.Skip("gccgo does not have SetCgoTraceback")
361 testenv
.MustHaveGoRun(t
)
363 // This test requires building various packages with -race, so
364 // it's somewhat slow.
366 t
.Skip("skipping test in -short mode")
369 exe
, err
:= buildTestProg(t
, "testprogcgo", "-race")
374 got
, err
:= testEnv(exec
.Command(exe
, "CgoRaceprof")).CombinedOutput()
379 if string(got
) != want
{
380 t
.Errorf("expected %q got %s", want
, got
)
384 func TestRaceSignal(t
*testing
.T
) {
386 if runtime
.GOOS
!= "linux" || runtime
.GOARCH
!= "amd64" {
387 t
.Skipf("not yet supported on %s/%s", runtime
.GOOS
, runtime
.GOARCH
)
390 testenv
.MustHaveGoRun(t
)
392 // This test requires building various packages with -race, so
393 // it's somewhat slow.
395 t
.Skip("skipping test in -short mode")
398 exe
, err
:= buildTestProg(t
, "testprogcgo", "-race")
403 got
, err
:= testEnv(exec
.Command(exe
, "CgoRaceSignal")).CombinedOutput()
409 if string(got
) != want
{
410 t
.Errorf("expected %q got %s", want
, got
)
414 func TestCgoNumGoroutine(t
*testing
.T
) {
415 switch runtime
.GOOS
{
416 case "windows", "plan9":
417 t
.Skipf("skipping numgoroutine test on %s", runtime
.GOOS
)
420 got
:= runTestProg(t
, "testprogcgo", "NumGoroutine")
423 t
.Errorf("expected %q got %v", want
, got
)