|
| 1 | +/* |
| 2 | +Copyright 2021 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package state |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/google/uuid" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + |
| 26 | + "github.com/dapr/components-contrib/lock" |
| 27 | + "github.com/dapr/components-contrib/metadata" |
| 28 | + "github.com/dapr/components-contrib/tests/conformance/utils" |
| 29 | + "github.com/dapr/kit/config" |
| 30 | +) |
| 31 | + |
| 32 | +type TestConfig struct { |
| 33 | + utils.CommonConfig |
| 34 | +} |
| 35 | + |
| 36 | +func NewTestConfig(component string, operations []string, configMap map[string]interface{}) (TestConfig, error) { |
| 37 | + testConfig := TestConfig{ |
| 38 | + CommonConfig: utils.CommonConfig{ |
| 39 | + ComponentType: "lock", |
| 40 | + ComponentName: component, |
| 41 | + Operations: utils.NewStringSet(operations...), |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + err := config.Decode(configMap, &testConfig) |
| 46 | + if err != nil { |
| 47 | + return testConfig, err |
| 48 | + } |
| 49 | + |
| 50 | + return testConfig, nil |
| 51 | +} |
| 52 | + |
| 53 | +// ConformanceTests runs conf tests for lock stores. |
| 54 | +func ConformanceTests(t *testing.T, props map[string]string, lockstore lock.Store, config TestConfig) { |
| 55 | + // Test vars |
| 56 | + key := strings.ReplaceAll(uuid.New().String(), "-", "") |
| 57 | + t.Logf("Base key for test: %s", key) |
| 58 | + |
| 59 | + t.Run("init", func(t *testing.T) { |
| 60 | + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| 61 | + defer cancel() |
| 62 | + err := lockstore.InitLockStore(ctx, lock.Metadata{Base: metadata.Base{ |
| 63 | + Properties: props, |
| 64 | + }}) |
| 65 | + require.NoError(t, err) |
| 66 | + }) |
| 67 | + |
| 68 | + // Don't run more tests if init failed |
| 69 | + if t.Failed() { |
| 70 | + t.Fatal("Init failed, stopping further tests") |
| 71 | + } |
| 72 | + |
| 73 | + const lockOwner = "conftest" |
| 74 | + lockKey1 := key + "-1" |
| 75 | + lockKey2 := key + "-2" |
| 76 | + |
| 77 | + var expirationCh *time.Timer |
| 78 | + |
| 79 | + t.Run("TryLock", func(t *testing.T) { |
| 80 | + // Acquire a lock |
| 81 | + t.Run("acquire lock1", func(t *testing.T) { |
| 82 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 83 | + defer cancel() |
| 84 | + res, err := lockstore.TryLock(ctx, &lock.TryLockRequest{ |
| 85 | + ResourceID: lockKey1, |
| 86 | + LockOwner: lockOwner, |
| 87 | + ExpiryInSeconds: 15, |
| 88 | + }) |
| 89 | + require.NoError(t, err) |
| 90 | + require.NotNil(t, res) |
| 91 | + assert.True(t, res.Success) |
| 92 | + }) |
| 93 | + |
| 94 | + // Acquire a second lock (with a shorter expiration) |
| 95 | + t.Run("acquire lock2", func(t *testing.T) { |
| 96 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 97 | + defer cancel() |
| 98 | + res, err := lockstore.TryLock(ctx, &lock.TryLockRequest{ |
| 99 | + ResourceID: lockKey2, |
| 100 | + LockOwner: lockOwner, |
| 101 | + ExpiryInSeconds: 3, |
| 102 | + }) |
| 103 | + require.NoError(t, err) |
| 104 | + require.NotNil(t, res) |
| 105 | + assert.True(t, res.Success) |
| 106 | + |
| 107 | + // Set expirationCh to when lock2 expires |
| 108 | + expirationCh = time.NewTimer(3 * time.Second) |
| 109 | + }) |
| 110 | + |
| 111 | + // Acquiring the same lock again should fail |
| 112 | + t.Run("fails to acquire existing lock", func(t *testing.T) { |
| 113 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 114 | + defer cancel() |
| 115 | + res, err := lockstore.TryLock(ctx, &lock.TryLockRequest{ |
| 116 | + ResourceID: lockKey1, |
| 117 | + LockOwner: lockOwner, |
| 118 | + ExpiryInSeconds: 15, |
| 119 | + }) |
| 120 | + require.NoError(t, err) |
| 121 | + require.NotNil(t, res) |
| 122 | + assert.False(t, res.Success) |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + t.Run("Unlock", func(t *testing.T) { |
| 127 | + t.Run("fails to unlock with nonexistent resource ID", func(t *testing.T) { |
| 128 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 129 | + defer cancel() |
| 130 | + res, err := lockstore.Unlock(ctx, &lock.UnlockRequest{ |
| 131 | + ResourceID: "nonexistent", |
| 132 | + LockOwner: lockOwner, |
| 133 | + }) |
| 134 | + require.NoError(t, err) |
| 135 | + require.NotNil(t, res) |
| 136 | + assert.Equal(t, lock.LockDoesNotExist, res.Status) |
| 137 | + }) |
| 138 | + |
| 139 | + t.Run("fails to unlock with wrong owner", func(t *testing.T) { |
| 140 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 141 | + defer cancel() |
| 142 | + res, err := lockstore.Unlock(ctx, &lock.UnlockRequest{ |
| 143 | + ResourceID: lockKey1, |
| 144 | + LockOwner: "nonowner", |
| 145 | + }) |
| 146 | + require.NoError(t, err) |
| 147 | + require.NotNil(t, res) |
| 148 | + assert.Equal(t, lock.LockBelongsToOthers, res.Status) |
| 149 | + }) |
| 150 | + |
| 151 | + t.Run("unlocks successfully", func(t *testing.T) { |
| 152 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 153 | + defer cancel() |
| 154 | + res, err := lockstore.Unlock(ctx, &lock.UnlockRequest{ |
| 155 | + ResourceID: lockKey1, |
| 156 | + LockOwner: lockOwner, |
| 157 | + }) |
| 158 | + require.NoError(t, err) |
| 159 | + require.NotNil(t, res) |
| 160 | + assert.Equal(t, lock.Success, res.Status) |
| 161 | + }) |
| 162 | + }) |
| 163 | + |
| 164 | + t.Run("lock expires", func(t *testing.T) { |
| 165 | + // Wait until the lock is supposed to expire |
| 166 | + <-expirationCh.C |
| 167 | + |
| 168 | + // Assert that the lock doesn't exist anymore - we should be able to re-acquire it |
| 169 | + assert.Eventually(t, func() bool { |
| 170 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 171 | + defer cancel() |
| 172 | + res, err := lockstore.TryLock(ctx, &lock.TryLockRequest{ |
| 173 | + ResourceID: lockKey2, |
| 174 | + LockOwner: lockOwner, |
| 175 | + ExpiryInSeconds: 3, |
| 176 | + }) |
| 177 | + return err == nil && res != nil && res.Success |
| 178 | + }, 5*time.Second, 100*time.Millisecond, "Lock 2 was not released in time after its scheduled expiration") |
| 179 | + }) |
| 180 | +} |
0 commit comments