(t *testing.T)
| 465 | } |
| 466 | |
| 467 | func TestContextSubNextMsgWithTimeoutCanceled(t *testing.T) { |
| 468 | s := RunDefaultServer() |
| 469 | defer s.Shutdown() |
| 470 | |
| 471 | nc := NewDefaultConnection(t) |
| 472 | defer nc.Close() |
| 473 | |
| 474 | ctx, cancelCB := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 475 | defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 476 | |
| 477 | sub, err := nc.SubscribeSync("fast") |
| 478 | if err != nil { |
| 479 | t.Fatalf("Expected to be able to subscribe: %s", err) |
| 480 | } |
| 481 | |
| 482 | for i := 0; i < 2; i++ { |
| 483 | err := nc.Publish("fast", []byte("OK")) |
| 484 | if err != nil { |
| 485 | t.Fatalf("Expected publish to not fail: %s", err) |
| 486 | } |
| 487 | // Enough time to get a couple of messages |
| 488 | time.Sleep(40 * time.Millisecond) |
| 489 | |
| 490 | msg, err := sub.NextMsgWithContext(ctx) |
| 491 | if err != nil { |
| 492 | t.Fatalf("Expected to receive message: %s", err) |
| 493 | } |
| 494 | got := string(msg.Data) |
| 495 | expected := "OK" |
| 496 | if got != expected { |
| 497 | t.Errorf("Expected to receive %s, got: %s", expected, got) |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // Cancel the context already so that rest of NextMsg calls fail. |
| 502 | cancelCB() |
| 503 | |
| 504 | _, err = sub.NextMsgWithContext(ctx) |
| 505 | if err == nil { |
| 506 | t.Fatal("Expected request with timeout context to fail") |
| 507 | } |
| 508 | |
| 509 | // Reported error is "context canceled" from Context package, |
| 510 | // which is not a timeout error. |
| 511 | type timeoutError interface { |
| 512 | Timeout() bool |
| 513 | } |
| 514 | if _, ok := err.(timeoutError); ok { |
| 515 | t.Errorf("Expected to not have a timeout error") |
| 516 | } |
| 517 | expected := `context canceled` |
| 518 | if !strings.Contains(err.Error(), expected) { |
| 519 | t.Errorf("Expected %q error, got: %q", expected, err.Error()) |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | func TestContextSubNextMsgWithCancel(t *testing.T) { |
| 524 | s := RunDefaultServer() |
nothing calls this directly
no test coverage detected