(t *testing.T)
| 747 | } |
| 748 | |
| 749 | func TestUnsubscribeAndNextMsgWithContext(t *testing.T) { |
| 750 | s := RunDefaultServer() |
| 751 | defer s.Shutdown() |
| 752 | |
| 753 | nc := NewDefaultConnection(t) |
| 754 | defer nc.Close() |
| 755 | |
| 756 | ctx, cancelCB := context.WithCancel(context.Background()) |
| 757 | defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 758 | |
| 759 | sub, err := nc.SubscribeSync("foo") |
| 760 | if err != nil { |
| 761 | t.Fatalf("Expected to be able to subscribe: %s", err) |
| 762 | } |
| 763 | sub.Unsubscribe() |
| 764 | if _, err = sub.NextMsgWithContext(ctx); err != nats.ErrBadSubscription { |
| 765 | t.Fatalf("Expected '%v', but got: '%v'", nats.ErrBadSubscription, err) |
| 766 | } |
| 767 | |
| 768 | ctx, cancelCB = context.WithCancel(context.Background()) |
| 769 | defer cancelCB() // should always be called, not discarded, to prevent context leak |
| 770 | |
| 771 | sub, err = nc.SubscribeSync("foo") |
| 772 | if err != nil { |
| 773 | t.Fatalf("Expected to be able to subscribe: %s", err) |
| 774 | } |
| 775 | |
| 776 | // Now make sure we get same error when unsubscribing from separate routine |
| 777 | // while in the call. |
| 778 | wg := sync.WaitGroup{} |
| 779 | wg.Add(1) |
| 780 | go func() { |
| 781 | time.Sleep(100 * time.Millisecond) |
| 782 | sub.Unsubscribe() |
| 783 | wg.Done() |
| 784 | }() |
| 785 | |
| 786 | if _, err = sub.NextMsgWithContext(ctx); err != nats.ErrBadSubscription { |
| 787 | t.Fatalf("Expected '%v', but got: '%v'", nats.ErrBadSubscription, err) |
| 788 | } |
| 789 | wg.Wait() |
| 790 | } |
| 791 | |
| 792 | func TestContextInvalid(t *testing.T) { |
| 793 | s := RunDefaultServer() |
nothing calls this directly
no test coverage detected