-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmonitor_test.go
108 lines (81 loc) · 2.63 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
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
package main
import (
"errors"
"os"
"testing"
"time"
)
func TestMonitorWithSuccessedResponse(t *testing.T) {
config := &Config{
requests: 2,
}
collector := make(chan *Record, config.requests)
context := NewContext(config)
monitor := NewMonitor(context, collector)
request1 := &Record{10, 10, nil}
request2 := &Record{20, 20, nil}
collector <- request1
collector <- request2
devnull, _ := os.Open(os.DevNull)
defer devnull.Close()
stdout := os.Stdout
os.Stdout = devnull
go monitor.Run()
stats := <-monitor.output
os.Stdout = stdout
if stats.totalRequests != config.requests {
t.Fatalf("expected %d requests, actual %d requests", config.requests, stats.totalRequests)
}
if stats.responseTimeData[0] != request1.responseTime || stats.responseTimeData[1] != request2.responseTime {
t.Fatalf("expected %s responseTimeData, actual %s responseTimeData", []time.Duration{request1.responseTime, request2.responseTime}, stats.responseTimeData)
}
if stats.totalReceived != request1.contentSize+request2.contentSize {
t.Fatalf("expected %d content received, actual %d content received", request1.contentSize+request2.contentSize, stats.totalReceived)
}
}
func TestMonitorWithFailedResponse(t *testing.T) {
ContinueOnError = true
config := &Config{
requests: 6,
}
collector := make(chan *Record, config.requests)
context := NewContext(config)
monitor := NewMonitor(context, collector)
dummy := errors.New("dummy error")
records := []*Record{
&Record{Error: &LengthError{dummy}},
&Record{Error: &ConnectError{dummy}},
&Record{Error: &ReceiveError{dummy}},
&Record{Error: &ExceptionError{dummy}},
&Record{Error: &ResponseError{dummy}},
&Record{Error: &ResponseTimeoutError{dummy}},
}
expectedStat := &Stats{
totalRequests: config.requests,
totalFailedReqeusts: 6,
errLength: 1,
errConnect: 1,
errReceive: 1,
errException: 2,
errResponse: 1,
}
for _, record := range records {
collector <- record
}
devnull, _ := os.Open(os.DevNull)
defer devnull.Close()
stdout := os.Stdout
os.Stdout = devnull
go monitor.Run()
actualStats := <-monitor.output
os.Stdout = stdout
if actualStats.totalRequests != expectedStat.totalRequests ||
actualStats.totalFailedReqeusts != expectedStat.totalFailedReqeusts ||
actualStats.errLength != expectedStat.errLength ||
actualStats.errConnect != expectedStat.errConnect ||
actualStats.errReceive != expectedStat.errReceive ||
actualStats.errException != expectedStat.errException ||
actualStats.errResponse != expectedStat.errResponse {
t.Fatalf("expected %#+v , actual %#+v", expectedStat, actualStats)
}
}