Skip to content

Commit

Permalink
Merge pull request #11 from fasaxc/txn-count
Browse files Browse the repository at this point in the history
Add NumOperations() method to Transaction.
  • Loading branch information
k8s-ci-robot authored Jun 18, 2024
2 parents 251bee7 + 2df88ca commit 57f949e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ func TestFakeCheck(t *testing.T) {
fake := NewFake(IPv4Family, "kube-proxy")

tx := fake.NewTransaction()

if tx.NumOperations() != 0 {
t.Errorf("empty transaction should have 0 operations, got %d", tx.NumOperations())
}

tx.Add(&Table{})
tx.Add(&Chain{
Expand Down Expand Up @@ -335,6 +339,10 @@ func TestFakeCheck(t *testing.T) {
t.Errorf("unexpected transaction content:\n%s", diff)
}

if tx.NumOperations() != 4 {
t.Errorf("unexpected number of operations: expected 4, got %d", tx.NumOperations())
}

err := fake.Run(context.Background(), tx)
if err != nil {
t.Fatalf("unexpected error from Run: %v", err)
Expand Down
5 changes: 5 additions & 0 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func (tx *Transaction) String() string {
return buf.String()
}

// NumOperations returns the number of operations queued in the transaction.
func (tx *Transaction) NumOperations() int {
return len(tx.operations)
}

func (tx *Transaction) operation(verb verb, obj Object) {
if tx.err != nil {
return
Expand Down

0 comments on commit 57f949e

Please sign in to comment.