Skip to content

Commit

Permalink
fix(service wait): Wrong check for error window
Browse files Browse the repository at this point in the history
The error window introduced with knative#644 had a wrong conditional. Fixed that and added a test which would have detected this.

Also, this should fix some issues which we tried to detect with knative#659.
  • Loading branch information
rhuss committed Feb 12, 2020
1 parent 3019f68 commit 0eab471
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
8 changes: 5 additions & 3 deletions pkg/wait/wait_for_ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type waitForReadyConfig struct {
type WaitForReady interface {

// Wait on resource the resource with this name until a given timeout
// and write event messages for unknown event to the status writer
// and write event messages for unknown event to the status writer.
// Returns an error (if any) and the overall time it took to wait
Wait(name string, timeout time.Duration, msgCallback MessageCallback) (error, time.Duration)
}

Expand Down Expand Up @@ -161,9 +162,10 @@ func (w *waitForReadyConfig) waitForReadyCondition(start time.Time, name string,
// to a true condition even after the condition went to false. If this is not the case within
// this window, then an error is returned.
// If there is already a timer running, we just log.
if errorTimer != nil {
if errorTimer == nil {
err := fmt.Errorf("%s: %s", cond.Reason, cond.Message)
errorTimer = time.AfterFunc(errorWindow, func() {
errChan <- fmt.Errorf("%s: %s", cond.Reason, cond.Message)
errChan <- err
})
}
}
Expand Down
50 changes: 30 additions & 20 deletions pkg/wait/wait_for_ready_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
type waitForReadyTestCase struct {
events []watch.Event
timeout time.Duration
errorExpected bool
errorText string
messagesExpected []string
}

Expand All @@ -54,12 +54,16 @@ func TestAddWaitForReady(t *testing.T) {
})
close(fakeWatchApi.eventChan)

if !tc.errorExpected && err != nil {
if tc.errorText == "" && err != nil {
t.Errorf("%d: Error received %v", i, err)
continue
}
if tc.errorExpected && err == nil {
t.Errorf("%d: No error but expected one", i)
if tc.errorText != "" {
if err == nil {
t.Errorf("%d: No error but expected one", i)
} else {
assert.ErrorContains(t, err, tc.errorText)
}
}

// check messages
Expand All @@ -75,20 +79,34 @@ func TestAddWaitForReady(t *testing.T) {
// Test cases which consists of a series of events to send and the expected behaviour.
func prepareTestCases(name string) []waitForReadyTestCase {
return []waitForReadyTestCase{
tc(peNormal, name, false),
tc(peError, name, true),
tc(peWrongGeneration, name, true),
tc(peTimeout, name, true),
tc(peReadyFalseWithinErrorWindow, name, false),
errorTest(name),
tc(peNormal, name, time.Second, ""),
tc(peWrongGeneration, name, 1*time.Second, "timeout"),
tc(peTimeout, name, time.Second, "timeout"),
tc(peReadyFalseWithinErrorWindow, name, time.Second, ""),
}
}

func tc(f func(name string) (evts []watch.Event, nrMessages int), name string, isError bool) waitForReadyTestCase {
func errorTest(name string) waitForReadyTestCase {
events := []watch.Event{
{watch.Added, CreateTestServiceWithConditions(name, corev1.ConditionUnknown, corev1.ConditionUnknown, "", "msg1")},
{watch.Modified, CreateTestServiceWithConditions(name, corev1.ConditionFalse, corev1.ConditionTrue, "FakeError", "Test Error")},
}

return waitForReadyTestCase{
events: events,
timeout: 3 * time.Second,
errorText: "FakeError",
messagesExpected: []string{"msg1", "Test Error"},
}
}

func tc(f func(name string) (evts []watch.Event, nrMessages int), name string, timeout time.Duration, errorTxt string) waitForReadyTestCase {
events, nrMsgs := f(name)
return waitForReadyTestCase{
events,
time.Second,
isError,
timeout,
errorTxt,
pMessages(nrMsgs),
}
}
Expand All @@ -110,14 +128,6 @@ func peNormal(name string) ([]watch.Event, int) {
}, len(messages)
}

func peError(name string) ([]watch.Event, int) {
messages := pMessages(1)
return []watch.Event{
{watch.Added, CreateTestServiceWithConditions(name, corev1.ConditionUnknown, corev1.ConditionUnknown, "", messages[0])},
{watch.Modified, CreateTestServiceWithConditions(name, corev1.ConditionFalse, corev1.ConditionTrue, "FakeError", "")},
}, len(messages)
}

func peTimeout(name string) ([]watch.Event, int) {
messages := pMessages(1)
return []watch.Event{
Expand Down

0 comments on commit 0eab471

Please sign in to comment.