-
Notifications
You must be signed in to change notification settings - Fork 0
/
memhttp_test.go
159 lines (148 loc) · 3.28 KB
/
memhttp_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package memhttp_test
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"sync"
"testing"
"time"
"go.akshayshah.org/attest"
"go.akshayshah.org/memhttp"
"go.akshayshah.org/memhttp/memhttptest"
)
const greeting = "hello world"
type greeter struct{}
func (h *greeter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(greeting))
}
func TestServer(t *testing.T) {
t.Parallel()
tests := []struct {
name string
opts []memhttp.Option
}{
{"default", nil},
{"plaintext", []memhttp.Option{memhttp.WithoutTLS()}},
{"http1", []memhttp.Option{memhttp.WithoutHTTP2()}},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
const concurrency = 100
srv := memhttptest.New(t, &greeter{}, tt.opts...)
var wg sync.WaitGroup
start := make(chan struct{})
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
defer wg.Done()
client := srv.Client()
req, err := http.NewRequestWithContext(
context.Background(),
http.MethodGet,
srv.URL(),
strings.NewReader(""),
)
attest.Ok(t, err)
<-start
res, err := client.Do(req)
attest.Ok(t, err)
attest.Equal(t, res.StatusCode, http.StatusOK, attest.Continue())
body, err := io.ReadAll(res.Body)
attest.Ok(t, err)
attest.Equal(t, string(body), greeting)
}()
}
close(start)
wg.Wait()
})
}
}
func TestRegisterOnShutdown(t *testing.T) {
t.Parallel()
srv, err := memhttp.New(&greeter{})
attest.Ok(t, err)
done := make(chan struct{})
srv.RegisterOnShutdown(func() {
close(done)
})
attest.Ok(t, srv.Shutdown(context.Background()))
select {
case <-done:
case <-time.After(5 * time.Second):
t.Error("OnShutdown hook didn't fire")
}
}
func Example() {
hello := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello, world!")
})
srv, err := memhttp.New(hello)
if err != nil {
panic(err)
}
defer srv.Close()
res, err := srv.Client().Get(srv.URL())
if err != nil {
panic(err)
}
fmt.Println(res.Status)
// Output:
// 200 OK
}
func ExampleServer_Transport() {
hello := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello, world!")
})
srv, err := memhttp.New(hello)
if err != nil {
panic(err)
}
defer srv.Close()
transport := srv.Transport()
transport.IdleConnTimeout = 10 * time.Second
client := &http.Client{Transport: transport}
res, err := client.Get(srv.URL())
if err != nil {
panic(err)
}
fmt.Println(res.Status)
// Output:
// 200 OK
}
func ExampleServer_Client() {
hello := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello, world!")
})
srv, err := memhttp.New(hello)
if err != nil {
panic(err)
}
defer srv.Close()
client := srv.Client()
client.Timeout = 10 * time.Second
res, err := client.Get(srv.URL())
if err != nil {
panic(err)
}
fmt.Println(res.Status)
// Output:
// 200 OK
}
func ExampleServer_Shutdown() {
hello := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello, world!")
})
srv, err := memhttp.New(hello)
if err != nil {
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
panic(err)
}
}