-
Notifications
You must be signed in to change notification settings - Fork 4
/
renewer_test.go
159 lines (153 loc) · 3.65 KB
/
renewer_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
package lease
import (
"testing"
"github.com/Sirupsen/logrus"
)
type renewerTest struct {
name string
prevState map[string]*Lease // leases we holds from the previous loop
managerBehavior map[method]args // manager expected behavior
expectedCalls map[method]int // manager expected calls
expectedHeldLeases []Lease // expected result while calling GetHeldLeases
}
var (
renewerId = "1"
lease1 = &Lease{Key: "foo", Owner: "2"}
lease2 = &Lease{Key: "bar", Owner: renewerId}
lease3 = &Lease{Key: "baz", Owner: renewerId}
)
var renewerTestCases = []renewerTest{
{
"we holds 0 leases. gets 2 leases from the manager belongs to this worker. expect to renew 2",
make(map[string]*Lease),
map[method]args{
methodList: {[]*Lease{lease2, lease3}},
methodRenew: {nil, nil},
},
map[method]int{
methodList: 1,
methodRenew: 2,
},
[]Lease{*lease2, *lease3},
},
{
"we holds 2 leases. gets 2 leases from the manager belongs to this worker. expect to renew 2",
map[string]*Lease{
lease2.Key: lease2,
lease3.Key: lease3,
},
map[method]args{
methodList: {[]*Lease{lease2, lease3}},
methodRenew: {nil, nil},
},
map[method]int{
methodList: 1,
methodRenew: 2,
},
[]Lease{*lease2, *lease3},
},
{
"we holds 1 lease. gets 2 leases from the manager. 1 belongs to this worker. expect to renew 1",
map[string]*Lease{
lease2.Key: lease2,
},
map[method]args{
methodList: {[]*Lease{lease1, lease2}},
methodRenew: {nil},
},
map[method]int{
methodList: 1,
methodRenew: 1,
},
[]Lease{*lease2},
},
{
"we holds 2 leases, but someone delete them from the db. expect to renew 0",
map[string]*Lease{
lease2.Key: lease2,
lease3.Key: lease3,
},
map[method]args{
methodList: {[]*Lease{}},
},
map[method]int{
methodList: 1,
methodRenew: 0,
},
[]Lease{},
},
{
"we holds 2 leases, but someone stoled 1 from us. expect to renew 1",
map[string]*Lease{
lease2.Key: lease2,
lease3.Key: lease3,
},
map[method]args{
methodList: {[]*Lease{
&Lease{Key: lease2.Key, Owner: "3"},
lease3,
}},
methodRenew: {nil},
},
map[method]int{
methodList: 1,
methodRenew: 1,
},
[]Lease{*lease3},
},
{
"we holds 2 leases, but someone stoled them from us. expect to renew 0",
map[string]*Lease{
lease2.Key: lease2,
lease3.Key: lease3,
},
map[method]args{
methodList: {[]*Lease{
&Lease{Key: lease2.Key, Owner: "3"},
&Lease{Key: lease3.Key, Owner: "4"},
}},
},
map[method]int{
methodList: 1,
methodRenew: 0,
},
[]Lease{},
},
}
func TestRenewerCases(t *testing.T) {
for _, test := range renewerTestCases {
logger := logrus.New()
logger.Level = logrus.PanicLevel
manager := newManagerMock(test.managerBehavior)
holder := &leaseHolder{
Config: &Config{WorkerId: renewerId, Logger: logger},
manager: manager,
heldLeases: test.prevState,
}
holder.Renew()
// test method calls expectations
for method, calls := range test.expectedCalls {
if n := manager.calls[method]; n != calls {
t.Errorf("%s: got\n\t%+v\nexpected\n\t%v", test.name, n, calls)
}
}
// test GetHeldLeases equality
leases := holder.GetHeldLeases()
if len(leases) != len(test.expectedHeldLeases) {
t.Errorf("%s\nGetHeldLeases - length: got\n\t%+v\nexpected\n\t%v",
test.name, len(leases), len(test.expectedHeldLeases))
}
for _, l1 := range test.expectedHeldLeases {
found := false
for _, l2 := range leases {
if l1.Key == l2.Key {
found = true
break
}
}
if !found {
t.Errorf("%s: expected lease to be exists in result:\n\t%+v\n", test.name, l1)
}
}
}
}