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

Function TestRequestGOB

test/enc_test.go:719–759  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

717}
718
719func TestRequestGOB(t *testing.T) {
720 ts := RunServerOnPort(ENC_TEST_PORT)
721 defer ts.Shutdown()
722
723 type Request struct {
724 Name string
725 }
726
727 type Person struct {
728 Name string
729 Age int
730 }
731
732 nc, err := nats.Connect(options.Url)
733 if err != nil {
734 t.Fatalf("Could not connect: %v", err)
735 }
736 defer nc.Close()
737
738 ec, err := nats.NewEncodedConn(nc, nats.GOB_ENCODER)
739 if err != nil {
740 t.Fatalf("Unable to create encoded connection: %v", err)
741 }
742 defer ec.Close()
743
744 ec.QueueSubscribe("foo.request", "g", func(subject, reply string, r *Request) {
745 if r.Name != "meg" {
746 t.Fatalf("Expected request to be 'meg', got %q", r)
747 }
748 response := &Person{Name: "meg", Age: 21}
749 ec.Publish(reply, response)
750 })
751
752 reply := Person{}
753 if err := ec.Request("foo.request", &Request{Name: "meg"}, &reply, time.Second); err != nil {
754 t.Fatalf("Failed to receive response: %v", err)
755 }
756 if reply.Name != "meg" || reply.Age != 21 {
757 t.Fatalf("Did not receive proper response, %+v", reply)
758 }
759}
760
761func TestContextEncodedRequestWithTimeout(t *testing.T) {
762 s := RunDefaultServer()

Callers

nothing calls this directly

Calls 8

CloseMethod · 0.95
QueueSubscribeMethod · 0.95
PublishMethod · 0.95
RequestMethod · 0.95
ConnectMethod · 0.80
FatalfMethod · 0.80
RunServerOnPortFunction · 0.70
CloseMethod · 0.45

Tested by

no test coverage detected