-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
115 lines (103 loc) · 2.98 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// File generated from our OpenAPI spec by Stainless.
package acme_test
import (
"context"
"fmt"
"net/http"
"os"
"testing"
"time"
"github.com/acme/acme-go"
"github.com/acme/acme-go/internal/testutil"
"github.com/acme/acme-go/option"
)
func TestContextCancel(t *testing.T) {
baseURL := "http://localhost:4010"
if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
baseURL = envURL
}
if !testutil.CheckTestServer(t, baseURL) {
return
}
client := acme.NewClient(
option.WithBaseURL(baseURL),
option.WithAPIKey("My API Key"),
)
cancelCtx, cancel := context.WithCancel(context.Background())
cancel()
res, err := client.Accounts.New(cancelCtx, acme.AccountNewParams{
Name: acme.F("My First Acme Account"),
})
if err == nil || res != nil {
t.Error("Expected there to be a cancel error and for the response to be nil")
}
}
// neverTransport never completes a request and waits for the Context to be done.
type neverTransport struct{}
func (t *neverTransport) RoundTrip(req *http.Request) (*http.Response, error) {
<-req.Context().Done()
return nil, fmt.Errorf("cancelled")
}
func TestContextCancelDelay(t *testing.T) {
baseURL := "http://localhost:4010"
if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
baseURL = envURL
}
if !testutil.CheckTestServer(t, baseURL) {
return
}
client := acme.NewClient(
option.WithBaseURL(baseURL),
option.WithAPIKey("My API Key"),
option.WithHTTPClient(&http.Client{Transport: &neverTransport{}}),
)
cancelCtx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(time.Millisecond * time.Duration(2))
cancel()
}()
res, err := client.Accounts.New(cancelCtx, acme.AccountNewParams{
Name: acme.F("My First Acme Account"),
})
if err == nil || res != nil {
t.Error("expected there to be a cancel error and for the response to be nil")
}
}
func TestContextDeadline(t *testing.T) {
baseURL := "http://localhost:4010"
if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
baseURL = envURL
}
if !testutil.CheckTestServer(t, baseURL) {
return
}
testTimeout := time.After(3 * time.Second)
testDone := make(chan bool)
deadline := time.Now().Add(100 * time.Millisecond)
deadlineCtx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
go func() {
client := acme.NewClient(
option.WithBaseURL(baseURL),
option.WithAPIKey("My API Key"),
option.WithHTTPClient(&http.Client{Transport: &neverTransport{}}),
)
res, err := client.Accounts.New(deadlineCtx, acme.AccountNewParams{
Name: acme.F("My First Acme Account"),
})
if err == nil || res != nil {
t.Error("expected there to be a deadline error and for the response to be nil")
}
testDone <- true
}()
select {
case <-testTimeout:
t.Fatal("client didn't finish in time")
case <-testDone:
diff := time.Now().Sub(deadline)
if diff < -20*time.Millisecond || 20*time.Millisecond < diff {
t.Logf("error difference: %v", diff)
t.Fatal("client did not return within 20ms of context deadline")
}
}
}