Skip to content

Commit 29f3669

Browse files
author
Aaron Schlesinger
committed
fix(pkg/sshd/lock_test.go): add a test for locking and unlocking repo locks many times
1 parent 096a32a commit 29f3669

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

pkg/sshd/lock_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,62 @@
11
package sshd
22

33
import (
4+
"errors"
5+
"sync"
46
"testing"
57
"time"
8+
9+
"github.com/arschles/assert"
610
)
711

812
const (
913
callbackTimeout = 1 * time.Second
1014
)
1115

16+
var (
17+
errWGTimedOut = errors.New("waitgroup wait timed out")
18+
)
19+
20+
// waitWithTimeout waits for wg.Done() until timeout expires. returns errWGTimedOut if timeout expired before wg.Done() returned, otherwise returns nil. this func is naturally leaky if wg.Done() never returns
21+
func waitWithTimeout(wg *sync.WaitGroup, timeout time.Duration) error {
22+
ch := make(chan struct{})
23+
go func() {
24+
defer close(ch)
25+
wg.Wait()
26+
}()
27+
select {
28+
case <-time.After(timeout):
29+
return errWGTimedOut
30+
case <-ch:
31+
return nil
32+
}
33+
}
34+
35+
func TestMultipleSameRepoLocks(t *testing.T) {
36+
var wg sync.WaitGroup
37+
const repo = "repo1"
38+
const numTries = 100
39+
lck := NewInMemoryRepositoryLock()
40+
assert.NoErr(t, lck.Lock(repo, 0*time.Second))
41+
for i := 0; i < numTries; i++ {
42+
wg.Add(1)
43+
go func() {
44+
defer wg.Done()
45+
assert.True(t, lck.Lock(repo, 0*time.Second) != nil, "lock of already locked repo should return error")
46+
}()
47+
}
48+
assert.NoErr(t, waitWithTimeout(&wg, 1*time.Second))
49+
assert.NoErr(t, lck.Unlock(repo, 0*time.Second))
50+
for i := 0; i < numTries; i++ {
51+
wg.Add(1)
52+
go func() {
53+
defer wg.Done()
54+
assert.True(t, lck.Unlock(repo, 0*time.Second) != nil, "unlock of already unlocked repo should return error")
55+
}()
56+
}
57+
assert.NoErr(t, waitWithTimeout(&wg, 1*time.Second))
58+
}
59+
1260
func TestSingleLock(t *testing.T) {
1361
rl := NewInMemoryRepositoryLock()
1462
key := "fakeid"

0 commit comments

Comments
 (0)