Example #1
0
func streamEvents(c *cli.Context) {
	issues := checkFlags(c)
	issues = append(issues, checkArguments(c, "events")...)

	if len(issues) > 0 {
		printHelpForCommand(c, issues, "events")
	}

	client := routing_api.NewClient(c.String("api"))

	config := buildOauthConfig(c)
	fetcher := token_fetcher.NewTokenFetcher(&config)
	eventSource, err := commands.Events(client, fetcher)
	if err != nil {
		fmt.Println("streaming events failed:", err)
		os.Exit(3)
	}

	for {
		e, err := eventSource.Next()
		if err != nil {
			fmt.Fprintf(os.Stderr, "Connection closed: %s", err.Error())
			break
		}

		event, _ := json.Marshal(e)
		fmt.Printf("%v\n", string(event))
	}
}
	var (
		client       *fake_routing_api.FakeClient
		tokenFetcher *fake_token_fetcher.FakeTokenFetcher
		eventSource  routing_api.EventSource
	)

	BeforeEach(func() {
		client = &fake_routing_api.FakeClient{}
		tokenFetcher = &fake_token_fetcher.FakeTokenFetcher{}
		tokenFetcher.FetchTokenReturns(&token_fetcher.Token{AccessToken: "token"}, nil)
		eventSource = &fake_routing_api.FakeEventSource{}
		client.SubscribeToEventsReturns(eventSource, nil)
	})

	It("list events", func() {
		eventsList, _ := commands.Events(client, tokenFetcher)
		Expect(client.SubscribeToEventsCallCount()).To(Equal(1))
		Expect(eventsList).To(Equal(eventSource))
	})

	It("fetches a token and sets it on the client", func() {
		commands.Events(client, tokenFetcher)
		Expect(client.SetTokenCallCount()).To(Equal(1))
		Expect(client.SetTokenArgsForCall(0)).To(Equal("token"))
	})

	Context("when SubscribeToEvents returns an error", func() {
		BeforeEach(func() {
			client.SubscribeToEventsReturns(nil, errors.New("Boom"))
		})