Repository navigation
Expand file tree
/
Copy pathserver_test.go
More file actions
242 lines (205 loc) · 7.94 KB
/
Copy pathserver_test.go
File metadata and controls
242 lines (205 loc) · 7.94 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"bufio"
"compress/gzip"
"context"
"io"
"net/http"
"net/http/httptest"
"runtime"
"sync"
"testing"
"time"
"github.com/ipfs/boxo/routing/http/server"
"github.com/stretchr/testify/require"
)
func TestRegisterPprof(t *testing.T) {
// The sampling rates registerPprof sets are process-wide, so put them back
// for the rest of the suite. SetMutexProfileFraction(-1) reads the current
// value without changing it; SetBlockProfileRate has no getter, so the
// best that can be done is to restore the runtime default of 0 (off).
oldMutex := runtime.SetMutexProfileFraction(-1)
t.Cleanup(func() {
runtime.SetMutexProfileFraction(oldMutex)
runtime.SetBlockProfileRate(0)
})
mux := http.NewServeMux()
registerPprof(mux)
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
for path, want := range map[string]string{
// The index lists the profiles, one of which is "goroutine"; the heap
// profile in debug mode starts with its own header.
"/debug/pprof/": "goroutine",
"/debug/pprof/heap?debug=1": "heap profile",
} {
res, err := http.Get(srv.URL + path)
require.NoError(t, err)
body, err := io.ReadAll(res.Body)
res.Body.Close()
require.NoError(t, err)
require.Equal(t, http.StatusOK, res.StatusCode, path)
require.Contains(t, string(body), want, path)
}
// A mux nobody registered on must not serve profiles: the endpoints come
// from the named registrations above, not from importing net/http/pprof.
bare := httptest.NewServer(http.NewServeMux())
t.Cleanup(bare.Close)
res, err := http.Get(bare.URL + "/debug/pprof/")
require.NoError(t, err)
res.Body.Close()
require.Equal(t, http.StatusNotFound, res.StatusCode)
}
// TestAPIMuxPprofGate is the test the unit test above cannot be: registerPprof
// on a mux of its own says nothing about what the API server serves. The first
// version of this change registered on http.DefaultServeMux, where
// net/http/pprof's init has already registered the same endpoints, so the
// profiles were served with the flag off and the flag on panicked on the
// conflicting patterns. Both show up here.
func TestAPIMuxPprofGate(t *testing.T) {
oldMutex := runtime.SetMutexProfileFraction(-1)
t.Cleanup(func() {
runtime.SetMutexProfileFraction(oldMutex)
runtime.SetBlockProfileRate(0)
})
for _, tc := range []struct {
name string
pprof bool
wantStatus int
}{
{name: "off", pprof: false, wantStatus: http.StatusNotFound},
{name: "on", pprof: true, wantStatus: http.StatusOK},
} {
t.Run(tc.name, func(t *testing.T) {
srv := httptest.NewServer(apiMux(http.NotFoundHandler(), &config{pprof: tc.pprof}))
t.Cleanup(srv.Close)
res, err := http.Get(srv.URL + "/debug/pprof/")
require.NoError(t, err)
res.Body.Close()
require.Equal(t, tc.wantStatus, res.StatusCode)
// The endpoints the server has always served are unaffected.
for _, path := range []string{"/version", "/debug/metrics/prometheus"} {
res, err := http.Get(srv.URL + path)
require.NoError(t, err)
res.Body.Close()
require.Equal(t, http.StatusOK, res.StatusCode, path)
}
})
}
}
func TestCombineRouters(t *testing.T) {
t.Parallel()
// Mock router for testing
mockRouter := composableRouter{}
// Check that combineRouters with DHT only returns sanitizeRouter, wrapped
// so it still gets a router span
v := combineRouters(nil, &bundledDHT{}, nil, nil, nil, nil, DNSAddrResolutionNever, false, 0, 0, 0)
require.IsType(t, tracedRouter{}, v)
require.IsType(t, sanitizeRouter{}, v.(tracedRouter).router)
require.Equal(t, "dht", v.(tracedRouter).name)
// Check that combineRouters with delegated routers only returns parallelRouter
v = combineRouters(nil, nil, nil, []router{mockRouter}, nil, nil, DNSAddrResolutionNever, false, 0, 0, 0)
require.IsType(t, parallelRouter{}, v)
// Check that combineRouters with both DHT and delegated routers returns parallelRouter
v = combineRouters(nil, &bundledDHT{}, nil, []router{mockRouter}, nil, nil, DNSAddrResolutionNever, false, 0, 0, 0)
require.IsType(t, parallelRouter{}, v)
// Check that a resolver wraps both branches in dnsAddrRouter
resolver, err := newDNSAddrResolver(nil)
require.NoError(t, err)
v = combineRouters(nil, &bundledDHT{}, nil, nil, nil, resolver, DNSAddrResolutionAppend, false, 0, 0, 0)
require.IsType(t, dnsAddrRouter{}, v)
v = combineRouters(nil, nil, nil, []router{mockRouter}, nil, resolver, DNSAddrResolutionAppend, false, 0, 0, 0)
require.IsType(t, dnsAddrRouter{}, v)
}
// A record that resolved early has to reach the client while later records
// are still being looked up. Compression middleware defaults defeat this by
// withholding small writes, so guard the behavior rather than the setting.
func TestCompressedNDJSONFlushesEachRecord(t *testing.T) {
t.Parallel()
// Shorter than httpcompression's default MinSize, so a buffering
// middleware would hold it back.
const firstRecord = `{"Schema":"peer","ID":"first"}` + "\n"
const secondRecord = `{"Schema":"peer","ID":"second"}` + "\n"
secondReady := make(chan struct{})
handlerDone := make(chan struct{})
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer close(handlerDone)
w.Header().Set("Content-Type", "application/x-ndjson")
_, err := w.Write([]byte(firstRecord))
require.NoError(t, err)
w.(http.Flusher).Flush()
// Stand in for a provider whose addresses are still being resolved.
<-secondReady
_, err = w.Write([]byte(secondRecord))
require.NoError(t, err)
w.(http.Flusher).Flush()
})
compress, err := newCompressionAdapter()
require.NoError(t, err)
srv := httptest.NewServer(compress(h))
t.Cleanup(srv.Close)
// Registered after srv.Close so it runs first: a failing assertion must
// not leave the handler parked while Close waits for it.
var releaseOnce sync.Once
release := func() { releaseOnce.Do(func() { close(secondReady) }) }
t.Cleanup(release)
// Bounds every step below. A buffering middleware withholds the response
// headers too, so without this the request itself would block forever.
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL, nil)
require.NoError(t, err)
req.Header.Set("Accept", "application/x-ndjson")
// Set explicitly so net/http does not transparently decompress for us.
req.Header.Set("Accept-Encoding", "gzip")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err, "response headers never arrived while the handler was still running: the response is buffered, not streamed")
t.Cleanup(func() { _ = resp.Body.Close() })
require.Equal(t, "gzip", resp.Header.Get("Content-Encoding"), "compression should still be applied")
type readResult struct {
line string
err error
}
lines := make(chan readResult, 2)
go func() {
zr, err := gzip.NewReader(resp.Body)
if err != nil {
lines <- readResult{err: err}
return
}
br := bufio.NewReader(zr)
for {
line, err := br.ReadString('\n')
lines <- readResult{line: line, err: err}
if err != nil {
return
}
}
}()
select {
case got := <-lines:
require.NoError(t, got.err)
require.Equal(t, firstRecord, got.line)
case <-time.After(15 * time.Second):
t.Fatal("first record never arrived while the handler was still running: the response is buffered, not streamed")
}
release()
select {
case got := <-lines:
require.NoError(t, got.err)
require.Equal(t, secondRecord, got.line)
case <-time.After(10 * time.Second):
t.Fatal("second record never arrived")
}
<-handlerDone
}
// The routing timeout has to leave the client room to receive the response.
// Helia's delegated routing client aborts the whole request at 30s and starts
// counting before needle does, so anything at or above that loses every
// record needle resolved.
func TestRoutingTimeoutLeavesRoomForClientDeadline(t *testing.T) {
t.Parallel()
const heliaClientTimeout = 30 * time.Second
require.Less(t, DefaultRoutingTimeout, heliaClientTimeout)
require.Less(t, DefaultRoutingTimeout, server.DefaultRoutingTimeout)
}