-
Notifications
You must be signed in to change notification settings - Fork 20
/
collector_test.go
148 lines (127 loc) · 3.12 KB
/
collector_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
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
package pyroscope
import (
"io"
"reflect"
"sync"
"testing"
"time"
"github.com/grafana/pyroscope-go/internal/testutil"
"github.com/grafana/pyroscope-go/upstream"
)
func Test_StartCPUProfile_interrupts_background_profiling(t *testing.T) {
logger := testutil.NewTestLogger()
collector := new(mockCollector)
c := newCPUProfileCollector(
"test",
new(mockUpstream),
logger,
100*time.Millisecond,
)
c.collector = collector
go c.Start()
<-collector.waitStartCPUProfile()
// Background profile is being collected.
// Try to interrupt it with StartCPUProfile.
start := collector.waitStartCPUProfile()
stop := collector.waitStopCPUProfile()
if err := c.StartCPUProfile(io.Discard); err != nil {
t.Fatal("failed to start CPU profiling")
}
<-stop
<-start
// Foreground profile is being collected.
// Resume background profiling with StopCPUProfile.
start = collector.waitStartCPUProfile()
stop = collector.waitStopCPUProfile()
c.StopCPUProfile()
<-stop
<-start
c.Stop()
if !reflect.DeepEqual(logger.Lines(), []string{
"starting cpu profile collector",
"cpu profile collector interrupted with StartCPUProfile",
"cpu profile collector restored",
"stopping cpu profile collector",
"stopping cpu profile collector stopped",
}) {
for _, line := range logger.Lines() {
t.Log(line)
}
t.Fatal("^ unexpected even sequence")
}
}
func Test_StartCPUProfile_blocks_Stop(t *testing.T) {
logger := testutil.NewTestLogger()
collector := new(mockCollector)
c := newCPUProfileCollector(
"test",
new(mockUpstream),
logger,
100*time.Millisecond,
)
c.collector = collector
go c.Start()
<-collector.waitStartCPUProfile()
// Background profile is being collected.
// Try to interrupt it with StartCPUProfile.
start := collector.waitStartCPUProfile()
stop := collector.waitStopCPUProfile()
if err := c.StartCPUProfile(io.Discard); err != nil {
t.Fatal("failed to start CPU profiling")
}
<-stop
<-start
go c.StopCPUProfile()
c.Stop()
if !reflect.DeepEqual(logger.Lines(), []string{
"starting cpu profile collector",
"cpu profile collector interrupted with StartCPUProfile",
"stopping cpu profile collector",
"cpu profile collector restored",
"stopping cpu profile collector stopped",
}) {
for _, line := range logger.Lines() {
t.Log(line)
}
t.Fatal("^ unexpected even sequence")
}
}
type mockCollector struct {
sync.Mutex
start chan struct{}
stop chan struct{}
}
func (m *mockCollector) waitStartCPUProfile() <-chan struct{} {
m.Lock()
c := make(chan struct{})
m.start = c
m.Unlock()
return c
}
func (m *mockCollector) waitStopCPUProfile() <-chan struct{} {
m.Lock()
c := make(chan struct{})
m.stop = c
m.Unlock()
return c
}
func (m *mockCollector) StartCPUProfile(_ io.Writer) error {
m.Lock()
if m.start != nil {
close(m.start)
m.start = nil
}
m.Unlock()
return nil
}
func (m *mockCollector) StopCPUProfile() {
m.Lock()
if m.stop != nil {
close(m.stop)
m.stop = nil
}
m.Unlock()
}
type mockUpstream struct{ uploaded []*upstream.UploadJob }
func (m *mockUpstream) Upload(j *upstream.UploadJob) { m.uploaded = append(m.uploaded, j) }
func (*mockUpstream) Flush() {}