github.com/aloncn/graphics-go@v0.0.1/src/runtime/crash_cgo_test.go (about)

     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.
     4  
     5  // +build cgo
     6  
     7  package runtime_test
     8  
     9  import (
    10  	"fmt"
    11  	"internal/testenv"
    12  	"os/exec"
    13  	"runtime"
    14  	"strings"
    15  	"testing"
    16  	"time"
    17  )
    18  
    19  func TestCgoCrashHandler(t *testing.T) {
    20  	testCrashHandler(t, true)
    21  }
    22  
    23  func TestCgoSignalDeadlock(t *testing.T) {
    24  	if testing.Short() && runtime.GOOS == "windows" {
    25  		t.Skip("Skipping in short mode") // takes up to 64 seconds
    26  	}
    27  	got := runTestProg(t, "testprogcgo", "CgoSignalDeadlock")
    28  	want := "OK\n"
    29  	if got != want {
    30  		t.Fatalf("expected %q, but got:\n%s", want, got)
    31  	}
    32  }
    33  
    34  func TestCgoTraceback(t *testing.T) {
    35  	got := runTestProg(t, "testprogcgo", "CgoTraceback")
    36  	want := "OK\n"
    37  	if got != want {
    38  		t.Fatalf("expected %q, but got:\n%s", want, got)
    39  	}
    40  }
    41  
    42  func TestCgoCallbackGC(t *testing.T) {
    43  	if runtime.GOOS == "plan9" || runtime.GOOS == "windows" {
    44  		t.Skipf("no pthreads on %s", runtime.GOOS)
    45  	}
    46  	if testing.Short() {
    47  		switch {
    48  		case runtime.GOOS == "dragonfly":
    49  			t.Skip("see golang.org/issue/11990")
    50  		case runtime.GOOS == "linux" && runtime.GOARCH == "arm":
    51  			t.Skip("too slow for arm builders")
    52  		}
    53  	}
    54  	got := runTestProg(t, "testprogcgo", "CgoCallbackGC")
    55  	want := "OK\n"
    56  	if got != want {
    57  		t.Fatalf("expected %q, but got:\n%s", want, got)
    58  	}
    59  }
    60  
    61  func TestCgoExternalThreadPanic(t *testing.T) {
    62  	if runtime.GOOS == "plan9" {
    63  		t.Skipf("no pthreads on %s", runtime.GOOS)
    64  	}
    65  	got := runTestProg(t, "testprogcgo", "CgoExternalThreadPanic")
    66  	want := "panic: BOOM"
    67  	if !strings.Contains(got, want) {
    68  		t.Fatalf("want failure containing %q. output:\n%s\n", want, got)
    69  	}
    70  }
    71  
    72  func TestCgoExternalThreadSIGPROF(t *testing.T) {
    73  	// issue 9456.
    74  	switch runtime.GOOS {
    75  	case "plan9", "windows":
    76  		t.Skipf("no pthreads on %s", runtime.GOOS)
    77  	case "darwin":
    78  		if runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" {
    79  			// static constructor needs external linking, but we don't support
    80  			// external linking on OS X 10.6.
    81  			out, err := exec.Command("uname", "-r").Output()
    82  			if err != nil {
    83  				t.Fatalf("uname -r failed: %v", err)
    84  			}
    85  			// OS X 10.6 == Darwin 10.x
    86  			if strings.HasPrefix(string(out), "10.") {
    87  				t.Skipf("no external linking on OS X 10.6")
    88  			}
    89  		}
    90  	}
    91  	if runtime.GOARCH == "ppc64" {
    92  		// TODO(austin) External linking not implemented on
    93  		// ppc64 (issue #8912)
    94  		t.Skipf("no external linking on ppc64")
    95  	}
    96  	got := runTestProg(t, "testprogcgo", "CgoExternalThreadSIGPROF")
    97  	want := "OK\n"
    98  	if got != want {
    99  		t.Fatalf("expected %q, but got:\n%s", want, got)
   100  	}
   101  }
   102  
   103  func TestCgoExternalThreadSignal(t *testing.T) {
   104  	// issue 10139
   105  	switch runtime.GOOS {
   106  	case "plan9", "windows":
   107  		t.Skipf("no pthreads on %s", runtime.GOOS)
   108  	}
   109  	got := runTestProg(t, "testprogcgo", "CgoExternalThreadSignal")
   110  	want := "OK\n"
   111  	if got != want {
   112  		t.Fatalf("expected %q, but got:\n%s", want, got)
   113  	}
   114  }
   115  
   116  func TestCgoDLLImports(t *testing.T) {
   117  	// test issue 9356
   118  	if runtime.GOOS != "windows" {
   119  		t.Skip("skipping windows specific test")
   120  	}
   121  	got := runTestProg(t, "testprogcgo", "CgoDLLImportsMain")
   122  	want := "OK\n"
   123  	if got != want {
   124  		t.Fatalf("expected %q, but got %v", want, got)
   125  	}
   126  }
   127  
   128  func TestCgoExecSignalMask(t *testing.T) {
   129  	// Test issue 13164.
   130  	switch runtime.GOOS {
   131  	case "windows", "plan9":
   132  		t.Skipf("skipping signal mask test on %s", runtime.GOOS)
   133  	}
   134  	got := runTestProg(t, "testprogcgo", "CgoExecSignalMask")
   135  	want := "OK\n"
   136  	if got != want {
   137  		t.Errorf("expected %q, got %v", want, got)
   138  	}
   139  }
   140  
   141  func TestEnsureDropM(t *testing.T) {
   142  	// Test for issue 13881.
   143  	switch runtime.GOOS {
   144  	case "windows", "plan9":
   145  		t.Skipf("skipping dropm test on %s", runtime.GOOS)
   146  	}
   147  	got := runTestProg(t, "testprogcgo", "EnsureDropM")
   148  	want := "OK\n"
   149  	if got != want {
   150  		t.Errorf("expected %q, got %v", want, got)
   151  	}
   152  }
   153  
   154  // Test for issue 14387.
   155  // Test that the program that doesn't need any cgo pointer checking
   156  // takes about the same amount of time with it as without it.
   157  func TestCgoCheckBytes(t *testing.T) {
   158  	// Make sure we don't count the build time as part of the run time.
   159  	testenv.MustHaveGoBuild(t)
   160  	exe, err := buildTestProg(t, "testprogcgo")
   161  	if err != nil {
   162  		t.Fatal(err)
   163  	}
   164  
   165  	// Try it 10 times to avoid flakiness.
   166  	const tries = 10
   167  	var tot1, tot2 time.Duration
   168  	for i := 0; i < tries; i++ {
   169  		cmd := testEnv(exec.Command(exe, "CgoCheckBytes"))
   170  		cmd.Env = append(cmd.Env, "GODEBUG=cgocheck=0", fmt.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i))
   171  
   172  		start := time.Now()
   173  		cmd.Run()
   174  		d1 := time.Since(start)
   175  
   176  		cmd = testEnv(exec.Command(exe, "CgoCheckBytes"))
   177  		cmd.Env = append(cmd.Env, fmt.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i))
   178  
   179  		start = time.Now()
   180  		cmd.Run()
   181  		d2 := time.Since(start)
   182  
   183  		if d1*20 > d2 {
   184  			// The slow version (d2) was less than 20 times
   185  			// slower than the fast version (d1), so OK.
   186  			return
   187  		}
   188  
   189  		tot1 += d1
   190  		tot2 += d2
   191  	}
   192  
   193  	t.Errorf("cgo check too slow: got %v, expected at most %v", tot2/tries, (tot1/tries)*20)
   194  }
   195  
   196  func TestCgoCCodeSIGPROF(t *testing.T) {
   197  	got := runTestProg(t, "testprogcgo", "CgoCCodeSIGPROF")
   198  	want := "OK\n"
   199  	if got != want {
   200  		t.Errorf("expected %q got %v", want, got)
   201  	}
   202  }