remove Sleep from TestLockWithContext
This commit is contained in:
parent
3d604851c2
commit
af2e289212
|
@ -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():
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue