MCPcopy
hub / github.com/nats-io/nats.go / TestContextOldRequestClosed

Function TestContextOldRequestClosed

test/context_test.go:290–319  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

288}
289
290func TestContextOldRequestClosed(t *testing.T) {
291 s := RunDefaultServer()
292 defer s.Shutdown()
293
294 nc, err := nats.Connect(nats.DefaultURL, nats.UseOldRequestStyle())
295 if err != nil {
296 t.Fatalf("Failed to connect: %v", err)
297 }
298 defer nc.Close()
299
300 ctx, cancelCB := context.WithTimeout(context.Background(), time.Second)
301 defer cancelCB() // should always be called, not discarded, to prevent context leak
302
303 errCh := make(chan error, 1)
304 start := time.Now()
305 go func() {
306 sub, _ := nc.SubscribeSync("checkClose")
307 defer sub.Unsubscribe()
308 _, err = nc.RequestWithContext(ctx, "checkClose", []byte("should be kicked out on close"))
309 errCh <- err
310 }()
311 time.Sleep(100 * time.Millisecond)
312 nc.Close()
313 if e := <-errCh; e != nats.ErrConnectionClosed {
314 t.Fatalf("Unexpected error: %v", e)
315 }
316 if dur := time.Since(start); dur >= time.Second {
317 t.Fatalf("Request took too long to bail out: %v", dur)
318 }
319}
320
321func TestContextRequestWithCancel(t *testing.T) {
322 s := RunDefaultServer()

Callers

nothing calls this directly

Calls 7

ConnectMethod · 0.80
FatalfMethod · 0.80
UnsubscribeMethod · 0.80
RunDefaultServerFunction · 0.70
SubscribeSyncMethod · 0.65
CloseMethod · 0.45
RequestWithContextMethod · 0.45

Tested by

no test coverage detected