From 2ac1e801ad8b490c287304c5d5d6268113a08802 Mon Sep 17 00:00:00 2001 From: Avinash Patnala Date: Thu, 19 Sep 2024 01:40:54 +0000 Subject: [PATCH] wrap fake client with mutex logic to resolve data race issues Signed-off-by: Avinash Patnala --- pkg/readiness/ready_tracker.go | 11 +++++++++++ pkg/readiness/ready_tracker_unit_test.go | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/readiness/ready_tracker.go b/pkg/readiness/ready_tracker.go index dffec0903e7..6ff2a9bf035 100644 --- a/pkg/readiness/ready_tracker.go +++ b/pkg/readiness/ready_tracker.go @@ -89,6 +89,17 @@ type Tracker struct { trackListerPredicateOverride retryPredicate } +type WrapFakeClientWithMutex struct { + listMutex sync.Mutex + fakeLister Lister +} + +func (w *WrapFakeClientWithMutex) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { + w.listMutex.Lock() + defer w.listMutex.Unlock() + return w.fakeLister.List(ctx, list, opts...) +} + // NewTracker creates a new Tracker and initializes the internal trackers. func NewTracker(lister Lister, mutationEnabled, externalDataEnabled, expansionEnabled bool) *Tracker { // TODO: Dereference crashOnFailureFetchingExpectations when we change crashOnFailureFetchingExpectations to a flag diff --git a/pkg/readiness/ready_tracker_unit_test.go b/pkg/readiness/ready_tracker_unit_test.go index ba5d039b941..74baa906704 100644 --- a/pkg/readiness/ready_tracker_unit_test.go +++ b/pkg/readiness/ready_tracker_unit_test.go @@ -211,7 +211,9 @@ func Test_ReadyTracker_TryCancelTemplate_No_Retries(t *testing.T) { // Verify that TryCancelTemplate must be called enough times to remove all retries before canceling a template. func Test_ReadyTracker_TryCancelTemplate_Retries(t *testing.T) { lister := fake.NewClientBuilder().WithScheme(mustInitializeScheme(runtime.NewScheme())).WithRuntimeObjects(convertedTemplate.DeepCopyObject()).Build() - rt := newTracker(lister, false, false, false, false, nil, func() objData { + wrapLister := WrapFakeClientWithMutex{fakeLister: lister} + + rt := newTracker(&wrapLister, false, false, false, false, nil, func() objData { return objData{retries: 2} })