Ejemplo n.º 1
0
func main() {
	c := config.DefaultConfig()
	if configFile != "" {
		c = config.InitConfigFromFile(configFile)
	}

	log.SetupLoggerFromConfig(c)

	router.NewRouter(c).Run()

	select {}
}
func (s *IntegrationSuite) TestNatsConnectivity(c *C) {
	proxyPort := nextAvailPort()
	statusPort := nextAvailPort()

	s.Config = SpecConfig(s.natsPort, statusPort, proxyPort)

	// ensure the threshold is longer than the interval that we check,
	// because we set the route's timestamp to time.Now() on the interval
	// as part of pausing
	s.Config.PruneStaleDropletsInterval = 1 * time.Second
	s.Config.DropletStaleThreshold = 2 * s.Config.PruneStaleDropletsInterval

	log.SetupLoggerFromConfig(s.Config)

	s.router = NewRouter(s.Config)

	s.router.Run()

	s.mbusClient = s.router.mbusClient

	staleCheckInterval := s.Config.PruneStaleDropletsInterval
	staleThreshold := s.Config.DropletStaleThreshold

	s.Config.DropletStaleThreshold = staleThreshold

	zombieApp := test.NewGreetApp([]route.Uri{"zombie.vcap.me"}, proxyPort, s.mbusClient, nil)
	zombieApp.Listen()

	runningApp := test.NewGreetApp([]route.Uri{"innocent.bystander.vcap.me"}, proxyPort, s.mbusClient, nil)
	runningApp.Listen()

	c.Assert(s.waitAppRegistered(zombieApp, 2*time.Second), Equals, true)
	c.Assert(s.waitAppRegistered(runningApp, 2*time.Second), Equals, true)

	heartbeatInterval := 200 * time.Millisecond
	zombieTicker := time.NewTicker(heartbeatInterval)
	runningTicker := time.NewTicker(heartbeatInterval)

	go func() {
		for {
			select {
			case <-zombieTicker.C:
				zombieApp.Register()
			case <-runningTicker.C:
				runningApp.Register()
			}
		}
	}()

	zombieApp.VerifyAppStatus(200, c)

	// Give enough time to register multiple times
	time.Sleep(heartbeatInterval * 3)

	// kill registration ticker => kill app (must be before stopping NATS since app.Register is fake and queues messages in memory)
	zombieTicker.Stop()

	natsPort := s.natsPort
	s.stopNats()

	// Give router time to make a bad decision (i.e. prune routes)
	time.Sleep(staleCheckInterval + staleThreshold + 250*time.Millisecond)

	// While NATS is down no routes should go down
	zombieApp.VerifyAppStatus(200, c)
	runningApp.VerifyAppStatus(200, c)

	s.startNats(natsPort)

	// Right after NATS starts up all routes should stay up
	zombieApp.VerifyAppStatus(200, c)
	runningApp.VerifyAppStatus(200, c)

	zombieGone := make(chan bool)

	go func() {
		for {
			// Finally the zombie is cleaned up. Maybe proactively enqueue Unregister events in DEA's.
			err := zombieApp.CheckAppStatus(404)
			if err != nil {
				time.Sleep(100 * time.Millisecond)
				continue
			}

			err = runningApp.CheckAppStatus(200)
			if err != nil {
				time.Sleep(100 * time.Millisecond)
				continue
			}

			zombieGone <- true

			break
		}
	}()

	select {
	case <-zombieGone:
	case <-time.After(staleCheckInterval + staleThreshold + 5*time.Second):
		c.Error("Zombie app was not pruned.")
	}
}