MCPcopy Create free account
hub / github.com/coder/envbuilder / TestCoderRPCTimeout

Function TestCoderRPCTimeout

log/coder_internal_test.go:238–307  ·  view source on GitHub ↗

nolint:paralleltest // We need to replace a global timeout.

(t *testing.T)

Source from the content-addressed store, hash-verified

236
237//nolint:paralleltest // We need to replace a global timeout.
238func TestCoderRPCTimeout(t *testing.T) {
239 // This timeout is picked with the current subtests in mind, it
240 // should not be changed without good reason.
241 testReplaceTimeout(t, &rpcConnectTimeout, 500*time.Millisecond)
242
243 // In this test, we just stand up an endpoint that does not
244 // do dRPC. We'll try to connect, fail to websocket upgrade
245 // and eventually give up after rpcConnectTimeout.
246 t.Run("V2/Err", func(t *testing.T) {
247 t.Parallel()
248
249 token := uuid.NewString()
250 handlerDone := make(chan struct{})
251 handlerWait := make(chan struct{})
252 var closeOnce sync.Once
253 handler := func(w http.ResponseWriter, r *http.Request) {
254 if r.URL.Path == "/api/v2/buildinfo" {
255 w.Header().Set("Content-Type", "application/json")
256 _, _ = w.Write([]byte(`{"version": "v2.9.0"}`))
257 return
258 }
259 defer closeOnce.Do(func() { close(handlerDone) })
260 <-handlerWait
261 w.WriteHeader(http.StatusOK)
262 }
263 srv := httptest.NewServer(http.HandlerFunc(handler))
264 defer srv.Close()
265
266 ctx, cancel := context.WithTimeout(context.Background(), rpcConnectTimeout/2)
267 defer cancel()
268 u, err := url.Parse(srv.URL)
269 require.NoError(t, err)
270 _, _, err = Coder(ctx, u, token)
271 require.ErrorContains(t, err, "failed to WebSocket dial")
272 require.ErrorIs(t, err, context.DeadlineExceeded)
273 close(handlerWait)
274 <-handlerDone
275 })
276
277 t.Run("V2/Timeout", func(t *testing.T) {
278 t.Parallel()
279
280 token := uuid.NewString()
281 handlerDone := make(chan struct{})
282 handlerWait := make(chan struct{})
283 var closeOnce sync.Once
284 handler := func(w http.ResponseWriter, r *http.Request) {
285 if r.URL.Path == "/api/v2/buildinfo" {
286 w.Header().Set("Content-Type", "application/json")
287 _, _ = w.Write([]byte(`{"version": "v2.9.0"}`))
288 return
289 }
290 defer closeOnce.Do(func() { close(handlerDone) })
291 <-handlerWait
292 w.WriteHeader(http.StatusOK)
293 }
294 srv := httptest.NewServer(http.HandlerFunc(handler))
295 defer srv.Close()

Callers

nothing calls this directly

Calls 4

testReplaceTimeoutFunction · 0.85
CoderFunction · 0.85
WriteMethod · 0.80
CloseMethod · 0.80

Tested by

no test coverage detected