Exemplo n.º 1
0
func (a *ASuite) TestAdvertiserAdvertisesUpdatedID(c *C) {
	mbus := mock_cfmessagebus.NewMockMessageBus()

	config := basil.Config{
		AdvertiseInterval: 100 * time.Millisecond,
	}

	advertisements := make(chan []byte)

	mbus.Subscribe("ssh.advertise", func(msg []byte) {
		advertisements <- msg
	})

	advertiser := NewAdvertiser(config)

	advertiser.Update(&State{ID: "some-unique-id"})

	advertiser.AdvertisePeriodically(mbus)

	msg1 := waitReceive(advertisements, 1*time.Second)
	c.Assert(string(msg1), Equals, `{"id":"some-unique-id"}`)

	advertiser.Update(&State{ID: "some-other-unique-id"})

	msg2 := waitReceive(advertisements, 1*time.Second)
	c.Assert(string(msg2), Equals, `{"id":"some-other-unique-id"}`)
}
Exemplo n.º 2
0
func (s *RSuite) TestRouterClientGreeting(c *C) {
	mbus := mock_cfmessagebus.NewMockMessageBus()

	routerClient := NewRouterClient("1.2.3.4", mbus)

	times := make(chan time.Time)

	routerClient.Periodically(func() {
		times <- time.Now()
	})

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

	err := routerClient.Greet()
	c.Assert(err, IsNil)

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

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

	c.Assert((*time2).Sub(*time1) >= 1*time.Second, Equals, true)
}
Exemplo n.º 3
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.º 4
0
func (a *ASuite) TestAdvertiserAdvertisesPeriodically(c *C) {
	mbus := mock_cfmessagebus.NewMockMessageBus()

	config := basil.Config{
		AdvertiseInterval: 100 * time.Millisecond,
	}

	advertisements := make(chan []byte)

	mbus.Subscribe("ssh.advertise", func(msg []byte) {
		advertisements <- msg
	})

	advertiser := NewAdvertiser(config)

	advertiser.Update(&State{ID: "some-unique-id"})

	advertiser.AdvertisePeriodically(mbus)

	msg1 := waitReceive(advertisements, 1*time.Second)
	c.Assert(msg1, NotNil)

	time1 := time.Now()

	msg2 := waitReceive(advertisements, 1*time.Second)
	c.Assert(msg2, NotNil)

	time2 := time.Now()

	c.Assert(time2.Sub(time1) >= 100*time.Millisecond, Equals, true)
}
Exemplo n.º 5
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.º 6
0
func (s *WSuite) TestReactingToRouterStart(c *C) {
	watcher := basil.NewStateWatcher(s.stateFile.Name())

	mbus := mock_cfmessagebus.NewMockMessageBus()

	err := ioutil.WriteFile(
		s.stateFile.Name(),
		[]byte(`{"id":"abc","sessions":{"abc":{"port":123,"container":"foo"}}}`),
		0644,
	)
	c.Assert(err, IsNil)

	err = ReactTo(watcher, mbus, basil.DefaultConfig)
	c.Assert(err, IsNil)

	registered := make(chan time.Time)

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

	mbus.Publish("router.start", []byte(`{"minimumRegisterIntervalInSeconds":1}`))

	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.º 7
0
func (s *WSuite) TestHandlingInitialState(c *C) {
	watcher := basil.NewStateWatcher(s.stateFile.Name())

	mbus := mock_cfmessagebus.NewMockMessageBus()

	err := ioutil.WriteFile(
		s.stateFile.Name(),
		[]byte(`{"id":"abc","sessions":{"abc":{"port":123,"container":"foo"}}}`),
		0644,
	)
	c.Assert(err, IsNil)

	registered := make(chan []byte)

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

	err = ReactTo(watcher, mbus, basil.DefaultConfig)
	c.Assert(err, IsNil)

	select {
	case msg := <-registered:
		c.Assert(string(msg), Equals, `{"uris":["abc"],"host":"127.0.0.1","port":123}`)
	case <-time.After(500 * time.Millisecond):
		c.Error("did not receive a router.register!")
	}
}
Exemplo n.º 8
0
func (s *WSuite) TestReactorSendsAdvertisements(c *C) {
	watcher := basil.NewStateWatcher(s.stateFile.Name())

	mbus := mock_cfmessagebus.NewMockMessageBus()

	err := ioutil.WriteFile(
		s.stateFile.Name(),
		[]byte(`{"id":"abc","sessions":{}}`),
		0644,
	)
	c.Assert(err, IsNil)

	config := basil.DefaultConfig
	config.AdvertiseInterval = 100 * time.Millisecond

	err = ReactTo(watcher, mbus, config)
	c.Assert(err, IsNil)

	advertised := make(chan time.Time)

	mbus.Subscribe("ssh.advertise", func(msg []byte) {
		advertised <- time.Now()
	})

	time1 := timedReceive(advertised, 1*time.Second)
	c.Assert(time1, NotNil)

	time2 := timedReceive(advertised, 1*time.Second)
	c.Assert(time2, NotNil)

	c.Assert((*time2).Sub(*time1) >= 100*time.Millisecond, Equals, true)
}
Exemplo n.º 9
0
func (a *ASuite) TestAdvertiserDoesNotAdvertiseWithoutState(c *C) {
	mbus := mock_cfmessagebus.NewMockMessageBus()

	config := basil.Config{
		AdvertiseInterval: 100 * time.Millisecond,
	}

	advertisements := make(chan []byte)

	mbus.Subscribe("ssh.advertise", func(msg []byte) {
		advertisements <- msg
	})

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

	msg1 := waitReceive(advertisements, 1*time.Second)
	c.Assert(msg1, IsNil)
}
Exemplo n.º 10
0
func (s *RSuite) TestRouterClientUnregistering(c *C) {
	mbus := mock_cfmessagebus.NewMockMessageBus()

	routerClient := NewRouterClient("1.2.3.4", mbus)

	registered := make(chan []byte)

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

	routerClient.Unregister(123, []string{"abc"})

	select {
	case msg := <-registered:
		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.unregister!")
	}
}
Exemplo n.º 11
0
func (s *WSuite) TestReactorSendsAdvertisementsWithUpdatedID(c *C) {
	watcher := basil.NewStateWatcher(s.stateFile.Name())

	mbus := mock_cfmessagebus.NewMockMessageBus()

	err := ioutil.WriteFile(
		s.stateFile.Name(),
		[]byte(`{"id":"abc","sessions":{}}`),
		0644,
	)
	c.Assert(err, IsNil)

	config := basil.DefaultConfig
	config.AdvertiseInterval = 100 * time.Millisecond

	err = ReactTo(watcher, mbus, config)
	c.Assert(err, IsNil)

	advertised := make(chan []byte)

	mbus.Subscribe("ssh.advertise", func(msg []byte) {
		advertised <- msg
	})

	msg1 := waitReceive(advertised, 2*time.Second)
	c.Assert(string(msg1), Equals, `{"id":"abc"}`)

	err = ioutil.WriteFile(
		s.stateFile.Name(),
		[]byte(`{"id":"def","sessions":{}}`),
		0644,
	)
	c.Assert(err, IsNil)

	msg2 := waitReceive(advertised, 2*time.Second)
	c.Assert(string(msg2), Equals, `{"id":"def"}`)
}
Exemplo n.º 12
0
	            "version":"123",
	            "indices":[1,2],
	            "running":{"123":2},
	            "flapping":false
	        }`

	jsonStopMessage := `{
	            "droplet":"abc",
	            "last_updated":1377816348,
	            "version":"123",
	            "instances":{"xyz":"123", "uvw":"123"},
	            "running":{"123":2}
	        }`

	BeforeEach(func() {
		fakeMessageBus = mock_cfmessagebus.NewMockMessageBus()
		listener = NewStartStopListener(fakeMessageBus)
	})

	Describe("when a start message arrives", func() {
		It("adds a start message to its list", func() {
			expectedStart := StartMessage{
				AppGuid:                   "abc",
				LastUpdated:               1377816348,
				AppVersion:                "123",
				IndicesToStart:            []int{1, 2},
				RunningInstancesByVersion: map[string]int{"123": 2},
				Flapping:                  false,
			}

			fakeMessageBus.PublishSync("health.start", []byte(jsonStartMessage))