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

Function TestUnsubscribeChanOnSubscriber

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

Source from the content-addressed store, hash-verified

1072}
1073
1074func TestUnsubscribeChanOnSubscriber(t *testing.T) {
1075 s := RunDefaultServer()
1076 defer s.Shutdown()
1077
1078 nc := NewDefaultConnection(t)
1079 defer nc.Close()
1080
1081 // Override default handler for test.
1082 nc.SetErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, _ error) {})
1083
1084 // Create our own channel.
1085 ch := make(chan *nats.Msg, 8)
1086 sub, _ := nc.ChanSubscribe("foo", ch)
1087
1088 // Send some messages to ourselves.
1089 total := 100
1090 for range total {
1091 nc.Publish("foo", []byte("Hello"))
1092 }
1093
1094 sub.Unsubscribe()
1095 for len(ch) > 0 {
1096 <-ch
1097 }
1098 // Make sure we can send to the channel still.
1099 // Test that we do not close it.
1100 ch <- &nats.Msg{}
1101}
1102
1103func TestCloseChanOnSubscriber(t *testing.T) {
1104 s := RunDefaultServer()

Callers

nothing calls this directly

Calls 7

SetErrorHandlerMethod · 0.80
UnsubscribeMethod · 0.80
RunDefaultServerFunction · 0.70
NewDefaultConnectionFunction · 0.70
ChanSubscribeMethod · 0.65
PublishMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected