github.com/searKing/golang/go@v1.2.117/runtime/extern_test.go (about) 1 package runtime_test 2 3 import ( 4 "fmt" 5 "regexp" 6 "strings" 7 "testing" 8 9 "github.com/searKing/golang/go/runtime" 10 ) 11 12 func TestGetCallerFrame(t *testing.T) { 13 // Example: 14 // github.com/searKing/golang/go/runtime_test.TestGetCallerFrame 15 frame := runtime.GetCallerFrame(1) 16 if match, _ := regexp.MatchString(`TestGetCaller(.*)`, frame.Function); !match { 17 t.Errorf("mismatch symbolized function name: %s", frame.Function) 18 } 19 } 20 21 func TestGetCaller(t *testing.T) { 22 // Example: 23 // github.com/searKing/golang/go/runtime_test.TestGetCaller 24 caller := runtime.GetCaller(1) 25 if match, _ := regexp.MatchString(`TestGetCaller(.*)`, caller); !match { 26 t.Errorf("mismatch symbolized function name: %s", caller) 27 } 28 } 29 30 func TestGetShortCaller(t *testing.T) { 31 // Example: 32 // TestGetCaller 33 caller := runtime.GetShortCaller(1) 34 if match, _ := regexp.MatchString(`TestGetShortCaller`, caller); !match { 35 t.Errorf("mismatch symbolized function name: %s", caller) 36 } 37 } 38 39 func caller() string { 40 function, file, line := runtime.GetCallerFuncFileLine(1) 41 return fmt.Sprintf("%s() %s:%d", function, file, line) 42 } 43 44 func TestGetCallerFunctionLine(t *testing.T) { 45 // Example: 46 // github.com/searKing/golang/go/runtime_test.caller() /Users/.../workspace/src/github.com/searKing/golang/go/runtime/extern_test.go:29 47 cfl := caller() 48 if match, _ := regexp.MatchString( 49 `.*runtime_test\.caller\(\) .*/extern_test.go:([0-9]+)`, cfl); !match { 50 t.Errorf("mismatch caller's caller file line: %s", cfl) 51 } 52 } 53 54 func shortCaller() string { 55 function, file, line := runtime.GetShortCallerFuncFileLine(1) 56 return fmt.Sprintf("%s() %s:%d", function, file, line) 57 } 58 59 func TestGetShortCallerFuncFileLine(t *testing.T) { 60 // Example: 61 // shortCaller() extern_test.go:44 62 cfl := shortCaller() 63 if match, _ := regexp.MatchString( 64 `shortCaller\(\) extern_test.go:([0-9]+)`, cfl); !match { 65 t.Errorf("mismatch caller's caller file line: %s", cfl) 66 } 67 } 68 69 func TestGetCallStack(t *testing.T) { 70 stk := runtime.GetCallStack(2 << 20) 71 72 // Example log: 73 // 74 // goroutine 19 [running]: 75 // github.com/searKing/golang/go/runtime_test.TestGetCallStack(0xc000082900) 76 // .../src/github.com/searKing/golang/go/runtime/extern_test.go:21 +0x3f 77 // testing.tRunner(0xc000082900, 0x12ffb18) 78 // /usr/local/go/src/testing/testing.go:1123 +0x1a3 79 // created by testing.(*T).Run 80 // /usr/local/go/src/testing/testing.go:1168 +0x648 81 lines := strings.Split(stk, "\n") 82 if len(lines) < 4 { 83 t.Fatalf("panic log should have 1 line of message, 1 line per goroutine and 2 lines per function call") 84 } 85 86 // The following regexp's verify that Kubernetes panic log matches Golang stdlib 87 // stacktrace pattern. We need to update these regexp's if stdlib changes its pattern. 88 if match, _ := regexp.MatchString(`goroutine [0-9]+ \[.+\]:`, lines[0]); !match { 89 t.Errorf("mismatch goroutine: %s", lines[1]) 90 } 91 if match, _ := regexp.MatchString(`TestGetCallStack(.*)`, lines[1]); !match { 92 t.Errorf("mismatch symbolized function name: %s", lines[1]) 93 } 94 if match, _ := regexp.MatchString(`extern_test\.go:[0-9]+ \+0x`, lines[2]); !match { 95 t.Errorf("mismatch file/line/offset information: %s", lines[2]) 96 } 97 if match, _ := regexp.MatchString(`TestGetCallStack(.*)`, stk); !match { 98 t.Errorf("mismatch symbolized function name: %s", stk) 99 } 100 }