-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathobject_locks_test.go
82 lines (77 loc) · 1.88 KB
/
object_locks_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
package kreconciler
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
type operation struct {
insert bool
id string
expectedError error
}
func TestObjectLocks(t *testing.T) {
testCases := map[string]struct {
inserts []operation
capacity int
endMap map[string]bool
}{
"insertsThenFree": {
inserts: []operation{
{insert: true, id: "a"},
{insert: true, id: "b"},
{insert: true, id: "c"},
{insert: true, id: "d"},
{insert: false, id: "a"},
{insert: false, id: "b"},
{insert: false, id: "c"},
{insert: false, id: "d"},
},
capacity: 4,
endMap: map[string]bool{},
},
"insertsMultiNoDupe": {
inserts: []operation{
{insert: true, id: "a"},
{insert: true, id: "a", expectedError: errAlreadyPresent},
{insert: true, id: "b"},
{insert: true, id: "b", expectedError: errAlreadyPresent},
},
capacity: 4,
endMap: map[string]bool{"a": true, "b": true},
},
"insertsAtCapacityOverflow": {
inserts: []operation{
{insert: true, id: "a"},
{insert: true, id: "b"},
{insert: true, id: "c", expectedError: errQueueOverflow},
},
capacity: 2,
endMap: map[string]bool{"a": true, "b": true},
},
"insertsAtCapacityNoOverflowAfterARemove": {
inserts: []operation{
{insert: true, id: "a"},
{insert: true, id: "b"},
{insert: true, id: "c", expectedError: errQueueOverflow},
{insert: false, id: "b"},
{insert: true, id: "c"},
},
capacity: 2,
endMap: map[string]bool{"a": true, "c": true},
},
}
for n, tt := range testCases {
t.Run(n, func(t *testing.T) {
locks := newObjectLocks(tt.capacity)
for _, v := range tt.inserts {
if v.insert {
err := locks.Take(v.id)
require.Equal(t, v.expectedError, err)
} else {
locks.Free(v.id)
}
}
assert.Equal(t, tt.endMap, locks.objects)
})
}
}