-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1629 from npinaeva/ocpbugs-11110
OCPBUGS-11110: [release-4.11] Batch potentially big transaction on egress firewall ACLs migration
- Loading branch information
Showing
3 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package batching | ||
|
||
import "fmt" | ||
|
||
func Batch[T any](batchSize int, data []T, eachFn func([]T) error) error { | ||
if batchSize < 1 { | ||
return fmt.Errorf("batchSize should be > 0, got %d", batchSize) | ||
} | ||
start := 0 | ||
dataLen := len(data) | ||
for start < dataLen { | ||
end := start + batchSize | ||
if end > dataLen { | ||
end = dataLen | ||
} | ||
err := eachFn(data[start:end]) | ||
if err != nil { | ||
return err | ||
} | ||
start = end | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package batching | ||
|
||
import ( | ||
"fmt" | ||
"github.com/onsi/ginkgo" | ||
"github.com/onsi/gomega" | ||
|
||
"strings" | ||
"testing" | ||
) | ||
|
||
type batchTestData struct { | ||
name string | ||
batchSize int | ||
data []int | ||
result []int | ||
expectErr string | ||
} | ||
|
||
func TestBatch(t *testing.T) { | ||
tt := []batchTestData{ | ||
{ | ||
name: "batch size should be > 0", | ||
batchSize: 0, | ||
data: []int{1, 2, 3}, | ||
expectErr: "batchSize should be > 0", | ||
}, | ||
{ | ||
name: "batchSize = 1", | ||
batchSize: 1, | ||
data: []int{1, 2, 3}, | ||
}, | ||
{ | ||
name: "batchSize > 1", | ||
batchSize: 2, | ||
data: []int{1, 2, 3}, | ||
}, | ||
{ | ||
name: "number of batches = 0", | ||
batchSize: 2, | ||
data: nil, | ||
}, | ||
{ | ||
name: "number of batches = 1", | ||
batchSize: 2, | ||
data: []int{1, 2}, | ||
}, | ||
{ | ||
name: "number of batches > 1", | ||
batchSize: 2, | ||
data: []int{1, 2, 3, 4}, | ||
}, | ||
{ | ||
name: "number of batches not int", | ||
batchSize: 2, | ||
data: []int{1, 2, 3, 4, 5}, | ||
}, | ||
} | ||
|
||
for _, tCase := range tt { | ||
g := gomega.NewGomegaWithT(t) | ||
ginkgo.By(tCase.name) | ||
var result []int | ||
batchNum := 0 | ||
err := Batch[int](tCase.batchSize, tCase.data, func(l []int) error { | ||
batchNum += 1 | ||
result = append(result, l...) | ||
return nil | ||
}) | ||
if err != nil { | ||
if tCase.expectErr != "" && strings.Contains(err.Error(), tCase.expectErr) { | ||
continue | ||
} | ||
t.Fatal(fmt.Sprintf("test %s failed: %v", tCase.name, err)) | ||
} | ||
// tCase.data/tCase.batchSize round up | ||
expectedBatchNum := (len(tCase.data) + tCase.batchSize - 1) / tCase.batchSize | ||
g.Expect(batchNum).To(gomega.Equal(expectedBatchNum)) | ||
g.Expect(result).To(gomega.Equal(tCase.data)) | ||
} | ||
} |