func NewEventFromBBS(bbsEvent models.Event) (receptor.Event, error) {
	switch bbsEvent := bbsEvent.(type) {
	case *models.ActualLRPCreatedEvent:
		actualLRP, evacuating := bbsEvent.ActualLrpGroup.Resolve()
		return receptor.NewActualLRPCreatedEvent(serialization.ActualLRPProtoToResponse(actualLRP, evacuating)), nil
	case *models.ActualLRPChangedEvent:
		before, evacuating := bbsEvent.Before.Resolve()
		after, evacuating := bbsEvent.After.Resolve()
		return receptor.NewActualLRPChangedEvent(
			serialization.ActualLRPProtoToResponse(before, evacuating),
			serialization.ActualLRPProtoToResponse(after, evacuating),
		), nil
	case *models.ActualLRPRemovedEvent:
		actualLRP, evacuating := bbsEvent.ActualLrpGroup.Resolve()
		return receptor.NewActualLRPRemovedEvent(serialization.ActualLRPProtoToResponse(actualLRP, evacuating)), nil
	case *models.DesiredLRPCreatedEvent:
		return receptor.NewDesiredLRPCreatedEvent(serialization.DesiredLRPProtoToResponse(bbsEvent.DesiredLrp)), nil
	case *models.DesiredLRPChangedEvent:
		return receptor.NewDesiredLRPChangedEvent(
			serialization.DesiredLRPProtoToResponse(bbsEvent.Before),
			serialization.DesiredLRPProtoToResponse(bbsEvent.After),
		), nil
	case *models.DesiredLRPRemovedEvent:
		return receptor.NewDesiredLRPRemovedEvent(serialization.DesiredLRPProtoToResponse(bbsEvent.DesiredLrp)), nil
	}
	return nil, fmt.Errorf("unknown event type: %#v", bbsEvent)
}
Exemplo n.º 2
0
func (w *watcher) watchActual(logger lager.Logger) (chan<- bool, <-chan error) {
	return w.bbs.WatchForActualLRPChanges(logger,
		func(created models.ActualLRP, evacuating bool) {
			logger.Debug("handling-actual-create")
			w.hub.Emit(receptor.NewActualLRPCreatedEvent(serialization.ActualLRPToResponse(created, evacuating)))
		},
		func(changed models.ActualLRPChange, evacuating bool) {
			logger.Debug("handling-actual-change")
			w.hub.Emit(receptor.NewActualLRPChangedEvent(
				serialization.ActualLRPToResponse(changed.Before, evacuating),
				serialization.ActualLRPToResponse(changed.After, evacuating),
			))
		},
		func(deleted models.ActualLRP, evacuating bool) {
			logger.Debug("handling-actual-delete")
			w.hub.Emit(receptor.NewActualLRPRemovedEvent(serialization.ActualLRPToResponse(deleted, evacuating)))
		})
}
			BeforeEach(func() {
				actualLRPResponse = serialization.ActualLRPProtoToResponse(
					&models.ActualLRP{
						ActualLRPKey: models.NewActualLRPKey("some-guid", 0, "some-domain"),
						State:        models.ActualLRPStateUnclaimed,
						Since:        1,
					},
					false,
				)
			})

			Context("when receiving a ActualLRPCreatedEvent", func() {
				var expectedEvent receptor.ActualLRPCreatedEvent

				BeforeEach(func() {
					expectedEvent = receptor.NewActualLRPCreatedEvent(actualLRPResponse)
					payload, err := json.Marshal(expectedEvent)
					Expect(err).NotTo(HaveOccurred())

					fakeRawEventSource.NextReturns(
						sse.Event{
							ID:   "sup",
							Name: string(expectedEvent.EventType()),
							Data: payload,
						},
						nil,
					)
				})

				It("returns the event", func() {
					event, err := eventSource.Next()
				eventChannel <- models.NewDesiredLRPCreatedEvent(desiredLRP)

				data, err := json.Marshal(receptor.NewDesiredLRPCreatedEvent(serialization.DesiredLRPProtoToResponse(desiredLRP)))
				Expect(err).NotTo(HaveOccurred())

				event, err := reader.Next()
				Expect(err).NotTo(HaveOccurred())
				Expect(event.ID).To(Equal("0"))
				Expect(event.Name).To(Equal(string(receptor.EventTypeDesiredLRPCreated)))
				Expect(event.Data).To(MatchJSON(data))

				actualLRP := models.NewUnclaimedActualLRP(models.NewActualLRPKey("some-guid", 3, "some-domain"), 0)
				actualLRPGroup := &models.ActualLRPGroup{Instance: actualLRP}
				eventChannel <- models.NewActualLRPCreatedEvent(actualLRPGroup)

				data, err = json.Marshal(receptor.NewActualLRPCreatedEvent(serialization.ActualLRPProtoToResponse(actualLRP, false)))
				Expect(err).NotTo(HaveOccurred())

				event, err = reader.Next()
				Expect(err).NotTo(HaveOccurred())
				Expect(event.ID).To(Equal("1"))
				Expect(event.Name).To(Equal(string(receptor.EventTypeActualLRPCreated)))
				Expect(event.Data).To(MatchJSON(data))
				eventChannel <- eventfakes.FakeEvent{"B"}

				close(done)
			})

			It("returns Content-Type as text/event-stream", func() {
				response := &http.Response{}
				Eventually(responseChan).Should(Receive(&response))