remove Sleep from TestLockWithContext

This commit is contained in:
James Bardin 2017-04-03 14:26:05 -04:00
parent 3d604851c2
commit af2e289212
2 changed files with 15 additions and 1 deletions

View File

@ -74,6 +74,9 @@ type Locker interface {
Unlock(id string) error
}
// test hook to verify that LockWithContext has attempted a lock
var postLockHook func()
// Lock the state, using the provided context for timeout and cancellation.
// This backs off slightly to an upper limit.
func LockWithContext(ctx context.Context, s State, info *LockInfo) (string, error) {
@ -96,6 +99,10 @@ func LockWithContext(ctx context.Context, s State, info *LockInfo) (string, erro
return "", fmt.Errorf("lock error missing ID: %s", err)
}
if postLockHook != nil {
postLockHook()
}
// there's an existing lock, wait and try again
select {
case <-ctx.Done():

View File

@ -74,11 +74,18 @@ func TestLockWithContext(t *testing.T) {
t.Fatal("lock should have failed immediately")
}
// block until LockwithContext has made a first attempt
attempted := make(chan struct{})
postLockHook = func() {
close(attempted)
postLockHook = nil
}
// unlock the state during LockWithContext
unlocked := make(chan struct{})
go func() {
defer close(unlocked)
time.Sleep(500 * time.Millisecond)
<-attempted
if err := s.Unlock(id); err != nil {
t.Fatal(err)
}