-
Notifications
You must be signed in to change notification settings - Fork 19
/
senders_test.go
39 lines (31 loc) · 912 Bytes
/
senders_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
package arangolite
import (
"context"
"io"
"net/http"
"testing"
"time"
)
func longRequest(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Waiting 2 seconds...")
time.Sleep(200 * time.Millisecond)
panic("The request was not canceled")
}
func runWebserver() {
go func() {
http.HandleFunc("/", longRequest)
http.ListenAndServe(":9999", nil)
}()
}
func TestSendCanBeCanceled(t *testing.T) {
runWebserver()
client := &http.Client{}
req, _ := http.NewRequest("GET", "http://localhost:9999", nil)
sender := basicSender{}
parent := context.Background()
ctx, cancel := context.WithTimeout(parent, 100*time.Millisecond)
defer cancel()
resp, err := sender.Send(ctx, client, req)
assertEqual(t, err.Error(), "the database HTTP request failed: Get http://localhost:9999: context deadline exceeded")
assertTrue(t, resp == nil, "The response of a canceled request should be nil")
}