MCPcopy
hub / github.com/prometheus/client_golang / TestHandlerMaxRequestsInFlight

Function TestHandlerMaxRequestsInFlight

prometheus/promhttp/http_test.go:309–345  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

307}
308
309func TestHandlerMaxRequestsInFlight(t *testing.T) {
310 reg := prometheus.NewRegistry()
311 handler := HandlerFor(reg, HandlerOpts{MaxRequestsInFlight: 1})
312 w1 := httptest.NewRecorder()
313 w2 := httptest.NewRecorder()
314 w3 := httptest.NewRecorder()
315 request, _ := http.NewRequest(http.MethodGet, "/", nil)
316 request.Header.Add(acceptHeader, acceptTextPlain)
317
318 c := blockingCollector{Block: make(chan struct{}), CollectStarted: make(chan struct{}, 1)}
319 reg.MustRegister(c)
320
321 rq1Done := make(chan struct{})
322 go func() {
323 handler.ServeHTTP(w1, request)
324 close(rq1Done)
325 }()
326 <-c.CollectStarted
327
328 handler.ServeHTTP(w2, request)
329
330 if got, want := w2.Code, http.StatusServiceUnavailable; got != want {
331 t.Errorf("got HTTP status code %d, want %d", got, want)
332 }
333 if got, want := w2.Body.String(), "Limit of concurrent requests reached (1), try again later.\n"; got != want {
334 t.Errorf("got body %q, want %q", got, want)
335 }
336
337 close(c.Block)
338 <-rq1Done
339
340 handler.ServeHTTP(w3, request)
341
342 if got, want := w3.Code, http.StatusOK; got != want {
343 t.Errorf("got HTTP status code %d, want %d", got, want)
344 }
345}
346
347func TestHandlerTimeout(t *testing.T) {
348 reg := prometheus.NewRegistry()

Callers

nothing calls this directly

Calls 6

MustRegisterMethod · 0.95
NewRegistryFunction · 0.92
HandlerForFunction · 0.85
AddMethod · 0.65
ServeHTTPMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected