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

Function TestClientChanAutoUnsub

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

Source from the content-addressed store, hash-verified

109}
110
111func TestClientChanAutoUnsub(t *testing.T) {
112 s := RunDefaultServer()
113 defer s.Shutdown()
114
115 nc := NewDefaultConnection(t)
116 defer nc.Close()
117 received := 0
118 max := 10
119 ch := make(chan *nats.Msg, max)
120 sub, _ := nc.ChanSubscribe("foo", ch)
121 sub.AutoUnsubscribe(max)
122 total := 100
123 for range total {
124 nc.Publish("foo", []byte("Hello"))
125 }
126 nc.Flush()
127
128 // Drain the channel
129 for {
130 select {
131 case <-ch:
132 received++
133 case <-time.After(10 * time.Millisecond):
134 if received != max {
135 t.Fatalf("Received %d msgs, wanted only %d", received, max)
136 }
137 if sub.IsValid() {
138 t.Fatal("Expected subscription to be invalid after hitting max")
139 }
140 if err := sub.AutoUnsubscribe(10); err == nil {
141 t.Fatal("Calling AutoUnsubscribe() ob closed subscription should fail")
142 }
143 return
144 }
145 }
146
147}
148
149func TestClientASyncAutoUnsub(t *testing.T) {
150 s := RunDefaultServer()

Callers

nothing calls this directly

Calls 9

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

Tested by

no test coverage detected