forked from rqlite/rqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin_test.go
203 lines (187 loc) · 4.81 KB
/
join_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
198
199
200
201
202
203
package cluster
import (
"context"
"net"
"testing"
"time"
"github.com/rqlite/rqlite/v8/cluster/proto"
"github.com/rqlite/rqlite/v8/cluster/servicetest"
pb "google.golang.org/protobuf/proto"
)
const numAttempts int = 3
const attemptInterval = 1 * time.Second
func Test_SingleJoinOK(t *testing.T) {
srv := servicetest.NewService()
srv.Handler = func(conn net.Conn) {
var p []byte
var err error
c := readCommand(conn)
if c == nil {
// Error on connection, so give up, as normal
// test exit can cause that too.
return
}
if c.Type != proto.Command_COMMAND_TYPE_JOIN {
t.Fatalf("unexpected command type: %d", c.Type)
}
jr := c.GetJoinRequest()
if jr == nil {
t.Fatal("join request is nil")
}
if exp, got := "id0", jr.Id; exp != got {
t.Fatalf("unexpected id, got %s, exp: %s", got, exp)
}
if exp, got := "1.2.3.4", jr.Address; exp != got {
t.Fatalf("unexpected addr, got %s, exp: %s", got, exp)
}
resp := &proto.CommandJoinResponse{}
p, err = pb.Marshal(resp)
if err != nil {
conn.Close()
}
writeBytesWithLength(conn, p)
}
srv.Start()
defer srv.Close()
c := NewClient(&simpleDialer{}, 0)
joiner := NewJoiner(c, numAttempts, attemptInterval)
addr, err := joiner.Do(context.Background(), []string{srv.Addr()}, "id0", "1.2.3.4", Voter)
if err != nil {
t.Fatal(err)
}
if exp, got := srv.Addr(), addr; exp != got {
t.Fatalf("unexpected addr, got %s, exp: %s", got, exp)
}
}
func Test_SingleJoinZeroAttempts(t *testing.T) {
srv := servicetest.NewService()
srv.Handler = func(conn net.Conn) {
t.Fatalf("handler should not have been called")
}
srv.Start()
defer srv.Close()
c := NewClient(&simpleDialer{}, 0)
joiner := NewJoiner(c, 0, attemptInterval)
_, err := joiner.Do(context.Background(), []string{srv.Addr()}, "id0", "1.2.3.4", Voter)
if err != ErrJoinFailed {
t.Fatalf("Incorrect error returned when zero attempts specified")
}
}
func Test_SingleJoinFail(t *testing.T) {
srv := servicetest.NewService()
srv.Handler = func(conn net.Conn) {
var p []byte
var err error
c := readCommand(conn)
if c == nil {
// Error on connection, so give up, as normal
// test exit can cause that too.
return
}
resp := &proto.CommandJoinResponse{
Error: "bad request",
}
p, err = pb.Marshal(resp)
if err != nil {
conn.Close()
}
writeBytesWithLength(conn, p)
}
srv.Start()
defer srv.Close()
c := NewClient(&simpleDialer{}, 0)
joiner := NewJoiner(c, numAttempts, attemptInterval)
_, err := joiner.Do(context.Background(), []string{srv.Addr()}, "id0", "1.2.3.4", Voter)
if err == nil {
t.Fatalf("expected error when joining bad node")
}
}
func Test_SingleJoinCancel(t *testing.T) {
srv := servicetest.NewService()
srv.Handler = func(conn net.Conn) {
var p []byte
var err error
c := readCommand(conn)
if c == nil {
// Error on connection, so give up, as normal
// test exit can cause that too.
return
}
resp := &proto.CommandJoinResponse{
Error: "bad request",
}
p, err = pb.Marshal(resp)
if err != nil {
conn.Close()
}
writeBytesWithLength(conn, p)
}
srv.Start()
defer srv.Close()
// Cancel the join in a few seconds time.
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(3 * time.Second)
cancel()
}()
c := NewClient(&simpleDialer{}, 0)
joiner := NewJoiner(c, 10, attemptInterval)
_, err := joiner.Do(ctx, []string{srv.Addr()}, "id0", "1.2.3.4", Voter)
if err != ErrJoinCanceled {
t.Fatalf("incorrect error returned when canceling: %s", err)
}
}
func Test_DoubleJoinOKSecondNode(t *testing.T) {
srv1 := servicetest.NewService()
srv1.Handler = func(conn net.Conn) {
var p []byte
var err error
c := readCommand(conn)
if c == nil {
// Error on connection, so give up, as normal
// test exit can cause that too.
return
}
resp := &proto.CommandJoinResponse{
Error: "bad request",
}
p, err = pb.Marshal(resp)
if err != nil {
conn.Close()
}
writeBytesWithLength(conn, p)
}
srv1.Start()
defer srv1.Close()
srv2 := servicetest.NewService()
srv2.Handler = func(conn net.Conn) {
var p []byte
var err error
c := readCommand(conn)
if c == nil {
// Error on connection, so give up, as normal
// test exit can cause that too.
return
}
if c.Type != proto.Command_COMMAND_TYPE_JOIN {
t.Fatalf("unexpected command type: %d", c.Type)
}
resp := &proto.CommandJoinResponse{}
p, err = pb.Marshal(resp)
if err != nil {
conn.Close()
}
writeBytesWithLength(conn, p)
}
srv2.Start()
defer srv2.Close()
c := NewClient(&simpleDialer{}, 0)
joiner := NewJoiner(c, numAttempts, attemptInterval)
addr, err := joiner.Do(context.Background(), []string{srv1.Addr(), srv2.Addr()}, "id0", "1.2.3.4", Voter)
if err != nil {
t.Fatal(err)
}
if exp, got := srv2.Addr(), addr; exp != got {
t.Fatalf("unexpected addr, got %s, exp: %s", got, exp)
}
}