-
Notifications
You must be signed in to change notification settings - Fork 480
/
lock.go
180 lines (158 loc) · 5.28 KB
/
lock.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
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package state
import (
"context"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/dapr/components-contrib/lock"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/components-contrib/tests/conformance/utils"
"github.com/dapr/kit/config"
)
type TestConfig struct {
utils.CommonConfig
}
func NewTestConfig(component string, operations []string, configMap map[string]interface{}) (TestConfig, error) {
testConfig := TestConfig{
CommonConfig: utils.CommonConfig{
ComponentType: "lock",
ComponentName: component,
Operations: utils.NewStringSet(operations...),
},
}
err := config.Decode(configMap, &testConfig)
if err != nil {
return testConfig, err
}
return testConfig, nil
}
// ConformanceTests runs conf tests for lock stores.
func ConformanceTests(t *testing.T, props map[string]string, lockstore lock.Store, config TestConfig) {
// Test vars
key := strings.ReplaceAll(uuid.New().String(), "-", "")
t.Logf("Base key for test: %s", key)
t.Run("init", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
err := lockstore.InitLockStore(ctx, lock.Metadata{Base: metadata.Base{
Properties: props,
}})
require.NoError(t, err)
})
// Don't run more tests if init failed
if t.Failed() {
t.Fatal("Init failed, stopping further tests")
}
const lockOwner = "conftest"
lockKey1 := key + "-1"
lockKey2 := key + "-2"
var expirationCh *time.Timer
t.Run("TryLock", func(t *testing.T) {
// Acquire a lock
t.Run("acquire lock1", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.TryLock(ctx, &lock.TryLockRequest{
ResourceID: lockKey1,
LockOwner: lockOwner,
ExpiryInSeconds: 15,
})
require.NoError(t, err)
require.NotNil(t, res)
assert.True(t, res.Success)
})
// Acquire a second lock (with a shorter expiration)
t.Run("acquire lock2", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.TryLock(ctx, &lock.TryLockRequest{
ResourceID: lockKey2,
LockOwner: lockOwner,
ExpiryInSeconds: 3,
})
require.NoError(t, err)
require.NotNil(t, res)
assert.True(t, res.Success)
// Set expirationCh to when lock2 expires
expirationCh = time.NewTimer(3 * time.Second)
})
// Acquiring the same lock again should fail
t.Run("fails to acquire existing lock", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.TryLock(ctx, &lock.TryLockRequest{
ResourceID: lockKey1,
LockOwner: lockOwner,
ExpiryInSeconds: 15,
})
require.NoError(t, err)
require.NotNil(t, res)
assert.False(t, res.Success)
})
})
t.Run("Unlock", func(t *testing.T) {
t.Run("fails to unlock with nonexistent resource ID", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.Unlock(ctx, &lock.UnlockRequest{
ResourceID: "nonexistent",
LockOwner: lockOwner,
})
require.NoError(t, err)
require.NotNil(t, res)
assert.Equal(t, lock.LockDoesNotExist, res.Status)
})
t.Run("fails to unlock with wrong owner", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.Unlock(ctx, &lock.UnlockRequest{
ResourceID: lockKey1,
LockOwner: "nonowner",
})
require.NoError(t, err)
require.NotNil(t, res)
assert.Equal(t, lock.LockBelongsToOthers, res.Status)
})
t.Run("unlocks successfully", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.Unlock(ctx, &lock.UnlockRequest{
ResourceID: lockKey1,
LockOwner: lockOwner,
})
require.NoError(t, err)
require.NotNil(t, res)
assert.Equal(t, lock.Success, res.Status)
})
})
t.Run("lock expires", func(t *testing.T) {
// Wait until the lock is supposed to expire
<-expirationCh.C
// Assert that the lock doesn't exist anymore - we should be able to re-acquire it
assert.Eventually(t, func() bool {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.TryLock(ctx, &lock.TryLockRequest{
ResourceID: lockKey2,
LockOwner: lockOwner,
ExpiryInSeconds: 3,
})
return err == nil && res != nil && res.Success
}, 5*time.Second, 100*time.Millisecond, "Lock 2 was not released in time after its scheduled expiration")
})
}