Skip to content

Commit

Permalink
helper: Add StateChangeConf.ContinuousTargetOccurence (int)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored and Radek Simko committed Jan 27, 2016
1 parent 24f10b0 commit b36ca45
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
23 changes: 21 additions & 2 deletions helper/resource/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type StateChangeConf struct {
Timeout time.Duration // The amount of time to wait before timeout
MinTimeout time.Duration // Smallest time to wait before refreshes
NotFoundChecks int // Number of times to allow not found

// This is to work around inconsistent APIs
ContinuousTargetOccurence int // Number of times the Target state has to occur continuously
}

// WaitForState watches an object and waits for it to achieve the state
Expand All @@ -49,12 +52,17 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) {
log.Printf("[DEBUG] Waiting for state to become: %s", conf.Target)

notfoundTick := 0
targetOccurence := 0

// Set a default for times to check for not found
if conf.NotFoundChecks == 0 {
conf.NotFoundChecks = 20
}

if conf.ContinuousTargetOccurence == 0 {
conf.ContinuousTargetOccurence = 1
}

var result interface{}
var resulterr error

Expand Down Expand Up @@ -88,7 +96,12 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) {

// If we're waiting for the absence of a thing, then return
if result == nil && len(conf.Target) == 0 {
return
targetOccurence += 1
if conf.ContinuousTargetOccurence == targetOccurence {
return
} else {
continue
}
}

if result == nil {
Expand All @@ -105,14 +118,20 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) {

for _, allowed := range conf.Target {
if currentState == allowed {
return
targetOccurence += 1
if conf.ContinuousTargetOccurence == targetOccurence {
return
} else {
continue
}
}
}

found := false
for _, allowed := range conf.Pending {
if currentState == allowed {
found = true
targetOccurence = 0
break
}
}
Expand Down
81 changes: 81 additions & 0 deletions helper/resource/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,87 @@ func SuccessfulStateRefreshFunc() StateRefreshFunc {
}
}

type StateGenerator struct {
position int
stateSequence []string
}

func (r *StateGenerator) NextState() (int, string, error) {
p, v := r.position, ""
if len(r.stateSequence)-1 >= p {
v = r.stateSequence[p]
} else {
return -1, "", errors.New("No more states available")
}

r.position += 1

return p, v, nil
}

func NewStateGenerator(sequence []string) *StateGenerator {
r := &StateGenerator{}
r.stateSequence = sequence

return r
}

func InconsistentStateRefreshFunc() StateRefreshFunc {
sequence := []string{
"done", "replicating",
"done", "done", "done",
"replicating",
"done", "done", "done",
}

r := NewStateGenerator(sequence)

return func() (interface{}, string, error) {
idx, s, err := r.NextState()
if err != nil {
return nil, "", err
}

return idx, s, nil
}
}

func TestWaitForState_inconsistent_positive(t *testing.T) {
conf := &StateChangeConf{
Pending: []string{"replicating"},
Target: "done",
Refresh: InconsistentStateRefreshFunc(),
Timeout: 10 * time.Second,
ContinuousTargetOccurence: 3,
}

idx, err := conf.WaitForState()

if err != nil {
t.Fatalf("err: %s", err)
}

if idx != 4 {
t.Fatalf("Expected index 4, given %d", idx.(int))
}
}

func TestWaitForState_inconsistent_negative(t *testing.T) {
conf := &StateChangeConf{
Pending: []string{"replicating"},
Target: "done",
Refresh: InconsistentStateRefreshFunc(),
Timeout: 10 * time.Second,
ContinuousTargetOccurence: 4,
}

_, err := conf.WaitForState()

if err == nil && err.Error() != "timeout while waiting for state to become 'done'" {
t.Fatalf("err: %s", err)
}
}

func TestWaitForState_timeout(t *testing.T) {
conf := &StateChangeConf{
Pending: []string{"pending", "incomplete"},
Expand Down

0 comments on commit b36ca45

Please sign in to comment.