tlog.app/go/loc@v0.6.2-0.20231112073106-b6382a0ac518/runtime_test.go (about) 1 package loc 2 3 import ( 4 "path/filepath" 5 "runtime" 6 "strconv" 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestRuntimeCaller(t *testing.T) { 15 rpc, rfile, rline, ok := runtime.Caller(0) 16 17 pc := Caller(0) 18 19 f := runtime.FuncForPC(rpc) 20 rname := f.Name() 21 name, file, line := pc.NameFileLine() 22 23 require.True(t, ok) 24 25 assert.Equal(t, rname, name) 26 assert.True(t, strings.HasSuffix(rfile, file)) 27 assert.Equal(t, rline, line-2) 28 29 assert.Equal(t, f.Entry(), uintptr(pc.FuncEntry())) 30 31 assert.Equal(t, f.Entry(), uintptr(FuncEntryFromFunc(TestRuntimeCaller))) 32 } 33 34 func TestRuntimeCallers(t *testing.T) { 35 var rpcs [2]uintptr 36 n := runtime.Callers(1, rpcs[:]) 37 38 pcs := Callers(0, 2) 39 40 var rsum string 41 42 frames := runtime.CallersFrames(rpcs[:n]) 43 i := 0 44 for { 45 fr, ok := frames.Next() 46 47 name, file, line := pcs[i].NameFileLine() 48 49 rline := fr.Line 50 51 if i != 0 { 52 rsum += " at " 53 } else { 54 rline += 2 // we called them from different lines 55 } 56 57 rsum += filepath.Base(fr.File) + ":" + strconv.Itoa(rline) 58 59 assert.Equal(t, fr.Function, name) 60 assert.True(t, strings.HasSuffix(fr.File, file)) 61 assert.Equal(t, rline, line) 62 63 i++ 64 if !ok { 65 break 66 } 67 } 68 69 if t.Failed() { 70 t.Logf("callers: %v", pcs) 71 t.Logf("runtime: %v", rsum) 72 } 73 } 74 75 func BenchmarkRuntimeCallerNameFileLine(b *testing.B) { 76 b.ReportAllocs() 77 78 var pc uintptr 79 var ok bool 80 for i := 0; i < b.N; i++ { 81 pc, _, _, ok = runtime.Caller(0) 82 83 f := runtime.FuncForPC(pc) 84 85 _ = f.Name() 86 } 87 88 if !ok { 89 b.Errorf("not ok") 90 } 91 } 92 93 func BenchmarkRuntimeCallerFileLine(b *testing.B) { 94 b.ReportAllocs() 95 96 var ok bool 97 for i := 0; i < b.N; i++ { 98 _, _, _, ok = runtime.Caller(0) 99 } 100 101 if !ok { 102 b.Errorf("not ok") 103 } 104 } 105 106 func gover() string { 107 return runtime.Version() 108 }