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

Function TestRespInbox

test/nats_test.go:1162–1195  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1160}
1161
1162func TestRespInbox(t *testing.T) {
1163 s := RunServerOnPort(-1)
1164 defer s.Shutdown()
1165
1166 nc, err := nats.Connect(s.ClientURL())
1167 if err != nil {
1168 t.Fatalf("Expected to connect to server, got %v", err)
1169 }
1170 defer nc.Close()
1171
1172 if _, err := nc.Subscribe("foo", func(msg *nats.Msg) {
1173 lastDot := strings.LastIndex(msg.Reply, ".")
1174 if lastDot == -1 {
1175 msg.Respond([]byte(fmt.Sprintf("Invalid reply subject: %q", msg.Reply)))
1176 return
1177 }
1178 lastToken := msg.Reply[lastDot+1:]
1179 replySuffixLen := 8
1180 if len(lastToken) != replySuffixLen {
1181 msg.Respond([]byte(fmt.Sprintf("Invalid last token: %q", lastToken)))
1182 return
1183 }
1184 msg.Respond(nil)
1185 }); err != nil {
1186 t.Fatalf("subscribe failed: %s", err)
1187 }
1188 resp, err := nc.Request("foo", []byte("check inbox"), time.Second)
1189 if err != nil {
1190 t.Fatalf("Request failed: %v", err)
1191 }
1192 if len(resp.Data) > 0 {
1193 t.Fatalf("Error: %s", resp.Data)
1194 }
1195}
1196
1197func TestInProcessConn(t *testing.T) {
1198 s := RunServerOnPort(-1)

Callers

nothing calls this directly

Calls 7

ConnectMethod · 0.80
FatalfMethod · 0.80
RunServerOnPortFunction · 0.70
SubscribeMethod · 0.65
RespondMethod · 0.65
CloseMethod · 0.45
RequestMethod · 0.45

Tested by

no test coverage detected