| 163 | } |
| 164 | |
| 165 | func TestFileStoreConcurrent(t *testing.T) { |
| 166 | tempDir := t.TempDir() |
| 167 | |
| 168 | // Creation |
| 169 | tempStore, err := New(tempDir, 0, 0) |
| 170 | assert.NilError(t, err, "temporary store creation should succeed") |
| 171 | |
| 172 | go func() { |
| 173 | lErr := tempStore.WithLock(func() error { |
| 174 | err := tempStore.Set([]byte("routine 1"), "concurrentkey") |
| 175 | assert.NilError(t, err, "writing should not error") |
| 176 | time.Sleep(1 * time.Second) |
| 177 | result, err := tempStore.Get("concurrentkey") |
| 178 | assert.NilError(t, err, "reading should not error") |
| 179 | assert.Assert(t, string(result) == "routine 1") |
| 180 | return nil |
| 181 | }) |
| 182 | assert.NilError(t, lErr, "locking should not error") |
| 183 | }() |
| 184 | |
| 185 | go func() { |
| 186 | time.Sleep(500 * time.Millisecond) |
| 187 | lErr := tempStore.WithLock(func() error { |
| 188 | err := tempStore.Set([]byte("routine 2"), "concurrentkey") |
| 189 | assert.NilError(t, err, "writing should not error") |
| 190 | time.Sleep(1 * time.Second) |
| 191 | result, err := tempStore.Get("concurrentkey") |
| 192 | assert.NilError(t, err, "reading should not error") |
| 193 | assert.Assert(t, string(result) == "routine 2") |
| 194 | return nil |
| 195 | }) |
| 196 | assert.NilError(t, lErr, "locking should not error") |
| 197 | }() |
| 198 | |
| 199 | lErr := tempStore.WithLock(func() error { |
| 200 | err := tempStore.Set([]byte("main routine 1"), "concurrentkey") |
| 201 | assert.NilError(t, err, "writing should not error") |
| 202 | time.Sleep(1 * time.Second) |
| 203 | result, err := tempStore.Get("concurrentkey") |
| 204 | assert.NilError(t, err, "reading should not error") |
| 205 | assert.Assert(t, string(result) == "main routine 1") |
| 206 | return nil |
| 207 | }) |
| 208 | assert.NilError(t, lErr, "locking should not error") |
| 209 | |
| 210 | time.Sleep(750 * time.Millisecond) |
| 211 | |
| 212 | lErr = tempStore.WithLock(func() error { |
| 213 | err := tempStore.Set([]byte("main routine 2"), "concurrentkey") |
| 214 | assert.NilError(t, err, "writing should not error") |
| 215 | time.Sleep(1 * time.Second) |
| 216 | result, err := tempStore.Get("concurrentkey") |
| 217 | assert.NilError(t, err, "reading should not error") |
| 218 | assert.Assert(t, string(result) == "main routine 2") |
| 219 | return nil |
| 220 | }) |
| 221 | assert.NilError(t, lErr, "locking should not error") |
| 222 | } |