remove Sleep from TestLockWithContext
This commit is contained in:
parent
3d604851c2
commit
af2e289212
|
@ -74,6 +74,9 @@ type Locker interface {
|
||||||
Unlock(id string) error
|
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.
|
// Lock the state, using the provided context for timeout and cancellation.
|
||||||
// This backs off slightly to an upper limit.
|
// This backs off slightly to an upper limit.
|
||||||
func LockWithContext(ctx context.Context, s State, info *LockInfo) (string, error) {
|
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)
|
return "", fmt.Errorf("lock error missing ID: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if postLockHook != nil {
|
||||||
|
postLockHook()
|
||||||
|
}
|
||||||
|
|
||||||
// there's an existing lock, wait and try again
|
// there's an existing lock, wait and try again
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
|
|
@ -74,11 +74,18 @@ func TestLockWithContext(t *testing.T) {
|
||||||
t.Fatal("lock should have failed immediately")
|
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
|
// unlock the state during LockWithContext
|
||||||
unlocked := make(chan struct{})
|
unlocked := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
defer close(unlocked)
|
defer close(unlocked)
|
||||||
time.Sleep(500 * time.Millisecond)
|
<-attempted
|
||||||
if err := s.Unlock(id); err != nil {
|
if err := s.Unlock(id); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue