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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
package xgb
import (
"bytes"
"errors"
"fmt"
"regexp"
"runtime"
"strconv"
"strings"
"testing"
"time"
)
type goroutine struct {
id int
name string
stack []byte
}
type leaks struct {
name string
goroutines map[int]goroutine
report []*leaks
}
func leaksMonitor(name string, monitors ...*leaks) *leaks {
return &leaks{
name,
leaks{}.collectGoroutines(),
monitors,
}
}
// ispired by https://golang.org/src/runtime/debug/stack.go?s=587:606#L21
// stack returns a formatted stack trace of all goroutines.
// It calls runtime.Stack with a large enough buffer to capture the entire trace.
func (_ leaks) stack() []byte {
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
return buf[:n]
}
buf = make([]byte, 2*len(buf))
}
}
func (l leaks) collectGoroutines() map[int]goroutine {
res := make(map[int]goroutine)
stacks := bytes.Split(l.stack(), []byte{'\n', '\n'})
regexpId := regexp.MustCompile(`^\s*goroutine\s*(\d+)`)
for _, st := range stacks {
lines := bytes.Split(st, []byte{'\n'})
if len(lines) < 2 {
panic("routine stach has less tnan two lines: " + string(st))
}
idMatches := regexpId.FindSubmatch(lines[0])
if len(idMatches) < 2 {
panic("no id found in goroutine stack's first line: " + string(lines[0]))
}
id, err := strconv.Atoi(string(idMatches[1]))
if err != nil {
panic("converting goroutine id to number error: " + err.Error())
}
if _, ok := res[id]; ok {
panic("2 goroutines with same id: " + strconv.Itoa(id))
}
name := strings.TrimSpace(string(lines[1]))
//filter out our stack routine
if strings.Contains(name, "xgb.leaks.stack") {
continue
}
res[id] = goroutine{id, name, st}
}
return res
}
func (l leaks) leakingGoroutines() []goroutine {
goroutines := l.collectGoroutines()
res := []goroutine{}
for id, gr := range goroutines {
if _, ok := l.goroutines[id]; ok {
continue
}
res = append(res, gr)
}
return res
}
func (l leaks) checkTesting(t *testing.T) {
if len(l.leakingGoroutines()) == 0 {
return
}
leakTimeout := time.Second
time.Sleep(leakTimeout)
//t.Logf("possible goroutine leakage, waiting %v", leakTimeout)
grs := l.leakingGoroutines()
for _, gr := range grs {
t.Errorf("%s: %s is leaking", l.name, gr.name)
//t.Errorf("%s: %s is leaking\n%v", l.name, gr.name, string(gr.stack))
}
for _, rl := range l.report {
rl.ignoreLeak(grs...)
}
}
func (l *leaks) ignoreLeak(grs ...goroutine) {
for _, gr := range grs {
l.goroutines[gr.id] = gr
}
}
type dNCEvent struct{}
func (_ dNCEvent) Bytes() []byte { return nil }
func (_ dNCEvent) String() string { return "dummy X server event" }
type dNCError struct {
seqId uint16
}
func (e dNCError) SequenceId() uint16 { return e.seqId }
func (_ dNCError) BadId() uint32 { return 0 }
func (_ dNCError) Error() string { return "dummy X server error reply" }
func TestConnOnNonBlockingDummyXServer(t *testing.T) {
timeout := time.Millisecond
NewErrorFuncs[255] = func(buf []byte) Error {
return dNCError{Get16(buf[2:])}
}
NewEventFuncs[128&127] = func(buf []byte) Event {
return dNCEvent{}
}
checkedReply := func(wantError bool) func(*Conn) error {
request := "reply"
if wantError {
request = "error"
}
return func(c *Conn) error {
cookie := c.NewCookie(true, true)
c.NewRequest([]byte(request), cookie)
_, err := cookie.Reply()
if wantError && err == nil {
return errors.New(fmt.Sprintf("checked request \"%v\" with reply resulted in nil error, want some error", request))
}
if !wantError && err != nil {
return errors.New(fmt.Sprintf("checked request \"%v\" with reply resulted in error %v, want nil error", request, err))
}
return nil
}
}
checkedNoreply := func(wantError bool) func(*Conn) error {
request := "noreply"
if wantError {
request = "error"
}
return func(c *Conn) error {
cookie := c.NewCookie(true, false)
c.NewRequest([]byte(request), cookie)
err := cookie.Check()
if wantError && err == nil {
return errors.New(fmt.Sprintf("checked request \"%v\" with no reply resulted in nil error, want some error", request))
}
if !wantError && err != nil {
return errors.New(fmt.Sprintf("checked request \"%v\" with no reply resulted in error %v, want nil error", request, err))
}
return nil
}
}
uncheckedReply := func(wantError bool) func(*Conn) error {
request := "reply"
if wantError {
request = "error"
}
return func(c *Conn) error {
cookie := c.NewCookie(false, true)
c.NewRequest([]byte(request), cookie)
_, err := cookie.Reply()
if err != nil {
return errors.New(fmt.Sprintf("unchecked request \"%v\" with reply resulted in %v, want nil", request, err))
}
return nil
}
}
uncheckedNoreply := func(wantError bool) func(*Conn) error {
request := "noreply"
if wantError {
request = "error"
}
return func(c *Conn) error {
cookie := c.NewCookie(false, false)
c.NewRequest([]byte(request), cookie)
return nil
}
}
event := func() func(*Conn) error {
return func(c *Conn) error {
_, err := c.conn.Write([]byte("event"))
if err != nil {
return errors.New(fmt.Sprintf("asked dummy server to send event, but resulted in error: %v\n", err))
}
return err
}
}
waitEvent := func(wantError bool) func(*Conn) error {
return func(c *Conn) error {
_, err := c.WaitForEvent()
if wantError && err == nil {
return errors.New(fmt.Sprintf("wait for event resulted in nil error, want some error"))
}
if !wantError && err != nil {
return errors.New(fmt.Sprintf("wait for event resulted in error %v, want nil error", err))
}
return nil
}
}
testCases := []struct {
description string
actions []func(*Conn) error
}{
{"close",
[]func(*Conn) error{},
},
{"checked requests with reply",
[]func(*Conn) error{
checkedReply(false),
checkedReply(true),
checkedReply(false),
checkedReply(true),
},
},
{"checked requests no reply",
[]func(*Conn) error{
checkedNoreply(false),
checkedNoreply(true),
checkedNoreply(false),
checkedNoreply(true),
},
},
{"unchecked requests with reply",
[]func(*Conn) error{
uncheckedReply(false),
uncheckedReply(true),
waitEvent(true),
uncheckedReply(false),
event(),
waitEvent(false),
},
},
{"unchecked requests no reply",
[]func(*Conn) error{
uncheckedNoreply(false),
uncheckedNoreply(true),
waitEvent(true),
uncheckedNoreply(false),
event(),
waitEvent(false),
},
},
{"unexpected conn close",
[]func(*Conn) error{
func(c *Conn) error {
c.conn.Close()
if ev, err := c.WaitForEvent(); ev != nil || err != nil {
return fmt.Errorf("after conn close WaitForEvent() = (%v, %v), want (nil, nil)", ev, err)
}
select {
case eoe, ok := <-c.eventChan:
if ok {
return fmt.Errorf("(*Conn).eventChan should be closed by now, but is not and returns %v", eoe)
}
case <-time.After(timeout):
return fmt.Errorf("(*Conn).eventChan should be closed by now, but is not and was blocking for %v", timeout)
}
return nil
},
},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
tclm := leaksMonitor("test case " + tc.description)
defer tclm.checkTesting(t)
seqId := uint16(1)
incrementSequenceId := func() {
// this has to be the same algorithm as in (*Conn).generateSeqIds
if seqId == uint16((1<<16)-1) {
seqId = 0
} else {
seqId++
}
}
dummyXreplyer := func(request []byte) []byte {
//fmt.Printf("dummyXreplyer got request: %s\n", string(request))
res := make([]byte, 32)
switch string(request) {
case "event":
res[0] = 128
//fmt.Printf("dummyXreplyer sent response: %v\n", res)
return res
case "error":
res[0] = 0 // error
res[1] = 255 // error function
default:
res[0] = 1 // reply
}
Put16(res[2:], seqId) // sequence number
incrementSequenceId()
if string(request) == "noreply" {
//fmt.Printf("dummyXreplyer no response sent\n")
return nil
}
//fmt.Printf("dummyXreplyer sent response: %v\n", res)
return res
}
sclm := leaksMonitor("after server close", tclm)
defer sclm.checkTesting(t)
s := newDummyNetConn("dummX", dummyXreplyer)
defer s.Close()
c, err := postNewConn(&Conn{conn: s})
if err != nil {
t.Errorf("connect to dummy server error: %v", err)
return
}
rlm := leaksMonitor("after actions end")
for _, action := range tc.actions {
if err := action(c); err != nil {
t.Error(err)
break
}
}
c.Close()
select {
case eoe, ok := <-c.eventChan:
if ok {
t.Errorf("(*Conn).eventChan should be closed after (*Conn),Close(), but is not and returned %v", eoe)
}
case <-time.After(timeout):
t.Errorf("(*Conn).eventChan should be closed after (*Conn),Close(), but is not and was blocking for %v", timeout)
}
rlm.checkTesting(t)
})
}
}
|