Skip to content

Commit

Permalink
fixes nil ptr panic bug (#207)
Browse files Browse the repository at this point in the history
Co-authored-by: Andreas Neumann <aneumann@mesosphere.com>
Signed-off-by: Ken Sipe <kensipe@gmail.com>
  • Loading branch information
kensipe and ANeumann82 authored Aug 25, 2020
1 parent ec93024 commit 4908507
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,26 @@ func Retry(ctx context.Context, fn func(context.Context) error, errValidationFun
errCh := make(chan error)
doneCh := make(chan struct{})

if fn == nil {
log.Println("retry func is nil and will be ignored")
return nil
}

// do { } while (err != nil): https://stackoverflow.com/a/32844744/10892393
for ok := true; ok; ok = lastErr != nil {
// run the function in a goroutine and close it once it is finished so that
// we can use select to wait for both the function return and the context deadline.

go func() {
// if the func we are calling panics, clean up and call it done
// the common case is when a shared reference, like a client, is nil and is called in the function
defer func() {
if r := recover(); r != nil {
//log.Println("retry func has panicked and will be ignored")
errCh <- errors.New("func passed to retry panicked. expected if testsuite is shutting down")
}
}()

if err := fn(ctx); err != nil {
errCh <- err
} else {
Expand Down
18 changes: 18 additions & 0 deletions pkg/test/utils/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ func TestRetryWithUnexpectedError(t *testing.T) {
assert.Equal(t, 1, index)
}

func TestRetryWithNil(t *testing.T) {
assert.Equal(t, nil, Retry(context.TODO(), nil, IsJSONSyntaxError))
}

func TestRetryWithNilFromFn(t *testing.T) {
assert.Equal(t, nil, Retry(context.TODO(), func(ctx context.Context) error {
return nil
}, IsJSONSyntaxError))
}

func TestRetryWithNilInFn(t *testing.T) {
client := RetryClient{}
var list runtime.Object
assert.Error(t, Retry(context.TODO(), func(ctx context.Context) error {
return client.Client.List(ctx, list)
}, IsJSONSyntaxError))
}

func TestRetryWithTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
Expand Down

0 comments on commit 4908507

Please sign in to comment.