-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmonitor_test.go
75 lines (62 loc) · 1.51 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
75
package dockerstats
import (
"strconv"
"sync"
"testing"
)
func TestNewMonitor(t *testing.T) {
maxRecieves := 5
var receiveCount int
m := NewMonitor()
m.Comm = testComm{func() ([]Stats, error) {
s := []Stats{
{Container: strconv.Itoa(receiveCount)},
}
receiveCount++
return s, nil
}}
for i := 0; i < maxRecieves; i++ {
s := <-m.Stream
if s.Error != nil {
t.Fatal(s.Error)
}
if len(s.Stats) != 1 {
t.Fatalf("Unexpected number of stats recieved on iteration %v, expected=1, got=%v.\n%v", i, len(s.Stats), s)
} else if s.Stats[0].Container != strconv.Itoa(i) {
t.Fatalf("Unexpected container stats recieved on iteration %v, expected=%v, got=%v.\n%v", i, i, s.Stats[0].Container, s)
}
}
}
func TestMonitor_Stop(t *testing.T) {
var mu sync.Mutex
m := NewMonitor()
var callCount int
m.Comm = testComm{func() ([]Stats, error) {
mu.Lock()
callCount++
mu.Unlock()
return make([]Stats, 0), nil
}}
s := <-m.Stream
if s.Error != nil {
t.Fatal(s.Error)
}
mu.Lock()
expected := callCount
mu.Unlock()
m.Stop()
// Read the values from the channel and ensure at the end that the channel
// has been closed.
for i := 0; i <= expected-1; i++ {
select {
case _, ok := <-m.Stream:
if ok && i == expected-1 {
t.Fatal("Expected stream to be closed after the last record is read")
}
}
}
// Ensure the callCount has stopped increasing.
if expected != callCount {
t.Fatalf("Unexpected callCount, Monitor should have stopped, expected=%v, got=%v", expected, callCount)
}
}