Exemplo n.º 1
0
func (s *SRSuite) TestRegistrarUpdateUnregisters(c *C) {
	mbus := mock_cfmessagebus.NewMockMessageBus()

	routerClient := basil.NewRouterClient("1.2.3.4", mbus)

	unregistered := make(chan []byte)

	mbus.Subscribe("router.unregister", func(msg []byte) {
		unregistered <- msg
	})

	registrar := NewRegistrar(routerClient)
	registrar.Update(&State{
		ID: "foo",
		Sessions: map[string]Session{
			"abc": Session{
				Port: 123,
			},
		},
	})

	registrar.Update(&State{
		ID:       "foo",
		Sessions: map[string]Session{},
	})

	select {
	case msg := <-unregistered:
		c.Assert(string(msg), Equals, `{"uris":["abc"],"host":"1.2.3.4","port":123}`)
	case <-time.After(500 * time.Millisecond):
		c.Error("did not receive a router.register!")
	}
}
Exemplo n.º 2
0
func (s *SRSuite) TestPeriodicRegistration(c *C) {
	mbus := mock_cfmessagebus.NewMockMessageBus()

	routerClient := basil.NewRouterClient("1.2.3.4", mbus)

	registrar := NewRegistrar(routerClient)
	registrar.Update(&State{
		ID: "foo",
		Sessions: map[string]Session{
			"abc": Session{
				Port: 123,
			},
		},
	})

	registered := make(chan time.Time)
	mbus.Subscribe("router.register", func(msg []byte) {
		registered <- time.Now()
	})

	mbus.RespondToChannel("router.greet", func([]byte) []byte {
		return []byte(`{"minimumRegisterIntervalInSeconds":1}`)
	})

	err := registrar.RegisterPeriodically()
	c.Assert(err, IsNil)

	time1 := timedReceive(registered, 2*time.Second)
	c.Assert(time1, NotNil)

	time2 := timedReceive(registered, 2*time.Second)
	c.Assert(time2, NotNil)

	c.Assert((*time2).Sub(*time1) >= 1*time.Second, Equals, true)
}
Exemplo n.º 3
0
func ReactTo(watcher *basil.StateWatcher, mbus cf.MessageBus, config basil.Config) error {
	routerClient := basil.NewRouterClient(config.Host, mbus)

	registrar := NewRegistrar(routerClient)

	err := registrar.RegisterPeriodically()
	if err != nil {
		return err
	}

	advertiser := NewAdvertiser(config)
	advertiser.AdvertisePeriodically(mbus)

	return watcher.OnStateChange(func(body io.Reader) {
		state, err := ParseState(body)
		if err != nil {
			log.Printf("failed to parse sshark state: %s\n", err)
			return
		}

		registrar.Update(state)
		advertiser.Update(state)
	})
}