-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtracer_test.go
More file actions
112 lines (101 loc) · 2.43 KB
/
Copy pathtracer_test.go
File metadata and controls
112 lines (101 loc) · 2.43 KB
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
package gotaskflow
import (
"bytes"
"encoding/json"
"sync"
"testing"
"time"
)
func TestTracerAddEvent(t *testing.T) {
tr := newTracer()
s := &span{
extra: attr{typ: nodeStatic, name: "task-a"},
begin: tr.start.Add(10 * time.Millisecond),
cost: 5 * time.Millisecond,
}
tr.AddEvent(s)
if len(tr.events) != 1 {
t.Fatalf("expected 1 event, got %d", len(tr.events))
}
ev := tr.events[0]
if ev.Name != "task-a" {
t.Errorf("expected name 'task-a', got %q", ev.Name)
}
if ev.Cat != string(nodeStatic) {
t.Errorf("expected cat %q, got %q", string(nodeStatic), ev.Cat)
}
if ev.Ph != "X" {
t.Errorf("expected ph 'X', got %q", ev.Ph)
}
if ev.Dur != 5000 {
t.Errorf("expected dur 5000, got %d", ev.Dur)
}
}
func TestTracerWithParent(t *testing.T) {
tr := newTracer()
parent := &span{
extra: attr{typ: nodeSubflow, name: "parent-flow"},
begin: tr.start,
cost: 20 * time.Millisecond,
}
child := &span{
extra: attr{typ: nodeStatic, name: "child-task"},
begin: tr.start.Add(5 * time.Millisecond),
cost: 10 * time.Millisecond,
parent: parent,
}
tr.AddEvent(child)
if tr.events[0].Args == nil {
t.Fatal("expected args with parent info")
}
if tr.events[0].Args["parent"] != "parent-flow" {
t.Errorf("expected parent 'parent-flow', got %q", tr.events[0].Args["parent"])
}
}
func TestTracerDraw(t *testing.T) {
tr := newTracer()
tr.AddEvent(&span{
extra: attr{typ: nodeStatic, name: "a"},
begin: tr.start,
cost: 1 * time.Millisecond,
})
var buf bytes.Buffer
if err := tr.draw(&buf); err != nil {
t.Fatalf("unexpected error: %v", err)
}
var events []chromeTraceEvent
if err := json.Unmarshal(buf.Bytes(), &events); err != nil {
t.Fatalf("output is not valid JSON: %v", err)
}
if len(events) != 1 {
t.Fatalf("expected 1 event in output, got %d", len(events))
}
}
func TestTracerConcurrentAddEvent(t *testing.T) {
tr := newTracer()
var wg sync.WaitGroup
n := 100
wg.Add(n)
for i := 0; i < n; i++ {
go func(i int) {
defer wg.Done()
tr.AddEvent(&span{
extra: attr{typ: nodeStatic, name: "task"},
begin: tr.start.Add(time.Duration(i) * time.Millisecond),
cost: 1 * time.Millisecond,
})
}(i)
}
wg.Wait()
if len(tr.events) != n {
t.Fatalf("expected %d events, got %d", n, len(tr.events))
}
// verify all tids are unique
tids := make(map[int64]bool)
for _, ev := range tr.events {
if tids[ev.Tid] {
t.Fatalf("duplicate tid: %d", ev.Tid)
}
tids[ev.Tid] = true
}
}