| 13 | ) |
| 14 | |
| 15 | func TestRequestContextClosure(t *testing.T) { |
| 16 | errCh := make(chan error) |
| 17 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | w.WriteHeader(http.StatusOK) |
| 19 | w.(http.Flusher).Flush() |
| 20 | ctx := r.Context() |
| 21 | <-ctx.Done() |
| 22 | errCh <- ctx.Err() |
| 23 | }) |
| 24 | srv := httpd.New("127.0.0.1:", h) |
| 25 | ctx, cancel := context.WithCancel(t.Context()) |
| 26 | require.NoError(t, srv.Start(ctx)) |
| 27 | res, err := http.Get(fmt.Sprintf("http://%s/", srv.Addr())) |
| 28 | require.NoError(t, err) |
| 29 | require.Equal(t, http.StatusOK, res.StatusCode) |
| 30 | cancel() |
| 31 | select { |
| 32 | case err = <-errCh: |
| 33 | case <-time.After(time.Second): |
| 34 | t.Fatalf("context did not cancel after one second") |
| 35 | } |
| 36 | assert.Equal(t, context.Canceled, err) |
| 37 | assert.NoError(t, srv.Wait()) |
| 38 | } |
| 39 | |
| 40 | func TestDeadlineExceeded(t *testing.T) { |
| 41 | old := httpd.ShutdownTimeout |