Skip to content

Commit

Permalink
feat(tests): log operation status when calling retryForever
Browse files Browse the repository at this point in the history
To better understand what exactly failed, `retryForever` now requires
that a string that contains a description of the operation be passed.
  • Loading branch information
jeff-held-aiven committed Feb 14, 2024
1 parent e5bbfed commit 8f16516
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion tests/projectvpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestProjectVPCID(t *testing.T) {

// Gets Aiven object
var kafkaAvnUpd *aiven.Service
require.NoError(t, retryForever(ctx, func() (bool, error) {
require.NoError(t, retryForever(ctx, fmt.Sprintf("migrate %s to VPC with ID %s", kafkaName, vpc2.Status.ID), func() (bool, error) {
kafkaAvnUpd, err = avnClient.Services.Get(ctx, testProject, kafkaName)
if err != nil {
return false, err
Expand Down
13 changes: 8 additions & 5 deletions tests/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

const (
retryInterval = time.Second * 5
retryInterval = time.Second * 10
createTimeout = time.Second * 15
waitRunningTimeout = time.Minute * 10
yamlBufferSize = 100
Expand Down Expand Up @@ -111,7 +111,7 @@ func (s *session) GetRunning(obj client.Object, keys ...string) error {
ctx, cancel := context.WithTimeout(s.ctx, waitRunningTimeout)
defer cancel()

return retryForever(ctx, func() (bool, error) {
return retryForever(ctx, fmt.Sprintf("verify %s is running", key), func() (bool, error) {
err := s.k8s.Get(ctx, key, obj)
if err != nil {
// The error is quite verbose
Expand Down Expand Up @@ -190,7 +190,7 @@ func (s *session) delete(o client.Object) error {

// Waits being deleted from kube
key := types.NamespacedName{Name: o.GetName(), Namespace: o.GetNamespace()}
return retryForever(s.ctx, func() (bool, error) {
return retryForever(s.ctx, fmt.Sprintf("delete %s", o.GetName()), func() (bool, error) {
err := s.k8s.Get(s.ctx, key, o)
return !isNotFound(err), nil
})
Expand All @@ -204,17 +204,20 @@ func (s *session) recover() {
}
}

func retryForever(ctx context.Context, f func() (bool, error)) (err error) {
func retryForever(ctx context.Context, operation string, f func() (bool, error)) (err error) {
retry := false
log.Printf("Starting operation: %s\n", operation)

for {
select {
case <-ctx.Done():
return fmt.Errorf("context timeout while retrying operation, error=%q", err)
return fmt.Errorf("context timeout while retrying operation: %s, error=%q", operation, err)
case <-time.After(retryInterval):
retry, err = f()
if retry {
continue
}
log.Printf("Operation %s finished\n", operation)
return err
}
}
Expand Down

0 comments on commit 8f16516

Please sign in to comment.