Skip to content

Commit

Permalink
fix: revert to previous behaviod polling forever
Browse files Browse the repository at this point in the history
The ctx passed into WaitForTaskToSucceed is only used to cancel HTTP reqests and not to cancel the wait.
  • Loading branch information
dkoshkin committed Jan 9, 2024
1 parent 5a4d7c6 commit 76bfd4e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions pkg/client/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ const (
)

// WaitForTaskToSucceed will poll indefinitely every 2 seconds for the task with uuid to have status of "SUCCEEDED".
// Returns an error from GetTaskStatus or a timeout error if the context is cancelled.
// The polling will not stop if the ctx is cancelled, it's only used for HTTP requests in the client.
// WaitForTaskToSucceed will exit immediately on an error getting the task.
func WaitForTaskToSucceed(ctx context.Context, conn *nutanixClientV3.Client, uuid string) error {
return wait.PollImmediateInfiniteWithContext(ctx, pollingInterval, func(ctx context.Context) (done bool, err error) {
return wait.PollImmediateInfinite(pollingInterval, func() (done bool, err error) {
status, getErr := GetTaskStatus(ctx, conn, uuid)
return status == statusSucceeded, getErr
})
Expand Down
11 changes: 7 additions & 4 deletions pkg/client/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/wait"

nutanixtestclient "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/test/helpers/prism-go-client/v3"
)
Expand Down Expand Up @@ -90,7 +89,10 @@ func Test_WaitForTaskCompletion(t *testing.T) {
client.Close()
})

ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
const (
timeout = time.Second * 1
)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
t.Cleanup(func() {
cancel()
})
Expand Down Expand Up @@ -124,10 +126,11 @@ func Test_WaitForTaskCompletion(t *testing.T) {
name: "timeout",
taskUUID: "timeout",
handler: func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"status": "PENDING"}`)
// always wait 1 second longer than the timeout to force the context to cancel
time.Sleep(timeout + time.Second)
},
ctx: ctx,
expectedErr: wait.ErrWaitTimeout,
expectedErr: context.DeadlineExceeded,
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 76bfd4e

Please sign in to comment.