annotate libgo/misc/cgo/test/issue18146.go @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Copyright 2016 The Go Authors. All rights reserved.
kono
parents:
diff changeset
2 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
3 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 // +build !windows
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 // Issue 18146: pthread_create failure during syscall.Exec.
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 package cgotest
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 import "C"
kono
parents:
diff changeset
12
kono
parents:
diff changeset
13 import (
kono
parents:
diff changeset
14 "bytes"
kono
parents:
diff changeset
15 "crypto/md5"
kono
parents:
diff changeset
16 "os"
kono
parents:
diff changeset
17 "os/exec"
kono
parents:
diff changeset
18 "runtime"
kono
parents:
diff changeset
19 "syscall"
kono
parents:
diff changeset
20 "testing"
kono
parents:
diff changeset
21 "time"
kono
parents:
diff changeset
22 )
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 func test18146(t *testing.T) {
kono
parents:
diff changeset
25 if runtime.GOOS == "darwin" {
kono
parents:
diff changeset
26 t.Skipf("skipping flaky test on %s; see golang.org/issue/18202", runtime.GOOS)
kono
parents:
diff changeset
27 }
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 if runtime.GOARCH == "mips" || runtime.GOARCH == "mips64" {
kono
parents:
diff changeset
30 t.Skipf("skipping on %s", runtime.GOARCH)
kono
parents:
diff changeset
31 }
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 attempts := 1000
kono
parents:
diff changeset
34 threads := 4
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 if testing.Short() {
kono
parents:
diff changeset
37 attempts = 100
kono
parents:
diff changeset
38 }
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 // Restrict the number of attempts based on RLIMIT_NPROC.
kono
parents:
diff changeset
41 // Tediously, RLIMIT_NPROC was left out of the syscall package,
kono
parents:
diff changeset
42 // probably because it is not in POSIX.1, so we define it here.
kono
parents:
diff changeset
43 // It is not defined on Solaris.
kono
parents:
diff changeset
44 var nproc int
kono
parents:
diff changeset
45 setNproc := true
kono
parents:
diff changeset
46 switch runtime.GOOS {
kono
parents:
diff changeset
47 default:
kono
parents:
diff changeset
48 setNproc = false
kono
parents:
diff changeset
49 case "linux":
kono
parents:
diff changeset
50 nproc = 6
kono
parents:
diff changeset
51 case "darwin", "dragonfly", "freebsd", "netbsd", "openbsd":
kono
parents:
diff changeset
52 nproc = 7
kono
parents:
diff changeset
53 case "aix":
kono
parents:
diff changeset
54 nproc = 9
kono
parents:
diff changeset
55 }
kono
parents:
diff changeset
56 if setNproc {
kono
parents:
diff changeset
57 var rlim syscall.Rlimit
kono
parents:
diff changeset
58 if syscall.Getrlimit(nproc, &rlim) == nil {
kono
parents:
diff changeset
59 max := int(rlim.Cur) / (threads + 5)
kono
parents:
diff changeset
60 if attempts > max {
kono
parents:
diff changeset
61 t.Logf("lowering attempts from %d to %d for RLIMIT_NPROC", attempts, max)
kono
parents:
diff changeset
62 attempts = max
kono
parents:
diff changeset
63 }
kono
parents:
diff changeset
64 }
kono
parents:
diff changeset
65 }
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 if os.Getenv("test18146") == "exec" {
kono
parents:
diff changeset
68 runtime.GOMAXPROCS(1)
kono
parents:
diff changeset
69 for n := threads; n > 0; n-- {
kono
parents:
diff changeset
70 go func() {
kono
parents:
diff changeset
71 for {
kono
parents:
diff changeset
72 _ = md5.Sum([]byte("Hello, !"))
kono
parents:
diff changeset
73 }
kono
parents:
diff changeset
74 }()
kono
parents:
diff changeset
75 }
kono
parents:
diff changeset
76 runtime.GOMAXPROCS(threads)
kono
parents:
diff changeset
77 argv := append(os.Args, "-test.run=NoSuchTestExists")
kono
parents:
diff changeset
78 if err := syscall.Exec(os.Args[0], argv, os.Environ()); err != nil {
kono
parents:
diff changeset
79 t.Fatal(err)
kono
parents:
diff changeset
80 }
kono
parents:
diff changeset
81 }
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 var cmds []*exec.Cmd
kono
parents:
diff changeset
84 defer func() {
kono
parents:
diff changeset
85 for _, cmd := range cmds {
kono
parents:
diff changeset
86 cmd.Process.Kill()
kono
parents:
diff changeset
87 }
kono
parents:
diff changeset
88 }()
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 args := append(append([]string(nil), os.Args[1:]...), "-test.run=Test18146")
kono
parents:
diff changeset
91 for n := attempts; n > 0; n-- {
kono
parents:
diff changeset
92 cmd := exec.Command(os.Args[0], args...)
kono
parents:
diff changeset
93 cmd.Env = append(os.Environ(), "test18146=exec")
kono
parents:
diff changeset
94 buf := bytes.NewBuffer(nil)
kono
parents:
diff changeset
95 cmd.Stdout = buf
kono
parents:
diff changeset
96 cmd.Stderr = buf
kono
parents:
diff changeset
97 if err := cmd.Start(); err != nil {
kono
parents:
diff changeset
98 // We are starting so many processes that on
kono
parents:
diff changeset
99 // some systems (problem seen on Darwin,
kono
parents:
diff changeset
100 // Dragonfly, OpenBSD) the fork call will fail
kono
parents:
diff changeset
101 // with EAGAIN.
kono
parents:
diff changeset
102 if pe, ok := err.(*os.PathError); ok {
kono
parents:
diff changeset
103 err = pe.Err
kono
parents:
diff changeset
104 }
kono
parents:
diff changeset
105 if se, ok := err.(syscall.Errno); ok && (se == syscall.EAGAIN || se == syscall.EMFILE) {
kono
parents:
diff changeset
106 time.Sleep(time.Millisecond)
kono
parents:
diff changeset
107 continue
kono
parents:
diff changeset
108 }
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 t.Error(err)
kono
parents:
diff changeset
111 return
kono
parents:
diff changeset
112 }
kono
parents:
diff changeset
113 cmds = append(cmds, cmd)
kono
parents:
diff changeset
114 }
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 failures := 0
kono
parents:
diff changeset
117 for _, cmd := range cmds {
kono
parents:
diff changeset
118 err := cmd.Wait()
kono
parents:
diff changeset
119 if err == nil {
kono
parents:
diff changeset
120 continue
kono
parents:
diff changeset
121 }
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 t.Errorf("syscall.Exec failed: %v\n%s", err, cmd.Stdout)
kono
parents:
diff changeset
124 failures++
kono
parents:
diff changeset
125 }
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 if failures > 0 {
kono
parents:
diff changeset
128 t.Logf("Failed %v of %v attempts.", failures, len(cmds))
kono
parents:
diff changeset
129 }
kono
parents:
diff changeset
130 }