Ejemplo n.º 1
0
func TestPopulator(t *testing.T) {

	g := Goblin(t)
	RegisterFailHandler(func(m string, _ ...int) { g.Fail(m) })

	g.Describe("stomp connection mock", func() {

		var headers stompngo.Headers
		var stomp = New()
		var message string

		g.BeforeEach(func() {
			stomp.Clear()

			// broker headers
			headers = stompngo.Headers{
				"accept-version", "1.1",
				"login", "admin",
				"passcode", "1234",
				"host", "localhost",
			}

			// message headers
			headers = stompngo.Headers{
				"persistent", "true",
				"destination", "/queue/funny",
			}
			message = "Foo Bar"
		})

		g.It("should be successful with all headers present", func() {
			Expect(stomp.Send(headers, message)).To(BeNil())
		})

		g.It("should not be successful if the destination header is blank", func() {
			headers = headers.Delete("destination")
			Expect(stomp.Send(headers, message)).NotTo(BeNil())
		})

		g.It("should answer if it is connected", func() {
			Expect(stomp.Connected()).To(BeTrue())
		})

		g.It("should be able to get messages back afterwards", func() {
			// expected behavior adding to chan
			for i := 0; i < 1000; i++ {
				Expect(stomp.Send(headers, message)).To(BeNil())
			}

			// should be messages in the chan
			Expect(len(stomp.Messages)).To(Equal(1000))

			// pop the messages off of the chan and verify
			for i := 0; i < 1000; i++ {
				msg := <-stomp.Messages
				expectedMessage := &MockStompMessage{
					Order: i,
					Headers: []string{
						"persistent",
						"true",
						"destination",
						"/queue/funny",
					},
					Message: "Foo Bar",
				}

				Expect(msg).To(Equal(*expectedMessage))
			}
		})

		g.It("should allow for a disconnect request", func() {
			err := stomp.Disconnect(stompngo.Headers{})
			Expect(err).NotTo(HaveOccurred())
			Expect(stomp.DisconnectCalled).To(BeTrue())
		})

		g.It("should allow a subscription", func() {
			Expect(stomp.SubscribeCalled).To(BeFalse())
			sub, err := stomp.Subscribe(stompngo.Headers{})
			Expect(err).NotTo(HaveOccurred())
			Expect(stomp.Subscription).ToNot(BeNil())
			Expect(sub).To(Equal(stomp.Subscription))
			Expect(stomp.SubscribeCalled).To(BeTrue())

			msg := stompngo.MessageData{
				Message: stompngo.Message{
					Body: []uint8(message),
				},
			}
			stomp.PutToSubscribe(msg)
			outMsg := <-sub
			Expect(outMsg.Message.BodyString()).To(Equal(message))
			Expect(string(outMsg.Message.Body)).To(Equal(message))
		})

		g.It("should allow an unsubscribe", func() {
			err := stomp.Unsubscribe(stompngo.Headers{})
			Expect(err).NotTo(HaveOccurred())
			Expect(stomp.Subscription).To(BeNil())
		})
	})
}