MCPcopy Create free account
hub / github.com/quay/clair / TestClientDisconnect

Function TestClientDisconnect

httptransport/error_test.go:14–58  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func TestClientDisconnect(t *testing.T) {
15 var status int
16 // Bunch of sequencing events:
17 reqStart := make(chan struct{})
18 reqDone := make(chan struct{})
19 handlerDone := make(chan struct{})
20
21 // Server side:
22 // - Emit reqStart once the request is received.
23 // - Emit handlerDone once the request is done and "status" should be populated.
24 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
25 defer func() { close(handlerDone) }()
26 w = httputil.ResponseRecorder(&status, nil, w)
27 ctx := zlog.Test(r.Context(), t) // The error handler emits logs.
28 close(reqStart)
29 <-ctx.Done()
30 apiError(ctx, w, http.StatusOK, "hello from the handler")
31 }))
32 t.Cleanup(srv.Close)
33
34 ctx, done := context.WithCancel(context.Background())
35 // Closing "done" will cancel the client connection.
36 req, err := http.NewRequestWithContext(ctx, "GET", srv.URL+"/", nil)
37 if err != nil {
38 t.Fatal(err)
39 }
40 // Emit reqDone when the client connection is done.
41 go func() {
42 _, err = srv.Client().Do(req)
43 close(reqDone)
44 }()
45
46 <-reqStart
47 done()
48 <-reqDone
49 t.Logf("got request error: %v", err)
50 if err == nil {
51 t.Error("expected non-nil error")
52 }
53
54 <-handlerDone
55 if got, want := status, statusClientClosedRequest; got != want {
56 t.Errorf("bad status code recorded: got: %d, want: %d", got, want)
57 }
58}

Callers

nothing calls this directly

Calls 4

ResponseRecorderFunction · 0.92
apiErrorFunction · 0.85
NewRequestWithContextMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected