Skip to content

Commit

Permalink
chore(internal): improve reliability of cancel delay test
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Sep 12, 2023
1 parent 91276bc commit 0954143
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package lithic_test

import (
"context"
"fmt"
"net/http"
"testing"
"time"

Expand All @@ -21,15 +23,23 @@ func TestCancel(t *testing.T) {
res, err := client.Cards.New(cancelCtx, lithic.CardNewParams{
Type: lithic.F(lithic.CardNewParamsTypeSingleUse),
})
if err == nil && res != nil {
if err == nil || res != nil {
t.Error("Expected there to be a cancel error and for the response to be nil")
}
}

type neverTransport struct{}

func (t *neverTransport) RoundTrip(req *http.Request) (*http.Response, error) {
<-req.Context().Done()
return nil, fmt.Errorf("cancelled")
}

func TestCancelDelay(t *testing.T) {
client := lithic.NewClient(
option.WithBaseURL("http://127.0.0.1:4010"),
option.WithAPIKey("APIKey"),
option.WithHTTPClient(&http.Client{Transport: &neverTransport{}}),
)
cancelCtx, cancel := context.WithCancel(context.Background())
go func() {
Expand All @@ -39,7 +49,7 @@ func TestCancelDelay(t *testing.T) {
res, err := client.Cards.New(cancelCtx, lithic.CardNewParams{
Type: lithic.F(lithic.CardNewParamsTypeSingleUse),
})
if err == nil && res != nil {
if err == nil || res != nil {
t.Error("Expected there to be a cancel error and for the response to be nil")
}
}

0 comments on commit 0954143

Please sign in to comment.