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

Function TesEncodedConnRequest

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

Source from the content-addressed store, hash-verified

620}
621
622func TesEncodedConnRequest(t *testing.T) {
623 ts := RunServerOnPort(ENC_TEST_PORT)
624 defer ts.Shutdown()
625
626 dch := make(chan bool)
627
628 opts := options
629 nc, _ := opts.Connect()
630 defer nc.Close()
631
632 c, err := nats.NewEncodedConn(nc, protobuf.PROTOBUF_ENCODER)
633 if err != nil {
634 t.Fatalf("Unable to create encoded connection: %v", err)
635 }
636 defer c.Close()
637
638 sentName := "Ivan"
639 recvName := "Kozlovic"
640
641 if _, err := c.Subscribe("foo", func(_, reply string, p *testdata.Person) {
642 if p.Name != sentName {
643 t.Fatalf("Got wrong name: %v instead of %v", p.Name, sentName)
644 }
645 c.Publish(reply, &testdata.Person{Name: recvName})
646 dch <- true
647 }); err != nil {
648 t.Fatalf("Unable to create subscription: %v", err)
649 }
650 if _, err := c.Subscribe("foo", func(_ string, p *testdata.Person) {
651 if p.Name != sentName {
652 t.Fatalf("Got wrong name: %v instead of %v", p.Name, sentName)
653 }
654 dch <- true
655 }); err != nil {
656 t.Fatalf("Unable to create subscription: %v", err)
657 }
658
659 if err := c.Publish("foo", &testdata.Person{Name: sentName}); err != nil {
660 t.Fatalf("Unable to publish: %v", err)
661 }
662
663 if err := Wait(dch); err != nil {
664 t.Fatal("Did not get message")
665 }
666 if err := Wait(dch); err != nil {
667 t.Fatal("Did not get message")
668 }
669
670 response := &testdata.Person{}
671 if err := c.Request("foo", &testdata.Person{Name: sentName}, response, 2*time.Second); err != nil {
672 t.Fatalf("Unable to publish: %v", err)
673 }
674 if response.Name != recvName {
675 t.Fatalf("Wrong response: %v instead of %v", response.Name, recvName)
676 }
677
678 if err := Wait(dch); err != nil {
679 t.Fatal("Did not get message")

Callers

nothing calls this directly

Calls 12

CloseMethod · 0.95
SubscribeMethod · 0.95
PublishMethod · 0.95
RequestMethod · 0.95
QueueSubscribeMethod · 0.95
LastErrorMethod · 0.95
ConnectMethod · 0.80
FatalfMethod · 0.80
RunServerOnPortFunction · 0.70
WaitFunction · 0.70
PublishMsgMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected