Ejemplo n.º 1
0
	})

	Describe("Emit", func() {
		var (
			heartbeatResponder emitter.RespondingByteEmitter
			testData           = []byte("hello")
		)

		BeforeEach(func() {
			heartbeatResponder, _ = emitter.NewHeartbeatResponder(wrappedEmitter, origin)
		})

		It("delegates to the wrapped emitter", func() {
			heartbeatResponder.Emit(testData)

			messages := wrappedEmitter.GetMessages()
			Expect(messages).To(HaveLen(1))
			Expect(messages[0]).To(Equal(testData))
		})

		It("increments the heartbeat counter", func() {
			id, _ := uuid.NewV4()

			heartbeatRequest := &control.ControlMessage{
				Origin:      proto.String("test"),
				Identifier:  factories.NewControlUUID(id),
				Timestamp:   proto.Int64(time.Now().UnixNano()),
				ControlType: control.ControlMessage_HeartbeatRequest.Enum(),
			}
			heartbeatResponder.Emit(testData)
			heartbeatResponder.Respond(heartbeatRequest)
	})

	Describe("NewHeartbeatEmitter", func() {
		It("requires non-nil args", func() {
			hbEmitter, err := emitter.NewHeartbeatEmitter(nil, origin)
			Expect(err).To(HaveOccurred())
			Expect(err.Error()).To(Equal("wrappedEmitter is nil"))
			Expect(hbEmitter).To(BeNil())
		})

		It("starts periodic heartbeat emission", func() {
			hbEmitter, err := emitter.NewHeartbeatEmitter(wrappedEmitter, origin)
			Expect(err).NotTo(HaveOccurred())
			Expect(hbEmitter).NotTo(BeNil())

			Eventually(func() int { return len(wrappedEmitter.GetMessages()) }).Should(BeNumerically(">=", 2))
		})

		It("logs an error when heartbeat emission fails", func() {
			wrappedEmitter.ReturnError = errors.New("fake error")

			logWriter := new(bytes.Buffer)
			log.SetOutput(logWriter)

			hbEmitter, _ := emitter.NewHeartbeatEmitter(wrappedEmitter, origin)

			Eventually(func() int { return len(wrappedEmitter.GetMessages()) }).Should(BeNumerically(">=", 2))

			loggedText := string(logWriter.Bytes())
			expectedText := "fake error"
			Expect(loggedText).To(ContainSubstring(expectedText))