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
  0532fb9e_6768_6147_8dd4_007779111607["TestContextCopyShouldNotCancel()"]
  ec506021_e45d_d3ca_515a_2723d59cd179["WriteHeader()"]
  0532fb9e_6768_6147_8dd4_007779111607 -->|calls| ec506021_e45d_d3ca_515a_2723d59cd179
  a007da10_d85d_14be_cdc7_4b615661025a["must()"]
  0532fb9e_6768_6147_8dd4_007779111607 -->|calls| a007da10_d85d_14be_cdc7_4b615661025a
  style 0532fb9e_6768_6147_8dd4_007779111607 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

Frequently Asked Questions

What does TestContextCopyShouldNotCancel() do?
TestContextCopyShouldNotCancel() is a function in the gin codebase.
What does TestContextCopyShouldNotCancel() call?
TestContextCopyShouldNotCancel() calls 2 function(s): WriteHeader, must.

Analyze Your Own Codebase

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

Try Supermodel Free