Example #1
0
func TestFlagsWork(t *testing.T) {

	db, err := openDB()
	if err != nil {
		log.Fatalln("Open database failed:", err)
	}

	defer db.Close()

	min_port_num := 1024
	max_port_num := 65535

	rand.Seed(time.Now().UnixNano())

	port := min_port_num + rand.Intn(max_port_num-min_port_num)

	log.Println("Use port", port)

	service := newDummyService("python-api/dummy_service.py", port)
	service.Stop() // if already run

	priv, err := vexillary.GenerateKey()
	if err != nil {
		log.Fatalln("Generate key failed:", err)
	}

	fillTestTeams(db.db)

	checker_path := "python-api/dummy_checker.py"

	for _, service := range []string{"Foo", "Bar", "Baz"} {

		err = steward.AddService(db.db,
			steward.Service{ID: -1, Name: service, Port: port,
				CheckerPath: checker_path, UDP: false})
		if err != nil {
			log.Fatalln("Add service failed:", err)
		}
	}

	round, err := steward.NewRound(db.db, time.Minute)
	if err != nil {
		log.Fatalln("Create new round failed:", err)
	}

	teams, err := steward.GetTeams(db.db)
	if err != nil {
		log.Fatalln("Get teams failed:", err)
	}

	services, err := steward.GetServices(db.db)
	if err != nil {
		log.Fatalln("Get services failed:", err)
	}

	err = checker.PutFlags(db.db, priv, round, teams, services)
	if err != nil {
		log.Fatalln("Put flags failed:", err)
	}

	// No services -> all down
	checkServicesStatus(db.db, round, teams, services, steward.StatusDown)

	// Start service
	service.Start()

	time.Sleep(time.Second)

	service.BrokeLogic()

	err = checker.PutFlags(db.db, priv, round, teams, services)
	if err != nil {
		log.Fatalln("Put flags failed:", err)
	}

	checkServicesStatus(db.db, round, teams, services, steward.StatusMumble)

	service.RestoreLogic()

	time.Sleep(time.Second)

	round, err = steward.NewRound(db.db, time.Minute)
	if err != nil {
		log.Fatalln("Create new round failed:", err)
	}

	log.Println("Put flags to correct service...")

	err = checker.PutFlags(db.db, priv, round, teams, services)
	if err != nil {
		log.Fatalln("Put flags failed:", err)
	}

	checkServicesStatus(db.db, round, teams, services, steward.StatusUP)

	log.Println("Check flags of correct service...")

	err = checker.CheckFlags(db.db, round, teams, services)
	if err != nil {
		log.Fatalln("Check flags failed:", err)
	}

	checkServicesStatus(db.db, round, teams, services, steward.StatusUP)

	service.ClearFlags()

	log.Println("Check flags of service with removed flags...")

	err = checker.CheckFlags(db.db, round, teams, services)
	if err != nil {
		log.Fatalln("Check flags failed:", err)
	}

	checkServicesStatus(db.db, round, teams, services, steward.StatusCorrupt)

	log.Println("Stop service...")
	service.Stop()

	log.Println("Check flags of stopped service...")

	err = checker.CheckFlags(db.db, round, teams, services)
	if err != nil {
		log.Fatalln("Check flags failed:", err)
	}

	checkServicesStatus(db.db, round, teams, services, steward.StatusDown)
}
Example #2
0
// Round start new round
func (g Game) Round(counters *sync.WaitGroup) (err error) {

	roundNo, err := steward.NewRound(g.db, g.roundLen)
	if err != nil {
		return
	}

	log.Println("New round", roundNo)

	err = checker.PutFlags(g.db, g.priv, roundNo, g.teams, g.services)
	if err != nil {
		return
	}

	round, err := steward.CurrentRound(g.db)
	if err != nil {
		return
	}

	roundEnd := round.StartTime.Add(round.Len)

	for time.Now().Before(roundEnd) {

		log.Println("Round", round.ID, "check start")

		err = checker.CheckFlags(g.db, round.ID, g.teams, g.services)
		if err != nil {
			return
		}

		timeout := RandomizeTimeout(g.timeout, g.timeout/3)

		if time.Now().Add(timeout).After(roundEnd) {
			break
		}

		log.Println("Round", round.ID, "check end, timeout", timeout)

		time.Sleep(timeout)
	}

	log.Println("Check", round.ID, "over, wait", time.Now().Sub(roundEnd))

	for time.Now().Before(roundEnd) {
		time.Sleep(time.Second / 10)
	}

	counters.Add(1)
	go func() {
		defer counters.Done()

		log.Println("Count round", round.ID, "start", time.Now())

		err = counter.CountRound(g.db, round.ID, g.teams, g.services)
		if err != nil {
			log.Println("Count round", round.ID, "failed:", err)
		}

		log.Println("Count round", round.ID, "end", time.Now())
	}()

	return
}