tailscale/metrics/metrics_test.go
Brad Fitzpatrick d1cb7a2639 metrics: use SYS_OPENAT
New systems like arm64 don't even have SYS_OPEN.
2021-09-02 15:28:19 -07:00

52 lines
1008 B
Go

// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package metrics
import (
"os"
"runtime"
"testing"
)
func TestCurrentFileDescriptors(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skipf("skipping on %v", runtime.GOOS)
}
n := CurrentFDs()
if n < 3 {
t.Fatalf("got %v; want >= 3", n)
}
allocs := int(testing.AllocsPerRun(100, func() {
n = CurrentFDs()
}))
if allocs != 0 {
t.Errorf("allocs = %v; want 0", allocs)
}
// Open some FDs.
const extra = 10
for i := 0; i < extra; i++ {
f, err := os.Open("/proc/self/stat")
if err != nil {
t.Fatal(err)
}
defer f.Close()
t.Logf("fds for #%v = %v", i, CurrentFDs())
}
n2 := CurrentFDs()
if n2 < n+extra {
t.Errorf("fds changed from %v => %v, want to %v", n, n2, n+extra)
}
}
func BenchmarkCurrentFileDescriptors(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = CurrentFDs()
}
}