adjust the inconsistent_negative test to match

This test unfortunately relies on the timing of the loops in
WaitForState, and the text of the error message. Adjust the timing so
the timeout isn't an even multiple of the poll interval, and make sure
we reach a minimum number of retries.
This commit is contained in:
James Bardin 2017-04-19 10:10:54 -04:00
parent af5e22cf94
commit 6601b9b8dd
1 changed files with 22 additions and 5 deletions

View File

@ -2,6 +2,8 @@ package resource
import ( import (
"errors" "errors"
"strings"
"sync/atomic"
"testing" "testing"
"time" "time"
) )
@ -109,11 +111,18 @@ func TestWaitForState_inconsistent_positive(t *testing.T) {
} }
func TestWaitForState_inconsistent_negative(t *testing.T) { func TestWaitForState_inconsistent_negative(t *testing.T) {
refreshCount := int64(0)
f := InconsistentStateRefreshFunc()
refresh := func() (interface{}, string, error) {
atomic.AddInt64(&refreshCount, 1)
return f()
}
conf := &StateChangeConf{ conf := &StateChangeConf{
Pending: []string{"replicating"}, Pending: []string{"replicating"},
Target: []string{"done"}, Target: []string{"done"},
Refresh: InconsistentStateRefreshFunc(), Refresh: refresh,
Timeout: 90 * time.Millisecond, Timeout: 85 * time.Millisecond,
PollInterval: 10 * time.Millisecond, PollInterval: 10 * time.Millisecond,
ContinuousTargetOccurence: 4, ContinuousTargetOccurence: 4,
} }
@ -123,9 +132,17 @@ func TestWaitForState_inconsistent_negative(t *testing.T) {
if err == nil { if err == nil {
t.Fatal("Expected timeout error. No error returned.") t.Fatal("Expected timeout error. No error returned.")
} }
expectedErr := "timeout while waiting for state to become 'done' (last state: 'done', timeout: 90ms)"
if err.Error() != expectedErr { // we can't guarantee the exact number of refresh calls in the tests by
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error()) // timing them, but we want to make sure the test at least went through th
// required states.
if atomic.LoadInt64(&refreshCount) < 6 {
t.Fatal("refreshed called too few times")
}
expectedErr := "timeout while waiting for state to become 'done'"
if !strings.HasPrefix(err.Error(), expectedErr) {
t.Fatalf("error prefix doesn't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
} }
} }