MCPcopy
hub / github.com/restic/restic / TestRoundtrip

Function TestRoundtrip

internal/backend/watchdog_roundtriper_test.go:43–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

41}
42
43func TestRoundtrip(t *testing.T) {
44 t.Parallel()
45
46 // at the higher delay values, it takes longer to transmit the request/response body
47 // than the roundTripper timeout
48 for _, delay := range []int{0, 1, 10, 20} {
49 t.Run(fmt.Sprintf("%v", delay), func(t *testing.T) {
50 msg := []byte("ping-pong-data")
51 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52 data, err := io.ReadAll(r.Body)
53 if err != nil {
54 w.WriteHeader(500)
55 return
56 }
57 w.WriteHeader(200)
58
59 // slowly send the reply
60 for len(data) >= 2 {
61 _, _ = w.Write(data[:2])
62 w.(http.Flusher).Flush()
63 data = data[2:]
64 time.Sleep(time.Duration(delay) * time.Millisecond)
65 }
66 _, _ = w.Write(data)
67 }))
68 defer srv.Close()
69
70 rt := newWatchdogRoundtripper(http.DefaultTransport, 100*time.Millisecond, 2)
71 req, err := http.NewRequestWithContext(context.TODO(), "GET", srv.URL, io.NopCloser(newSlowReader(bytes.NewReader(msg), time.Duration(delay)*time.Millisecond)))
72 rtest.OK(t, err)
73
74 resp, err := rt.RoundTrip(req)
75 rtest.OK(t, err)
76 rtest.Equals(t, 200, resp.StatusCode, "unexpected status code")
77
78 response, err := io.ReadAll(resp.Body)
79 rtest.OK(t, err)
80 rtest.Equals(t, msg, response, "unexpected response")
81
82 rtest.OK(t, resp.Body.Close())
83 })
84 }
85}
86
87func TestCanceledRoundtrip(t *testing.T) {
88 rt := newWatchdogRoundtripper(http.DefaultTransport, time.Second, 2)

Callers

nothing calls this directly

Calls 8

newWatchdogRoundtripperFunction · 0.85
newSlowReaderFunction · 0.85
RunMethod · 0.80
CloseMethod · 0.65
WriteMethod · 0.45
FlushMethod · 0.45
RoundTripMethod · 0.45
EqualsMethod · 0.45

Tested by

no test coverage detected