Debian Codegraph @

trace/event_test.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Code generated by "gen.bash" from internal/trace; DO NOT EDIT.
6
7 //go:build go1.23
8
9 package trace
10
11 import (
12 "fmt"
13 "golang.org/x/exp/trace/internal/diff"
14 "reflect"
15 "slices"
16 "testing"
17 "time"
18 )
19
20 func TestMakeEvent(t *testing.T) {
21 checkTime := func(t *testing.T, ev Event, want Time) {
22 t.Helper()
23 if ev.Time() != want {
24 t.Errorf("expected time to be %d, got %d", want, ev.Time())
25 }
26 }
27 checkValid := func(t *testing.T, err error, valid bool) bool {
28 t.Helper()
29 if valid && err == nil {
30 return true
31 }
32 if valid && err != nil {
33 t.Errorf("expected no error, got %v", err)
34 } else if !valid && err == nil {
35 t.Errorf("expected error, got %v", err)
36 }
37 return false
38 }
39 type stackType string
40 const (
41 schedStack stackType = "sched stack"
42 stStack stackType = "state transition stack"
43 )
44 checkStack := func(t *testing.T, got Stack, want Stack, which stackType) {
45 t.Helper()
46 diff := diff.Diff("want", []byte(want.String()), "got", []byte(got.String()))
47 if len(diff) > 0 {
48 t.Errorf("unexpected %s: %s", which, diff)
49 }
50 }
51 stk1 := MakeStack([]StackFrame{
52 {PC: 1, Func: "foo", File: "foo.go", Line: 10},
53 {PC: 2, Func: "bar", File: "bar.go", Line: 20},
54 })
55 stk2 := MakeStack([]StackFrame{
56 {PC: 1, Func: "foo", File: "foo.go", Line: 10},
57 {PC: 2, Func: "bar", File: "bar.go", Line: 20},
58 })
59
60 t.Run("Metric", func(t *testing.T) {
61 tests := []struct {
62 name string
63 metric string
64 val uint64
65 stack Stack
66 valid bool
67 }{
68 {name: "gomaxprocs", metric: "/sched/gomaxprocs:threads", valid: true, val: 1, stack: NoStack},
69 {name: "gomaxprocs with stack", metric: "/sched/gomaxprocs:threads", valid: true, val: 1, stack: stk1},
70 {name: "heap objects", metric: "/memory/classes/heap/objects:bytes", valid: true, val: 2, stack: NoStack},
71 {name: "heap goal", metric: "/gc/heap/goal:bytes", valid: true, val: 3, stack: NoStack},
72 {name: "invalid metric", metric: "/test", valid: false, val: 4, stack: NoStack},
73 }
74 for i, test := range tests {
75 t.Run(test.name, func(t *testing.T) {
76 ev, err := MakeEvent(EventConfig[Metric]{
77 Kind: EventMetric,
78 Time: Time(42 + i),
79 Details: Metric{Name: test.metric, Value: Uint64Value(test.val)},
80 Stack: test.stack,
81 })
82 if !checkValid(t, err, test.valid) {
83 return
84 }
85 checkTime(t, ev, Time(42+i))
86 checkStack(t, ev.Stack(), test.stack, schedStack)
87 got := ev.Metric()
88 if got.Name != test.metric {
89 t.Errorf("expected name to be %q, got %q", test.metric, got.Name)
90 }
91 if got.Value.Uint64() != test.val {
92 t.Errorf("expected value to be %d, got %d", test.val, got.Value.Uint64())
93 }
94 })
95 }
96 })
97
98 t.Run("Label", func(t *testing.T) {
99 ev, err := MakeEvent(EventConfig[Label]{
100 Kind: EventLabel,
101 Time: 42,
102 Details: Label{Label: "test", Resource: MakeResourceID(GoID(23))},
103 })
104 if !checkValid(t, err, true) {
105 return
106 }
107 label := ev.Label()
108 if label.Label != "test" {
109 t.Errorf("expected label to be test, got %q", label.Label)
110 }
111 if label.Resource.Kind != ResourceGoroutine {
112 t.Errorf("expected label resource to be goroutine, got %d", label.Resource.Kind)
113 }
114 if label.Resource.id != 23 {
115 t.Errorf("expected label resource to be 23, got %d", label.Resource.id)
116 }
117 checkTime(t, ev, 42)
118 })
119
120 t.Run("Range", func(t *testing.T) {
121 tests := []struct {
122 kind EventKind
123 name string
124 scope ResourceID
125 valid bool
126 }{
127 {kind: EventRangeBegin, name: "GC concurrent mark phase", scope: ResourceID{}, valid: true},
128 {kind: EventRangeActive, name: "GC concurrent mark phase", scope: ResourceID{}, valid: true},
129 {kind: EventRangeEnd, name: "GC concurrent mark phase", scope: ResourceID{}, valid: true},
130 {kind: EventMetric, name: "GC concurrent mark phase", scope: ResourceID{}, valid: false},
131 {kind: EventRangeBegin, name: "GC concurrent mark phase - INVALID", scope: ResourceID{}, valid: false},
132
133 {kind: EventRangeBegin, name: "GC incremental sweep", scope: MakeResourceID(ProcID(1)), valid: true},
134 {kind: EventRangeActive, name: "GC incremental sweep", scope: MakeResourceID(ProcID(2)), valid: true},
135 {kind: EventRangeEnd, name: "GC incremental sweep", scope: MakeResourceID(ProcID(3)), valid: true},
136 {kind: EventMetric, name: "GC incremental sweep", scope: MakeResourceID(ProcID(4)), valid: false},
137 {kind: EventRangeBegin, name: "GC incremental sweep - INVALID", scope: MakeResourceID(ProcID(5)), valid: false},
138
139 {kind: EventRangeBegin, name: "GC mark assist", scope: MakeResourceID(GoID(1)), valid: true},
140 {kind: EventRangeActive, name: "GC mark assist", scope: MakeResourceID(GoID(2)), valid: true},
141 {kind: EventRangeEnd, name: "GC mark assist", scope: MakeResourceID(GoID(3)), valid: true},
142 {kind: EventMetric, name: "GC mark assist", scope: MakeResourceID(GoID(4)), valid: false},
143 {kind: EventRangeBegin, name: "GC mark assist - INVALID", scope: MakeResourceID(GoID(5)), valid: false},
144
145 {kind: EventRangeBegin, name: "stop-the-world (for a good reason)", scope: MakeResourceID(GoID(1)), valid: true},
146 {kind: EventRangeActive, name: "stop-the-world (for a good reason)", scope: MakeResourceID(GoID(2)), valid: false},
147 {kind: EventRangeEnd, name: "stop-the-world (for a good reason)", scope: MakeResourceID(GoID(3)), valid: true},
148 {kind: EventMetric, name: "stop-the-world (for a good reason)", scope: MakeResourceID(GoID(4)), valid: false},
149 {kind: EventRangeBegin, name: "stop-the-world (for a good reason) - INVALID", scope: MakeResourceID(GoID(5)), valid: false},
150 }
151
152 for i, test := range tests {
153 name := fmt.Sprintf("%s/%s/%s", test.kind, test.name, test.scope)
154 t.Run(name, func(t *testing.T) {
155 ev, err := MakeEvent(EventConfig[Range]{
156 Time: Time(42 + i),
157 Kind: test.kind,
158 Details: Range{Name: test.name, Scope: test.scope},
159 })
160 if !checkValid(t, err, test.valid) {
161 return
162 }
163 got := ev.Range()
164 if got.Name != test.name {
165 t.Errorf("expected name to be %q, got %q", test.name, got.Name)
166 }
167 if ev.Kind() != test.kind {
168 t.Errorf("expected kind to be %s, got %s", test.kind, ev.Kind())
169 }
170 if got.Scope.String() != test.scope.String() {
171 t.Errorf("expected scope to be %s, got %s", test.scope.String(), got.Scope.String())
172 }
173 checkTime(t, ev, Time(42+i))
174 })
175 }
176 })
177
178 t.Run("GoroutineTransition", func(t *testing.T) {
179 const anotherG = 999 // indicates hat sched g is different from transition g
180 tests := []struct {
181 name string
182 g GoID
183 stack Stack
184 stG GoID
185 from GoState
186 to GoState
187 reason string
188 stStack Stack
189 valid bool
190 }{
191 {
192 name: "EvGoCreate",
193 g: anotherG,
194 stack: stk1,
195 stG: 1,
196 from: GoNotExist,
197 to: GoRunnable,
198 reason: "",
199 stStack: stk2,
200 valid: true,
201 },
202 {
203 name: "EvGoCreateBlocked",
204 g: anotherG,
205 stack: stk1,
206 stG: 2,
207 from: GoNotExist,
208 to: GoWaiting,
209 reason: "",
210 stStack: stk2,
211 valid: true,
212 },
213 {
214 name: "EvGoCreateSyscall",
215 g: anotherG,
216 stack: NoStack,
217 stG: 3,
218 from: GoNotExist,
219 to: GoSyscall,
220 reason: "",
221 stStack: NoStack,
222 valid: true,
223 },
224 {
225 name: "EvGoStart",
226 g: anotherG,
227 stack: NoStack,
228 stG: 4,
229 from: GoRunnable,
230 to: GoRunning,
231 reason: "",
232 stStack: NoStack,
233 valid: true,
234 },
235 {
236 name: "EvGoDestroy",
237 g: 5,
238 stack: NoStack,
239 stG: 5,
240 from: GoRunning,
241 to: GoNotExist,
242 reason: "",
243 stStack: NoStack,
244 valid: true,
245 },
246 {
247 name: "EvGoDestroySyscall",
248 g: 6,
249 stack: NoStack,
250 stG: 6,
251 from: GoSyscall,
252 to: GoNotExist,
253 reason: "",
254 stStack: NoStack,
255 valid: true,
256 },
257 {
258 name: "EvGoStop",
259 g: 7,
260 stack: stk1,
261 stG: 7,
262 from: GoRunning,
263 to: GoRunnable,
264 reason: "preempted",
265 stStack: stk1,
266 valid: true,
267 },
268 {
269 name: "EvGoBlock",
270 g: 8,
271 stack: stk1,
272 stG: 8,
273 from: GoRunning,
274 to: GoWaiting,
275 reason: "blocked",
276 stStack: stk1,
277 valid: true,
278 },
279 {
280 name: "EvGoUnblock",
281 g: 9,
282 stack: stk1,
283 stG: anotherG,
284 from: GoWaiting,
285 to: GoRunnable,
286 reason: "",
287 stStack: NoStack,
288 valid: true,
289 },
290 // N.b. EvGoUnblock, EvGoSwitch and EvGoSwitchDestroy cannot be
291 // distinguished from each other in Event form, so MakeEvent only
292 // produces EvGoUnblock events for Waiting -> Runnable transitions.
293 {
294 name: "EvGoSyscallBegin",
295 g: 10,
296 stack: stk1,
297 stG: 10,
298 from: GoRunning,
299 to: GoSyscall,
300 reason: "",
301 stStack: stk1,
302 valid: true,
303 },
304 {
305 name: "EvGoSyscallEnd",
306 g: 11,
307 stack: NoStack,
308 stG: 11,
309 from: GoSyscall,
310 to: GoRunning,
311 reason: "",
312 stStack: NoStack,
313 valid: true,
314 },
315 {
316 name: "EvGoSyscallEndBlocked",
317 g: 12,
318 stack: NoStack,
319 stG: 12,
320 from: GoSyscall,
321 to: GoRunnable,
322 reason: "",
323 stStack: NoStack,
324 valid: true,
325 },
326 // TODO(felixge): Use coverage testsing to check if we need all these GoStatus/GoStatusStack cases
327 {
328 name: "GoStatus Undetermined->Waiting",
329 g: anotherG,
330 stack: NoStack,
331 stG: 13,
332 from: GoUndetermined,
333 to: GoWaiting,
334 reason: "",
335 stStack: NoStack,
336 valid: true,
337 },
338 {
339 name: "GoStatus Undetermined->Running",
340 g: anotherG,
341 stack: NoStack,
342 stG: 14,
343 from: GoUndetermined,
344 to: GoRunning,
345 reason: "",
346 stStack: NoStack,
347 valid: true,
348 },
349 {
350 name: "GoStatusStack Undetermined->Waiting",
351 g: anotherG,
352 stack: stk1,
353 stG: 15,
354 from: GoUndetermined,
355 to: GoWaiting,
356 reason: "",
357 stStack: stk1,
358 valid: true,
359 },
360 {
361 name: "GoStatusStack Undetermined->Runnable",
362 g: anotherG,
363 stack: stk1,
364 stG: 16,
365 from: GoUndetermined,
366 to: GoRunnable,
367 reason: "",
368 stStack: stk1,
369 valid: true,
370 },
371 {
372 name: "GoStatus Runnable->Runnable",
373 g: anotherG,
374 stack: NoStack,
375 stG: 17,
376 from: GoRunnable,
377 to: GoRunnable,
378 reason: "",
379 stStack: NoStack,
380 valid: true,
381 },
382 {
383 name: "GoStatus Runnable->Running",
384 g: anotherG,
385 stack: NoStack,
386 stG: 18,
387 from: GoRunnable,
388 to: GoRunning,
389 reason: "",
390 stStack: NoStack,
391 valid: true,
392 },
393 {
394 name: "invalid NotExits->NotExists",
395 g: anotherG,
396 stack: stk1,
397 stG: 18,
398 from: GoNotExist,
399 to: GoNotExist,
400 reason: "",
401 stStack: NoStack,
402 valid: false,
403 },
404 {
405 name: "invalid Running->Undetermined",
406 g: anotherG,
407 stack: stk1,
408 stG: 19,
409 from: GoRunning,
410 to: GoUndetermined,
411 reason: "",
412 stStack: NoStack,
413 valid: false,
414 },
415 }
416
417 for i, test := range tests {
418 t.Run(test.name, func(t *testing.T) {
419 st := MakeGoStateTransition(test.stG, test.from, test.to)
420 st.Stack = test.stStack
421 st.Reason = test.reason
422 ev, err := MakeEvent(EventConfig[StateTransition]{
423 Kind: EventStateTransition,
424 Time: Time(42 + i),
425 Goroutine: test.g,
426 Stack: test.stack,
427 Details: st,
428 })
429 if !checkValid(t, err, test.valid) {
430 return
431 }
432 checkStack(t, ev.Stack(), test.stack, schedStack)
433 if ev.Goroutine() != test.g {
434 t.Errorf("expected goroutine to be %d, got %d", test.g, ev.Goroutine())
435 }
436 got := ev.StateTransition()
437 if got.Resource.Goroutine() != test.stG {
438 t.Errorf("expected resource to be %d, got %d", test.stG, got.Resource.Goroutine())
439 }
440 from, to := got.Goroutine()
441 if from != test.from {
442 t.Errorf("from got=%s want=%s", from, test.from)
443 }
444 if to != test.to {
445 t.Errorf("to got=%s want=%s", to, test.to)
446 }
447 if got.Reason != test.reason {
448 t.Errorf("expected reason to be %s, got %s", test.reason, got.Reason)
449 }
450 checkStack(t, got.Stack, test.stStack, stStack)
451 checkTime(t, ev, Time(42+i))
452 })
453 }
454 })
455
456 t.Run("ProcTransition", func(t *testing.T) {
457 tests := []struct {
458 name string
459 proc ProcID
460 schedProc ProcID
461 from ProcState
462 to ProcState
463 valid bool
464 }{
465 {name: "ProcStart", proc: 1, schedProc: 99, from: ProcIdle, to: ProcRunning, valid: true},
466 {name: "ProcStop", proc: 2, schedProc: 2, from: ProcRunning, to: ProcIdle, valid: true},
467 {name: "ProcSteal", proc: 3, schedProc: 99, from: ProcRunning, to: ProcIdle, valid: true},
468 {name: "ProcSteal lost info", proc: 4, schedProc: 99, from: ProcIdle, to: ProcIdle, valid: true},
469 {name: "ProcStatus", proc: 5, schedProc: 99, from: ProcUndetermined, to: ProcRunning, valid: true},
470 }
471 for i, test := range tests {
472 t.Run(test.name, func(t *testing.T) {
473 st := MakeProcStateTransition(test.proc, test.from, test.to)
474 ev, err := MakeEvent(EventConfig[StateTransition]{
475 Kind: EventStateTransition,
476 Time: Time(42 + i),
477 Proc: test.schedProc,
478 Details: st,
479 })
480 if !checkValid(t, err, test.valid) {
481 return
482 }
483 checkTime(t, ev, Time(42+i))
484 gotSt := ev.StateTransition()
485 from, to := gotSt.Proc()
486 if from != test.from {
487 t.Errorf("from got=%s want=%s", from, test.from)
488 }
489 if to != test.to {
490 t.Errorf("to got=%s want=%s", to, test.to)
491 }
492 if ev.Proc() != test.schedProc {
493 t.Errorf("expected proc to be %d, got %d", test.schedProc, ev.Proc())
494 }
495 if gotSt.Resource.Proc() != test.proc {
496 t.Errorf("expected resource to be %d, got %d", test.proc, gotSt.Resource.Proc())
497 }
498 })
499 }
500 })
501
502 t.Run("Sync", func(t *testing.T) {
503 tests := []struct {
504 name string
505 kind EventKind
506 n int
507 clock *ClockSnapshot
508 batches map[string][]ExperimentalBatch
509 valid bool
510 }{
511 {
512 name: "invalid kind",
513 n: 1,
514 valid: false,
515 },
516 {
517 name: "N",
518 kind: EventSync,
519 n: 1,
520 batches: map[string][]ExperimentalBatch{},
521 valid: true,
522 },
523 {
524 name: "N+ClockSnapshot",
525 kind: EventSync,
526 n: 1,
527 batches: map[string][]ExperimentalBatch{},
528 clock: &ClockSnapshot{
529 Trace: 1,
530 Wall: time.Unix(59, 123456789),
531 Mono: 2,
532 },
533 valid: true,
534 },
535 {
536 name: "N+Batches",
537 kind: EventSync,
538 n: 1,
539 batches: map[string][]ExperimentalBatch{
540 "AllocFree": {{Thread: 1, Data: []byte{1, 2, 3}}},
541 },
542 valid: true,
543 },
544 {
545 name: "unknown experiment",
546 kind: EventSync,
547 n: 1,
548 batches: map[string][]ExperimentalBatch{
549 "does-not-exist": {{Thread: 1, Data: []byte{1, 2, 3}}},
550 },
551 valid: false,
552 },
553 }
554 for i, test := range tests {
555 t.Run(test.name, func(t *testing.T) {
556 ev, err := MakeEvent(EventConfig[Sync]{
557 Kind: test.kind,
558 Time: Time(42 + i),
559 Details: Sync{N: test.n, ClockSnapshot: test.clock, ExperimentalBatches: test.batches},
560 })
561 if !checkValid(t, err, test.valid) {
562 return
563 }
564 got := ev.Sync()
565 checkTime(t, ev, Time(42+i))
566 if got.N != test.n {
567 t.Errorf("expected N to be %d, got %d", test.n, got.N)
568 }
569 if test.clock != nil && got.ClockSnapshot == nil {
570 t.Fatalf("expected ClockSnapshot to be non-nil")
571 } else if test.clock == nil && got.ClockSnapshot != nil {
572 t.Fatalf("expected ClockSnapshot to be nil")
573 } else if test.clock != nil && got.ClockSnapshot != nil {
574 if got.ClockSnapshot.Trace != test.clock.Trace {
575 t.Errorf("expected ClockSnapshot.Trace to be %d, got %d", test.clock.Trace, got.ClockSnapshot.Trace)
576 }
577 if !got.ClockSnapshot.Wall.Equal(test.clock.Wall) {
578 t.Errorf("expected ClockSnapshot.Wall to be %s, got %s", test.clock.Wall, got.ClockSnapshot.Wall)
579 }
580 if got.ClockSnapshot.Mono != test.clock.Mono {
581 t.Errorf("expected ClockSnapshot.Mono to be %d, got %d", test.clock.Mono, got.ClockSnapshot.Mono)
582 }
583 }
584 if !reflect.DeepEqual(got.ExperimentalBatches, test.batches) {
585 t.Errorf("expected ExperimentalBatches to be %#v, got %#v", test.batches, got.ExperimentalBatches)
586 }
587 })
588 }
589 })
590
591 t.Run("Task", func(t *testing.T) {
592 tests := []struct {
593 name string
594 kind EventKind
595 id TaskID
596 parent TaskID
597 typ string
598 valid bool
599 }{
600 {name: "no task", kind: EventTaskBegin, id: NoTask, parent: 1, typ: "type-0", valid: false},
601 {name: "invalid kind", kind: EventMetric, id: 1, parent: 2, typ: "type-1", valid: false},
602 {name: "EvUserTaskBegin", kind: EventTaskBegin, id: 2, parent: 3, typ: "type-2", valid: true},
603 {name: "EvUserTaskEnd", kind: EventTaskEnd, id: 3, parent: 4, typ: "type-3", valid: true},
604 {name: "no parent", kind: EventTaskBegin, id: 4, parent: NoTask, typ: "type-4", valid: true},
605 }
606
607 for i, test := range tests {
608 t.Run(test.name, func(t *testing.T) {
609 ev, err := MakeEvent(EventConfig[Task]{
610 Kind: test.kind,
611 Time: Time(42 + i),
612 Details: Task{ID: test.id, Parent: test.parent, Type: test.typ},
613 })
614 if !checkValid(t, err, test.valid) {
615 return
616 }
617 checkTime(t, ev, Time(42+i))
618 got := ev.Task()
619 if got.ID != test.id {
620 t.Errorf("expected ID to be %d, got %d", test.id, got.ID)
621 }
622 if got.Parent != test.parent {
623 t.Errorf("expected Parent to be %d, got %d", test.parent, got.Parent)
624 }
625 if got.Type != test.typ {
626 t.Errorf("expected Type to be %s, got %s", test.typ, got.Type)
627 }
628 })
629 }
630 })
631
632 t.Run("Region", func(t *testing.T) {
633 tests := []struct {
634 name string
635 kind EventKind
636 task TaskID
637 typ string
638 valid bool
639 }{
640 {name: "invalid kind", kind: EventMetric, task: 1, typ: "type-1", valid: false},
641 {name: "EvUserRegionBegin", kind: EventRegionBegin, task: 2, typ: "type-2", valid: true},
642 {name: "EvUserRegionEnd", kind: EventRegionEnd, task: 3, typ: "type-3", valid: true},
643 }
644
645 for i, test := range tests {
646 t.Run(test.name, func(t *testing.T) {
647 ev, err := MakeEvent(EventConfig[Region]{
648 Kind: test.kind,
649 Time: Time(42 + i),
650 Details: Region{Task: test.task, Type: test.typ},
651 })
652 if !checkValid(t, err, test.valid) {
653 return
654 }
655 checkTime(t, ev, Time(42+i))
656 got := ev.Region()
657 if got.Task != test.task {
658 t.Errorf("expected Task to be %d, got %d", test.task, got.Task)
659 }
660 if got.Type != test.typ {
661 t.Errorf("expected Type to be %s, got %s", test.typ, got.Type)
662 }
663 })
664 }
665 })
666
667 t.Run("Log", func(t *testing.T) {
668 tests := []struct {
669 name string
670 kind EventKind
671 task TaskID
672 category string
673 message string
674 valid bool
675 }{
676 {name: "invalid kind", kind: EventMetric, task: 1, category: "category-1", message: "message-1", valid: false},
677 {name: "basic", kind: EventLog, task: 2, category: "category-2", message: "message-2", valid: true},
678 }
679
680 for i, test := range tests {
681 t.Run(test.name, func(t *testing.T) {
682 ev, err := MakeEvent(EventConfig[Log]{
683 Kind: test.kind,
684 Time: Time(42 + i),
685 Details: Log{Task: test.task, Category: test.category, Message: test.message},
686 })
687 if !checkValid(t, err, test.valid) {
688 return
689 }
690 checkTime(t, ev, Time(42+i))
691 got := ev.Log()
692 if got.Task != test.task {
693 t.Errorf("expected Task to be %d, got %d", test.task, got.Task)
694 }
695 if got.Category != test.category {
696 t.Errorf("expected Category to be %s, got %s", test.category, got.Category)
697 }
698 if got.Message != test.message {
699 t.Errorf("expected Message to be %s, got %s", test.message, got.Message)
700 }
701 })
702 }
703
704 })
705
706 t.Run("StackSample", func(t *testing.T) {
707 tests := []struct {
708 name string
709 kind EventKind
710 stack Stack
711 valid bool
712 }{
713 {name: "invalid kind", kind: EventMetric, stack: stk1, valid: false},
714 {name: "basic", kind: EventStackSample, stack: stk1, valid: true},
715 }
716
717 for i, test := range tests {
718 t.Run(test.name, func(t *testing.T) {
719 ev, err := MakeEvent(EventConfig[StackSample]{
720 Kind: test.kind,
721 Time: Time(42 + i),
722 Stack: test.stack,
723 // N.b. Details defaults to StackSample{}, so we can
724 // omit it here.
725 })
726 if !checkValid(t, err, test.valid) {
727 return
728 }
729 checkTime(t, ev, Time(42+i))
730 got := ev.Stack()
731 checkStack(t, got, test.stack, schedStack)
732 })
733 }
734
735 })
736 }
737
738 func TestMakeStack(t *testing.T) {
739 frames := []StackFrame{
740 {PC: 1, Func: "foo", File: "foo.go", Line: 10},
741 {PC: 2, Func: "bar", File: "bar.go", Line: 20},
742 }
743 got := slices.Collect(MakeStack(frames).Frames())
744 if len(got) != len(frames) {
745 t.Errorf("got=%d want=%d", len(got), len(frames))
746 }
747 for i := range got {
748 if got[i] != frames[i] {
749 t.Errorf("got=%v want=%v", got[i], frames[i])
750 }
751 }
752 }
753
754 func TestPanicEvent(t *testing.T) {
755 // Use a sync event for this because it doesn't have any extra metadata.
756 ev := syncEvent(nil, 0, 0)
757
758 mustPanic(t, func() {
759 _ = ev.Range()
760 })
761 mustPanic(t, func() {
762 _ = ev.Metric()
763 })
764 mustPanic(t, func() {
765 _ = ev.Log()
766 })
767 mustPanic(t, func() {
768 _ = ev.Task()
769 })
770 mustPanic(t, func() {
771 _ = ev.Region()
772 })
773 mustPanic(t, func() {
774 _ = ev.Label()
775 })
776 mustPanic(t, func() {
777 _ = ev.RangeAttributes()
778 })
779 }
780
781 func mustPanic(t *testing.T, f func()) {
782 defer func() {
783 if r := recover(); r == nil {
784 t.Fatal("failed to panic")
785 }
786 }()
787 f()
788 }