-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_test.go
74 lines (64 loc) · 1.4 KB
/
monitor_test.go
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
package notifymem
import (
"testing"
"time"
)
func TestNewMonitor(t *testing.T) {
// test NewMonitor
n := NewNotifier()
mOpts := MonitorOptions{
Interval: 2 * time.Second,
ResendDelay: 30 * time.Second,
Threshold: 80,
}
_, err := NewMonitor(n, mOpts)
if err != nil {
t.Fatalf("expected nil, got: %s", err)
}
mOpts.Interval = 0 * time.Millisecond
_, err = NewMonitor(n, mOpts)
if err == nil {
t.Fatalf("expected bad interval error, got nil")
}
mOpts.Interval = 2 * time.Second
mOpts.ResendDelay = 0 * time.Second
_, err = NewMonitor(n, mOpts)
if err == nil {
t.Fatalf("expected bad resend delay error, got nil")
}
mOpts.ResendDelay = 30 * time.Second
mOpts.Threshold = -1
_, err = NewMonitor(n, mOpts)
if err == nil {
t.Fatalf("expected bad threshold error, got nil")
}
}
type mockNotifier struct {
notifyCalled bool
}
func (m *mockNotifier) Notify(title, message string) error {
m.notifyCalled = true
return nil
}
func TestMonitorNotify(t *testing.T) {
// test Monitor.Notify
m := &Monitor{}
m.notifier = &mockNotifier{}
m.notify(80)
if !m.notifier.(*mockNotifier).notifyCalled {
t.Fatalf("expected notify to be called")
}
}
func TestMemoryUsage(t *testing.T) {
mem := memory{
total: 100,
avail: 50,
}
usage, err := memoryUsage(mem)
if err != nil {
t.Fatalf("expected nil, got: %s", err)
}
if usage != 50 {
t.Fatalf("expected 50, got: %d", usage)
}
}