add test for proper cancelation

This commit is contained in:
James Bardin 2017-04-19 18:06:41 -04:00
parent eb4b45941c
commit 14bea66f4e
1 changed files with 56 additions and 0 deletions

View File

@ -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")
}