Skip to content

fix(rueidiscompatmock): make Cache() mockable via the expectation queue - #1023

Merged
rueian merged 11 commits into
redis:mainfrom
SomSamantray:fix/rueidiscompatmock-cache-mock
Aug 24, 2026
Merged

rueian merged 11 commits into
redis:mainfrom
SomSamantray:fix/rueidiscompatmock-cache-mock

Conversation

@SomSamantray

@SomSamantray SomSamantray commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

rueidiscompatmock now supports mocking client-side caching. Cache(ttl) commands dispatch through the existing Expect* expectation queue instead of panicking with unexpected call to DoCache (issue #972). The new DoCache stub reuses the same ordered queue as Do/DoMulti, so no Expect* API changed and the TTL argument is deliberately not matched (documented in the README).

Design notes:

  • The fix lives in rueidiscompatmock only; rueidiscompat and the root mock/ package are untouched. This follows the maintainer's direction on fix(rueidiscompat): make CacheCompat a mockable interface #1021 instead of the rejected interface-ization of CacheCompat.
  • DoMultiCache is not stubbed: the rueidiscompat cache surface never calls it (verified against the receiver block in rueidiscompat/adapter.go).

Validated with the module's full test suite including -race. Eight new TestCacheCompat* tests cover dispatch through both adapter entry points, multi-command queues, Cacheable-wrapped commands, nil/error results, TTL irrelevance, and unmatched-command reporting.

Fixes #972.

Related: #1021


Note

Low Risk
Test-helper only; no production Redis client or auth/data path changes. Matching origin (cache vs plain) is a behavior change for existing mock tests that mix Cache() with unmarked Expect* calls.

Overview
Makes rueidiscompat.Cache(ttl) commands mockable in rueidiscompatmock instead of panicking on DoCache. Cached calls reuse the existing Expect* queue; TTL is not matched.

Matching is origin-aware: ExpectGet("k").ViaCache() only matches rdb.Cache(ttl).Get(...), and unmarked expectations only match plain calls. Unordered matching still prefers the origin-specific entry rather than the first same-command expectation.

ViaCache() is added on all expected-result types. Unmatched cache calls report via cache in the error. DoMultiCache is not stubbed.

Reviewed by Cursor Bugbot for commit def365b. Bugbot is set up for automated code reviews on this repo. Configure here.

Comment thread docs/residual-review-findings/0045d5f.md Outdated
Comment thread rueidiscompatmock/adapter_test.go Outdated
…tations

Addresses review feedback on redis#1023: sharing ExpectGet between a plain Get
and a Cache(ttl).Get() call meant a test couldn't require that a specific
call went through the cache path. ViaCache() is an opt-in chainable
modifier on every Expected* builder; a marked expectation only matches a
call routed through DoCache, while unmarked expectations keep matching
either origin exactly as before.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 59eaff3. Configure here.

Comment thread rueidiscompatmock/adapter.go
SomSamantray and others added 2 commits August 19, 2026 10:14
…ectations

Cursor Bugbot found that in MatchExpectationsInOrder(false) mode, an
unmarked expectation queued ahead of a ViaCache()-marked expectation for
the same command could "steal" a cached call, since the unordered scan
picked the first matching entry regardless of specificity. matchLocked
now scans for a requireCache=true candidate first when the call arrived
via DoCache, falling back to the original scan otherwise. Plain calls are
unaffected. Verified the new regression test actually fails without the
fix before restoring it.
…uted calls

Per @rueian's review on redis#1023: a call routed through Cache(ttl) could
previously be satisfied by an unmarked expectation as a compatibility
fallback, so ExpectationsWereMet() passed even when a test never
asserted the call went through the cache path. Matching is now
symmetric: expectation.matches rejects the call whenever
e.requireCache != viaCache, so a plain ExpectGet only matches a plain
call and a ViaCache() expectation only matches a cache-routed call.

Updates the TestCacheCompat* suite for the new contract and replaces
the two tests that codified the old fallback with tests proving it no
longer applies. Documents the symmetric behavior in
rueidiscompatmock/README.md.
Comment thread rueidiscompatmock/viacache.go Outdated
The comment on ViaCache() still described the old "unmarked
expectations match either origin" fallback that was removed in
3852a76. Flagged as stale by @rueian on redis#1023.
Comment thread rueidiscompatmock/adapter.go Outdated
Comment thread rueidiscompatmock/adapter_test.go Outdated
Comment thread rueidiscompatmock/adapter_test.go
…tion error, remove duplicate test, gofmt

Addresses three review comments from @rueian on redis#1023:

- adapter.go: the "no expectation for command" error now appends
  "via cache" when the unmatched call was routed through DoCache, so
  a forgotten .ViaCache() is distinguishable from an ordinary mismatch
  at a glance. Factored into a shared unmatchedErr helper used by both
  consume and consumeWithRaw. Added TestCacheCompatUnmatchedPlainCallOmitsViaCache
  and tightened TestCacheCompatUnmatched to assert the suffix.
- adapter_test.go: removed TestCacheCompatGet, which became an exact
  duplicate of TestCacheCompatViaCacheRequired once ViaCache() was
  added to it in 3852a76.
- adapter_test.go: applied gofmt (100*time.Millisecond, no spaces
  around *) per review suggestion.
Comment thread rueidiscompatmock/viacache.go Outdated
@rueian
rueian merged commit 900cbb5 into redis:main Aug 24, 2026
4 of 5 checks passed
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.

Facing issue in mocking while migrating from go-redis, mockery mocks

2 participants