forked from cilium/ebpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fd.go
34 lines (27 loc) · 735 Bytes
/
fd.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
package fdtrace
import (
"fmt"
"os"
"runtime"
"testing"
"github.com/isu-kim/ebpf/internal/sys"
)
// TestMain runs m with sys.FD leak tracing enabled.
func TestMain(m *testing.M) {
// fn can either be invoked asynchronously by the gc or during disabling of
// the leak tracer below. Don't terminate the program immediately, instead
// capture a boolean that will be used to set the exit code. This avoids races
// and gives all events the chance to be written to stderr.
var leak bool
sys.OnLeakFD(func(fs *runtime.Frames) {
fmt.Fprintln(os.Stderr, "leaked fd created at:")
fmt.Fprintln(os.Stderr, sys.FormatFrames(fs))
leak = true
})
ret := m.Run()
sys.OnLeakFD(nil)
if leak {
ret = 99
}
os.Exit(ret)
}