Skip to content

Commit

Permalink
Add a test for canceling resources with a timeout
Browse files Browse the repository at this point in the history
When I was answering
#261 (comment), I was
looking at our implementation of `Cancel`. I noticed there wasn't a test for this edge
case, and added one.
  • Loading branch information
iwahbe committed Feb 28, 2025
1 parent 3726327 commit 56c9bae
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,34 @@ func TestCancelTimeout(t *testing.T) {
assert.ErrorIs(t, err, context.DeadlineExceeded)
})
}

// Test that a `Cancel` call will cancel in-flight operations, even if they already have a
// timeout associated with them.
func TestCancelCreateWithTimeout(t *testing.T) {
t.Parallel()

// Used to block until the create call has started.
//
// We use this because we want to ensure that our request is in-flight when it is
// canceled.
hasCreated := make(chan bool)

s := integration.NewServer("cancel", semver.MustParse("1.2.3"), cancel.Wrap(p.Provider{
Create: func(ctx context.Context, _ p.CreateRequest) (p.CreateResponse, error) {
hasCreated <- true
<-ctx.Done()
return p.CreateResponse{}, ctx.Err()
},
}))

go func() {
<-hasCreated
err := s.Cancel()
require.NoError(t, err)
}()

_, err := s.Create(p.CreateRequest{
Timeout: 1,
})
assert.ErrorIs(t, err, context.Canceled)
}

0 comments on commit 56c9bae

Please sign in to comment.