Testing
Tests shouldn’t depend on whatever a live source happens to say. Detent gives you three tools for deterministic knobs: in-memory test clients, CLI overrides, and defaults-only clients.
In-Memory Test Knobs
Section titled “In-Memory Test Knobs”detent::testing::TestKnobs builds a client from an in-memory snapshot, with
no files, no sources and no background thread:
#include "detent/testing.hh"
detent::Snapshot snap;snap.knobs["checkout:killswitch_new_path"] = detent::KnobDef{.default_value = 1};snap.knobs["search:max_results"] = detent::KnobDef{.default_value = 3};
auto guard = detent::testing::TestKnobs(snap).set_thread_local();
// Within this thread, detent::global::* now reads the test snapshot.assert(detent::global::get_as<bool>("checkout:killswitch_new_path").value());assert(detent::global::get_or_panic("search:max_results") == 3);The guard installs the test client as a thread-local override for the
detent::global adapter. Destroying the guard restores whatever was active
before, so guards nest and tests can’t leak knobs into one another.
Rollouts and group overrides work as they do in production:
detent::KnobDef def{.default_value = 0, .rollout_pct = 10, .rollout_value = 1};def.overrides["employees"] = std::int64_t{1};def.overrides["beta"] = detent::GroupRollout{.rollout_pct = 50};snap.knobs["payments:new_processor"] = def;CLI Overrides
Section titled “CLI Overrides”CLI overrides pin a knob for one process, shadowing both baked defaults and every live snapshot:
opts.cli_overrides = { "checkout:killswitch_new_path=true", "search:max_results=999",};They’re ideal for debugging a single process locally, or for integration tests that run a real binary. The rules are strict on purpose:
- Lowercase
trueandfalseparse as 1 and 0; anything else must be a signed base-10 64-bit integer. - A name given twice is a startup error. There’s no “last one wins”.
- An override is checked before the snapshot, so it answers even for a knob
no snapshot defines. Reading it with
get_as<bool>takes only 0 and 1; any other value is aConversionFailederror. - Overrides are fixed at startup and stay in effect across every update.
Detent parses the name=value pairs; splitting your own command line is up to
your application. A typical integration is a repeatable flag:
my_app --knob checkout:killswitch_new_path=true --knob search:max_results=999Defaults-Only Clients
Section titled “Defaults-Only Clients”A client with defaults_path and no source reads exactly what’s in the file,
forever. That’s a good fit for integration tests that want the real loading
path without a live source:
detent::InitOptions opts;opts.defaults_path = "testdata/defaults.json";detent::Client client(std::move(opts));