-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
80 lines (70 loc) · 1.38 KB
/
helpers_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
package chainsaw
import (
"bytes"
"strings"
"sync"
)
// Various helper stuff for the tests.
// stringLogger implements io.Writer interface and is useful for testing
// things that write to io.Writers. Each write is stored in a string.
type stringLogger struct {
loglines []string
mu sync.Mutex
}
func (s *stringLogger) Write(p []byte) (n int, err error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.loglines == nil {
s.loglines = make([]string, 0)
}
s.loglines = append(s.loglines, string(p))
return len(p), nil
}
func (s *stringLogger) contains(substring string) bool {
s.mu.Lock()
defer s.mu.Unlock()
for _, line := range s.loglines {
if strings.Contains(line, substring) {
return true
}
}
return false
}
type SafeInt struct {
value int
m sync.RWMutex
}
func (i *SafeInt) Inc() {
i.m.Lock()
defer i.m.Unlock()
i.value++
}
func (i *SafeInt) Get() int {
i.m.RLock()
defer i.m.RUnlock()
return i.value
}
type SafeBuffer struct {
b bytes.Buffer
m sync.Mutex
}
func (b *SafeBuffer) Read(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Read(p)
}
func (b *SafeBuffer) Write(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Write(p)
}
func (b *SafeBuffer) String() string {
b.m.Lock()
defer b.m.Unlock()
return b.b.String()
}
func (b *SafeBuffer) Bytes() []byte {
b.m.Lock()
defer b.m.Unlock()
return b.b.Bytes()
}