MCPcopy
hub / github.com/go-chi/chi / TestThrottleClientTimeout

Function TestThrottleClientTimeout

middleware/throttle_test.go:58–88  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

56}
57
58func TestThrottleClientTimeout(t *testing.T) {
59 r := chi.NewRouter()
60
61 r.Use(ThrottleBacklog(10, 50, time.Second*10))
62
63 r.Get("/", func(w http.ResponseWriter, r *http.Request) {
64 w.WriteHeader(http.StatusOK)
65 time.Sleep(time.Second * 5) // Expensive operation.
66 w.Write(testContent)
67 })
68
69 server := httptest.NewServer(r)
70 defer server.Close()
71
72 client := http.Client{
73 Timeout: time.Second * 3, // Maximum waiting time.
74 }
75
76 var wg sync.WaitGroup
77
78 for i := range 10 {
79 wg.Add(1)
80 go func(i int) {
81 defer wg.Done()
82 _, err := client.Get(server.URL)
83 assertError(t, err)
84 }(i)
85 }
86
87 wg.Wait()
88}
89
90func TestThrottleTriggerGatewayTimeout(t *testing.T) {
91 r := chi.NewRouter()

Callers

nothing calls this directly

Calls 8

UseMethod · 0.95
GetMethod · 0.95
ThrottleBacklogFunction · 0.85
assertErrorFunction · 0.85
CloseMethod · 0.80
AddMethod · 0.80
WriteMethod · 0.65
WriteHeaderMethod · 0.45

Tested by

no test coverage detected