-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallframes_test.go
55 lines (44 loc) · 1.15 KB
/
callframes_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package elk
import (
"fmt"
"runtime"
"testing"
"github.com/studio-b12/elk/internal/assert"
)
func TestFrames(t *testing.T) {
stack := newCallStack(1, 3)
assert.Equal(t, stack.offset, 1)
assert.Equal(t, 3, len(stack.ptrs))
assert.Nil(t, stack.frames)
frames := stack.Frames()
assert.Equal(t, 2, len(frames))
assert.Equal(t, 3, len(stack.frames))
}
// This test checks what happens when the creation of a call
// stack happens in an anonymous function, which is then cleaned
// up by the garbage collector. Afterwards, Frames() is called on
// the CallStack to resolve the runtime.Frame objects from the
// internal slice of stack pointers.
func Test_stackCaptureGC(t *testing.T) {
var cs *CallStack
var cleanedUp bool
{
f := func() {
cs = newCallStack(0, 10)
}
runtime.SetFinalizer(&f, func(a any) {
cleanedUp = true
})
f()
}
// GC gets called twice to first call the registered
// finalizer and then actually clean up the object.
// See the documentation of runtime.SetFinalizer for
// more information.
runtime.GC()
runtime.GC()
assert.True(t, cleanedUp)
for _, frame := range cs.Frames() {
fmt.Printf("frame: %s\n", frame)
}
}