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
|
|
|
)
|
|
|
|
|
2023-07-01 04:53:21 +00:00
|
|
|
func TestLabelMap(t *testing.T) {
|
|
|
|
var m LabelMap
|
|
|
|
m.GetIncrFunc("foo")(1)
|
|
|
|
m.GetIncrFunc("bar")(2)
|
|
|
|
if g, w := m.Get("foo").Value(), int64(1); g != w {
|
|
|
|
t.Errorf("foo = %v; want %v", g, w)
|
|
|
|
}
|
|
|
|
if g, w := m.Get("bar").Value(), int64(2); g != w {
|
|
|
|
t.Errorf("bar = %v; want %v", g, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2024-04-16 20:15:13 +00:00
|
|
|
for i := range extra {
|
2021-09-02 18:49:32 +00:00
|
|
|
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()
|
2024-04-16 20:15:13 +00:00
|
|
|
for range b.N {
|
2021-09-02 18:14:14 +00:00
|
|
|
_ = CurrentFDs()
|
|
|
|
}
|
|
|
|
}
|