-
Notifications
You must be signed in to change notification settings - Fork 4
/
manager_test.go
451 lines (398 loc) · 12.3 KB
/
manager_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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package lease
import (
"errors"
"fmt"
"testing"
"github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/jpillora/backoff"
)
func TestCreateTable(t *testing.T) {
client := newClientMock(map[method]args{
methodCreateTable: {
// getting "already exists error"
awserr.New("ResourceInUseException", "", errors.New("")),
// getting error, should retry until maxCreateRetries
nil, nil, nil,
// create table finished successfully
new(dynamodb.CreateTableOutput),
},
methodDescribeTable: {
&dynamodb.DescribeTableOutput{Table: &dynamodb.TableDescription{
TableStatus: aws.String(dynamodb.TableStatusActive),
}},
},
})
manager := newTestManager(client)
err := manager.CreateLeaseTable()
assert(t, err == nil, "expect not to fail while getting 'table already exist' error")
assert(t, client.calls[methodCreateTable] == 1, "number of calls should be 1")
err = manager.CreateLeaseTable()
assert(t, client.calls[methodCreateTable] == 4, "should retry 4 times")
assert(t, err != nil, "expect to returns the error")
err = manager.CreateLeaseTable()
assert(t, err == nil, "expect not to fail when the request success")
assert(t, client.calls[methodCreateTable] == 5, "number of calls should be 5")
}
func TestListLeases(t *testing.T) {
client := newClientMock(map[method]args{
methodScan: {
// getting error from dynamodb
nil, nil, nil,
// scan table finished successfully
&dynamodb.ScanOutput{
Items: []map[string]*dynamodb.AttributeValue{
{"leaseKey": {S: aws.String("foo")}},
{"leaseKey": {S: aws.String("bar")}},
{"leaseKey": {S: aws.String("baz")}},
},
},
},
})
manager := newTestManager(client)
leases, err := manager.ListLeases()
assert(t, err != nil, "expect to returns the error")
assert(t, client.calls[methodScan] == 3, "number of calls should be 3")
leases, err = manager.ListLeases()
assert(t, err == nil, "expect not to fail when the request success")
assert(t, client.calls[methodScan] == 4, "number of calls should be 4")
expectedLeases := []string{"foo", "bar", "baz"}
for i := range leases {
k1, k2 := leases[i].Key, expectedLeases[i]
assert(t, k1 == k2, fmt.Sprintf("expect %s to equal %s", k1, k1))
}
}
func TestRenewLease(t *testing.T) {
client := newClientMock(map[method]args{
methodUpdateItem: {
// update item finsihed successfully
new(dynamodb.UpdateItemOutput),
// getting error from dynamodb
nil, nil,
},
})
manager := newTestManager(client)
leaseToRenew := &Lease{Key: "foo", Counter: 10, Owner: "o1"}
err := manager.RenewLease(leaseToRenew)
assert(t, err == nil, "expect not to fail")
assert(t, leaseToRenew.Counter == 11, "expect leaseCounter to be 11")
err = manager.RenewLease(leaseToRenew)
assert(t, err != nil, "expect to returns the error")
assert(t, leaseToRenew.Counter == 11, "expect leaseCounter to be 11")
assert(t, client.calls[methodUpdateItem] == 3, "number of calls should be 3")
}
func TestEvictLease(t *testing.T) {
client := newClientMock(map[method]args{
methodUpdateItem: {
// getting error from dynamodb
nil, nil,
// update item finsihed successfully
new(dynamodb.UpdateItemOutput),
},
})
manager := newTestManager(client)
leaseToEvict := &Lease{Key: "foo", Counter: 10, Owner: "o1"}
err := manager.EvictLease(leaseToEvict)
assert(t, err != nil, "expect to returns the error")
assert(t, leaseToEvict.Owner == "o1", "expect leaseOwner to be the same")
assert(t, client.calls[methodUpdateItem] == 2, "number of calls should be 2")
err = manager.EvictLease(leaseToEvict)
assert(t, err == nil, "expect not to fail")
assert(t, leaseToEvict.Counter == 10, "expect leaseCounter to be the same")
assert(t, leaseToEvict.Owner == "NULL", "expect leaseOwner to be the 'NULL'")
}
func TestTakeLease(t *testing.T) {
client := newClientMock(map[method]args{
methodUpdateItem: {
// getting error from dynamodb
nil, nil,
// update item finsihed successfully
new(dynamodb.UpdateItemOutput),
},
})
manager := newTestManager(client)
leaseToTake := &Lease{Key: "foo", Counter: 10, Owner: "o1"}
err := manager.TakeLease(leaseToTake)
assert(t, err != nil, "expect to returns the error")
assert(t, leaseToTake.Owner == "o1" && leaseToTake.Counter == 10, "expect leaseOwner and leaseCounter to be the same")
err = manager.TakeLease(leaseToTake)
assert(t, err == nil, "expect not to fail")
assert(t, leaseToTake.Owner == manager.WorkerId, "expect owner to equal workerId")
assert(t, leaseToTake.Counter == 11, "expect counter to be increment by 1")
}
func TestDeleteLease(t *testing.T) {
client := newClientMock(map[method]args{
methodDeleteItem: {
// delete item finished successfully
new(dynamodb.DeleteItemOutput),
// getting "conditional error"
awserr.New("ConditionalCheckFailedException", "", errors.New("")),
// getting error from dynamodb
nil, nil,
},
})
manager := newTestManager(client)
leaseToDelete := &Lease{Key: "foo"}
err := manager.DeleteLease(leaseToDelete)
assert(t, err == nil, "expect not to fail")
assert(t, client.calls[methodDeleteItem] == 1, "expect number of calls to equal 1")
err = manager.DeleteLease(leaseToDelete)
assert(t, err != nil, "expect returns the conditional error")
assert(t, client.calls[methodDeleteItem] == 2, "expect number of calls to equal 2")
}
func TestCreateLease(t *testing.T) {
client := newClientMock(map[method]args{
methodPutItem: {
// delete item finished successfully
&dynamodb.PutItemOutput{
Attributes: map[string]*dynamodb.AttributeValue{
"leaseKey": {
S: aws.String("bar"),
},
"leaseOwner": {
S: aws.String("workerId"),
},
"leaseCounter": {
N: aws.String("1"),
},
},
},
// getting "conditional error"
awserr.New("ConditionalCheckFailedException", "", errors.New("")),
// getting error from dynamodb
nil, nil, nil,
},
})
manager := newTestManager(client)
manager.WorkerId = "workerId"
leaseToCreate := &Lease{Key: "bar"}
lease, err := manager.CreateLease(leaseToCreate)
assert(t, err == nil, "expect CreateLease not to fail")
assert(t, client.calls[methodPutItem] == 1, "expect number of calls to equal 1")
assert(t, lease.Owner == manager.WorkerId && lease.Counter == 1, "expect taking the lease")
_, err = manager.CreateLease(leaseToCreate)
assert(t, err != nil, "expect CreateLease to fail")
assert(t, client.calls[methodPutItem] == 2, "expect not retry on conditional failure")
_, err = manager.CreateLease(leaseToCreate)
assert(t, err != nil, "expect CreateLease to fail")
assert(t, client.calls[methodPutItem] == 5, "expect CreateLease to retry 3 times")
}
type (
method int
args []interface{}
)
const (
// Manager methods
methodCreate = iota
methodLCreate
methodUpdate
methodDelete
methodRenew
methodEvict
methodTake
methodList
// Clientface methods
methodScan
methodPutItem
methodUpdateItem
methodDeleteItem
methodCreateTable
methodDescribeTable
)
func (m method) String() string {
inter := "Manager"
if m > methodList {
inter = "Clientface"
}
return fmt.Sprintf("%s.%s", inter, methodNames[m])
}
var methodNames = map[method]string{
methodCreate: "CreateLeaseTable",
methodLCreate: "CreateLease",
methodDelete: "DeleteLease",
methodRenew: "RenewLease",
methodEvict: "EvictLease",
methodTake: "TakeLease",
methodList: "ListLeases",
methodScan: "Scan",
methodPutItem: "PutItem",
methodUpdateItem: "UpdateItem",
methodDeleteItem: "DeleteItem",
methodCreateTable: "CreateTable",
methodDescribeTable: "DescribeTable",
}
type clientMock struct {
calls map[method]int // method name: call times
result map[method]args // expected behavior
}
func newClientMock(behavior map[method]args) *clientMock {
return &clientMock{
calls: make(map[method]int),
result: behavior,
}
}
func (c *clientMock) mcalled(name method) int {
if _, ok := c.calls[name]; !ok {
c.calls[name] = 1
} else {
c.calls[name]++
}
return c.calls[name]
}
func (c *clientMock) Scan(*dynamodb.ScanInput) (out *dynamodb.ScanOutput, err error) {
i := c.mcalled(methodScan)
if v := c.result[methodScan][i-1]; v != nil {
out = v.(*dynamodb.ScanOutput)
} else {
err = errors.New("scan failed")
}
return
}
func (c *clientMock) PutItem(*dynamodb.PutItemInput) (*dynamodb.PutItemOutput, error) {
i := c.mcalled(methodPutItem)
result := c.result[methodPutItem][i-1]
if result != nil {
out, ok := result.(*dynamodb.PutItemOutput)
if ok {
return out, nil
}
// allows custom errors. for example: 'ConditionalFailed'
err, ok := result.(awserr.Error)
return nil, err
}
return nil, errors.New("put item failed")
}
func (c *clientMock) UpdateItem(*dynamodb.UpdateItemInput) (*dynamodb.UpdateItemOutput, error) {
i := c.mcalled(methodUpdateItem)
result := c.result[methodUpdateItem][i-1]
if result != nil {
out, ok := result.(*dynamodb.UpdateItemOutput)
if ok {
return out, nil
}
// allows custom errors. for example: 'ConditionalFailed'
err, ok := result.(awserr.Error)
return nil, err
}
return nil, errors.New("update item failed")
}
func (c *clientMock) DeleteItem(*dynamodb.DeleteItemInput) (*dynamodb.DeleteItemOutput, error) {
i := c.mcalled(methodDeleteItem)
result := c.result[methodDeleteItem][i-1]
if result != nil {
out, ok := result.(*dynamodb.DeleteItemOutput)
if ok {
return out, nil
}
// allows custom errors. for example: 'ConditionalFailed'
err, ok := result.(awserr.Error)
return nil, err
}
return nil, errors.New("delete item failed")
}
func (c *clientMock) CreateTable(*dynamodb.CreateTableInput) (*dynamodb.CreateTableOutput, error) {
i := c.mcalled(methodCreateTable)
result := c.result[methodCreateTable][i-1]
if result != nil {
out, ok := result.(*dynamodb.CreateTableOutput)
if ok {
return out, nil
}
// allows custom errors. for example: 'ConditionalFailed'
err, ok := result.(awserr.Error)
return nil, err
}
return nil, errors.New("create table failed")
}
func (c *clientMock) DescribeTable(*dynamodb.DescribeTableInput) (*dynamodb.DescribeTableOutput, error) {
c.mcalled(methodDescribeTable)
result := c.result[methodDescribeTable][0]
if result != nil {
out, ok := result.(*dynamodb.DescribeTableOutput)
if ok {
return out, nil
}
// allows custom errors. for example: 'ConditionalFailed'
err, ok := result.(awserr.Error)
return nil, err
}
return nil, errors.New("describe table failed")
}
func newTestManager(client Clientface) *LeaseManager {
logger := logrus.New()
logger.Level = logrus.PanicLevel
config := &Config{
WorkerId: "1",
LeaseTable: "test",
Logger: logger,
Client: client,
Backoff: &Backoff{b: &backoff.Backoff{Min: 0, Max: 0}},
}
config.defaults()
return &LeaseManager{config, newSerializer()}
}
type managerMock struct {
calls map[method]int // method name: call times
result map[method]args // expected behavior
}
func newManagerMock(behavior map[method]args) *managerMock {
return &managerMock{
calls: make(map[method]int),
result: behavior,
}
}
func (m *managerMock) mcalled(name method) int {
if _, ok := m.calls[name]; !ok {
m.calls[name] = 1
} else {
m.calls[name]++
}
return m.calls[name]
}
// record all method calls and return the stubed behavior
// for all functions that returns "error" as a result
func (m *managerMock) errOnly(name method) (err error) {
i := m.mcalled(name)
if v := m.result[name][i-1]; v != nil {
err = v.(error)
}
return
}
func (m *managerMock) CreateLeaseTable() error {
return m.errOnly(methodCreate)
}
func (m *managerMock) DeleteLease(*Lease) error {
return m.errOnly(methodDelete)
}
func (m *managerMock) CreateLease(l *Lease) (*Lease, error) {
return l, m.errOnly(methodLCreate)
}
func (m *managerMock) UpdateLease(l *Lease) (*Lease, error) {
return l, m.errOnly(methodUpdate)
}
func (m *managerMock) RenewLease(*Lease) error {
return m.errOnly(methodRenew)
}
func (m *managerMock) TakeLease(*Lease) error {
return m.errOnly(methodTake)
}
func (m *managerMock) EvictLease(l *Lease) error {
l.Owner = "NULL"
return m.errOnly(methodEvict)
}
func (m *managerMock) ListLeases() (leases []*Lease, err error) {
i := m.mcalled(methodList)
if v := m.result[methodList][i-1]; v != nil {
leases = v.([]*Lease)
} else {
err = errors.New("list leases failed")
}
return
}
func assert(t *testing.T, cond bool, reason string) {
if !cond {
t.Error(reason)
}
}