-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_test.go
More file actions
119 lines (95 loc) · 2.74 KB
/
monitor_test.go
File metadata and controls
119 lines (95 loc) · 2.74 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"testing"
"time"
)
func TestResourceMonitorCreation(t *testing.T) {
monitor := NewResourceMonitor(1 * time.Second)
if monitor == nil {
t.Fatal("Failed to create resource monitor")
}
if monitor.updateInterval != 1*time.Second {
t.Errorf("Expected update interval 1s, got %v", monitor.updateInterval)
}
}
func TestResourceMonitorStartStop(t *testing.T) {
monitor := NewResourceMonitor(100 * time.Millisecond)
monitor.Start()
// Let it run for a bit
time.Sleep(500 * time.Millisecond)
// Get metrics
metrics := monitor.GetMetrics()
if metrics.Timestamp.IsZero() {
t.Error("Expected metrics to be updated")
}
monitor.Stop()
// Verify it can be called multiple times
monitor.Stop()
}
func TestResourceMonitorMetrics(t *testing.T) {
monitor := NewResourceMonitor(100 * time.Millisecond)
monitor.Start()
defer monitor.Stop()
// Wait for metrics to be collected
time.Sleep(500 * time.Millisecond)
metrics := monitor.GetMetrics()
// CPU should be a valid percentage
if metrics.CPUPercent < 0 || metrics.CPUPercent > 100 {
t.Errorf("Invalid CPU percentage: %f", metrics.CPUPercent)
}
// Memory should be positive
if metrics.MemoryUsedMB < 0 {
t.Errorf("Invalid memory usage: %f", metrics.MemoryUsedMB)
}
// Memory percentage should be valid
if metrics.MemoryPercent < 0 || metrics.MemoryPercent > 100 {
t.Errorf("Invalid memory percentage: %f", metrics.MemoryPercent)
}
}
func TestCanRunJobWithConstraints(t *testing.T) {
monitor := NewResourceMonitor(100 * time.Millisecond)
monitor.Start()
defer monitor.Stop()
// Wait for initial metrics
time.Sleep(500 * time.Millisecond)
// Test with very high constraints (should always pass)
highConstraints := ResourceConstraints{
MaxCPUPercent: 100.0,
MaxMemoryMB: 100000.0,
MaxIOOpsPerSec: 1000000,
}
if !monitor.CanRunJob(highConstraints) {
t.Error("Should be able to run job with high constraints")
}
// Test with very low constraints (likely to fail)
lowConstraints := ResourceConstraints{
MaxCPUPercent: 0.01,
MaxMemoryMB: 0.01,
MaxIOOpsPerSec: 1,
}
canRun := monitor.CanRunJob(lowConstraints)
// This might pass or fail depending on system state, just verify it returns a boolean
_ = canRun
}
func TestGetGoRoutineCount(t *testing.T) {
count := GetGoRoutineCount()
if count <= 0 {
t.Errorf("Expected positive goroutine count, got %d", count)
}
// Start some goroutines
done := make(chan bool)
for i := 0; i < 5; i++ {
go func() {
time.Sleep(100 * time.Millisecond)
done <- true
}()
}
newCount := GetGoRoutineCount()
if newCount <= count {
t.Logf("Warning: Expected more goroutines after spawning. Before: %d, After: %d", count, newCount)
}
// Wait for goroutines to finish
for i := 0; i < 5; i++ {
<-done
}
}