-
Notifications
You must be signed in to change notification settings - Fork 11
/
helpers_test.go
197 lines (158 loc) · 3.94 KB
/
helpers_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package yagnats
import (
"bytes"
"errors"
"fmt"
"net"
"os/exec"
"strconv"
"sync"
"time"
)
type FakeConnectionProvider struct {
ReadBuffer string
WriteBuffer []byte
ReturnsError bool
}
func (c *FakeConnectionProvider) ProvideConnection() (*Connection, error) {
if c.ReturnsError {
err := errors.New("error on dialing")
return nil, err
}
connection := NewConnection("", "", "")
connection.conn = &fakeConn{
ReadBuffer: bytes.NewBuffer([]byte(c.ReadBuffer)),
WriteBuffer: bytes.NewBuffer(c.WriteBuffer),
}
go connection.receivePackets()
return connection, nil
}
type DisconnectingConnectionProvider struct {
ReadBuffers []string
WriteBuffer []byte
}
func (c *DisconnectingConnectionProvider) ProvideConnection() (*Connection, error) {
if len(c.ReadBuffers) == 0 {
return nil, errors.New("no more connections")
}
buf := c.ReadBuffers[0]
c.ReadBuffers = c.ReadBuffers[1:]
connection := NewConnection("", "", "")
connection.conn = &fakeConn{
ReadBuffer: bytes.NewBuffer([]byte(buf)),
WriteBuffer: bytes.NewBuffer(c.WriteBuffer),
}
go connection.receivePackets()
return connection, nil
}
func startNats(port int) *exec.Cmd {
cmd := exec.Command("gnatsd", "-p", strconv.Itoa(port), "--user", "nats", "--pass", "nats")
err := cmd.Start()
if err != nil {
fmt.Printf("NATS failed to start: %v\n", err)
}
err = waitUntilNatsUp(port)
if err != nil {
panic("Cannot connect to NATS")
}
return cmd
}
func startNatsTLS(port int) *exec.Cmd {
cmd := exec.Command("gnatsd", "-p", strconv.Itoa(port), "--user", "nats", "--pass", "nats", "--tls", "--tlscert", "./assets/server-cert.pem", "--tlskey", "./assets/server-pkey.pem")
err := cmd.Start()
if err != nil {
fmt.Printf("NATS failed to start: %v\n", err)
}
err = waitUntilNatsUp(port)
if err != nil {
panic("Cannot connect to NATS")
}
return cmd
}
func startNatsMutualTLS(port int) *exec.Cmd {
cmd := exec.Command("gnatsd", "-p", strconv.Itoa(port), "--user", "nats", "--pass", "nats", "--tls", "--tlscert", "./assets/server-cert.pem", "--tlskey", "./assets/server-pkey.pem", "--tlsverify", "--tlscacert", "./assets/ca.pem")
err := cmd.Start()
if err != nil {
fmt.Printf("NATS failed to start: %v\n", err)
}
err = waitUntilNatsUp(port)
if err != nil {
panic("Cannot connect to NATS")
}
return cmd
}
func stopCmd(cmd *exec.Cmd) {
cmd.Process.Kill()
cmd.Wait()
}
func waitUntilNatsUp(port int) error {
maxWait := 10
for i := 0; i < maxWait; i++ {
time.Sleep(500 * time.Millisecond)
_, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err == nil {
return nil
}
}
return errors.New("Waited too long for NATS to start")
}
func waitUntilNatsDown(port int) error {
maxWait := 10
for i := 0; i < maxWait; i++ {
time.Sleep(500 * time.Millisecond)
_, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
return nil
}
}
return errors.New("Waited too long for NATS to stop")
}
type fakeConn struct {
ReadBuffer *bytes.Buffer
WriteBuffer *bytes.Buffer
WriteChan chan []byte
Closed bool
sync.RWMutex
}
func (f *fakeConn) Read(b []byte) (n int, err error) {
f.RLock()
defer f.RUnlock()
if f.Closed {
return 0, errors.New("buffer closed")
}
return f.ReadBuffer.Read(b)
}
func (f *fakeConn) Write(b []byte) (n int, err error) {
f.Lock()
defer f.Unlock()
if f.Closed {
return 0, errors.New("buffer closed")
}
if f.WriteChan != nil {
f.WriteChan <- b
}
return f.WriteBuffer.Write(b)
}
func (f *fakeConn) Close() error {
f.Lock()
defer f.Unlock()
f.Closed = true
return nil
}
func (f *fakeConn) SetDeadline(time.Time) error {
return nil
}
func (f *fakeConn) SetReadDeadline(time.Time) error {
return nil
}
func (f *fakeConn) SetWriteDeadline(time.Time) error {
return nil
}
func (f *fakeConn) LocalAddr() net.Addr {
addr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:4222")
return addr
}
func (f *fakeConn) RemoteAddr() net.Addr {
addr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:65525")
return addr
}