Skip to content

Commit

Permalink
test(healthcontroller): add tests to compute rolling surge func
Browse files Browse the repository at this point in the history
  • Loading branch information
hspedro committed Jul 24, 2024
1 parent bdefff0 commit 73eade6
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions internal/core/operations/healthcontroller/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,3 +1467,79 @@ func newValidScheduler(autoscaling *autoscaling.Autoscaling) *entities.Scheduler
Autoscaling: autoscaling,
}
}

func TestComputeRollingSurgeVariants(t *testing.T) {
testCases := []struct {
name string
maxSurge string
desiredNumberOfRooms int
totalRoomsAmount int
expectedSurgeAmount int
expectError bool
}{
{
name: "Standard surge calculation",
maxSurge: "10",
desiredNumberOfRooms: 20,
totalRoomsAmount: 15,
expectedSurgeAmount: 5,
expectError: false,
},
{
name: "Low amount of rooms",
maxSurge: "10",
desiredNumberOfRooms: 5,
totalRoomsAmount: 3,
expectedSurgeAmount: 10, // Surge is fixed at 10, regardless of the low number of rooms
expectError: false,
},
{
name: "High amount of rooms above 1000",
maxSurge: "30%",
desiredNumberOfRooms: 1500,
totalRoomsAmount: 1000,
expectedSurgeAmount: 10, // Surge is fixed at 10, even for high number of rooms
expectError: false,
},
{
name: "Relative maxSurge with rooms above 1000",
maxSurge: "25%",
desiredNumberOfRooms: 1200,
totalRoomsAmount: 1000,
expectedSurgeAmount: 300, // 25% of 1200 is 300
expectError: false,
},
{
name: "Invalid maxSurge string format",
maxSurge: "twenty%", // Invalid because it's not a number
desiredNumberOfRooms: 300,
totalRoomsAmount: 250,
expectedSurgeAmount: 1, // Expected to be 1 because the function should return an error
expectError: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
scheduler := &entities.Scheduler{
MaxSurge: tc.maxSurge,
}

ex := &Executor{}

Check failure on line 1528 in internal/core/operations/healthcontroller/executor_test.go

View workflow job for this annotation

GitHub Actions / Unit tests

undefined: Executor

Check failure on line 1528 in internal/core/operations/healthcontroller/executor_test.go

View workflow job for this annotation

GitHub Actions / Run linters

undefined: Executor (typecheck)

surgeAmount, err := ex.computeRollingSurge(scheduler, tc.desiredNumberOfRooms, tc.totalRoomsAmount)
if tc.expectError {
if err == nil {
t.Errorf("expected an error but got none")
}
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if surgeAmount != tc.expectedSurgeAmount {
t.Errorf("expected surge amount to be %d, but got %d", tc.expectedSurgeAmount, surgeAmount)
}
}
})
}
}

0 comments on commit 73eade6

Please sign in to comment.