func TestNilConnection(t *testing.T) { var nc *nats.Conn data := []byte("ok") // Publish if err := nc.Publish("foo", data); err == nil || err != nats.ErrInvalidConnection { t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) } if err := nc.PublishMsg(nil); err == nil || err != nats.ErrInvalidMsg { t.Fatalf("Expected ErrInvalidMsg error, got %v\n", err) } if err := nc.PublishMsg(&nats.Msg{}); err == nil || err != nats.ErrInvalidConnection { t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) } if err := nc.PublishRequest("foo", "reply", data); err == nil || err != nats.ErrInvalidConnection { t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) } // Subscribe if _, err := nc.Subscribe("foo", nil); err == nil || err != nats.ErrInvalidConnection { t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) } if _, err := nc.SubscribeSync("foo"); err == nil || err != nats.ErrInvalidConnection { t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) } if _, err := nc.QueueSubscribe("foo", "bar", nil); err == nil || err != nats.ErrInvalidConnection { t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) } ch := make(chan *nats.Msg) if _, err := nc.ChanSubscribe("foo", ch); err == nil || err != nats.ErrInvalidConnection { t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) } if _, err := nc.ChanQueueSubscribe("foo", "bar", ch); err == nil || err != nats.ErrInvalidConnection { t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) } if _, err := nc.QueueSubscribeSyncWithChan("foo", "bar", ch); err == nil || err != nats.ErrInvalidConnection { t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) } // Flush if err := nc.Flush(); err == nil || err != nats.ErrInvalidConnection { t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) } if err := nc.FlushTimeout(time.Millisecond); err == nil || err != nats.ErrInvalidConnection { t.Fatalf("Expected ErrInvalidConnection error, got %v\n", err) } // Nil Subscribers var sub *nats.Subscription if sub.Type() != nats.NilSubscription { t.Fatalf("Got wrong type for nil subscription, %v\n", sub.Type()) } if sub.IsValid() { t.Fatalf("Expected IsValid() to return false") } if err := sub.Unsubscribe(); err == nil || err != nats.ErrBadSubscription { t.Fatalf("Expected Unsubscribe to return proper error, got %v\n", err) } if err := sub.AutoUnsubscribe(1); err == nil || err != nats.ErrBadSubscription { t.Fatalf("Expected ErrBadSubscription error, got %v\n", err) } if _, err := sub.NextMsg(time.Millisecond); err == nil || err != nats.ErrBadSubscription { t.Fatalf("Expected ErrBadSubscription error, got %v\n", err) } if _, err := sub.QueuedMsgs(); err == nil || err != nats.ErrBadSubscription { t.Fatalf("Expected ErrBadSubscription error, got %v\n", err) } if _, _, err := sub.Pending(); err == nil || err != nats.ErrBadSubscription { t.Fatalf("Expected ErrBadSubscription error, got %v\n", err) } if _, _, err := sub.MaxPending(); err == nil || err != nats.ErrBadSubscription { t.Fatalf("Expected ErrBadSubscription error, got %v\n", err) } if err := sub.ClearMaxPending(); err == nil || err != nats.ErrBadSubscription { t.Fatalf("Expected ErrBadSubscription error, got %v\n", err) } if _, _, err := sub.PendingLimits(); err == nil || err != nats.ErrBadSubscription { t.Fatalf("Expected ErrBadSubscription error, got %v\n", err) } if err := sub.SetPendingLimits(1, 1); err == nil || err != nats.ErrBadSubscription { t.Fatalf("Expected ErrBadSubscription error, got %v\n", err) } if _, err := sub.Delivered(); err == nil || err != nats.ErrBadSubscription { t.Fatalf("Expected ErrBadSubscription error, got %v\n", err) } if _, err := sub.Dropped(); err == nil || err != nats.ErrBadSubscription { t.Fatalf("Expected ErrBadSubscription error, got %v\n", err) } }
func connect(conn *nats.Conn, subj string, timeout time.Duration) (*nats.Msg, string, error) { inbox := nats.NewInbox() s, err := conn.Subscribe(inbox, nil) if err != nil { return nil, "", err } s.AutoUnsubscribe(1) err = conn.PublishRequest(subj, inbox, nil) if err != nil { return nil, "", err } msg, err := s.NextMsg(timeout) if err != nil { return nil, "", err } return msg, inbox, nil }
}) }) Context("when a greeting message is received", func() { BeforeEach(func() { process = ifrit.Invoke(sub) Eventually(process.Ready()).Should(BeClosed()) }) It("responds", func() { msgChan := make(chan *nats.Msg, 1) _, err := natsClient.ChanSubscribe("router.greet.test.response", msgChan) Expect(err).ToNot(HaveOccurred()) err = natsClient.PublishRequest("router.greet", "router.greet.test.response", []byte{}) Expect(err).ToNot(HaveOccurred()) var msg *nats.Msg Eventually(msgChan).Should(Receive(&msg)) Expect(msg).ToNot(BeNil()) var message common.RouterStart err = json.Unmarshal(msg.Data, &message) Expect(err).ToNot(HaveOccurred()) Expect(message.Id).To(Equal(subOpts.ID)) Expect(message.Hosts).ToNot(BeEmpty()) Expect(message.MinimumRegisterIntervalInSeconds).To(Equal(subOpts.MinimumRegisterIntervalInSeconds)) Expect(message.PruneThresholdInSeconds).To(Equal(subOpts.PruneThresholdInSeconds)) })
Expect(err).ToNot(HaveOccurred()) } } }) Context("NATS", func() { Context("Router Greetings", func() { It("RouterGreets", func() { response := make(chan []byte) mbusClient.Subscribe("router.greet.test.response", func(msg *nats.Msg) { response <- msg.Data }) mbusClient.PublishRequest("router.greet", "router.greet.test.response", []byte{}) var msg []byte Eventually(response).Should(Receive(&msg)) var message common.RouterStart err := json.Unmarshal(msg, &message) Expect(err).NotTo(HaveOccurred()) Expect(message.MinimumRegisterIntervalInSeconds).To(Equal(5)) Expect(message.PruneThresholdInSeconds).To(Equal(120)) }) It("handles a empty reply on greet", func() { err := mbusClient.PublishRequest("router.greet", "", []byte{}) Expect(err).NotTo(HaveOccurred())
_, err = mbusClient.Subscribe("subject", func(msg *nats.Msg) { defer GinkgoRecover() data := make(map[string]interface{}) err := json.Unmarshal(msg.Data, &data) Expect(err).ToNot(HaveOccurred()) for _, key := range members { _, ok := data[key] Expect(ok).To(BeTrue()) } close(done) }) Expect(err).ToNot(HaveOccurred()) err = mbusClient.PublishRequest("vcap.component.discover", "subject", []byte("")) Expect(err).ToNot(HaveOccurred()) Eventually(done).Should(BeClosed()) }) It("publishes to vcap.component.announce on start-up", func() { done := make(chan struct{}) members := []string{ "type", "index", "host", "credentials", "start", "uuid", "uptime",