mercury-distributed-cache 4.12.15

Opt-in distributed cache for Mercury: a Redis-backed L2 cache as ONE composable action function (v1.cache.redis) over opaque bytes, reachable from all three layers (import from the application, never the engine)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//
// Copyright 2018-2026 Accenture Technology
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

//! The `v1.cache.redis` contract end to end against the in-process RESP
//! double, THROUGH the event system so the whole path is real — the Rust twin
//! of the Java `RedisCacheTest` (the function contract), `RedisCacheStoreTest`
//! (every operation, the key-prefix namespace, the TTL-from-birth discipline)
//! and `CacheRedisHealthCheckTest` (the `redis.health` binding). One booted
//! platform, sequential scenarios (repo convention: the platform boots once
//! per process).

use redis_test_double as common;

use std::collections::HashMap;
use std::time::{Duration, Instant};

use async_trait::async_trait;
use distributed_cache::{handle, runtime, RedisCacheStore, CACHE_ROUTE, HEALTH_ROUTE};
use platform_core::{
    main_application, overrides, AppError, AutoStart, EntryPoint, EventEnvelope, Platform,
    PostOffice,
};
use redis_connection::{RedisBackend, RedisConfig};
use rmpv::Value;

const TIMEOUT: Duration = Duration::from_secs(8);
const PREFIX: &str = "app1:";

#[main_application]
struct CacheTestApp;

#[async_trait]
impl EntryPoint for CacheTestApp {
    async fn start(&self, _args: &[String]) -> Result<(), AppError> {
        Ok(())
    }
}

// ---- helpers ----

fn headers(pairs: &[(&str, &str)]) -> HashMap<String, String> {
    pairs
        .iter()
        .map(|(k, v)| (k.to_string(), v.to_string()))
        .collect()
}

fn bytes(text: &str) -> Value {
    Value::Binary(text.as_bytes().to_vec())
}

async fn call(po: &PostOffice, pairs: &[(&str, &str)], body: Value) -> EventEnvelope {
    let mut event = EventEnvelope::new().set_to(CACHE_ROUTE).set_raw_body(body);
    for (k, v) in pairs {
        event = event.set_header(k, v);
    }
    po.request(event, TIMEOUT).await.expect("cache reply")
}

fn body_text(reply: &EventEnvelope) -> String {
    match reply.body() {
        Value::String(text) => text.as_str().unwrap_or_default().to_string(),
        other => format!("{other}"),
    }
}

/// The wire-visible record for a key (prefixed) and the seconds left on it.
fn ttl_of(store: &common::SharedStore, key: &str) -> Option<Duration> {
    let map = store.lock().expect("raw store");
    map.get(&format!("{PREFIX}{key}").into_bytes())
        .map(|entry| {
            entry
                .expires_at
                .map(|at| at.saturating_duration_since(Instant::now()))
                .unwrap_or(Duration::ZERO)
        })
}

fn assert_ttl_within(store: &common::SharedStore, key: &str, max: Duration, what: &str) {
    let ttl = ttl_of(store, key).unwrap_or_else(|| panic!("{what}: {key} must be stored"));
    assert!(
        ttl > Duration::ZERO && ttl <= max,
        "{what}: unexpected ttl {ttl:?} for {key}"
    );
}

fn journal_count(journal: &common::CommandJournal, command: &str) -> usize {
    journal
        .lock()
        .expect("journal")
        .iter()
        .filter(|c| *c == command)
        .count()
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn distributed_cache_contract() {
    let (port, raw_store, journal) = common::start_resp_double("7.4.1").await;
    platform_core::resources::prepend_resource_root("tests/resources");
    overrides::set("redis.cache.enabled", "true");
    overrides::set("redis.host", "127.0.0.1");
    overrides::set("redis.port", &port.to_string());
    overrides::set("redis.cache.key.prefix", PREFIX);
    overrides::set("redis.cache.default.ttl", "10m");
    overrides::set("redis.health.startup.grace", "0s");
    AutoStart::main(vec![]).await.expect("lifecycle");
    let platform = Platform::get_instance();
    let po = PostOffice::new(&platform);

    // 1) the deployment story: link the crate, enable the switch, and both
    // functions self-register through the preload inventory
    assert!(
        platform.has_route(CACHE_ROUTE),
        "{CACHE_ROUTE} must self-register"
    );
    assert!(
        platform.has_route(HEALTH_ROUTE),
        "{HEALTH_ROUTE} must self-register"
    );
    assert!(
        runtime::current().is_none(),
        "the connection is built lazily - nothing opened at start-up"
    );

    // 2) PUT returns true and GET round-trips the bytes; the record lives under
    // the PREFIXED key with the default TTL from creation (Java
    // putReturnsTrueAndGetRoundTrips + putThenGetRoundTripsBytesWithATtl)
    let stored = call(&po, &[("action", "PUT"), ("key", "k1")], bytes("hi")).await;
    assert_eq!(200, stored.status(), "{}", body_text(&stored));
    assert_eq!(&Value::Boolean(true), stored.body());
    assert_ttl_within(
        &raw_store,
        "k1",
        Duration::from_secs(600),
        "PUT sets the default TTL",
    );
    assert!(
        !raw_store
            .lock()
            .expect("raw store")
            .contains_key(&b"k1".to_vec()),
        "the key must be namespaced by the prefix"
    );
    let read = call(&po, &[("action", "GET"), ("key", "k1")], Value::Nil).await;
    assert_eq!(&bytes("hi"), read.body());
    assert!(
        runtime::current().is_some(),
        "the first call opened the shared connection"
    );

    // 3) the action is case-insensitive (Java actionIsCaseInsensitive)
    let lower = call(&po, &[("action", "get"), ("key", "k1")], Value::Nil).await;
    assert_eq!(&bytes("hi"), lower.body());

    // 4) a miss is a null body, never an error (Java getMissReturnsNull)
    let miss = call(&po, &[("action", "GET"), ("key", "absent")], Value::Nil).await;
    assert_eq!(200, miss.status());
    assert_eq!(&Value::Nil, miss.body());

    // 5) a String body is stored as its UTF-8 bytes (Java aStringBodyIsStoredAsUtf8Bytes)
    call(
        &po,
        &[("action", "PUT"), ("key", "text")],
        Value::from("text-value"),
    )
    .await;
    let text = call(&po, &[("action", "GET"), ("key", "text")], Value::Nil).await;
    assert_eq!(&bytes("text-value"), text.body());

    // 6) the ttl header bounds the key's TTL (Java ttlHeaderIsHonoured)
    call(
        &po,
        &[("action", "PUT"), ("key", "short"), ("ttl", "30s")],
        bytes("v"),
    )
    .await;
    assert_ttl_within(
        &raw_store,
        "short",
        Duration::from_secs(30),
        "the 30s ttl header",
    );
    let bad_ttl = call(
        &po,
        &[("action", "PUT"), ("key", "x"), ("ttl", "soon")],
        bytes("v"),
    )
    .await;
    assert_eq!(400, bad_ttl.status());
    assert!(body_text(&bad_ttl).contains("Invalid 'ttl'"));

    // 7) DELETE returns the count removed, then nothing (Java deleteReturnsCount
    // + deleteReturnsTheCountRemoved)
    let removed = call(&po, &[("action", "DELETE"), ("key", "k1")], Value::Nil).await;
    assert_eq!(&Value::from(1), removed.body());
    let again = call(&po, &[("action", "DELETE"), ("key", "k1")], Value::Nil).await;
    assert_eq!(
        &Value::from(0),
        again.body(),
        "deleting an absent key removes nothing"
    );
    let gone = call(&po, &[("action", "GET"), ("key", "k1")], Value::Nil).await;
    assert_eq!(&Value::Nil, gone.body());

    // 8) PUT_IF_NOT_PRESENT is atomic SET NX EX: stored once, then refused, the
    // original untouched, a TTL on the key (Java putIfNotPresentReturnsBoolean +
    // putIfAbsentStoresOnlyWhenAbsentAndIsAtomicWithTtl)
    let first = call(
        &po,
        &[("action", "PUT_IF_NOT_PRESENT"), ("key", "once")],
        bytes("first"),
    )
    .await;
    assert_eq!(&Value::Boolean(true), first.body(), "stored when absent");
    let second = call(
        &po,
        &[("action", "PUT_IF_NOT_PRESENT"), ("key", "once")],
        bytes("second"),
    )
    .await;
    assert_eq!(
        &Value::Boolean(false),
        second.body(),
        "not stored when present"
    );
    let kept = call(&po, &[("action", "GET"), ("key", "once")], Value::Nil).await;
    assert_eq!(
        &bytes("first"),
        kept.body(),
        "the original value is untouched"
    );
    assert_ttl_within(
        &raw_store,
        "once",
        Duration::from_secs(600),
        "SET NX EX sets a TTL atomically",
    );
    assert!(
        journal_count(&journal, "SET") >= 2,
        "put-if-absent must be ONE SET command"
    );

    // 9) MGET returns a map of the present keys, misses omitted, keys
    // unprefixed, request order (Java mgetReturnsAMapOfPresentKeys +
    // mgetReturnsPresentKeysAndOmitsMisses + keyPrefix...IsStrippedFromMget)
    call(&po, &[("action", "PUT"), ("key", "a")], bytes("1")).await;
    call(&po, &[("action", "PUT"), ("key", "c")], bytes("3")).await;
    let found = call(
        &po,
        &[("action", "MGET")],
        Value::Array(vec![Value::from("a"), Value::from("b"), Value::from("c")]),
    )
    .await;
    assert_eq!(
        &Value::Map(vec![
            (Value::from("a"), bytes("1")),
            (Value::from("c"), bytes("3")),
        ]),
        found.body(),
        "misses are omitted, not null entries; keys come back without the prefix"
    );
    assert_eq!(1, journal_count(&journal, "MGET"), "one MGET round trip");

    // 10) MPUT writes every entry with its own TTL in one pipelined round trip,
    // non-atomic (no MULTI) - then reads back (Java mputWritesEveryEntryThenReadsBack
    // + mputPipelinesEveryEntryWithItsTtl)
    let setex_before = journal_count(&journal, "SETEX");
    let multi_before = journal_count(&journal, "MULTI");
    let mput = call(
        &po,
        &[("action", "MPUT"), ("ttl", "2m")],
        Value::Map(vec![
            (Value::from("m1"), bytes("one")),
            (Value::from("m2"), Value::from("two")),
        ]),
    )
    .await;
    assert_eq!(&Value::Boolean(true), mput.body());
    assert_eq!(
        setex_before + 2,
        journal_count(&journal, "SETEX"),
        "one SETEX per entry"
    );
    assert_eq!(
        multi_before,
        journal_count(&journal, "MULTI"),
        "MPUT is pipelined, not a transaction"
    );
    assert_ttl_within(
        &raw_store,
        "m1",
        Duration::from_secs(120),
        "each MPUT entry carries the ttl",
    );
    assert_ttl_within(
        &raw_store,
        "m2",
        Duration::from_secs(120),
        "each MPUT entry carries the ttl",
    );
    let m2 = call(&po, &[("action", "GET"), ("key", "m2")], Value::Nil).await;
    assert_eq!(
        &bytes("two"),
        m2.body(),
        "a String entry value is stored as UTF-8"
    );

    // 11) the FIFO list: push returns the length, len counts, pop drains oldest
    // first, a drained list ceases to exist, and the key carries a TTL from the
    // atomic RPUSH+EXPIRE step (Java listPushPopLen + listPushPopLenAreFifoWithATtl)
    let multi_before = journal_count(&journal, "MULTI");
    let n1 = call(
        &po,
        &[("action", "LIST_PUSH"), ("key", "q")],
        bytes("first"),
    )
    .await;
    assert_eq!(&Value::from(1), n1.body());
    let n2 = call(
        &po,
        &[("action", "LIST_PUSH"), ("key", "q")],
        bytes("second"),
    )
    .await;
    assert_eq!(&Value::from(2), n2.body());
    assert_eq!(
        multi_before + 2,
        journal_count(&journal, "MULTI"),
        "each push is one MULTI/EXEC step"
    );
    assert_ttl_within(
        &raw_store,
        "q",
        Duration::from_secs(600),
        "LIST_PUSH sets a TTL atomically",
    );
    let len = call(&po, &[("action", "LIST_LEN"), ("key", "q")], Value::Nil).await;
    assert_eq!(&Value::from(2), len.body());
    let p1 = call(&po, &[("action", "LIST_POP"), ("key", "q")], Value::Nil).await;
    assert_eq!(&bytes("first"), p1.body(), "FIFO: the oldest value first");
    let p2 = call(&po, &[("action", "LIST_POP"), ("key", "q")], Value::Nil).await;
    assert_eq!(&bytes("second"), p2.body());
    let empty = call(&po, &[("action", "LIST_POP"), ("key", "q")], Value::Nil).await;
    assert_eq!(
        &Value::Nil,
        empty.body(),
        "popping an empty list returns null"
    );
    let drained = call(&po, &[("action", "LIST_LEN"), ("key", "q")], Value::Nil).await;
    assert_eq!(
        &Value::from(0),
        drained.body(),
        "a drained list ceases to exist"
    );

    // 12) the rejections, surfaced as the event's error (Java missingActionIsRejected,
    // unsupportedActionIsRejected, putWithoutAValueIsRejected, mgetWithoutAListBodyIsRejected)
    let no_action = call(&po, &[("key", "k1")], Value::Nil).await;
    assert_eq!(400, no_action.status());
    assert!(
        body_text(&no_action).contains("action"),
        "{}",
        body_text(&no_action)
    );
    let incr = call(&po, &[("action", "INCR"), ("key", "k1")], Value::Nil).await;
    assert_eq!(400, incr.status());
    assert!(
        body_text(&incr).contains("INCR"),
        "the unsupported action is named"
    );
    let no_value = call(&po, &[("action", "PUT"), ("key", "k1")], Value::Nil).await;
    assert_eq!(400, no_value.status());
    assert!(
        body_text(&no_value).contains("value"),
        "{}",
        body_text(&no_value)
    );
    let no_list = call(&po, &[("action", "MGET")], bytes("not-a-list")).await;
    assert_eq!(400, no_list.status());
    assert!(
        body_text(&no_list).contains("MGET requires a List"),
        "{}",
        body_text(&no_list)
    );
    let no_map = call(&po, &[("action", "MPUT")], bytes("not-a-map")).await;
    assert_eq!(400, no_map.status());
    assert!(
        body_text(&no_map).contains("MPUT requires a Map"),
        "{}",
        body_text(&no_map)
    );
    let no_key = call(&po, &[("action", "GET")], Value::Nil).await;
    assert_eq!(400, no_key.status());
    assert!(
        body_text(&no_key).contains("Missing 'key'"),
        "{}",
        body_text(&no_key)
    );

    // 13) redis.health: info names the dependency from the plain namespace, and
    // a live probe finds the server reachable (Java infoResolvesThePlainRedisNamespaceHref;
    // the grace was set to 0s so the probe is live at once)
    let info = po
        .request(
            EventEnvelope::new()
                .set_to(HEALTH_ROUTE)
                .set_header("type", "info"),
            TIMEOUT,
        )
        .await
        .expect("info reply");
    let info: serde_json::Value = info.body_as().expect("json body");
    assert_eq!("redis", info["service"]);
    assert_eq!(format!("127.0.0.1:{port}"), info["href"]);
    let health = po
        .request(
            EventEnvelope::new()
                .set_to(HEALTH_ROUTE)
                .set_header("type", "health"),
            TIMEOUT,
        )
        .await
        .expect("health reply");
    assert_eq!(200, health.status());
    let health: serde_json::Value = health.body_as().expect("json body");
    assert_eq!("Redis is reachable", health["status"]);

    // 14) the empty bulk cases are no-ops at the store level (Java mgetOnEmptyKeysIsAnEmptyMap
    // + mputOnEmptyMapIsANoOp) - driven through the reuse seam with an explicit store
    let backend = RedisBackend::connect(&RedisConfig::new("127.0.0.1", port, "", false, 0, 2000))
        .await
        .expect("connect");
    let store = RedisCacheStore::new(backend, "", 60);
    let empty_mget = handle(
        &headers(&[("action", "MGET")]),
        &Value::Array(vec![]),
        &store,
    )
    .await
    .expect("empty mget");
    assert_eq!(&Value::Map(vec![]), empty_mget.body());
    let empty_mput = handle(&headers(&[("action", "MPUT")]), &Value::Map(vec![]), &store)
        .await
        .expect("empty mput");
    assert_eq!(&Value::Boolean(true), empty_mput.body());
    assert_eq!(60, store.default_ttl_seconds());
    assert!(!store.backend().cluster());

    // 15) shutdown releases the shared connection; the next call rebuilds it
    // from live configuration (the lazy runtime's whole point)
    runtime::shutdown();
    assert!(runtime::current().is_none());
    let rebuilt = call(&po, &[("action", "GET"), ("key", "once")], Value::Nil).await;
    assert_eq!(
        &bytes("first"),
        rebuilt.body(),
        "the store rebuilt on demand"
    );
    assert!(runtime::current().is_some());
}