| 110 | } |
| 111 | |
| 112 | func TestLockBlocksUntilUnlocked(t *testing.T) { |
| 113 | lockPath := filepath.Join(t.TempDir(), "contended.lock") |
| 114 | |
| 115 | _, unlock, err := flock.TryLock(lockPath) |
| 116 | require.NoError(t, err) |
| 117 | |
| 118 | acquired := make(chan struct{}) |
| 119 | go func() { |
| 120 | _, unlock2, err := flock.Lock(t.Context(), lockPath) |
| 121 | assert.NoError(t, err) |
| 122 | close(acquired) |
| 123 | unlock2() |
| 124 | }() |
| 125 | |
| 126 | select { |
| 127 | case <-acquired: |
| 128 | t.Fatal("Lock returned while the lock was still held") |
| 129 | case <-time.After(100 * time.Millisecond): |
| 130 | } |
| 131 | |
| 132 | unlock() |
| 133 | |
| 134 | select { |
| 135 | case <-acquired: |
| 136 | case <-time.After(2 * time.Second): |
| 137 | t.Fatal("Lock did not acquire after the holder released it") |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestLockReturnsNonContentionError(t *testing.T) { |
| 142 | lockPath := filepath.Join(t.TempDir(), "no", "such", "dir", "test.lock") |