Home / Function/ TestContextCopyShouldNotCancel() — gin Function Reference

TestContextCopyShouldNotCancel() — gin Function Reference

Architecture documentation for the TestContextCopyShouldNotCancel() function in context_test.go from the gin codebase.

Entity Profile

Dependency Diagram

graph TD
  99401cc0_4394_d932_8cf3_0264df2c834c["TestContextCopyShouldNotCancel()"]
  ebe0ae48_a62b_a38f_5bac_5bbbd96fc508["context_test.go"]
  99401cc0_4394_d932_8cf3_0264df2c834c -->|defined in| ebe0ae48_a62b_a38f_5bac_5bbbd96fc508
  71015ed1_8ed9_a61c_5854_8cd81f04fa79["must()"]
  99401cc0_4394_d932_8cf3_0264df2c834c -->|calls| 71015ed1_8ed9_a61c_5854_8cd81f04fa79
  style 99401cc0_4394_d932_8cf3_0264df2c834c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

context_test.go lines 3242–3304

func TestContextCopyShouldNotCancel(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(http.StatusOK)
	}))
	defer srv.Close()

	ensureRequestIsOver := make(chan struct{})

	wg := &sync.WaitGroup{}

	r := New()
	r.GET("/", func(ginctx *Context) {
		wg.Add(1)

		ginctx = ginctx.Copy()

		// start async goroutine for calling srv
		go func() {
			defer wg.Done()

			<-ensureRequestIsOver // ensure request is done

			req, err := http.NewRequestWithContext(ginctx, http.MethodGet, srv.URL, nil)
			must(err)

			res, err := http.DefaultClient.Do(req)
			if err != nil {
				t.Error(fmt.Errorf("request error: %w", err))
				return
			}

			if res.StatusCode != http.StatusOK {
				t.Error(fmt.Errorf("unexpected status code: %s", res.Status))
			}
		}()
	})

	l, err := net.Listen("tcp", ":0")
	must(err)
	go func() {
		s := &http.Server{
			Handler: r,
		}

		must(s.Serve(l))
	}()

	addr := strings.Split(l.Addr().String(), ":")
	res, err := http.Get(fmt.Sprintf("http://%s:%s/", localhostIP, addr[len(addr)-1]))
	if err != nil {
		t.Error(fmt.Errorf("request error: %w", err))
		return
	}

	close(ensureRequestIsOver)

	if res.StatusCode != http.StatusOK {
		t.Error(fmt.Errorf("unexpected status code: %s", res.Status))
		return
	}

	wg.Wait()
}

Domain

Subdomains

Defined In

Calls

Frequently Asked Questions

What does TestContextCopyShouldNotCancel() do?
TestContextCopyShouldNotCancel() is a function in the gin codebase, defined in context_test.go.
Where is TestContextCopyShouldNotCancel() defined?
TestContextCopyShouldNotCancel() is defined in context_test.go at line 3242.
What does TestContextCopyShouldNotCancel() call?
TestContextCopyShouldNotCancel() calls 1 function(s): must.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free