func NewStartStopListener(messageBus yagnats.NATSConn, conf *config.Config) *StartStopListener { listener := &StartStopListener{ messageBus: messageBus, } messageBus.Subscribe(conf.SenderNatsStartSubject, func(message *nats.Msg) { startMessage, err := models.NewStartMessageFromJSON([]byte(message.Data)) if err != nil { panic(err) } listener.mutex.Lock() listener.starts = append(listener.starts, startMessage) listener.mutex.Unlock() }) messageBus.Subscribe(conf.SenderNatsStopSubject, func(message *nats.Msg) { stopMessage, err := models.NewStopMessageFromJSON([]byte(message.Data)) if err != nil { panic(err) } listener.mutex.Lock() listener.stops = append(listener.stops, stopMessage) listener.mutex.Unlock() }) return listener }
func NewStartStopListener(messageBus yagnats.NATSClient, conf *config.Config) *StartStopListener { listener := &StartStopListener{ messageBus: messageBus, } messageBus.Subscribe(conf.SenderNatsStartSubject, func(message *yagnats.Message) { startMessage, err := models.NewStartMessageFromJSON([]byte(message.Payload)) if err != nil { panic(err) } listener.Starts = append(listener.Starts, startMessage) }) messageBus.Subscribe(conf.SenderNatsStopSubject, func(message *yagnats.Message) { stopMessage, err := models.NewStopMessageFromJSON([]byte(message.Payload)) if err != nil { panic(err) } listener.Stops = append(listener.Stops, stopMessage) }) return listener }
}) It("should leave the messages in the queue", func() { messages, _ := store.GetPendingStartMessages() Ω(messages).Should(HaveLen(1)) }) }) Context("and it is time to send the message", func() { BeforeEach(func() { timeProvider.TimeToProvide = time.Unix(130, 0) }) It("should send the message", func() { Ω(messageBus.PublishedMessages["hm9000.start"]).Should(HaveLen(1)) message, _ := models.NewStartMessageFromJSON([]byte(messageBus.PublishedMessages["hm9000.start"][0].Payload)) Ω(message).Should(Equal(models.StartMessage{ AppGuid: app.AppGuid, AppVersion: app.AppVersion, InstanceIndex: 0, MessageId: pendingMessage.MessageId, })) }) It("should increment the metrics for that message", func() { Ω(metricsAccountant.IncrementedStarts).Should(ContainElement(pendingMessage)) }) It("should not error", func() { Ω(err).ShouldNot(HaveOccurred()) })
}) It("should leave the messages in the queue", func() { messages, _ := store.GetPendingStartMessages() Ω(messages).Should(HaveLen(1)) }) }) Context("and it is time to send the message", func() { BeforeEach(func() { timeProvider.TimeToProvide = time.Unix(130, 0) }) It("should send the message", func() { Ω(messageBus.PublishedMessages("hm9000.start")).Should(HaveLen(1)) message, _ := models.NewStartMessageFromJSON([]byte(messageBus.PublishedMessages("hm9000.start")[0].Data)) Ω(message).Should(Equal(models.StartMessage{ AppGuid: app.AppGuid, AppVersion: app.AppVersion, InstanceIndex: 0, MessageId: pendingMessage.MessageId, })) }) It("should increment the metrics for that message", func() { Ω(metricsAccountant.IncrementedStarts).Should(ContainElement(pendingMessage)) }) It("should not error", func() { Ω(err).ShouldNot(HaveOccurred()) })