Repository navigation
Expand file tree
/
Copy pathfdcount.go
More file actions
163 lines (139 loc) · 3.67 KB
/
Copy pathfdcount.go
File metadata and controls
163 lines (139 loc) · 3.67 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
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
package fdcount
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
)
var (
maxAssertAttempts = uint(7)
)
type Counter struct {
match string
startingLines string
startingCount int
}
// Returns a count of the file descriptors matching the given string (not a
// regex). Also returns a Counter that can be used to check the delta of file
// descriptors after this point.
//
// Currently only works on systems that have the lsof command available.
//
// see https://groups.google.com/forum/#!topic/golang-nuts/c0AnWXjzNIA
//
func Matching(match string) (int, *Counter, error) {
c := &Counter{match: match}
// Count initial file descriptors
out, err := runLsof()
if err != nil {
return 0, nil, err
}
c.startingLines, c.startingCount = c.matchingLines(out)
return c.startingCount, c, nil
}
// WaitUntilNoneMatch waits until no file descriptors match the given string, or
// the timeout is hit.
func WaitUntilNoneMatch(match string, timeout time.Duration) error {
start := time.Now()
var out []byte
var err error
var count int
for time.Now().Sub(start) < timeout {
out, err = runLsof()
if err != nil {
return err
}
_, count = matchingLines(match, out)
if count == 0 {
// Success!
return nil
}
time.Sleep(50 * time.Millisecond)
}
return fmt.Errorf("%d lines still match %v\n\n%v", count, match, string(out))
}
// Asserts that the number of file descriptors added/removed since Counter was
// created equals the given number.
func (c *Counter) AssertDelta(expected int) error {
var err error
for try := uint(0); try < maxAssertAttempts; try++ {
err = c.doAssertDelta(expected)
if err == nil {
return nil
}
// Count didn't match, could be we have some lingering descriptors, wait
// and then try again.
time.Sleep((50 << try) * time.Millisecond)
}
return err
}
func (c *Counter) doAssertDelta(expected int) error {
out, err := runLsof()
if err != nil {
return err
}
endingLines, endingCount := c.matchingLines(out)
actual := endingCount - c.startingCount
if actual != expected {
return fmt.Errorf("Unexpected TCP file descriptor count. Expected %d, have %d.\n\n%s",
expected, actual, lsofDelta(c.startingLines, endingLines))
}
return nil
}
func (c *Counter) matchingLines(out []byte) (string, int) {
return matchingLines(c.match, out)
}
func matchingLines(match string, out []byte) (string, int) {
lines := make([]string, 0)
for _, line := range strings.Split(string(out), "\n") {
if strings.Contains(line, match) {
lines = append(lines, line)
}
}
return strings.Join(lines, "\n"), len(lines)
}
func runLsof() ([]byte, error) {
out, err := exec.Command("lsof", "-p", fmt.Sprintf("%v", os.Getpid())).Output()
if err != nil {
err = fmt.Errorf("Unable to run lsof: %v", err)
}
return out, err
}
func lsofDelta(start string, end string) string {
startLines := strings.Split(start, "\n")
endLines := strings.Split(end, "\n")
added := make(map[string]interface{})
removed := make(map[string]interface{})
for _, line := range startLines {
removed[line] = nil
}
for _, line := range endLines {
added[line] = nil
delete(removed, line)
}
for _, line := range startLines {
delete(added, line)
}
a := make([]string, 0, len(added))
r := make([]string, 0, len(removed))
for line := range added {
a = append(a, line)
}
for line := range removed {
r = append(r, line)
}
result := ""
if len(r) > 0 {
result = fmt.Sprintf("Removed file descriptors\n-----------------------------\n%v\n",
strings.Join(r, "\n"))
}
if len(a) > 0 {
if len(r) > 0 {
result = result + "\n"
}
result = fmt.Sprintf("%sNew file descriptors\n-----------------------------\n%v\n",
result, strings.Join(a, "\n"))
}
return result
}