Repository navigation
fix(multi_layer): reject non-module layers instead of silently no-opping - #54
Merged
Merged
Conversation
Cache.MultiLayer documented four layer forms and one worked. A bare adapter
(Cache.ETS), a {Adapter, opts} tuple, and __MODULE__ were all documented; the
first two dispatched through
cache_module?(m) = function_exported?(m, :get, 1) and function_exported?(m, :put, 2)
which an adapter cannot satisfy — an adapter exports get/3 and put/5 — and the
else branches returned {:ok, nil} and :ok.
Those are success shapes, and that is what made this survive: a layer that
always misses is indistinguishable from a cold cache, and a write that no-ops
is indistinguishable from one that worked. A cascade with an inert layer
behaves exactly like a working one, so any test asserting only "the cascade
returns the right value" passes either way.
Layers are cache modules. validate_layers!/2 rejects anything else at startup,
naming the offending element and how to wrap it. The no-match dispatch branches
are gone — there is no longer any way for a layer to fail quietly, because the
only fallback available to them was a success shape.
Also fixes a test that was passing for the wrong reason: MultiLayerFetch listed
[Cache.ETS] as its only layer, so on_fetch fired against a cascade of zero
working layers — a total miss that was guaranteed rather than observed.
The moduledoc now documents only the form that works, plus the constraint that
a cache cannot be its own layer (its get/1 is this strategy's get/4, so the
cascade would recurse).
Tests: 9 new (5 rejection incl. a positive control, 4 proving a validated
cascade is really two layers — written as slow-layer-only writes that are then
removed, since that is the one assertion an inert fast layer cannot fake).
Suite 330/343; the 13 failures are RedisJSONTest against a local Redis with no
RedisJSON module, and are identical on an unmodified tree.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #54 +/- ##
==========================================
+ Coverage 83.87% 83.89% +0.02%
==========================================
Files 24 24
Lines 707 708 +1
==========================================
+ Hits 593 594 +1
Misses 114 114 ☔ View full report in Codecov by Harness. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cache.MultiLayerdocumented four layer forms and one worked.use CachemoduleCache.ETS{AdapterModule, opts}__MODULE__in its own layer listget/1is the strategy'sget/4)The mechanism
Dispatch gated on:
An adapter exports
get/3andput/5, so that predicate is false forCache.ETS, and bothlayer_get/3andlayer_put/5fell through to{:ok, nil}and:ok.child_spec/1also ignored the layer list entirely, so "will be auto-started and supervised" never happened either. Both halves of one unimplemented feature.Why it survived: the failure mode is a plausible success
{:ok, nil}and:okare success shapes. A layer that always misses is indistinguishable from a cold cache; a write that no-ops is indistinguishable from one that worked. A cascade with an inert layer behaves exactly like a working one, so any test asserting only "the cascade returns the right value" passes either way.Concretely, in this repo:
TestCache.MultiLayerFetchlisted[Cache.ETS]as its only layer, soon_fetchwas firing against a cascade of zero working layers — a total miss that was guaranteed rather than observed. The test passed and could not have failed. Fixed here to use a real module.The change
Layers are
use Cachemodules.validate_layers!/2rejects anything else at startup, naming the offending element and how to wrap it:The no-match dispatch branches are gone. There is no longer any path by which a layer fails quietly, because the only fallback available to those branches was a success shape.
Adapter support was written first and then withdrawn as the wrong call: it needed ~110 lines of
child_specplumbing, name derivation, index threading andNimbleOptionsdefaulting. Rejection is a dozen, and adapters-as-layers can be added later if ever wanted.Docs now describe only the form that works, plus the constraint that a cache cannot be its own layer.
Tests
9 new — 5 rejection cases including a positive control proving a valid list is still accepted, and 4 proving a validated cascade is genuinely two layers.
The two-layer tests are written as write to the slow layer only, then remove it, and require the next read to still hit — the one assertion an inert fast layer cannot fake. A test that passes identically with one layer or two would not have caught the original defect, and none did.
The 14 failures are pre-existing and environmental: 13
Cache.RedisJSONTest(ERR unknown command 'JSON.SET'— no RedisJSON module on the local Redis) and 1Cache.RedisHashTestconnection flake under parallel load. Both verified identical on an unmodifiedorigin/main, and the hash test passes 21/21 in isolation on both branches.