Skip to content

Commit

Permalink
Add IsEmpty functions for Ops and ReconcileOps.
Browse files Browse the repository at this point in the history
 * (M) rib/reconciler/reconcile(_test)?.go
   - add an `IsEmpty` method for both `Ops` and `ReconcileOps` to
     determine whether they include any transactions.
  • Loading branch information
robshakir committed Nov 2, 2023
1 parent 3f936ef commit 594d43a
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
10 changes: 10 additions & 0 deletions rib/reconciler/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ func (o *Ops) Merge(in *Ops) {
o.TopLevel = append(o.TopLevel, in.TopLevel...)
}

// IsEmpty determines whether the specified Ops contains any operations.
func (o *Ops) IsEmpty() bool {
return len(o.NH) == 0 && len(o.NHG) == 0 && len(o.TopLevel) == 0
}

// ReconcileOps stores the operations that are required for a specific reconciliation
// run.
type ReconcileOps struct {
Expand All @@ -166,6 +171,11 @@ func (r *ReconcileOps) Merge(in *ReconcileOps) {
r.Delete.Merge(in.Delete)
}

// IsEmpty determines whether the specified ReconcileOps contains any operations.
func (r *ReconcileOps) IsEmpty() bool {
return r.Add.IsEmpty() && r.Delete.IsEmpty() && r.Replace.IsEmpty()
}

// NewReconcileOps returns a new reconcileOps struct with the fields initialised.
func NewReconcileOps() *ReconcileOps {
return &ReconcileOps{
Expand Down
94 changes: 94 additions & 0 deletions rib/reconciler/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1592,3 +1592,97 @@ func TestMergeReconcileOps(t *testing.T) {
})
}
}

func TestReconcileOpsIsEmpty(t *testing.T) {
tests := []struct {
desc string
in *ReconcileOps
want bool
}{{
desc: "empty",
in: NewReconcileOps(),
want: true,
}, {
desc: "not empty: add",
in: &ReconcileOps{
Add: &Ops{
NH: []*spb.AFTOperation{{}},
NHG: []*spb.AFTOperation{{}},
TopLevel: []*spb.AFTOperation{{}},
},
Delete: &Ops{},
Replace: &Ops{},
},
want: false,
}, {
desc: "not empty: delete",
in: &ReconcileOps{
Add: &Ops{},
Delete: &Ops{
NH: []*spb.AFTOperation{{}},
NHG: []*spb.AFTOperation{{}},
TopLevel: []*spb.AFTOperation{{}},
},
Replace: &Ops{},
},
want: false,
}, {
desc: "not empty: replace",
in: &ReconcileOps{
Add: &Ops{},
Delete: &Ops{},
Replace: &Ops{
NH: []*spb.AFTOperation{{}},
NHG: []*spb.AFTOperation{{}},
TopLevel: []*spb.AFTOperation{{}},
},
},
want: false,
}}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
if got := tt.in.IsEmpty(); got != tt.want {
t.Fatalf("(%v).IsEmpty(): did not get expected result, got: %v, want: %v", tt.in, got, tt.want)
}
})
}
}

func TestOpsIsEmpty(t *testing.T) {
tests := []struct {
desc string
in *Ops
want bool
}{{
desc: "empty",
in: &Ops{},
want: true,
}, {
desc: "not empty: top-level",
in: &Ops{
TopLevel: []*spb.AFTOperation{{}},
},
want: false,
}, {
desc: "not empty: nhg",
in: &Ops{
NHG: []*spb.AFTOperation{{}},
},
want: false,
}, {
desc: "not empty: nh",
in: &Ops{
NH: []*spb.AFTOperation{{}},
},
want: false,
}}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
if got := tt.in.IsEmpty(); got != tt.want {
t.Fatalf("(%v).IsEmpty(): did not get expected result, got: %v, want: %v", tt.in, got, tt.want)
}
})
}
}

0 comments on commit 594d43a

Please sign in to comment.