Skip to content

Commit

Permalink
Add NumOperations() method to Transaction.
Browse files Browse the repository at this point in the history
Useful for checking if the transation is empty
before trying to execute it, for example.
  • Loading branch information
fasaxc committed Jun 18, 2024
1 parent 251bee7 commit 2df88ca
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 2df88ca

Please sign in to comment.