File: stacktrace_test.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (151 lines) | stat: -rw-r--r-- 4,055 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package internal

import (
	"bytes"
	"encoding/json"
	"strings"
	"testing"

	"github.com/newrelic/go-agent/internal/stacktracetest"
)

func TestGetStackTrace(t *testing.T) {
	stack := GetStackTrace()
	js, err := json.Marshal(stack)
	if nil != err {
		t.Fatal(err)
	}
	if nil == js {
		t.Fatal(string(js))
	}
}

func TestLongStackTraceLimitsFrames(t *testing.T) {
	st := stacktracetest.CountedCall(maxStackTraceFrames+20, func() []uintptr {
		return GetStackTrace()
	})
	if len(st) != maxStackTraceFrames {
		t.Error("Unexpected size of stacktrace", maxStackTraceFrames, len(st))
	}
	l := len(StackTrace(st).frames())
	if l != maxStackTraceFrames {
		t.Error("Unexpected number of frames", maxStackTraceFrames, l)
	}
}

func TestManyStackTraceFramesLimitsOutput(t *testing.T) {
	frames := make([]stacktraceFrame, maxStackTraceFrames+20)
	expect := `[
	{},{},{},{},{},{},{},{},{},{},
	{},{},{},{},{},{},{},{},{},{},
	{},{},{},{},{},{},{},{},{},{},	
	{},{},{},{},{},{},{},{},{},{},	
	{},{},{},{},{},{},{},{},{},{},	
	{},{},{},{},{},{},{},{},{},{},	
	{},{},{},{},{},{},{},{},{},{},	
	{},{},{},{},{},{},{},{},{},{},	
	{},{},{},{},{},{},{},{},{},{},	
	{},{},{},{},{},{},{},{},{},{}	
	]`
	estimate := 256 * len(frames)
	output := bytes.NewBuffer(make([]byte, 0, estimate))
	writeFrames(output, frames)
	if CompactJSONString(expect) != output.String() {
		t.Error("Unexpected JSON output", CompactJSONString(expect), output.String())
	}
}

func TestStacktraceFrames(t *testing.T) {
	// This stacktrace taken from Go 1.11
	inputFrames := []stacktraceFrame{
		{
			File: "/Users/will/Desktop/gopath/src/github.com/newrelic/go-agent/internal/stacktrace.go",
			Name: "github.com/newrelic/go-agent/internal.GetStackTrace",
			Line: 17,
		},
		{
			File: "/Users/will/Desktop/gopath/src/github.com/newrelic/go-agent/internal_txn.go",
			Name: "github.com/newrelic/go-agent.(*txn).NoticeError",
			Line: 696,
		},
		{
			File: "\u003cautogenerated\u003e",
			Name: "go.(*struct { github.com/newrelic/go-agent.threadWithExtras }).NoticeError",
			Line: 1,
		},
		{
			File: "/Users/will/Desktop/gopath/src/github.com/newrelic/go-agent/internal_attributes_test.go",
			Name: "github.com/newrelic/go-agent.TestAddAttributeSecurityPolicyDisablesInclude",
			Line: 68,
		},
		{
			File: "/Users/will/.gvm/gos/go1.11/src/testing/testing.go",
			Name: "testing.tRunner",
			Line: 827,
		},
		{
			File: "/Users/will/.gvm/gos/go1.11/src/runtime/asm_amd64.s",
			Name: "runtime.goexit",
			Line: 1333,
		},
	}
	buf := &bytes.Buffer{}
	writeFrames(buf, inputFrames)
	expectedJSON := `[
		{
			"name":"testing.tRunner",
			"filepath":"/Users/will/.gvm/gos/go1.11/src/testing/testing.go",
			"line":827
		},
		{
			"name":"runtime.goexit",
			"filepath":"/Users/will/.gvm/gos/go1.11/src/runtime/asm_amd64.s",
			"line":1333
		}
	]`
	testExpectedJSON(t, expectedJSON, buf.String())
}

func TestStackTraceTopFrame(t *testing.T) {
	// This test uses a separate package since the stacktrace code removes
	// the top stack frames which are in packages "newrelic" and "internal".
	stackJSON := stacktracetest.TopStackFrame(func() []byte {
		st := GetStackTrace()
		js, _ := json.Marshal(st)
		return js
	})

	stack := []struct {
		Name     string `json:"name"`
		FilePath string `json:"filepath"`
		Line     int    `json:"line"`
	}{}
	if err := json.Unmarshal(stackJSON, &stack); err != nil {
		t.Fatal(err)
	}
	if len(stack) < 2 {
		t.Fatal(string(stackJSON))
	}
	if stack[0].Name != "stacktracetest.TopStackFrame" {
		t.Error(string(stackJSON))
	}
	if stack[0].Line != 9 {
		t.Error(string(stackJSON))
	}
	if !strings.Contains(stack[0].FilePath, "go-agent/internal/stacktracetest/stacktracetest.go") {
		t.Error(string(stackJSON))
	}
}

func TestFramesCount(t *testing.T) {
	st := stacktracetest.CountedCall(3, func() []uintptr {
		return GetStackTrace()
	})
	frames := StackTrace(st).frames()
	if len(st) != len(frames) {
		t.Error("Invalid # of frames", len(st), len(frames))
	}
}