Skip to content

Commit 1296e66

Browse files
Fix flakiness on optimized tests by sorting results by name
Since results where created in parallel the order of want and got could be different. This is fine as we don't care about the order, however it lead to tests being flaky. This commit sorts slices in cmp.Diff by name to remove flakiness.
1 parent 2721c3c commit 1296e66

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

pkg/forwardingrules/optimized/compare_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/google/go-cmp/cmp"
7+
"github.com/google/go-cmp/cmp/cmpopts"
78
"k8s.io/ingress-gce/pkg/composite"
89
"k8s.io/ingress-gce/pkg/forwardingrules"
910
"k8s.io/ingress-gce/pkg/forwardingrules/optimized"
@@ -198,7 +199,11 @@ func TestCompare(t *testing.T) {
198199
t.Fatalf("optimized.Compare() error = %v", err)
199200
}
200201

201-
if diff := cmp.Diff(ops, tC.wantCalls); diff != "" {
202+
// We execute those in parallel so we don't care about the order at each stage
203+
sortFROpt := cmpopts.SortSlices(func(a, b *composite.ForwardingRule) bool { return a.Name < b.Name })
204+
sortDeletesOpt := cmpopts.SortSlices(func(a, b string) bool { return a < b })
205+
206+
if diff := cmp.Diff(ops, tC.wantCalls, sortFROpt, sortDeletesOpt); diff != "" {
202207
t.Errorf("want != got, (-want, +got):\n%s", diff)
203208
}
204209
})

pkg/forwardingrules/optimized/fetch_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ func TestFetch(t *testing.T) {
8989
}
9090

9191
ignoreOpts := cmpopts.IgnoreFields(composite.ForwardingRule{}, "Version", "SelfLink", "Region")
92-
if diff := cmp.Diff(tC.want, got, ignoreOpts); diff != "" {
92+
sortOpt := cmpopts.SortSlices(func(a, b *composite.ForwardingRule) bool { return a.Name < b.Name })
93+
94+
if diff := cmp.Diff(tC.want, got, ignoreOpts, sortOpt); diff != "" {
9395
t.Errorf("Fetch(%q) returned diff (-want +got):\n%s", tC.link, diff)
9496
}
9597
})

0 commit comments

Comments
 (0)