Skip to content

fix(multi_layer): reject non-module layers instead of silently no-opping - #54

Merged
MikaAK merged 2 commits into
mainfrom
fix/multilayer-adapter-layers
Sep 3, 2026
Merged

MikaAK merged 2 commits into
mainfrom
fix/multilayer-adapter-layers

Conversation

@MikaAK

@MikaAK MikaAK commented Sep 3, 2026

Copy link
Copy Markdown
Owner

Cache.MultiLayer documented four layer forms and one worked.

Documented form Reality before this PR
a use Cache module works
a bare adapter, e.g. Cache.ETS silent permanent miss, writes discarded
{AdapterModule, opts} no matching clause — raises
__MODULE__ in its own layer list recurses (its get/1 is the strategy's get/4)

The mechanism

Dispatch gated on:

cache_module?(m) = function_exported?(m, :get, 1) and function_exported?(m, :put, 2)

An adapter exports get/3 and put/5, so that predicate is false for Cache.ETS, and both layer_get/3 and layer_put/5 fell through to {:ok, nil} and :ok. child_spec/1 also 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 :ok are 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.MultiLayerFetch listed [Cache.ETS] as its only layer, so on_fetch was 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 Cache modules. validate_layers!/2 rejects anything else at startup, naming the offending element and how to wrap it:

:my_cache: every Cache.MultiLayer layer must be a `use Cache` module, got:

    Cache.ETS

An adapter (Cache.ETS, Cache.Redis) is not a layer — it is stateless and
exports get/3, while a layer must export get/1. Wrap it in a module: ...

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_spec plumbing, name derivation, index threading and NimbleOptions defaulting. 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.

suite  370/384

The 14 failures are pre-existing and environmental: 13 Cache.RedisJSONTest (ERR unknown command 'JSON.SET' — no RedisJSON module on the local Redis) and 1 Cache.RedisHashTest connection flake under parallel load. Both verified identical on an unmodified origin/main, and the hash test passes 21/21 in isolation on both branches.

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

codecov Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.89%. Comparing base (68c62a9) to head (7d27362).

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.
📢 Have feedback on the report? Share it here.

@MikaAK
MikaAK merged commit 5a2700f into main Sep 3, 2026
7 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.

1 participant