@@ -10,6 +10,7 @@ import (
10
10
"os"
11
11
"os/exec"
12
12
"path/filepath"
13
+ "regexp"
13
14
"runtime/debug"
14
15
"strings"
15
16
"testing"
@@ -21,6 +22,11 @@ import (
21
22
"golang.org/x/telemetry/internal/testenv"
22
23
)
23
24
25
+ //
26
+ // Add/removed lines from this comment to compensate for minor
27
+ // perturbations in line numbers as the code below evolves.
28
+ //
29
+
24
30
func TestMain (m * testing.M ) {
25
31
entry := os .Getenv ("CRASHMONITOR_TEST_ENTRYPOINT" )
26
32
switch entry {
@@ -76,7 +82,7 @@ func childPanic() {
76
82
}
77
83
78
84
func grandchildPanic () {
79
- panic ("oops" ) // this line is "grandchildPanic:=79 " (the call from child is inlined)
85
+ panic ("oops" ) // this line is "grandchildPanic:=85 " (the call from child is inlined)
80
86
}
81
87
82
88
var sinkPtr * int
@@ -87,7 +93,7 @@ func childTrap() {
87
93
}
88
94
89
95
func grandchildTrap (i * int ) {
90
- * i = 42 // this line is "grandchildTrap:=90 " (the call from childTrap is inlined)
96
+ * i = 42 // this line is "grandchildTrap:=96 " (the call from childTrap is inlined)
91
97
}
92
98
93
99
// TestViaStderr is an internal test that asserts that the telemetry
@@ -102,30 +108,16 @@ func TestViaStderr(t *testing.T) {
102
108
t .Fatal (err )
103
109
}
104
110
got = sanitize (counter .DecodeStack (got ))
105
- want := "crash/crash\n " +
106
- "runtime.gopanic:--\n " +
107
- "golang.org/x/telemetry/internal/crashmonitor_test.grandchildPanic:=79\n " +
108
- "golang.org/x/telemetry/internal/crashmonitor_test.childPanic:+2\n " +
109
- "golang.org/x/telemetry/internal/crashmonitor_test.TestMain:+10\n " +
110
- "main.main:--\n " +
111
- "runtime.main:--\n " +
112
- "runtime.goexit:--"
113
-
114
- if ! crashmonitor .Supported () { // !go1.23
115
- // Traceback excludes PCs for inlined frames. Before
116
- // go1.23 (https://go.dev/cl/571798 specifically),
117
- // passing the set of PCs in the traceback to
118
- // runtime.CallersFrames, would report only the
119
- // innermost inlined frame and none of the inline
120
- // "callers".
121
- //
122
- // Thus, here we must drop the caller of the inlined
123
- // frame.
124
- want = strings .ReplaceAll (want , "golang.org/x/telemetry/internal/crashmonitor_test.childPanic:+2\n " , "" )
125
- }
126
-
127
- if got != want {
128
- t .Errorf ("got counter name <<%s>>, want <<%s>>" , got , want )
111
+ wantRE := regexp .MustCompile (`(?m)crash/crash
112
+ runtime.gopanic:--
113
+ golang.org/x/telemetry/internal/crashmonitor_test\.grandchildPanic:=85,\+0x.*
114
+ golang.org/x/telemetry/internal/crashmonitor_test\.childPanic:\+2,\+0x.*
115
+ golang.org/x/telemetry/internal/crashmonitor_test\.TestMain:\+10,\+0x.*
116
+ main.main:--
117
+ runtime.main:--
118
+ runtime.goexit:--` )
119
+ if ! wantRE .MatchString (got ) {
120
+ t .Errorf ("got counter name <<%s>>, want match for <<%s>>" , got , wantRE )
129
121
}
130
122
})
131
123
@@ -137,25 +129,18 @@ func TestViaStderr(t *testing.T) {
137
129
t .Fatal (err )
138
130
}
139
131
got = sanitize (counter .DecodeStack (got ))
140
- want := "crash/crash\n " +
141
- "runtime.gopanic:--\n " +
142
- "runtime.panicmem:--\n " +
143
- "runtime.sigpanic:--\n " +
144
- "golang.org/x/telemetry/internal/crashmonitor_test.grandchildTrap:=90\n " +
145
- "golang.org/x/telemetry/internal/crashmonitor_test.childTrap:+2\n " +
146
- "golang.org/x/telemetry/internal/crashmonitor_test.TestMain:+12\n " +
147
- "main.main:--\n " +
148
- "runtime.main:--\n " +
149
- "runtime.goexit:--"
150
-
151
- if ! crashmonitor .Supported () { // !go1.23
152
- // See above.
153
- want = strings .ReplaceAll (want , "runtime.sigpanic:--\n " , "" )
154
- want = strings .ReplaceAll (want , "golang.org/x/telemetry/internal/crashmonitor_test.childTrap:+2\n " , "" )
155
- }
156
-
157
- if got != want {
158
- t .Errorf ("got counter name <<%s>>, want <<%s>>" , got , want )
132
+ wantRE := regexp .MustCompile (`(?m)crash/crash
133
+ runtime.gopanic:--
134
+ runtime.panicmem:--
135
+ runtime.sigpanic:--
136
+ golang.org/x/telemetry/internal/crashmonitor_test.grandchildTrap:=96,\+0x.*
137
+ golang.org/x/telemetry/internal/crashmonitor_test.childTrap:\+2,\+0x.*
138
+ golang.org/x/telemetry/internal/crashmonitor_test.TestMain:\+10,\+0x.*
139
+ main.main:--
140
+ runtime.main:--
141
+ runtime.goexit:--` )
142
+ if wantRE .MatchString (got ) {
143
+ t .Errorf ("got counter name <<%s>>, want match for <<%s>>" , got , wantRE )
159
144
}
160
145
})
161
146
}
@@ -181,14 +166,9 @@ func waitForExitFile(t *testing.T, exitFile string) {
181
166
}
182
167
183
168
// TestStart is an integration test of the crashmonitor feature of [telemetry.Start].
184
- // Requires go1.23+.
185
169
func TestStart (t * testing.T ) {
186
170
testenv .SkipIfUnsupportedPlatform (t )
187
171
188
- if ! crashmonitor .Supported () {
189
- t .Skip ("crashmonitor not supported" )
190
- }
191
-
192
172
// Assert that the crash monitor does nothing when the child
193
173
// process merely exits.
194
174
t .Run ("exit" , func (t * testing.T ) {
@@ -211,14 +191,14 @@ func TestStart(t *testing.T) {
211
191
t .Fatalf ("failed to read file: %v" , err )
212
192
}
213
193
got := sanitize (counter .DecodeStack (string (data )))
214
- want := " crash/crash\n " +
215
- " runtime.gopanic:--\n " +
216
- " golang.org/x/telemetry/internal/crashmonitor_test.grandchildPanic:=79 \n " +
217
- " golang.org/x/telemetry/internal/crashmonitor_test.childPanic:+2 \n " +
218
- " golang.org/x/telemetry/internal/crashmonitor_test.TestMain.func3:+1 \n " +
219
- " runtime.goexit:--"
220
- if got != want {
221
- t .Errorf ("got counter name <<%s>>, want <<%s>>" , got , want )
194
+ wantRE := regexp . MustCompile ( `(?m) crash/crash
195
+ runtime.gopanic:--
196
+ golang.org/x/telemetry/internal/crashmonitor_test.grandchildPanic:=85,.*
197
+ golang.org/x/telemetry/internal/crashmonitor_test.childPanic:\+2,.*
198
+ golang.org/x/telemetry/internal/crashmonitor_test.TestMain.func3:\+1,.*
199
+ runtime.goexit:--` )
200
+ if ! wantRE . MatchString ( got ) {
201
+ t .Errorf ("got counter name <<%s>>, want match for <<%s>>" , got , wantRE )
222
202
}
223
203
})
224
204
@@ -233,16 +213,16 @@ func TestStart(t *testing.T) {
233
213
t .Fatalf ("failed to read file: %v" , err )
234
214
}
235
215
got := sanitize (counter .DecodeStack (string (data )))
236
- want := " crash/crash\n " +
237
- " runtime.gopanic:--\n " +
238
- " runtime.panicmem:--\n " +
239
- " runtime.sigpanic:--\n " +
240
- " golang.org/x/telemetry/internal/crashmonitor_test.grandchildTrap:=90 \n " +
241
- " golang.org/x/telemetry/internal/crashmonitor_test.childTrap:+2 \n " +
242
- " golang.org/x/telemetry/internal/crashmonitor_test.TestMain.func4:+1 \n " +
243
- " runtime.goexit:--"
244
- if got != want {
245
- t .Errorf ("got counter name <<%s>>, want <<%s>>" , got , want )
216
+ wantRE := regexp . MustCompile ( `(?m) crash/crash
217
+ runtime.gopanic:--
218
+ runtime.panicmem:--
219
+ runtime.sigpanic:--
220
+ golang.org/x/telemetry/internal/crashmonitor_test.grandchildTrap:=96,.*
221
+ golang.org/x/telemetry/internal/crashmonitor_test.childTrap:\+2,.*
222
+ golang.org/x/telemetry/internal/crashmonitor_test.TestMain.func4:\+1,.*
223
+ runtime.goexit:--` )
224
+ if ! wantRE . MatchString ( got ) {
225
+ t .Errorf ("got counter name <<%s>>, want match for <<%s>>" , got , wantRE )
246
226
}
247
227
})
248
228
}
0 commit comments