2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-09-02 18:14:14 +00:00
|
|
|
|
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
2021-09-02 18:49:32 +00:00
|
|
|
"os"
|
2021-09-02 18:14:14 +00:00
|
|
|
"runtime"
|
|
|
|
"testing"
|
2021-10-27 23:21:44 +00:00
|
|
|
|
|
|
|
"tailscale.com/tstest"
|
2021-09-02 18:14:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCurrentFileDescriptors(t *testing.T) {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
t.Skipf("skipping on %v", runtime.GOOS)
|
|
|
|
}
|
2021-09-02 18:49:32 +00:00
|
|
|
n := CurrentFDs()
|
|
|
|
if n < 3 {
|
|
|
|
t.Fatalf("got %v; want >= 3", n)
|
|
|
|
}
|
|
|
|
|
2021-10-27 23:21:44 +00:00
|
|
|
err := tstest.MinAllocsPerRun(t, 0, func() {
|
2021-09-02 18:49:32 +00:00
|
|
|
n = CurrentFDs()
|
2021-10-27 23:21:44 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2021-09-02 18:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2021-09-02 22:28:19 +00:00
|
|
|
t.Logf("fds for #%v = %v", i, CurrentFDs())
|
2021-09-02 18:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n2 := CurrentFDs()
|
2021-09-02 22:28:19 +00:00
|
|
|
if n2 < n+extra {
|
2021-09-02 18:49:32 +00:00
|
|
|
t.Errorf("fds changed from %v => %v, want to %v", n, n2, n+extra)
|
2021-09-02 18:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCurrentFileDescriptors(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_ = CurrentFDs()
|
|
|
|
}
|
|
|
|
}
|