(t *testing.T)
| 12 | ) |
| 13 | |
| 14 | func TestClientTimeout(t *testing.T) { |
| 15 | testLog := NewTestLog() |
| 16 | defer testLog.Close() |
| 17 | |
| 18 | d := 200 * time.Millisecond |
| 19 | |
| 20 | payload := []byte("backend reply") |
| 21 | backend := startTestServer(payload, 0, func(r *http.Request) { |
| 22 | time.Sleep(2 * d) |
| 23 | }) |
| 24 | |
| 25 | defer backend.Close() |
| 26 | |
| 27 | doc := fmt.Sprintf(`hello: * -> "%s"`, backend.URL) |
| 28 | tp, err := newTestProxy(doc, FlagsNone) |
| 29 | if err != nil { |
| 30 | t.Error() |
| 31 | return |
| 32 | } |
| 33 | |
| 34 | ps := httptest.NewServer(tp.proxy) |
| 35 | defer func() { |
| 36 | ps.Close() |
| 37 | tp.close() |
| 38 | }() |
| 39 | |
| 40 | req, err := http.NewRequest("GET", ps.URL, nil) |
| 41 | if err != nil { |
| 42 | t.Errorf("Failed to create request: %v", err) |
| 43 | } |
| 44 | |
| 45 | rsp, err := (&http.Client{ |
| 46 | Timeout: d, |
| 47 | }).Do(req) |
| 48 | |
| 49 | if err == nil { |
| 50 | t.Error("err should not be nil") |
| 51 | } |
| 52 | if rsp != nil { |
| 53 | t.Error("response should be nil") |
| 54 | } |
| 55 | |
| 56 | const msgErrClientTimeout = "context canceled" |
| 57 | if err = testLog.WaitFor(msgErrClientTimeout, 3*d); err != nil { |
| 58 | t.Errorf("log should contain '%s'", msgErrClientTimeout) |
| 59 | } |
| 60 | const msgErrClientCanceledAfter = "client canceled after" |
| 61 | if err = testLog.WaitFor(msgErrClientCanceledAfter, 3*d); err != nil { |
| 62 | t.Errorf("log should contain '%s'", msgErrClientCanceledAfter) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestClientCancellation(t *testing.T) { |
| 67 | testLog := NewTestLog() |
nothing calls this directly
no test coverage detected
searching dependent graphs…