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

Function TestClientASyncAutoUnsub

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

Source from the content-addressed store, hash-verified

147}
148
149func TestClientASyncAutoUnsub(t *testing.T) {
150 s := RunDefaultServer()
151 defer s.Shutdown()
152
153 nc := NewDefaultConnection(t)
154 defer nc.Close()
155 received := int32(0)
156 max := int32(10)
157 sub, err := nc.Subscribe("foo", func(_ *nats.Msg) {
158 atomic.AddInt32(&received, 1)
159 })
160 if err != nil {
161 t.Fatal("Failed to subscribe: ", err)
162 }
163 sub.AutoUnsubscribe(int(max))
164 total := 100
165 for range total {
166 nc.Publish("foo", []byte("Hello"))
167 }
168 nc.Flush()
169 time.Sleep(10 * time.Millisecond)
170
171 if atomic.LoadInt32(&received) != max {
172 t.Fatalf("Received %d msgs, wanted only %d\n", received, max)
173 }
174 if err := sub.AutoUnsubscribe(10); err == nil {
175 t.Fatal("Calling AutoUnsubscribe() on closed subscription should fail")
176 }
177}
178
179func TestAutoUnsubAndReconnect(t *testing.T) {
180 s := RunDefaultServer()

Callers

nothing calls this directly

Calls 8

AutoUnsubscribeMethod · 0.80
FatalfMethod · 0.80
RunDefaultServerFunction · 0.70
NewDefaultConnectionFunction · 0.70
SubscribeMethod · 0.65
PublishMethod · 0.65
CloseMethod · 0.45
FlushMethod · 0.45

Tested by

no test coverage detected