Exemple #1
0
func (r *Reader) Init(config jsonstruct.JSONStruct) error {
	port := config.IntWithDefault(Port, DefaultPort)
	remoteAddr := config.StringWithDefault(RemoteWriterAddr, DefaultRemoteWriterAddr)
	resp, err := http.DefaultClient.Get(fmt.Sprintf("http://%s:%d/", remoteAddr, port))
	if err != nil {
		return err
	}

	r.sseReader = vitosse.NewReadCloser(resp.Body)

	return nil
}
Exemple #2
0
			Expect(err).NotTo(HaveOccurred())
		})

		Context("when failing to subscribe to the event hub", func() {
			BeforeEach(func() {
				hub.Close()
			})

			It("returns an internal server error", func() {
				Expect(response.StatusCode).To(Equal(http.StatusInternalServerError))
			})
		})

		Context("when successfully subscribing to the event hub", func() {
			It("emits events from the hub to the connection", func() {
				reader := sse.NewReadCloser(response.Body)

				hub.Emit(&eventfakes.FakeEvent{"A"})
				encodedPayload := base64.StdEncoding.EncodeToString([]byte("A"))

				Expect(reader.Next()).To(Equal(sse.Event{
					ID:   "0",
					Name: "fake",
					Data: []byte(encodedPayload),
				}))

				hub.Emit(&eventfakes.FakeEvent{"B"})

				encodedPayload = base64.StdEncoding.EncodeToString([]byte("B"))
				Expect(reader.Next()).To(Equal(sse.Event{
					ID:   "1",
Exemple #3
0
			Expect(count).To(Equal(10))

			count, err = writer.Write(message2)
			Expect(err).NotTo(HaveOccurred())
			Expect(count).To(Equal(1024))

			err = writer.Close()
			Expect(err).NotTo(HaveOccurred())

			close(done)
		}()

		resp, err := http.Get(fmt.Sprintf("http://localhost:%d", port))
		Expect(err).NotTo(HaveOccurred())

		reader := vitosse.NewReadCloser(resp.Body)

		event, err := reader.Next()
		Expect(err).NotTo(HaveOccurred())
		Expect(event.Data).To(Equal(message1))

		event, err = reader.Next()
		Expect(err).NotTo(HaveOccurred())
		Expect(event.Data).To(Equal(message2))

		Eventually(done).Should(BeClosed())
	}, 10)

	It("stops attempting to write when closed", func() {
		done := make(chan struct{})
		go func() {