tailscale/metrics/metrics_test.go
Brad Fitzpatrick 7c1d6e35a5 all: use Go 1.22 range-over-int
Updates #11058

Change-Id: I35e7ef9b90e83cac04ca93fd964ad00ed5b48430
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2024-04-16 15:32:38 -07:00

65 lines
1.2 KiB
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package metrics
import (
"os"
"runtime"
"testing"
"tailscale.com/tstest"
)
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)
}
}
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)
}
err := tstest.MinAllocsPerRun(t, 0, func() {
n = CurrentFDs()
})
if err != nil {
t.Fatal(err)
}
// Open some FDs.
const extra = 10
for i := range extra {
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 range b.N {
_ = CurrentFDs()
}
}