Example #1
0
func (r *Router) Run() <-chan error {
	r.registry.StartPruningCycle()

	r.RegisterComponent()

	// Subscribe register/unregister router
	r.SubscribeRegister()
	r.HandleGreetings()
	r.SubscribeUnregister()

	// Kickstart sending start messages
	r.SendStartMessage()

	// Send start again on reconnect
	r.mbusClient.ConnectedCallback = func() {
		r.SendStartMessage()
	}

	// Schedule flushing active app's app_id
	r.ScheduleFlushApps()

	// Wait for one start message send interval, such that the router's registry
	// can be populated before serving requests.
	if r.config.StartResponseDelayInterval != 0 {
		r.logger.Infof("Waiting %s before listening...", r.config.StartResponseDelayInterval)
		time.Sleep(r.config.StartResponseDelayInterval)
	}

	server := http.Server{
		Handler: autowire.InstrumentedHandler(r.proxy),
	}

	errChan := make(chan error, 1)

	listener, err := net.Listen("tcp", fmt.Sprintf(":%d", r.config.Port))
	if err != nil {
		r.logger.Fatalf("net.Listen: %s", err)
		errChan <- err
		return errChan
	}

	r.listener = listener
	r.logger.Infof("Listening on %s", listener.Addr())

	go func() {
		err := server.Serve(listener)
		errChan <- err
	}()

	return errChan
}
					if envelope.GetOrigin() != origin {
						panic("origin not as expected")
					}

					func() {
						lock.Lock()
						defer lock.Unlock()
						receivedEvents[eventId] = true
					}()
				}
			}()

			httpListener, err := net.Listen("tcp", "localhost:0")
			Expect(err).ToNot(HaveOccurred())
			defer httpListener.Close()
			httpHandler := autowire.InstrumentedHandler(FakeHandler{})
			go http.Serve(httpListener, httpHandler)

			_, err = http.Get("http://" + httpListener.Addr().String())
			Expect(err).ToNot(HaveOccurred())

			expectedEventTypes := []string{"HttpStartClient", "HttpStartServer", "HttpStopServer", "HttpStopClient"}

			for _, eventType := range expectedEventTypes {
				Eventually(func() bool {
					lock.RLock()
					defer lock.RUnlock()
					_, ok := receivedEvents[eventType]
					return ok
				}).Should(BeTrue())
			}
		})

		It("sets http.DefaultTransport to the non-instrumented default", func() {
			os.Setenv("DROPSONDE_ORIGIN", "")
			autowire.Initialize()

			Expect(reflect.TypeOf(http.DefaultTransport).Elem().Name()).To(Equal("Transport"))
		})

		Describe("InstrumentedHandler", func() {
			It("returns the given Handler with no changes", func() {
				os.Setenv("DROPSONDE_ORIGIN", "")
				fake := FakeHandler{}
				autowire.Initialize()

				Expect(autowire.InstrumentedHandler(fake)).To(Equal(fake))
			})
		})

		Describe("InstrumentedRoundTripper", func() {
			It("returns the given RoundTripper with no changes", func() {
				fake := FakeRoundTripper{}
				os.Setenv("DROPSONDE_ORIGIN", "")
				autowire.Initialize()

				Expect(autowire.InstrumentedRoundTripper(fake)).To(Equal(fake))
			})
		})
	})
})