-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtaker_test.go
120 lines (114 loc) · 3.09 KB
/
taker_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
package lease
import (
"testing"
"time"
"github.com/Sirupsen/logrus"
)
type takerTest 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
}
var (
takerId = "3"
)
var takerTestCases = []takerTest{
{
`2 workers(incloding me). 2 leases.
and I does not hold any lease. expect to stole 1`,
make(map[string]*Lease),
map[method]args{
methodList: {[]*Lease{
&Lease{Key: "foo", Owner: "1", lastRenewal: time.Now()},
&Lease{Key: "bar", Owner: "1", lastRenewal: time.Now()},
}},
methodTake: {nil},
},
map[method]int{
methodList: 1,
methodTake: 1,
},
},
{
`2 workers(incloding me). 1 leases.
and I does not hold any lease. expect to do nothing`,
make(map[string]*Lease),
map[method]args{
methodList: {[]*Lease{
&Lease{Key: "foo", Owner: "1", lastRenewal: time.Now()},
}},
methodTake: {nil},
},
map[method]int{
methodList: 1,
methodTake: 0,
},
},
{
`2 workers, 3 leases, and all of them expired.
I does not hold any leases, and this is my first scanning.
expect to take two leases.`,
make(map[string]*Lease),
map[method]args{
methodList: {[]*Lease{
&Lease{Key: "foo", Owner: "1", lastRenewal: time.Now().Add(-time.Hour)},
&Lease{Key: "bar", Owner: "1", lastRenewal: time.Now().Add(-time.Hour)},
&Lease{Key: "baz", Owner: "1", lastRenewal: time.Now().Add(-time.Hour)},
}},
methodTake: {nil, nil},
},
map[method]int{
methodList: 1,
methodTake: 2,
},
},
{
`threre are 2 workers(including me), and 3 leases.
worker "1" helds them.
but now, he stopped and not hold them any more.
expect to evict all these leases and take them.`,
map[string]*Lease{
"foo": &Lease{Key: "foo", Owner: "1", lastRenewal: time.Now().Add(-time.Hour), Counter: 10},
"bar": &Lease{Key: "bar", Owner: "1", lastRenewal: time.Now().Add(-time.Hour), Counter: 10},
"baz": &Lease{Key: "baz", Owner: "1", lastRenewal: time.Now().Add(-time.Hour), Counter: 10},
},
map[method]args{
methodList: {[]*Lease{
&Lease{Key: "foo", Owner: "1", Counter: 10},
&Lease{Key: "bar", Owner: "1", Counter: 10},
&Lease{Key: "baz", Owner: "1", Counter: 10},
}},
methodTake: {nil, nil, nil},
methodEvict: {nil, nil, nil},
},
map[method]int{
methodList: 1,
methodTake: 3,
methodEvict: 3,
},
},
}
func TestTakerCases(t *testing.T) {
for _, test := range takerTestCases {
logger := logrus.New()
logger.Level = logrus.PanicLevel
manager := newManagerMock(test.managerBehavior)
taker := &leaseTaker{
Config: &Config{WorkerId: takerId,
Logger: logger,
ExpireAfter: time.Minute,
MaxLeasesToStealAtOneTime: 1,
},
manager: manager,
allLeases: test.prevState,
}
taker.Take()
// 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)
}
}
}
}