Skip to content

fix(testing): make FakeClient thread-safe - #2759

Open
FirePheonix wants to merge 1 commit into
kyverno:mainfrom
FirePheonix:fix-fake-client-thread-safety
Open

fix(testing): make FakeClient thread-safe#2759
FirePheonix wants to merge 1 commit into
kyverno:mainfrom
FirePheonix:fix-fake-client-thread-safety

Conversation

@FirePheonix

Copy link
Copy Markdown

Explanation

There was a TODO: not thread safe in pkg/client/testing/fake_client.go, and i've done some changes as to what's happening in there..

This PR represents a fix of existing test infrastructure behavior by making the FakeClient testing utility thread-safe.
reviously, when unit tests executed mock client assertions in parallel goroutines, concurrent calls to FakeClient methods caused data races while mutating and reading the internal call counter.

BEFORE:
image

Adding mutex synchronization guarantees that parallel test execution remains deterministic, reliable, and free of data races.

Proposed change:
image

Related issue

Resolves technical debt and addresses the // TODO: not thread safe comment in pkg/client/testing/fake_client.go.

Proposed Changes

  • Thread Synchronization: Embedded sync.RWMutex into FakeClient in pkg/client/testing/fake_client.go.
  • Atomic Call Indexing: Protected c.numCalls reads and increments with c.mu.Lock() / c.mu.Unlock() across all client methods (Get, List, Create, Update, Delete, Patch, IsObjectNamespaced, RESTMapper, and SubResource).
  • Safe Reader Access: Protected NumCalls() using c.mu.RLock() / c.mu.RUnlock().
  • Unit Test Coverage: Added a concurrent unit test (TestFakeClient/Concurrent_calls) in pkg/client/testing/fake_client_test.go that runs 100 parallel goroutines to verify race-free execution.

Checklist

  • I have read the contributing guidelines.
  • I have read the PR documentation guide and followed the process including adding proof manifests to this PR.
  • This is a bug fix and I have added unit tests that prove my fix is effective.

Further Comments

This refactoring cleans up internal test infrastructure debt without changing any public client interfaces or behavior. All existing and new unit tests pass cleanly (go test -v ./pkg/client/testing/...).

Signed-off-by: Shubham Singh <shubhsoch@gmail.com>
@FirePheonix
FirePheonix requested a review from a team as a code owner August 2, 2026 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant