-
Notifications
You must be signed in to change notification settings - Fork 17
/
controller_test.go
140 lines (128 loc) · 3.46 KB
/
controller_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
package raft
import (
"context"
"testing"
"github.com/golang/mock/gomock"
membershipmock "github.com/shaj13/raft/internal/mocks/membership"
raftenginemock "github.com/shaj13/raft/internal/mocks/raftengine"
"github.com/shaj13/raft/internal/raftpb"
"github.com/shaj13/raft/internal/transport"
"github.com/stretchr/testify/require"
"go.etcd.io/etcd/raft/v3"
etcdraftpb "go.etcd.io/etcd/raft/v3/raftpb"
)
func TestControllerPush(t *testing.T) {
ctrl := gomock.NewController(t)
eng := raftenginemock.NewMockEngine(ctrl)
eng.EXPECT().Push(gomock.Any()).Return(nil)
c := new(controller)
c.engine = eng
err := c.Push(context.TODO(), 0, etcdraftpb.Message{})
require.NoError(t, err)
}
func TestControllerPromoteMember(t *testing.T) {
ctrl := gomock.NewController(t)
eng := raftenginemock.NewMockEngine(ctrl)
eng.EXPECT().Status().Return(raft.Status{}, ErrNotLeader).AnyTimes()
n := new(Node)
n.engine = eng
n.exec = testPreCond
c := new(controller)
c.node = n
err := c.PromoteMember(context.TODO(), 0, RawMember{})
require.Equal(t, ErrNotLeader, err)
}
func TestControllerJoin(t *testing.T) {
table := []struct {
expect func(c *controller)
raw *RawMember
err error
id uint64
}{
{
expect: func(c *controller) {
ctrl := gomock.NewController(t)
pool := membershipmock.NewMockPool(ctrl)
pool.EXPECT().Get(gomock.Any()).Return(nil, false)
eng := raftenginemock.NewMockEngine(ctrl)
eng.EXPECT().Status().Return(raft.Status{}, nil)
eng.
EXPECT().
ProposeConfChange(gomock.Any(), gomock.Any(), gomock.Eq(etcdraftpb.ConfChangeAddNode)).
Return(ErrNotLeader)
n := new(Node)
n.exec = testPreCond
n.engine = eng
n.pool = pool
c.node = n
},
raw: &RawMember{ID: 10},
err: ErrNotLeader,
},
{
expect: func(c *controller) {
ctrl := gomock.NewController(t)
pool := membershipmock.NewMockPool(ctrl)
eng := raftenginemock.NewMockEngine(ctrl)
mem := membershipmock.NewMockMember(ctrl)
mem.EXPECT().Type().Return(VoterMember)
pool.EXPECT().Get(gomock.Any()).Return(mem, true).MaxTimes(2)
pool.EXPECT().Snapshot().Return(nil)
eng.EXPECT().Status().Return(raft.Status{}, nil)
eng.EXPECT().ProposeConfChange(gomock.Any(), gomock.Any(), gomock.Eq(etcdraftpb.ConfChangeUpdateNode)).Return(nil)
n := new(Node)
n.exec = testPreCond
n.engine = eng
n.pool = pool
c.node = n
c.pool = pool
},
raw: &RawMember{ID: 123},
id: 123,
},
}
for _, tt := range table {
c := new(controller)
tt.expect(c)
resp, err := c.Join(context.TODO(), 0, tt.raw)
require.Equal(t, tt.err, err)
if tt.err == nil {
require.NotNil(t, resp)
require.Equal(t, tt.id, resp.ID)
}
}
}
func TestRouterMethodsErr(t *testing.T) {
ctx := context.TODO()
noGroup := uint64(0)
table := []func(r *router) error{
func(r *router) error {
return r.PromoteMember(ctx, noGroup, raftpb.Member{})
},
func(r *router) error {
_, err := r.Join(ctx, noGroup, nil)
return err
},
func(r *router) error {
return r.Push(ctx, noGroup, etcdraftpb.Message{})
},
}
for _, tt := range table {
r := new(router)
err := tt(r)
require.Error(t, err)
require.Contains(t, err.Error(), "unknown group")
}
}
func TestRouterAddRemove(t *testing.T) {
gid := uint64(100)
ctrl := new(controller)
r := new(router)
r.ctrls = map[uint64]transport.Controller{}
r.add(gid, ctrl)
got, _ := r.get(gid)
require.Equal(t, ctrl, got)
r.remove(gid)
_, err := r.get(gid)
require.Error(t, err)
}