From af2e289212a2c9b838de38a4bd652634b8c27ada Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 3 Apr 2017 14:26:05 -0400 Subject: [PATCH] remove Sleep from TestLockWithContext --- state/state.go | 7 +++++++ state/state_test.go | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/state/state.go b/state/state.go index 67cf91e61..f6c4f16d4 100644 --- a/state/state.go +++ b/state/state.go @@ -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(): diff --git a/state/state_test.go b/state/state_test.go index df7a6fd05..a8fdec6ab 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -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) }