forked from cloudfoundry-attic/gibson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_client_test.go
113 lines (70 loc) · 2.88 KB
/
router_client_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
package gibson
import (
"time"
"github.com/cloudfoundry/yagnats"
"github.com/cloudfoundry/yagnats/fakeyagnats"
. "launchpad.net/gocheck"
)
type RCSuite struct{}
func init() {
Suite(&RCSuite{})
}
func (s *RCSuite) TestRouterClientRegistering(c *C) {
mbus := fakeyagnats.New()
routerClient := NewCFRouterClient("1.2.3.4", mbus)
routerClient.Register(123, "abc")
registrations := mbus.PublishedMessages("router.register")
c.Assert(len(registrations), Not(Equals), 0)
c.Assert(string(registrations[0].Payload), Equals,
`{"uris":["abc"],"host":"1.2.3.4","port":123,"private_instance_id":"` + routerClient.PrivateInstanceId + `"}`)
}
func (s *RCSuite) TestRouterClientUnregistering(c *C) {
mbus := fakeyagnats.New()
routerClient := NewCFRouterClient("1.2.3.4", mbus)
routerClient.Unregister(123, "abc")
unregistrations := mbus.PublishedMessages("router.unregister")
c.Assert(len(unregistrations), Not(Equals), 0)
c.Assert(string(unregistrations[0].Payload), Equals,
`{"uris":["abc"],"host":"1.2.3.4","port":123,"private_instance_id":"` + routerClient.PrivateInstanceId + `"}`)
}
func (s *RCSuite) TestRouterClientRouterStartHandling(c *C) {
mbus := fakeyagnats.New()
routerClient := NewCFRouterClient("1.2.3.4", mbus)
err := routerClient.Greet()
c.Assert(err, IsNil)
startCallback := mbus.Subscriptions("router.start")[0]
startCallback.Callback(&yagnats.Message{
Payload: []byte(`{"minimumRegisterIntervalInSeconds":1}`),
})
routerClient.Register(123, "abc")
c.Assert(len(mbus.PublishedMessages("router.register")), Equals, 1)
time.Sleep(600 * time.Millisecond)
c.Assert(len(mbus.PublishedMessages("router.register")), Equals, 1)
time.Sleep(600 * time.Millisecond)
c.Assert(len(mbus.PublishedMessages("router.register")), Equals, 2)
time.Sleep(600 * time.Millisecond)
c.Assert(len(mbus.PublishedMessages("router.register")), Equals, 2)
time.Sleep(600 * time.Millisecond)
c.Assert(len(mbus.PublishedMessages("router.register")), Equals, 3)
}
func (s *RCSuite) TestRouterClientGreeting(c *C) {
mbus := fakeyagnats.New()
routerClient := NewCFRouterClient("1.2.3.4", mbus)
routerClient.Register(123, "abc")
err := routerClient.Greet()
c.Assert(err, IsNil)
greetMsg := mbus.PublishedMessages("router.greet")[0]
greetCallback := mbus.Subscriptions(greetMsg.ReplyTo)[0]
greetCallback.Callback(&yagnats.Message{
Payload: []byte(`{"minimumRegisterIntervalInSeconds":1}`),
})
c.Assert(len(mbus.PublishedMessages("router.register")), Equals, 1)
time.Sleep(600 * time.Millisecond)
c.Assert(len(mbus.PublishedMessages("router.register")), Equals, 1)
time.Sleep(600 * time.Millisecond)
c.Assert(len(mbus.PublishedMessages("router.register")), Equals, 2)
time.Sleep(600 * time.Millisecond)
c.Assert(len(mbus.PublishedMessages("router.register")), Equals, 2)
time.Sleep(600 * time.Millisecond)
c.Assert(len(mbus.PublishedMessages("router.register")), Equals, 3)
}