(t *testing.T)
| 226 | } |
| 227 | |
| 228 | func TestLockWriteBlocksRead(t *testing.T) { |
| 229 | t.Parallel() |
| 230 | |
| 231 | var waitGroup sync.WaitGroup |
| 232 | |
| 233 | var concurrentKey uint32 |
| 234 | |
| 235 | tempDir := t.TempDir() |
| 236 | |
| 237 | waitGroup.Add(2) |
| 238 | |
| 239 | // Start a lock, set the key, sleep 1s and confirm the key is still the same |
| 240 | go func() { |
| 241 | defer waitGroup.Done() |
| 242 | |
| 243 | lErr := filesystem.WithLock(tempDir, func() error { |
| 244 | time.Sleep(1 * time.Second) |
| 245 | |
| 246 | atomic.StoreUint32(&concurrentKey, routine1) |
| 247 | |
| 248 | assert.Equal(t, atomic.LoadUint32(&concurrentKey), routine1) |
| 249 | |
| 250 | return nil |
| 251 | }) |
| 252 | |
| 253 | assert.NilError(t, lErr, "locking should not error") |
| 254 | }() |
| 255 | |
| 256 | time.Sleep(50 * time.Millisecond) |
| 257 | |
| 258 | // Start a readonly lock immediately |
| 259 | // Confirm the key has been set by the write lock |
| 260 | go func() { |
| 261 | defer waitGroup.Done() |
| 262 | |
| 263 | lErr := filesystem.WithReadOnlyLock(tempDir, func() error { |
| 264 | assert.Equal(t, atomic.LoadUint32(&concurrentKey), routine1) |
| 265 | |
| 266 | return nil |
| 267 | }) |
| 268 | |
| 269 | assert.NilError(t, lErr, "locking should not error") |
| 270 | }() |
| 271 | |
| 272 | waitGroup.Wait() |
| 273 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…