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

Function TestClientSyncAutoUnsub

test/sub_test.go:75–109  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

73}
74
75func TestClientSyncAutoUnsub(t *testing.T) {
76 s := RunDefaultServer()
77 defer s.Shutdown()
78
79 nc := NewDefaultConnection(t)
80 defer nc.Close()
81 received := 0
82 max := 10
83 sub, _ := nc.SubscribeSync("foo")
84 sub.AutoUnsubscribe(max)
85 total := 100
86 for range total {
87 nc.Publish("foo", []byte("Hello"))
88 }
89 nc.Flush()
90 for {
91 _, err := sub.NextMsg(10 * time.Millisecond)
92 if err != nil {
93 if err != nats.ErrMaxMessages {
94 t.Fatalf("Expected '%v', but got: '%v'\n", nats.ErrMaxMessages, err.Error())
95 }
96 break
97 }
98 received++
99 }
100 if received != max {
101 t.Fatalf("Received %d msgs, wanted only %d\n", received, max)
102 }
103 if sub.IsValid() {
104 t.Fatal("Expected subscription to be invalid after hitting max")
105 }
106 if err := sub.AutoUnsubscribe(10); err == nil {
107 t.Fatal("Calling AutoUnsubscribe() ob closed subscription should fail")
108 }
109}
110
111func TestClientChanAutoUnsub(t *testing.T) {
112 s := RunDefaultServer()

Callers

nothing calls this directly

Calls 11

AutoUnsubscribeMethod · 0.80
NextMsgMethod · 0.80
FatalfMethod · 0.80
IsValidMethod · 0.80
RunDefaultServerFunction · 0.70
NewDefaultConnectionFunction · 0.70
SubscribeSyncMethod · 0.65
PublishMethod · 0.65
ErrorMethod · 0.65
CloseMethod · 0.45
FlushMethod · 0.45

Tested by

no test coverage detected