Adding batch calculator utility and test cases for it.
This commit is contained in:
parent
ace5da6bd3
commit
b73954cd7c
2 changed files with 56 additions and 0 deletions
19
util.go
19
util.go
|
@ -99,3 +99,22 @@ func validateAuroraURL(location string) (string, error) {
|
|||
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func calculateCurrentBatch(updatingInstances int32, batchSizes []int32) int {
|
||||
for i, size := range batchSizes {
|
||||
updatingInstances -= size
|
||||
if updatingInstances <= 0 {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
// Overflow batches
|
||||
batchCount := len(batchSizes) - 1
|
||||
lastBatchIndex := len(batchSizes) - 1
|
||||
batchCount += int(updatingInstances / batchSizes[lastBatchIndex])
|
||||
|
||||
if updatingInstances%batchSizes[lastBatchIndex] != 0 {
|
||||
batchCount++
|
||||
}
|
||||
return batchCount
|
||||
}
|
||||
|
|
37
util_test.go
37
util_test.go
|
@ -63,3 +63,40 @@ func TestAuroraURLValidator(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCurrentBatchCalculator(t *testing.T) {
|
||||
t.Run("singleBatchOverflow", func(t *testing.T) {
|
||||
curBatch := calculateCurrentBatch(10, []int32{2})
|
||||
assert.Equal(t, 4, curBatch)
|
||||
})
|
||||
|
||||
t.Run("noInstancesUpdating", func(t *testing.T) {
|
||||
curBatch := calculateCurrentBatch(0, []int32{2})
|
||||
assert.Equal(t, 0, curBatch)
|
||||
})
|
||||
|
||||
t.Run("evenMatchSingleBatch", func(t *testing.T) {
|
||||
curBatch := calculateCurrentBatch(2, []int32{2})
|
||||
assert.Equal(t, 0, curBatch)
|
||||
})
|
||||
|
||||
t.Run("moreInstancesThanBatches", func(t *testing.T) {
|
||||
curBatch := calculateCurrentBatch(5, []int32{1, 2})
|
||||
assert.Equal(t, 2, curBatch)
|
||||
})
|
||||
|
||||
t.Run("moreInstancesThanBatchesDecreasing", func(t *testing.T) {
|
||||
curBatch := calculateCurrentBatch(5, []int32{2, 1})
|
||||
assert.Equal(t, 3, curBatch)
|
||||
})
|
||||
|
||||
t.Run("unevenFit", func(t *testing.T) {
|
||||
curBatch := calculateCurrentBatch(2, []int32{1, 2})
|
||||
assert.Equal(t, 1, curBatch)
|
||||
})
|
||||
|
||||
t.Run("halfWay", func(t *testing.T) {
|
||||
curBatch := calculateCurrentBatch(1, []int32{1, 2})
|
||||
assert.Equal(t, 0, curBatch)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue