Skip to content

Commit 6a34b3e

Browse files
author
Aaron Schlesinger
committed
fix(pkg/sshd): move utility func to common file
1 parent 29f3669 commit 6a34b3e

2 files changed

Lines changed: 26 additions & 20 deletions

File tree

pkg/sshd/common_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package sshd
2+
3+
import (
4+
"errors"
5+
"sync"
6+
"time"
7+
)
8+
9+
var (
10+
errWGTimedOut = errors.New("waitgroup wait timed out")
11+
)
12+
13+
// 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
14+
func waitWithTimeout(wg *sync.WaitGroup, timeout time.Duration) error {
15+
ch := make(chan struct{})
16+
go func() {
17+
defer close(ch)
18+
wg.Wait()
19+
}()
20+
select {
21+
case <-time.After(timeout):
22+
return errWGTimedOut
23+
case <-ch:
24+
return nil
25+
}
26+
}

pkg/sshd/lock_test.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package sshd
22

33
import (
4-
"errors"
54
"sync"
65
"testing"
76
"time"
@@ -13,25 +12,6 @@ const (
1312
callbackTimeout = 1 * time.Second
1413
)
1514

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-
3515
func TestMultipleSameRepoLocks(t *testing.T) {
3616
var wg sync.WaitGroup
3717
const repo = "repo1"

0 commit comments

Comments
 (0)