Skip to content

Commit 8cc816f

Browse files
authored
fix(sshd): re-enable TestConcurrentPushSameRepo (#361)
Fixes deis/builder#250 Also fixes intermittent races in tests for wrapInLock
1 parent c90d40b commit 8cc816f

2 files changed

Lines changed: 21 additions & 31 deletions

File tree

pkg/sshd/lock_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const (
1414
)
1515

1616
var (
17-
gitError = errors.New("git receive error")
17+
errGitReceive = errors.New("git receive error")
1818
)
1919

2020
func TestMultipleSameRepoLocks(t *testing.T) {
@@ -92,15 +92,20 @@ func TestDoubleLockUnlock(t *testing.T) {
9292
}
9393

9494
func TestWrapInLock(t *testing.T) {
95-
lck := NewInMemoryRepositoryLock(0)
96-
assert.NoErr(t, wrapInLock(lck, "repo", func() error {
95+
const repoName = "repo"
96+
lck := NewInMemoryRepositoryLock(100 * time.Second)
97+
assert.NoErr(t, wrapInLock(lck, repoName, func() error {
9798
return nil
9899
}))
99-
assert.Err(t, gitError, wrapInLock(lck, "repo", func() error {
100-
return gitError
100+
assert.NoErr(t, lck.Lock(repoName))
101+
assert.Err(t, errAlreadyLocked, wrapInLock(lck, repoName, func() error {
102+
return errGitReceive
103+
}))
104+
assert.Err(t, errAlreadyLocked, wrapInLock(lck, repoName, func() error {
105+
return nil
101106
}))
102-
assert.NoErr(t, lck.Lock("repo"))
103-
assert.Err(t, errAlreadyLocked, wrapInLock(lck, "repo", func() error {
107+
assert.NoErr(t, lck.Unlock(repoName))
108+
assert.NoErr(t, wrapInLock(lck, repoName, func() error {
104109
return nil
105110
}))
106111
}

pkg/sshd/server_test.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ func TestPushInvalidArgsLength(t *testing.T) {
142142

143143
// TestConcurrentPushSameRepo tests many concurrent pushes, each to the same repo
144144
func TestConcurrentPushSameRepo(t *testing.T) {
145-
t.Skip("skipping because the global lock prevents testing the repository lock, for multiple concurrent pushes to the same repo")
146-
t.SkipNow()
147145
const testingServerAddr = "127.0.0.1:2245"
148146
key, err := sshTestingHostKey()
149147
assert.NoErr(t, err)
@@ -153,7 +151,7 @@ func TestConcurrentPushSameRepo(t *testing.T) {
153151
cfg.AddHostKey(key)
154152

155153
c := NewCircuit()
156-
pushLock := NewInMemoryRepositoryLock(0)
154+
pushLock := NewInMemoryRepositoryLock(500 * time.Millisecond)
157155
runServer(cfg, c, pushLock, testingServerAddr, 2*time.Second, t)
158156

159157
// Give server time to initialize.
@@ -165,45 +163,32 @@ func TestConcurrentPushSameRepo(t *testing.T) {
165163
client, err := ssh.Dial("tcp", testingServerAddr, clientConfig())
166164
assert.NoErr(t, err)
167165

168-
const numPushers = 10
166+
const numPushers = 4
169167
outCh := make(chan *sshSessionOutput, numPushers)
170168
for i := 0; i < numPushers; i++ {
171169
go func() {
172-
sess, err := client.NewSession()
173-
assert.NoErr(t, err)
170+
sess, newSessErr := client.NewSession()
171+
assert.NoErr(t, newSessErr)
174172
defer sess.Close()
175-
out, err := sess.Output("git-upload-pack /demo.git")
176-
outCh <- &sshSessionOutput{outStr: string(out), err: err}
173+
out, outErr := sess.Output("git-upload-pack /demo.git")
174+
outCh <- &sshSessionOutput{outStr: string(out), err: outErr}
177175
}()
178176
}
179177

178+
// ensure at least 1 output was successful
180179
foundOK := false
181180
to := 1 * time.Second
182-
multiPushLine, err := gitPktLineStr(multiplePush)
183-
assert.NoErr(t, err)
184181
for i := 0; i < numPushers; i++ {
185182
select {
186183
case sessOut := <-outCh:
187-
output := sessOut.outStr
188-
err := sessOut.err
189-
if output != multiPushLine && output != "OK" {
190-
t.Fatalf("expected 'OK' or '%s', but got '%s' (error '%s')", multiPushLine, output, err)
191-
}
192-
193-
if sessOut.err != nil {
194-
t.Fatalf("found '%s' output with an error '%s'", output, err)
195-
}
196-
197-
if !foundOK && output == "OK" {
184+
if sessOut.outStr == "OK" {
198185
foundOK = true
199-
} else if output == "OK" {
200-
t.Fatalf("found second 'OK' when shouldn't have")
201186
}
202-
203187
case <-time.After(to):
204188
t.Fatalf("didn't receive an output within %s", to)
205189
}
206190
}
191+
assert.True(t, foundOK, "no SSH requests were successful")
207192
}
208193

209194
// TestConcurrentPushDifferentRepo tests many concurrent pushes, each to a different repo

0 commit comments

Comments
 (0)