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

Function TestUnsubscribeAndNextMsgWithContext

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

Source from the content-addressed store, hash-verified

747}
748
749func 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
792func TestContextInvalid(t *testing.T) {
793 s := RunDefaultServer()

Callers

nothing calls this directly

Calls 9

FatalfMethod · 0.80
UnsubscribeMethod · 0.80
NextMsgWithContextMethod · 0.80
RunDefaultServerFunction · 0.70
NewDefaultConnectionFunction · 0.70
SubscribeSyncMethod · 0.65
AddMethod · 0.65
DoneMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected