(t *testing.T)
| 288 | } |
| 289 | |
| 290 | func 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 | |
| 321 | func TestContextRequestWithCancel(t *testing.T) { |
| 322 | s := RunDefaultServer() |
nothing calls this directly
no test coverage detected