-
Notifications
You must be signed in to change notification settings - Fork 1
/
traffic_monitors_test.go
64 lines (55 loc) · 1.8 KB
/
traffic_monitors_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
package main_test
import (
"fmt"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/wchan2/bloodhound"
)
var _ = Describe(`SummaryStatsTrafficMonitor`, func() {
Describe(`#Monitor`, func() {
var (
trafficMonitor TrafficMonitor
notification *notificationMock
events []Event
)
BeforeEach(func() {
notification = new(notificationMock)
trafficMonitor = NewSummaryStatsTrafficMonitor(1*time.Second, notification)
currentTime := time.Now()
events = []Event{
{
Destination: `destination1`,
Payload: []byte(`random payload`),
Time: currentTime,
},
{
Destination: `destination1`,
Payload: []byte(`another random payload`),
Time: currentTime.Add(1 * time.Second),
},
{
Destination: `destination2`,
Payload: []byte(`yet another random payload`),
Time: currentTime.Add(2 * time.Second),
},
}
})
JustBeforeEach(func() {
for _, event := range events {
trafficMonitor.Monitor(event)
}
})
It(`calculates the summary statistics`, func() {
destination1TotalPayload := len(events[0].Payload) + len(events[1].Payload)
destination1AvgPayload := float64(destination1TotalPayload) / 2
destination2TotalPayload := len(events[2].Payload)
destination2AvgPayload := float64(destination2TotalPayload) / 1
statisticsFormat := "Destination: %s\nAverage Payload: %f\nTotal Payload: %d\nCount: %d\n"
Eventually(func() string { return notification.message }, 2*time.Second, 500*time.Millisecond).Should(And(
ContainSubstring(fmt.Sprintf(statisticsFormat, "destination1", destination1AvgPayload, destination1TotalPayload, 2)),
ContainSubstring(fmt.Sprintf(statisticsFormat, "destination2", destination2AvgPayload, destination2TotalPayload, 1)),
))
})
})
})