/
service_test.go
143 lines (125 loc) · 4.14 KB
/
service_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package switchboard_test
import (
"bytes"
"encoding/json"
"time"
"github.com/coreos/go-etcd/etcd"
"github.com/jkakar/switchboard"
. "gopkg.in/check.v1"
)
type ServiceTest struct {
client *etcd.Client
}
var _ = Suite(&ServiceTest{})
func (s *ServiceTest) SetUpTest(c *C) {
s.client = etcd.NewClient([]string{"http://127.0.0.1:4001"})
s.client.Delete("test", true)
}
// Register creates a service record to represent the service and registers it
// in etcd.
func (s *ServiceTest) TestRegister(c *C) {
address := "http://localhost:8080"
routes := switchboard.Routes{"GET": []string{"/users", "/user/:id"}}
service := switchboard.NewService("test", s.client, address, routes)
record, err := service.Register(0)
c.Assert(err, IsNil)
key := "test/" + service.ID()
sort := false
recursive := true
response, err := s.client.Get(key, sort, recursive)
c.Assert(err, IsNil)
recordJSON, _ := json.Marshal(record)
c.Assert(response.Node.Value, Equals, bytes.NewBuffer(recordJSON).String())
}
// Register is effectively a no-op if the service record already exists in
// etcd.
func (s *ServiceTest) TestRegisterDuplicate(c *C) {
address := "http://localhost:8080"
routes := switchboard.Routes{"GET": []string{"/users", "/user/:id"}}
service := switchboard.NewService("test", s.client, address, routes)
_, err := service.Register(0)
c.Assert(err, IsNil)
_, err = service.Register(0)
c.Assert(err, IsNil)
}
// Unregister returns an error if the service isn't registered in etcd.
func (s *ServiceTest) TestUnregisterUnregisteredService(c *C) {
address := "http://localhost:8080"
routes := switchboard.Routes{"GET": []string{"/users", "/user/:id"}}
service := switchboard.NewService("test", s.client, address, routes)
err := service.Unregister()
c.Assert(err.(*etcd.EtcdError).ErrorCode, Equals, 100)
}
// Unregister deletes the service record in etcd.
func (s *ServiceTest) TestUnregister(c *C) {
address := "http://localhost:8080"
routes := switchboard.Routes{"GET": []string{"/users", "/user/:id"}}
service := switchboard.NewService("test", s.client, address, routes)
_, err := service.Register(0)
c.Assert(err, IsNil)
err = service.Unregister()
c.Assert(err, IsNil)
key := "test/" + service.ID()
sort := false
recursive := true
_, err = s.client.Get(key, sort, recursive)
c.Assert(err.(*etcd.EtcdError).ErrorCode, Equals, 100)
}
// Broadcast stops pushing changes to etcd when when a bool value is sent to
// the stop channel.
func (s *ServiceTest) TestBroadcastStops(c *C) {
address := "http://localhost:8080"
routes := switchboard.Routes{"GET": []string{"/users", "/user/:id"}}
service := switchboard.NewService("test", s.client, address, routes)
stop := make(chan bool)
stopped := make(chan bool)
go func() {
service.Broadcast(2, 1, stop)
stopped <- true
}()
stop <- true
c.Assert(<-stopped, Equals, true)
}
// Broadcast registers a service with etcd.
func (s *ServiceTest) TestBroadcast(c *C) {
// Create a new service.
address := "http://localhost:8080"
routes := switchboard.Routes{"GET": []string{"/users", "/user/:id"}}
service := switchboard.NewService("test", s.client, address, routes)
// Start broadcasting service changes to etcd.
stop := make(chan bool)
stopped := make(chan bool)
go func() {
service.Broadcast(2, 1, stop)
stopped <- true
}()
// Cleanly shutdown the broadcaster no matter how the test finishes.
defer func() {
stop <- true
c.Assert(<-stopped, Equals, true)
}()
// Janky logic to wait for the broadcaster to write a value to etcd that
// can be read back will fail when service registration doesn't complete
// within 500ms.
receivedUpdate := false
for i := 0; i < 500; i++ {
key := "test/" + service.ID()
sort := false
recursive := true
response, err := s.client.Get(key, sort, recursive)
if err != nil {
time.Sleep(time.Duration(1) * time.Millisecond)
continue
} else {
record := switchboard.ServiceRecord{
ID: service.ID(),
Address: service.Address(),
Routes: service.Routes()}
recordJSON, _ := json.Marshal(record)
c.Assert(response.Node.Value, Equals, bytes.NewBuffer(recordJSON).String())
receivedUpdate = true
break
}
}
c.Assert(receivedUpdate, Equals, true)
}