...
1
2
3
4
5
6
7
8 package main
9
10
27 import "C"
28
29 import (
30 "fmt"
31 "runtime"
32 "strings"
33 )
34
35 var baseGoroutines int
36
37 func init() {
38 register("NumGoroutine", NumGoroutine)
39 }
40
41 func NumGoroutine() {
42
43
44
45 if _, ok := checkNumGoroutine("first", 1+baseGoroutines); !ok {
46 return
47 }
48
49
50 if C.CheckNumGoroutine(); !callbackok {
51 return
52 }
53
54
55 if _, ok := checkNumGoroutine("third", 1+baseGoroutines); !ok {
56 return
57 }
58
59 fmt.Println("OK")
60 }
61
62 func checkNumGoroutine(label string, want int) (string, bool) {
63 n := runtime.NumGoroutine()
64 if n != want {
65 fmt.Printf("%s NumGoroutine: want %d; got %d\n", label, want, n)
66 return "", false
67 }
68
69 sbuf := make([]byte, 32<<10)
70 sbuf = sbuf[:runtime.Stack(sbuf, true)]
71 n = strings.Count(string(sbuf), "goroutine ")
72 if n != want {
73 fmt.Printf("%s Stack: want %d; got %d:\n%s\n", label, want, n, sbuf)
74 return "", false
75 }
76 return string(sbuf), true
77 }
78
79 var callbackok bool
80
81
82 func CallbackNumGoroutine() {
83 stk, ok := checkNumGoroutine("second", 2+baseGoroutines)
84 if !ok {
85 return
86 }
87 if !strings.Contains(stk, "CallbackNumGoroutine") {
88 fmt.Printf("missing CallbackNumGoroutine from stack:\n%s\n", stk)
89 return
90 }
91
92 callbackok = true
93 }
94
View as plain text