This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
tcp_test.go
163 lines (135 loc) · 3.48 KB
/
tcp_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"io"
"io/ioutil"
"net"
"strconv"
"github.com/flynn/strowger/types"
. "github.com/titanous/gocheck"
)
func NewTCPTestServer(prefix string) *TCPTestServer {
s := &TCPTestServer{prefix: prefix}
var err error
s.l, err = net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
s.Addr = s.l.Addr().String()
go s.Serve()
return s
}
type TCPTestServer struct {
Addr string
prefix string
l net.Listener
}
func (s *TCPTestServer) Serve() {
for {
conn, err := s.l.Accept()
if err != nil {
return
}
go func() {
conn.Write([]byte(s.prefix))
io.Copy(conn, conn)
conn.Close()
}()
}
}
func (s *TCPTestServer) Close() error { return s.l.Close() }
const firstTCPPort, lastTCPPort = 10000, 10009
func newTCPListener(etcd *fakeEtcd) (*TCPListener, *fakeDiscoverd, error) {
discoverd := newFakeDiscoverd()
if etcd == nil {
etcd = newFakeEtcd()
}
l := NewTCPListener("127.0.0.1", firstTCPPort, lastTCPPort, NewEtcdDataStore(etcd, "/strowger/tcp/"), discoverd)
return l, discoverd, l.Start()
}
func assertTCPConn(c *C, addr, prefix string) {
conn, err := net.Dial("tcp", addr)
c.Assert(err, IsNil)
conn.Write([]byte("asdf"))
conn.(*net.TCPConn).CloseWrite()
res, err := ioutil.ReadAll(conn)
conn.Close()
c.Assert(err, IsNil)
c.Assert(string(res), Equals, prefix+"asdf")
}
func (s *S) TestAddTCPRoute(c *C) {
const addr, port, portInt = "127.0.0.1:45000", "45000", 45000
srv1 := NewTCPTestServer("1")
srv2 := NewTCPTestServer("2")
defer srv1.Close()
defer srv2.Close()
l, discoverd, err := newTCPListener(nil)
c.Assert(err, IsNil)
defer l.Close()
discoverd.Register("test", srv1.Addr)
defer discoverd.UnregisterAll()
r := addTCPRoute(c, l, portInt)
assertTCPConn(c, addr, "1")
discoverd.Unregister("test", srv1.Addr)
discoverd.Register("test", srv2.Addr)
assertTCPConn(c, addr, "2")
wait := waitForEvent(c, l, "remove", r.Route.ID)
err = l.RemoveRoute(r.Route.ID)
c.Assert(err, IsNil)
wait()
_, err = net.Dial("tcp", addr)
c.Assert(err, Not(IsNil))
}
func addTCPRoute(c *C, l *TCPListener, port int) *strowger.TCPRoute {
wait := waitForEvent(c, l, "set", "")
r := (&strowger.TCPRoute{
Service: "test",
Port: port,
}).ToRoute()
err := l.AddRoute(r)
c.Assert(err, IsNil)
wait()
return r.TCPRoute()
}
func (s *S) TestInitialTCPSync(c *C) {
const addr, port = "127.0.0.1:45000", 45000
etcd := newFakeEtcd()
l, _, err := newTCPListener(etcd)
c.Assert(err, IsNil)
addTCPRoute(c, l, port)
l.Close()
srv := NewTCPTestServer("1")
defer srv.Close()
l, discoverd, err := newTCPListener(etcd)
c.Assert(err, IsNil)
defer l.Close()
discoverd.Register("test", srv.Addr)
defer discoverd.UnregisterAll()
assertTCPConn(c, addr, "1")
}
func (s *S) TestTCPPortAllocation(c *C) {
l, discoverd, err := newTCPListener(nil)
c.Assert(err, IsNil)
defer l.Close()
for i := 0; i < 2; i++ {
ports := make([]string, 0, 10)
for j := 0; j < 10; j++ {
route := addTCPRoute(c, l, 0)
c.Assert(route.Port >= firstTCPPort && route.Port <= lastTCPPort, Equals, true)
port := strconv.Itoa(route.Port)
ports = append(ports, route.ID)
srv := NewTCPTestServer(port)
defer srv.Close()
discoverd.Register("test", srv.Addr)
assertTCPConn(c, "127.0.0.1:"+port, port)
discoverd.UnregisterAll()
}
r := (&strowger.TCPRoute{Service: "test"}).ToRoute()
err := l.AddRoute(r)
c.Assert(err, Equals, ErrNoPorts)
for _, port := range ports {
wait := waitForEvent(c, l, "remove", port)
l.RemoveRoute(port)
wait()
}
}
}