This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
services_delegate_test.go
106 lines (89 loc) · 3.9 KB
/
services_delegate_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
package main
import (
"testing"
"github.com/NinesStack/sidecar/catalog"
. "github.com/smartystreets/goconvey/convey"
)
func Test_GetBroadcasts(t *testing.T) {
Convey("When handing back broadcast messages", t, func() {
state := catalog.NewServicesState()
delegate := NewServicesDelegate(state)
bCast := [][]byte{
[]byte(`{"ID":"d419fa7ad1a7","Name":"/dockercon-6adfe629eebc91","Image":"nginx:latest","Created":"2015-02-25T19:04:46Z","Hostname":"docker2","Ports":[{"Type":"tcp","Port":10234}],"Updated":"2015-03-04T01:12:46.669648453Z","Status":0}`),
[]byte(`{"ID":"deadbeefabba","Name":"/dockercon-6c01869525db08","Image":"nginx:latest","Created":"2015-02-25T19:04:46Z","Hostname":"docker2","Ports":[{"Type":"tcp","Port":10234}],"Updated":"2015-03-04T01:12:46.669648453Z","Status":0}`),
}
bCast2 := [][]byte{
[]byte(`{"ID":"1b3295bf300f","Name":"/romantic_brown","Image":"0415448f2cc2","Created":"2014-10-02T23:58:48Z","Hostname":"docker1","Ports":[{"Type":"tcp","Port":9494}],"Updated":"2015-03-04T01:12:32.630357657Z","Status":0}`),
[]byte(`{"ID":"deadbeefabba","Name":"/dockercon-6c01869525db08","Image":"nginx:latest","Created":"2015-02-25T19:04:46Z","Hostname":"docker2","Ports":[{"Type":"tcp","Port":10234}],"Updated":"2015-03-04T01:12:46.669648453Z","Status":0}`),
}
Convey("Start()", func() {
Convey("Kicks off goroutine", func() {
So(delegate.Started, ShouldBeFalse)
delegate.Start()
So(delegate.Started, ShouldBeTrue)
})
})
Convey("NotifyMsg()", func() {
Convey("Pushes a message into the channel", func() {
delegate.NotifyMsg(bCast[0])
msg := <-delegate.notifications
So(msg, ShouldNotBeNil)
So(msg, ShouldResemble, bCast[0])
})
})
Convey("GetBroadcasts()", func() {
Convey("Returns nil when there is nothing to send", func() {
So(delegate.GetBroadcasts(3, 1398), ShouldBeNil)
})
Convey("Returns from the pending list when nothing in the channel", func() {
data := []byte("data")
delegate.pendingBroadcasts = [][]byte{data}
result := delegate.GetBroadcasts(3, 1398)
So(string(result[0]), ShouldEqual, string(data))
So(len(result), ShouldEqual, 1)
})
Convey("Returns what's in the channel", func() {
state.Broadcasts = make(chan [][]byte, 1)
state.Broadcasts <- bCast
result := delegate.GetBroadcasts(3, 1398)
So(len(result), ShouldEqual, 2)
So(string(result[0]), ShouldEqual, string(bCast[0]))
So(string(result[1]), ShouldEqual, string(bCast[1]))
So(len(delegate.pendingBroadcasts), ShouldEqual, 0)
})
Convey("Returns what's left when nothing is new", func() {
delegate.pendingBroadcasts = bCast
result := delegate.GetBroadcasts(3, 1398)
So(len(result), ShouldEqual, 2)
So(string(result[0]), ShouldEqual, string(bCast[0]))
So(string(result[1]), ShouldEqual, string(bCast[1]))
So(len(delegate.pendingBroadcasts), ShouldEqual, 0)
})
Convey("Returns what's left and what's new when it fits", func() {
state.Broadcasts = make(chan [][]byte, 1)
delegate.pendingBroadcasts = bCast
state.Broadcasts <- bCast2
result := delegate.GetBroadcasts(3, 1398)
So(len(result), ShouldEqual, 4)
for i, entry := range append(bCast2, bCast...) {
So(string(result[i]), ShouldEqual, string(entry))
}
So(len(delegate.pendingBroadcasts), ShouldEqual, 0)
})
Convey("Many runs with leftovers don't leave junk or bad buffers", func() {
state.Broadcasts = make(chan [][]byte, 1)
delegate.pendingBroadcasts = bCast
state.Broadcasts <- append(bCast2, bCast...)
delegate.GetBroadcasts(3, 100)
delegate.GetBroadcasts(3, 300) // 1 message fits here
delegate.GetBroadcasts(3, 100)
result := delegate.GetBroadcasts(3, 1398)
So(len(result), ShouldEqual, 5)
for i, entry := range append(bCast2[1:], bCast...) {
So(string(result[i]), ShouldEqual, string(entry))
}
So(len(delegate.pendingBroadcasts), ShouldEqual, 0)
})
})
})
}