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