forked from onsi/gomega
-
Notifications
You must be signed in to change notification settings - Fork 0
/
have_leaked_matcher_test.go
280 lines (223 loc) · 6.81 KB
/
have_leaked_matcher_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package gleak
import (
"os"
"os/signal"
"sync"
"time"
"github.com/onsi/gomega/gleak/goroutine"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
// Note: Go's stack dumps (backtraces) always contain forward slashes, even on
// Windows. The following tests thus work the same both on *nix and Windows.
var _ = Describe("HaveLeaked", func() {
It("renders indented goroutine information including (malformed) backtrace", func() {
gs := []goroutine.Goroutine{
{
ID: 42,
State: "stoned",
Backtrace: `main.foo.func1()
/home/foo/test.go:6 +0x28
created by main.foo
/home/foo/test.go:5 +0x64
`,
},
}
m := HaveLeaked().(*HaveLeakedMatcher)
Expect(m.listGoroutines(gs, 1)).To(Equal(` goroutine 42 [stoned]
main.foo.func1() at foo/test.go:6
created by main.foo at foo/test.go:5`))
gs = []goroutine.Goroutine{
{
ID: 42,
State: "stoned",
Backtrace: `main.foo.func1()
/home/foo/test.go:6 +0x28
created by main.foo
/home/foo/test.go:5 +0x64`,
},
}
Expect(m.listGoroutines(gs, 1)).To(Equal(` goroutine 42 [stoned]
main.foo.func1() at foo/test.go:6
created by main.foo at foo/test.go:5`))
gs = []goroutine.Goroutine{
{
ID: 42,
State: "stoned",
Backtrace: `main.foo.func1()
/home/foo/test.go:6 +0x28
created by main.foo
/home/foo/test.go:5`,
},
}
Expect(m.listGoroutines(gs, 1)).To(Equal(` goroutine 42 [stoned]
main.foo.func1() at foo/test.go:6
created by main.foo at foo/test.go:5`))
gs = []goroutine.Goroutine{
{
ID: 42,
State: "stoned",
Backtrace: `main.foo.func1()
/home/foo/test.go:6 +0x28
created by main.foo`,
},
}
Expect(m.listGoroutines(gs, 1)).To(Equal(` goroutine 42 [stoned]
main.foo.func1() at foo/test.go:6
created by main.foo`))
})
It("considers testing and runtime goroutines not to be leaks", func() {
Eventually(Goroutines).WithTimeout(2*time.Second).WithPolling(250*time.Millisecond).
ShouldNot(HaveLeaked(), "should not find any leaks by default")
})
When("using signals", func() {
It("doesn't find leaks", func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
Eventually(Goroutines).WithTimeout(2*time.Second).WithPolling(250*time.Millisecond).
ShouldNot(HaveLeaked(), "found signal.Notify leaks")
signal.Reset(os.Interrupt)
Eventually(Goroutines).WithTimeout(2*time.Second).WithPolling(250*time.Millisecond).
ShouldNot(HaveLeaked(), "found signal.Reset leaks")
})
})
It("checks against list of expected goroutines", func() {
By("taking a snapshot")
gs := Goroutines()
m := HaveLeaked(gs)
By("starting a goroutine")
done := make(chan struct{})
var once sync.Once
go func() {
<-done
}()
defer once.Do(func() { close(done) })
By("detecting the goroutine")
Expect(m.Match(Goroutines())).To(BeTrue())
By("terminating the goroutine and ensuring it has terminated")
once.Do(func() { close(done) })
Eventually(func() (bool, error) {
return m.Match(Goroutines())
}).Should(BeFalse())
})
Context("failure messages", func() {
var snapshot []goroutine.Goroutine
BeforeEach(func() {
snapshot = Goroutines()
done := make(chan struct{})
go func() {
<-done
}()
DeferCleanup(func() {
close(done)
Eventually(Goroutines).ShouldNot(HaveLeaked(snapshot))
})
})
It("returns a failure message", func() {
m := HaveLeaked(snapshot)
gs := Goroutines()
Expect(m.Match(gs)).To(BeTrue())
Expect(m.FailureMessage(gs)).To(MatchRegexp(`Expected to leak 1 goroutines:
goroutine \d+ \[.+\]
.* at .*:\d+
created by .* at .*:\d+`))
})
It("returns a negated failure message", func() {
m := HaveLeaked(snapshot)
gs := Goroutines()
Expect(m.Match(gs)).To(BeTrue())
Expect(m.NegatedFailureMessage(gs)).To(MatchRegexp(`Expected not to leak 1 goroutines:
goroutine \d+ \[.+\]
.* at .*:\d+
created by .* at .*:\d+`))
})
When("things go wrong", func() {
It("rejects unsupported filter args types", func() {
Expect(func() { _ = HaveLeaked(42) }).To(PanicWith(
"HaveLeaked expected a string, []Goroutine, or GomegaMatcher, but got:\n <int>: 42"))
})
It("accepts plain strings as filters", func() {
m := HaveLeaked("foo.bar")
Expect(m.Match([]goroutine.Goroutine{
{TopFunction: "foo.bar"},
})).To(BeFalse())
})
It("expects actual to be a slice of goroutine.Goroutine", func() {
m := HaveLeaked()
Expect(m.Match(nil)).Error().To(MatchError(
"HaveLeaked matcher expects an array or slice of goroutines. Got:\n <nil>: nil"))
Expect(m.Match("foo!")).Error().To(MatchError(
"HaveLeaked matcher expects an array or slice of goroutines. Got:\n <string>: foo!"))
Expect(m.Match([]string{"foo!"})).Error().To(MatchError(
"HaveLeaked matcher expects an array or slice of goroutines. Got:\n <[]string | len:1, cap:1>: [\"foo!\"]"))
})
It("handles filter matcher errors", func() {
m := HaveLeaked(HaveField("foobar", BeNil()))
Expect(m.Match([]goroutine.Goroutine{
{ID: 0},
})).Error().To(HaveOccurred())
})
})
})
Context("wrapped around test nodes", func() {
var snapshot []goroutine.Goroutine
When("not leaking", func() {
BeforeEach(func() {
snapshot = Goroutines()
})
AfterEach(func() {
Eventually(Goroutines).ShouldNot(HaveLeaked(snapshot))
})
It("doesn't leak in test", func() {
// nothing
})
})
When("leaking", func() {
done := make(chan struct{})
BeforeEach(func() {
snapshot = Goroutines()
})
AfterEach(func() {
Expect(Goroutines()).To(HaveLeaked(snapshot))
close(done)
Eventually(Goroutines).ShouldNot(HaveLeaked(snapshot))
})
It("leaks in test", func() {
go func() {
<-done
}()
})
})
})
Context("handling file names and paths in backtraces", func() {
When("ReportFilenameWithPath is true", Ordered, func() {
var oldState bool
BeforeAll(func() {
oldState = ReportFilenameWithPath
ReportFilenameWithPath = true
DeferCleanup(func() {
ReportFilenameWithPath = oldState
})
})
It("doesn't shorten filenames", func() {
Expect(formatFilename("/home/foo/bar/baz.go")).To(Equal("/home/foo/bar/baz.go"))
})
})
When("ReportFilenameWithPath is false", Ordered, func() {
var oldState bool
BeforeAll(func() {
oldState = ReportFilenameWithPath
ReportFilenameWithPath = false
DeferCleanup(func() {
ReportFilenameWithPath = oldState
})
})
It("does return only package and filename, but no path", func() {
Expect(formatFilename("/home/foo/bar/baz.go")).To(Equal("bar/baz.go"))
Expect(formatFilename("/bar/baz.go")).To(Equal("bar/baz.go"))
Expect(formatFilename("/baz.go")).To(Equal("baz.go"))
Expect(formatFilename("/")).To(Equal("/"))
})
})
})
})