MCPcopy
hub / github.com/cortexlabs/cortex / TestProxyHandlerBreakerTimeout

Function TestProxyHandlerBreakerTimeout

pkg/proxy/handler_test.go:79–110  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

77}
78
79func TestProxyHandlerBreakerTimeout(t *testing.T) {
80 // This test sends a request which will take a long time to complete.
81 // Then another one with a very short context timeout.
82 // Verifies that the second one fails with timeout.
83 seen := make(chan struct{})
84 resp := make(chan struct{})
85 defer close(resp) // Allow all requests to pass through.
86 blockHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
87 seen <- struct{}{}
88 <-resp
89 })
90 breaker := proxy.NewBreaker(proxy.BreakerParams{
91 QueueDepth: 1, MaxConcurrency: 1, InitialCapacity: 1,
92 })
93 h := proxy.Handler(breaker, blockHandler)
94
95 go func() {
96 h(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, userContainerHost, nil))
97 }()
98
99 // Wait until the first request has entered the handler.
100 <-seen
101
102 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
103 defer cancel()
104
105 rec := httptest.NewRecorder()
106 h(rec, httptest.NewRequest(http.MethodGet, userContainerHost, nil).WithContext(ctx))
107
108 require.Equal(t, http.StatusServiceUnavailable, rec.Code)
109 require.True(t, strings.Contains(rec.Body.String(), context.DeadlineExceeded.Error()))
110}

Callers

nothing calls this directly

Calls 5

NewBreakerFunction · 0.92
HandlerFunction · 0.92
EqualMethod · 0.80
ErrorMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected