github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/broker/federation/choria_nats_egest_test.go (about) 1 // Copyright (c) 2017-2022, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package federation 6 7 import ( 8 "context" 9 10 "github.com/choria-io/go-choria/integration/testutil" 11 "github.com/choria-io/go-choria/protocol" 12 . "github.com/onsi/ginkgo/v2" 13 . "github.com/onsi/gomega" 14 "github.com/onsi/gomega/gbytes" 15 log "github.com/sirupsen/logrus" 16 ) 17 18 var _ = Describe("Choria NATS Egest", func() { 19 var ( 20 request protocol.Request 21 reply protocol.Reply 22 sreply protocol.SecureReply 23 connector *pooledWorker 24 manager *stubConnectionManager 25 in chainmessage 26 logbuf *gbytes.Buffer 27 logger *log.Entry 28 ctx context.Context 29 cancel func() 30 ) 31 32 BeforeEach(func() { 33 ctx, cancel = context.WithCancel(context.Background()) 34 var gblogger *log.Logger 35 logbuf, gblogger = testutil.GbytesLogger(log.DebugLevel) 36 logger = log.NewEntry(gblogger) 37 38 rid, err := c.NewRequestID() 39 Expect(err).ToNot(HaveOccurred()) 40 41 request, err = c.NewRequest(protocol.RequestV1, "test", "tester", "choria=tester", 60, rid, "mcollective") 42 Expect(err).ToNot(HaveOccurred()) 43 request.SetMessage([]byte(`{"hello":"world"}`)) 44 45 reply, err = c.NewReply(request) 46 Expect(err).ToNot(HaveOccurred()) 47 48 sreply, err = c.NewSecureReply(reply) 49 Expect(err).ToNot(HaveOccurred()) 50 51 in = chainmessage{} 52 in.Message, err = c.NewTransportForSecureReply(sreply) 53 Expect(err).ToNot(HaveOccurred()) 54 55 broker, _ := NewFederationBroker("test", c) 56 connector, err = NewChoriaNatsEgest(1, Unconnected, 10, broker, logger) 57 Expect(err).ToNot(HaveOccurred()) 58 59 manager = &stubConnectionManager{} 60 connector.connection = manager 61 62 go connector.Run(ctx) 63 }) 64 65 AfterEach(func() { 66 cancel() 67 }) 68 69 It("Should send the message to every target", func() { 70 in.RequestID = "80a1ac20463745c0b12cfe6e3db61dff" 71 in.Targets = []string{"target.1", "target.2"} 72 73 connector.in <- in 74 75 Eventually(logbuf).Should(gbytes.Say("Publishing message '80a1ac20463745c0b12cfe6e3db61dff' to 2 target\\(s\\)")) 76 77 j, _ := in.Message.JSON() 78 79 msg := <-manager.connection.Outq 80 Expect(msg[0]).To(Equal("target.1")) 81 Expect(msg[1]).To(Equal(string(j))) 82 83 msg = <-manager.connection.Outq 84 Expect(msg[0]).To(Equal("target.2")) 85 Expect(msg[1]).To(Equal(string(j))) 86 }) 87 88 It("Should discard messages with no targets", func() { 89 in.RequestID = "80a1ac20463745c0b12cfe6e3db61dff" 90 connector.in <- in 91 92 Eventually(logbuf).Should(gbytes.Say("Received message '80a1ac20463745c0b12cfe6e3db61dff' with no targets, discarding")) 93 }) 94 95 It("Should support Quit", func() { 96 cancel() 97 Eventually(logbuf).Should(gbytes.Say("Worker routine choria_nats_egest exiting")) 98 }) 99 })