-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner_test.go
124 lines (96 loc) · 3.27 KB
/
scanner_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
package scanner
import (
"context"
"sort"
"strings"
"testing"
"time"
)
func setup() (scanner *Scanner, services []Service, output chan Status) {
scanner = New()
services = []Service{
Example{status: ExampleStatus{value: "ok"}},
Example{status: ExampleStatus{value: "good"}},
}
output = make(chan Status, len(services))
return
}
func TestScan(t *testing.T) {
t.Run("checks all services", func(t *testing.T) {
scanner, services, output := setup()
scanner.Scan(context.Background(), output, services...)
responses := []Status{}
for response := range output {
responses = append(responses, response)
}
if checks, expected := len(responses), len(services); checks != expected {
t.Errorf("Expected %d elements, got %d", expected, checks)
}
})
t.Run("allows to retrieve the status of all services", func(t *testing.T) {
scanner, services, output := setup()
scanner.Scan(context.Background(), output, services...)
all := []string{}
for status := range output {
all = append(all, status.Value().(string))
}
sort.Strings(all)
if result, expected := strings.Join(all, " "), "good ok"; result != expected {
t.Errorf("Expected '%s', got '%s'", expected, result)
}
})
}
func TestScanAndWait(t *testing.T) {
t.Run("checks all services", func(t *testing.T) {
scanner, services, _ := setup()
responses := scanner.ScanAndWait(context.Background(), services...)
if checks, expected := len(responses), len(services); checks != expected {
t.Errorf("Expected %d elements, got %d", expected, checks)
}
})
t.Run("allows to retrieve the status of all services", func(t *testing.T) {
scanner, services, _ := setup()
responses := scanner.ScanAndWait(context.Background(), services...)
all := []string{}
for _, status := range responses {
all = append(all, status.Value().(string))
}
sort.Strings(all)
if result, expected := strings.Join(all, " "), "good ok"; result != expected {
t.Errorf("Expected '%s', got '%s'", expected, result)
}
})
}
func TestSetWorkerCount(t *testing.T) {
t.Run("allows to set a limit to concurrency", func(t *testing.T) {
scanner, _, _ := setup()
scanner.SetWorkerCount(1)
services := []Service{
Example{status: ExampleStatus{value: "ok"}, duration: 200 * time.Millisecond},
Example{status: ExampleStatus{value: "good"}, duration: 300 * time.Millisecond},
}
start := time.Now()
_ = scanner.ScanAndWait(context.Background(), services...)
end := time.Now()
elapsed := end.Sub(start)
if expected := 500 * time.Millisecond; elapsed < expected {
t.Errorf("Expected checks to take at least %d duration, got %d", expected, elapsed)
}
})
t.Run("allows to disable the concurrency limit", func(t *testing.T) {
scanner, _, _ := setup()
scanner.SetWorkerCount(1)
scanner.SetWorkerCount(0)
services := []Service{
Example{status: ExampleStatus{value: "ok"}, duration: 200 * time.Millisecond},
Example{status: ExampleStatus{value: "good"}, duration: 300 * time.Millisecond},
}
start := time.Now()
_ = scanner.ScanAndWait(context.Background(), services...)
end := time.Now()
elapsed := end.Sub(start)
if expected := 400 * time.Millisecond; elapsed > expected {
t.Errorf("Expected checks to take at most %d duration (conservative approximation), got %d", expected, elapsed)
}
})
}