-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemoryusage_test.go
More file actions
95 lines (80 loc) · 2.02 KB
/
memoryusage_test.go
File metadata and controls
95 lines (80 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package memoryusage_test
import (
"os"
"path/filepath"
"strconv"
"testing"
"github.com/kitproj/coding-context-cli/pkg/codingcontext/memoryusage"
)
func TestReadCurrent(t *testing.T) {
t.Parallel()
tests := []struct {
name string
fileContent string
wantBytes int64
wantErr bool
}{
{
name: "valid memory value",
fileContent: "12345678\n",
wantBytes: 12345678,
},
{
name: "valid value without newline",
fileContent: "999999",
wantBytes: 999999,
},
{
name: "zero value",
fileContent: "0\n",
wantBytes: 0,
},
{
name: "invalid non-numeric content",
fileContent: "abc\n",
wantErr: true,
},
{
name: "empty content",
fileContent: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
memFile := filepath.Join(tmpDir, "memory.current")
if err := os.WriteFile(memFile, []byte(tt.fileContent), 0o600); err != nil {
t.Fatalf("failed to write temp file: %v", err)
}
got, err := memoryusage.ReadCurrentFromPath(memFile)
if (err != nil) != tt.wantErr {
t.Errorf("ReadCurrentFromPath() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && got != tt.wantBytes {
t.Errorf("ReadCurrentFromPath() = %d, want %d", got, tt.wantBytes)
}
})
}
}
func TestReadCurrent_FileNotFound(t *testing.T) {
t.Parallel()
_, err := memoryusage.ReadCurrentFromPath("/nonexistent/path/memory.current")
if err == nil {
t.Error("ReadCurrentFromPath() expected error for missing file, got nil")
}
}
func TestReadCurrent_LiveCgroup(t *testing.T) {
t.Parallel()
bytes, err := memoryusage.ReadCurrent()
if err != nil {
// On systems without cgroup v2 memory.current this is expected.
t.Skipf("cgroup v2 memory.current not available: %v", err)
}
if bytes < 0 {
t.Errorf("ReadCurrent() returned negative value %d", bytes)
}
t.Logf("current cgroup memory usage: %s bytes", strconv.FormatInt(bytes, 10))
}