Skip to content

Commit

Permalink
Fix race in stress-test-e2e.
Browse files Browse the repository at this point in the history
Add a function which would return error instead failing the test.
Add error stream to finish stress test gracefully in case there is no
capacity in a cluster for a specific test.
It is forbidden to call t.Fatalf() from go routine other than main test
routine.
  • Loading branch information
aLekSer committed Sep 25, 2019
1 parent 550a30d commit e8692b2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
45 changes: 37 additions & 8 deletions test/e2e/fleet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,9 @@ func TestScaleUpAndDownInParallelStressTest(t *testing.T) {
framework.WaitForFleetCondition(t, flt, e2e.FleetReadyCount(0))
}
}

errors := make(chan error)
var wg sync.WaitGroup
finished := make(chan bool, 1)

for fleetNumber, flt := range fleets {
wg.Add(1)
Expand All @@ -815,19 +816,46 @@ func TestScaleUpAndDownInParallelStressTest(t *testing.T) {
}()

if fleetNumber%2 == 0 {
scaleDownStats.ReportDuration(scaleAndWait(t, flt, 0), nil)
duration, err := scaleAndWait(t, flt, 0)
if err != nil {
fmt.Println(err)
errors <- err
return
}
scaleDownStats.ReportDuration(duration, nil)
}
for i := 0; i < repeatCount; i++ {
if time.Now().After(deadline) {
break
}
scaleUpStats.ReportDuration(scaleAndWait(t, flt, fleetSize), nil)
scaleDownStats.ReportDuration(scaleAndWait(t, flt, 0), nil)
duration, err := scaleAndWait(t, flt, fleetSize)
if err != nil {
fmt.Println(err)
errors <- err
return
}
scaleUpStats.ReportDuration(duration, nil)
duration, err = scaleAndWait(t, flt, 0)
if err != nil {
fmt.Println(err)
errors <- err
return
}
scaleDownStats.ReportDuration(duration, nil)
}
}(fleetNumber, flt)
}
go func() {
wg.Wait()
close(finished)
}()

wg.Wait()
select {
case <-finished:
case err := <-errors:
t.Fatalf("Error in waiting for a fleet to scale: %s", err)
}
fmt.Println("We are Done")
}

// Creates a fleet and one GameServer with Packed scheduling.
Expand Down Expand Up @@ -999,11 +1027,12 @@ func schedulingFleetPatch(t *testing.T,
return fltRes
}

func scaleAndWait(t *testing.T, flt *agonesv1.Fleet, fleetSize int32) time.Duration {
func scaleAndWait(t *testing.T, flt *agonesv1.Fleet, fleetSize int32) (duration time.Duration, err error) {
t0 := time.Now()
scaleFleetSubresource(t, flt, fleetSize)
framework.WaitForFleetCondition(t, flt, e2e.FleetReadyCount(fleetSize))
return time.Since(t0)
err = framework.WaitForFleetConditionParallel(t, flt, e2e.FleetReadyCount(fleetSize))
duration = time.Since(t0)
return
}

// scaleFleetPatch creates a patch to apply to a Fleet.
Expand Down
14 changes: 12 additions & 2 deletions test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,17 @@ func (f *Framework) WaitForGameServerState(gs *agonesv1.GameServer, state agones
}

// WaitForFleetCondition waits for the Fleet to be in a specific condition or fails the test if the condition can't be met in 5 minutes.
// nolint: dupl
func (f *Framework) WaitForFleetCondition(t *testing.T, flt *agonesv1.Fleet, condition func(fleet *agonesv1.Fleet) bool) {
t.Helper()
err := f.WaitForFleetConditionParallel(t, flt, condition)
if err != nil {
// Do not call Fatalf() from go routine other than main test go routine, because it could cause a race
t.Fatalf("error waiting for fleet condition on fleet %v", flt.Name)
}
}

// WaitForFleetConditionParallel waits for the Fleet to be in a specific condition or returns an error if the condition can't be met in 5 minutes.
func (f *Framework) WaitForFleetConditionParallel(t *testing.T, flt *agonesv1.Fleet, condition func(fleet *agonesv1.Fleet) bool) error {
t.Helper()
logrus.WithField("fleet", flt.Name).Info("waiting for fleet condition")
err := wait.PollImmediate(2*time.Second, 5*time.Minute, func() (bool, error) {
Expand All @@ -143,8 +152,9 @@ func (f *Framework) WaitForFleetCondition(t *testing.T, flt *agonesv1.Fleet, con
})
if err != nil {
logrus.WithField("fleet", flt.Name).WithError(err).Info("error waiting for fleet condition")
t.Fatalf("error waiting for fleet condition on fleet %v", flt.Name)
return err
}
return nil
}

// WaitForFleetAutoScalerCondition waits for the FleetAutoscaler to be in a specific condition or fails the test if the condition can't be met in 2 minutes.
Expand Down

0 comments on commit e8692b2

Please sign in to comment.