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

Function TestMsgRespond

test/basic_test.go:474–506  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

472}
473
474func TestMsgRespond(t *testing.T) {
475 s := RunDefaultServer()
476 defer s.Shutdown()
477 nc := NewDefaultConnection(t)
478 defer nc.Close()
479
480 m := &nats.Msg{}
481 if err := m.Respond(nil); err != nats.ErrMsgNotBound {
482 t.Fatal("Expected ErrMsgNotBound error")
483 }
484
485 sub, err := nc.Subscribe("req", func(msg *nats.Msg) {
486 msg.Respond([]byte("42"))
487 })
488 if err != nil {
489 t.Fatal("Failed to subscribe: ", err)
490 }
491
492 // Fake the bound notion by assigning Sub directly to test no reply.
493 m.Sub = sub
494 if err := m.Respond(nil); err != nats.ErrMsgNoReply {
495 t.Fatal("Expected ErrMsgNoReply error")
496 }
497
498 response, err := nc.Request("req", []byte("help"), 50*time.Millisecond)
499 if err != nil {
500 t.Fatal("Request Failed: ", err)
501 }
502
503 if string(response.Data) != "42" {
504 t.Fatalf("Expected '42', got %q", response.Data)
505 }
506}
507
508func TestFlush(t *testing.T) {
509 s := RunDefaultServer()

Callers

nothing calls this directly

Calls 8

RespondMethod · 0.95
FatalfMethod · 0.80
RunDefaultServerFunction · 0.70
NewDefaultConnectionFunction · 0.70
SubscribeMethod · 0.65
RespondMethod · 0.65
CloseMethod · 0.45
RequestMethod · 0.45

Tested by

no test coverage detected