staros/network_test.go

68 lines
1.7 KiB
Go
Raw Permalink Normal View History

//go:build linux
// +build linux
2021-06-04 10:44:53 +08:00
package staros
import (
"testing"
"time"
2021-06-04 10:44:53 +08:00
)
func Test_TrimSpace(t *testing.T) {
}
func TestAnalyseNetFilesSkipsShortLines(t *testing.T) {
data := []byte("sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode\nshort\n")
res, err := analyseNetFiles(data, nil, "tcp")
if err != nil {
t.Fatal(err)
}
if len(res) != 0 {
t.Fatalf("expected no parsed connections, got %d", len(res))
}
}
func TestNetSpeedsRejectsInvalidDuration(t *testing.T) {
_, err := NetSpeeds(0)
if err == nil {
t.Fatal("expected invalid duration error")
}
_, err = NetSpeeds(-time.Second)
if err == nil {
t.Fatal("expected invalid duration error")
}
}
func TestUniqueStrings(t *testing.T) {
got := uniqueStrings([]string{"tcp", "udp", "tcp"})
if len(got) != 2 {
t.Fatalf("expected 2 unique values, got %d", len(got))
}
if got[0] != "tcp" || got[1] != "udp" {
t.Fatalf("unexpected order: %#v", got)
}
}
func TestParseProcStatusKB(t *testing.T) {
if got := parseProcStatusKB("12 kB"); got != 12*1024 {
t.Fatalf("expected 12288, got %d", got)
}
if got := parseProcStatusKB(""); got != 0 {
t.Fatalf("expected 0, got %d", got)
}
}
func TestProcStartTimeFromStatHandlesProcessNameWithSpacesAndParens(t *testing.T) {
stat := []byte("42 (name with ) parens) S 1 1 1 0 -1 4194560 0 0 0 0 0 0 0 0 20 0 1 0 12345")
got, ok := procStartTimeFromStat(stat)
if !ok {
t.Fatal("expected proc stat start time to parse")
}
ticks := int64(clockTicks())
want := time.Unix(StartTime().Unix()+12345/ticks, (12345%ticks)*int64(time.Second)/ticks)
if !got.Equal(want) {
t.Fatalf("unexpected start time: got %s want %s", got, want)
}
}