-
Notifications
You must be signed in to change notification settings - Fork 1
/
macbook_test.go
137 lines (121 loc) · 3.28 KB
/
macbook_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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package computer
import (
"testing"
"github.com/stretchr/testify/mock"
)
type mockedCPU struct {
mock.Mock
}
func (m *mockedCPU) Usage() int {
args := m.Called()
return args.Get(0).(int)
}
type mockedGPU struct {
mock.Mock
}
func (m *mockedGPU) Usage() int {
args := m.Called()
return args.Get(0).(int)
}
type mockedRAM struct {
mock.Mock
}
func (m *mockedRAM) FreeMemory() int {
args := m.Called()
return args.Get(0).(int)
}
func TestMacbook(t *testing.T) {
type depFields struct {
cpu *mockedCPU
gpu *mockedGPU
memory *mockedRAM
}
type args struct {
cpuThreshold, gpuThreshold, memoryThreshold int
}
tests := []struct {
name string
in *args
out error
on func(*depFields)
assert func(*testing.T, *depFields)
}{
{
name: "when CPU usage larger than CPU threshold diagnose return CpuUtilizationError",
in: &args{50, 60, 1000},
out: CpuUtilizationError,
on: func(df *depFields) {
df.cpu.On("Usage").Return(60) // 60% CPU usage
},
assert: func(t *testing.T, df *depFields) {
df.cpu.AssertNumberOfCalls(t, "Usage", 1)
},
},
{
name: "when GPU usage larger than GPU threshold diagnose return GpuUsageError",
in: &args{50, 90, 1000},
out: GpuUsageError,
on: func(df *depFields) {
df.cpu.On("Usage").Return(40) // 40% CPU usage less than cpuThreshold
df.gpu.On("Usage").Return(95) // 95% gpu usage larger than gpuThreshold
},
assert: func(t *testing.T, df *depFields) {
df.cpu.AssertNumberOfCalls(t, "Usage", 1)
df.gpu.AssertNumberOfCalls(t, "Usage", 1)
},
},
{
name: "when Free memory less than memory threshold diagnose return MemoryUsageError",
in: &args{50, 90, 1000},
out: MemoryUsageError,
on: func(df *depFields) {
df.cpu.On("Usage").Return(40) // 40% CPU usage less than cpuThreshold
df.gpu.On("Usage").Return(50) // 50% gpu usage less than gpuThreshold
df.memory.On("FreeMemory").Return(900) // 900 MB free memory left so it is less than 1000 mb threshold
},
assert: func(t *testing.T, df *depFields) {
df.cpu.AssertNumberOfCalls(t, "Usage", 1)
df.gpu.AssertNumberOfCalls(t, "Usage", 1)
df.memory.AssertNumberOfCalls(t, "FreeMemory", 1)
},
},
{
name: "when all thresholds not hit return nil",
in: &args{50, 90, 1000},
out: nil,
on: func(df *depFields) {
df.cpu.On("Usage").Return(40) // 40% CPU usage less than cpuThreshold
df.gpu.On("Usage").Return(50) // 50% gpu usage less than gpuThreshold
df.memory.On("FreeMemory").Return(2000) // 2000 MB free memory left so it is larger than 1000 mb threshold
},
assert: func(t *testing.T, df *depFields) {
df.cpu.AssertNumberOfCalls(t, "Usage", 1)
df.gpu.AssertNumberOfCalls(t, "Usage", 1)
df.memory.AssertNumberOfCalls(t, "FreeMemory", 1)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// arrange
f := &depFields{
&mockedCPU{},
&mockedGPU{},
&mockedRAM{},
}
mb := NewMacBook(f.cpu, f.gpu, f.memory)
if tt.on != nil {
tt.on(f)
}
// act
err := mb.Diagnose(tt.in.cpuThreshold, tt.in.gpuThreshold, tt.in.memoryThreshold)
// assert
if err != tt.out {
t.Errorf("got %v, want %v", err, tt.out)
}
if tt.assert != nil {
tt.assert(t, f)
}
})
}
}