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

Function TestJetStreamSubscribe_SkipConsumerLookup

test/js_test.go:1075–1227  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1073}
1074
1075func TestJetStreamSubscribe_SkipConsumerLookup(t *testing.T) {
1076 s := RunBasicJetStreamServer()
1077 defer shutdownJSServerAndRemoveStorage(t, s)
1078
1079 nc, js := jsClient(t, s)
1080 defer nc.Close()
1081 _, err := js.AddStream(&nats.StreamConfig{
1082 Name: "TEST",
1083 Subjects: []string{"foo"},
1084 })
1085 if err != nil {
1086 t.Fatalf("Unexpected error: %v", err)
1087 }
1088
1089 _, err = js.AddConsumer("TEST", &nats.ConsumerConfig{
1090 Name: "cons",
1091 DeliverSubject: "_INBOX.foo",
1092 AckPolicy: nats.AckExplicitPolicy,
1093 })
1094 if err != nil {
1095 t.Fatalf("Unexpected error: %v", err)
1096 }
1097
1098 // for checking whether subscribe looks up the consumer
1099 infoSub, err := nc.SubscribeSync("$JS.API.CONSUMER.INFO.TEST.*")
1100 if err != nil {
1101 t.Fatalf("Unexpected error: %v", err)
1102 }
1103 defer infoSub.Unsubscribe()
1104
1105 // for checking whether subscribe creates the consumer
1106 createConsSub, err := nc.SubscribeSync("$JS.API.CONSUMER.CREATE.>")
1107 if err != nil {
1108 t.Fatalf("Unexpected error: %v", err)
1109 }
1110 defer createConsSub.Unsubscribe()
1111 t.Run("use Bind to skip consumer lookup and create", func(t *testing.T) {
1112 sub, err := js.SubscribeSync("", nats.Bind("TEST", "cons"), nats.SkipConsumerLookup(), nats.DeliverSubject("_INBOX.foo"))
1113 if err != nil {
1114 t.Fatalf("Unexpected error: %v", err)
1115 }
1116 defer sub.Unsubscribe()
1117 // we should get timeout waiting for msg on CONSUMER.INFO
1118 if msg, err := infoSub.NextMsg(50 * time.Millisecond); err == nil {
1119 t.Fatalf("Expected to skip consumer lookup; got message on %q", msg.Subject)
1120 }
1121
1122 // we should get timeout waiting for msg on CONSUMER.CREATE
1123 if msg, err := createConsSub.NextMsg(50 * time.Millisecond); err == nil {
1124 t.Fatalf("Expected to skip consumer create; got message on %q", msg.Subject)
1125 }
1126 if _, err := js.Publish("foo", []byte("msg")); err != nil {
1127 t.Fatalf("Unexpected error: %s", err)
1128 }
1129 if _, err := sub.NextMsg(100 * time.Millisecond); err != nil {
1130 t.Fatalf("Expected to receive msg; got: %s", err)
1131 }
1132 })

Callers

nothing calls this directly

Calls 12

FatalfMethod · 0.80
UnsubscribeMethod · 0.80
NextMsgMethod · 0.80
RunBasicJetStreamServerFunction · 0.70
jsClientFunction · 0.70
AddStreamMethod · 0.65
AddConsumerMethod · 0.65
SubscribeSyncMethod · 0.65
PublishMethod · 0.65
ErrorMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected