Skip to content

Commit

Permalink
infinity
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Smith <robertdavidsmith@yahoo.com>
  • Loading branch information
robertdavidsmith committed Nov 8, 2024
1 parent 95f6a01 commit 35cbee8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
10 changes: 6 additions & 4 deletions internal/scheduler/internaltypes/resource_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,12 @@ func multiplyResource(res int64, multiplier float64) int64 {
// If res is zero, we assume infinity trumps zero, and return int64 maxValue.
// This gives the right behavior when the result is used as a cap,
// as an infinity multiplier means "never apply cap".
if multiplier == math.Inf(1) {
return math.MaxInt64
} else if multiplier == math.Inf(-1) {
return math.MinInt64
if math.IsInf(multiplier, 0) {
if (multiplier < 0) == (res < 0) {
return math.MaxInt64
} else {
return math.MinInt64
}
}

return int64(float64(res) * multiplier)
Expand Down
31 changes: 15 additions & 16 deletions internal/scheduler/internaltypes/resource_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,26 @@ func TestMultiply_HandlesInfinityCorrectly(t *testing.T) {
assert.Equal(t, int64(math.MaxInt64), result1.GetByNameZeroIfMissing("memory"))

result2 := testResourceList(factory, "100", "0").Multiply(testResourceFractionList(factory, 0.75, math.Inf(1), 1))
assert.Equal(t, int64(75000), result2.GetByNameZeroIfMissing("cpu"))
assert.Equal(t, int64(math.MaxInt64), result2.GetByNameZeroIfMissing("memory"))

result3 := testResourceList(factory, "100", "0").Multiply(testResourceFractionList(factory, 0.75, math.Inf(-1), 1))
assert.Equal(t, int64(75000), result3.GetByNameZeroIfMissing("cpu"))
result3 := testResourceList(factory, "100", "-100Ki").Multiply(testResourceFractionList(factory, 0.75, math.Inf(1), 1))
assert.Equal(t, int64(math.MinInt64), result3.GetByNameZeroIfMissing("memory"))
}

func TestMultiply_HandlesMinusInfinityCorrectly(t *testing.T) {
factory := testFactory()

result1 := testResourceList(factory, "100", "100Ki").Multiply(testResourceFractionList(factory, 0.75, math.Inf(-1), 1))
assert.Equal(t, int64(75000), result1.GetByNameZeroIfMissing("cpu"))
assert.Equal(t, int64(math.MinInt64), result1.GetByNameZeroIfMissing("memory"))

result2 := testResourceList(factory, "100", "0").Multiply(testResourceFractionList(factory, 0.75, math.Inf(-1), 1))
assert.Equal(t, int64(math.MinInt64), result2.GetByNameZeroIfMissing("memory"))

result3 := testResourceList(factory, "100", "-100Ki").Multiply(testResourceFractionList(factory, 0.75, math.Inf(-1), 1))
assert.Equal(t, int64(math.MaxInt64), result3.GetByNameZeroIfMissing("memory"))
}

func TestMultiply_HandlesEmptyCorrectly(t *testing.T) {
factory := testFactory()

Expand Down Expand Up @@ -365,19 +377,6 @@ func TestNegate_HandlesEmptyCorrectly(t *testing.T) {
assert.Equal(t, ResourceList{}, ResourceList{}.Negate())
}

func TestScale(t *testing.T) {
factory := testFactory()
assert.Equal(t, testResourceList(factory, "4", "2Ki"), testResourceList(factory, "2", "1Ki").Scale(2.0))
assert.Equal(t, testResourceList(factory, "2", "1Ki"), testResourceList(factory, "2", "1Ki").Scale(1.0))
assert.Equal(t, testResourceList(factory, "0", "0Ki"), testResourceList(factory, "2", "1Ki").Scale(0.0))
assert.Equal(t, testResourceList(factory, "2", "-1Ki"), testResourceList(factory, "-2", "1Ki").Scale(-1.0))
}

func TestScale_HandlesEmptyCorrectly(t *testing.T) {
assert.Equal(t, ResourceList{}, ResourceList{}.Scale(0.0))
assert.Equal(t, ResourceList{}, ResourceList{}.Scale(1.0))
}

func testResourceList(factory *ResourceListFactory, cpu string, memory string) ResourceList {
return factory.FromJobResourceListIgnoreUnknown(map[string]k8sResource.Quantity{
"cpu": k8sResource.MustParse(cpu),
Expand Down

0 comments on commit 35cbee8

Please sign in to comment.