Skip to content

Commit

Permalink
add test for proper cancelation
Browse files Browse the repository at this point in the history
  • Loading branch information
jbardin committed Apr 19, 2017
1 parent eb4b459 commit 14bea66
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions helper/resource/state_test.go
Expand Up @@ -171,6 +171,62 @@ func TestWaitForState_timeout(t *testing.T) {
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
}

if obj != nil {
t.Fatalf("should not return obj")
}
}

// Make sure a timeout actually cancels the refresh goroutine and waits for its
// return.
func TestWaitForState_cancel(t *testing.T) {
// make this refresh func block until we cancel it
cancel := make(chan struct{})
refresh := func() (interface{}, string, error) {
<-cancel
return nil, "pending", nil
}
conf := &StateChangeConf{
Pending: []string{"pending", "incomplete"},
Target: []string{"running"},
Refresh: refresh,
Timeout: 10 * time.Millisecond,
PollInterval: 10 * time.Second,
}

var obj interface{}
var err error

waitDone := make(chan struct{})
go func() {
defer close(waitDone)
obj, err = conf.WaitForState()
}()

// make sure WaitForState is blocked
select {
case <-waitDone:
t.Fatal("WaitForState returned too early")
case <-time.After(10 * time.Millisecond):
}

// unlock the refresh function
close(cancel)
// make sure WaitForState returns
select {
case <-waitDone:
case <-time.After(time.Second):
t.Fatal("WaitForState didn't return after refresh finished")
}

if err == nil {
t.Fatal("Expected timeout error. No error returned.")
}

expectedErr := "timeout while waiting for state to become 'running'"
if !strings.HasPrefix(err.Error(), expectedErr) {
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
}

if obj != nil {
t.Fatalf("should not return obj")
}
Expand Down

0 comments on commit 14bea66

Please sign in to comment.