sassi 0.1.0-beta.4

Typed cache substrate for Rust applications with composable predicate algebra and cross-runtime trait queries.
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#![cfg(all(feature = "serde", feature = "runtime-tokio"))]

//! Behavior tests for the whole-Punnu snapshot wrapper:
//! [`Punnu::snapshot_postcard`] and [`Punnu::restore_postcard`].
//!
//! The wrapper auto-dispatches between entries-only and
//! internal-state shapes based on the wire kind byte, so these tests
//! cover both happy paths and the kind/version rejection edges.

use async_trait::async_trait;
use sassi::{
    BackendError, BackendFailureMode, BackendInvalidationStream, BackendKeyspace, CacheBackend,
    Cacheable, Field, Punnu, PunnuConfig, PunnuRestoreStats, PunnuSnapshotError, SnapshotMode,
    WireFormatError, wire,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Notify;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct E {
    id: i64,
    label: String,
}

#[derive(Default)]
struct EFields {
    #[allow(dead_code)]
    id: Field<E, i64>,
}

impl Cacheable for E {
    type Id = i64;
    type Fields = EFields;

    fn id(&self) -> i64 {
        self.id
    }

    fn fields() -> EFields {
        EFields {
            id: Field::new("id", |e| &e.id),
        }
    }
}

/// Distinct cacheable type for cross-type rejection tests.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct F {
    id: i64,
}

#[derive(Default)]
struct FFields {
    #[allow(dead_code)]
    id: Field<F, i64>,
}

impl Cacheable for F {
    type Id = i64;
    type Fields = FFields;

    fn id(&self) -> i64 {
        self.id
    }

    fn fields() -> FFields {
        FFields {
            id: Field::new("id", |f| &f.id),
        }
    }
}

// =====================================================================
// EntriesOnly mode
// =====================================================================

#[test]
fn snapshot_mode_default_is_entries_only() {
    assert_eq!(SnapshotMode::default(), SnapshotMode::EntriesOnly);
}

#[tokio::test]
async fn snapshot_postcard_entries_only_matches_export_entries_postcard_byte_for_byte() {
    // The wrapper in EntriesOnly mode should produce the same byte
    // stream as `export_entries_postcard` so existing readers cannot
    // tell them apart and the wire format is preserved.
    let pool = Punnu::<E>::builder().build();
    pool.insert(E {
        id: 1,
        label: "one".into(),
    })
    .await
    .unwrap();
    pool.insert(E {
        id: 2,
        label: "two".into(),
    })
    .await
    .unwrap();

    let direct = pool.export_entries_postcard().unwrap();
    let wrapped = pool.snapshot_postcard(SnapshotMode::EntriesOnly).unwrap();

    assert_eq!(direct, wrapped);
}

#[tokio::test]
async fn restore_postcard_accepts_legacy_export_entries_postcard_byte_stream() {
    // The wrapper must accept byte streams produced by the historical
    // entries-only API. Adopters that already persist
    // `export_entries_postcard` bytes can switch to the new wrapper at
    // restore time without re-shaping their on-disk data.
    let donor = Punnu::<E>::builder().build();
    donor
        .insert(E {
            id: 7,
            label: "seven".into(),
        })
        .await
        .unwrap();
    let bytes = donor.export_entries_postcard().unwrap();

    let pool = Punnu::<E>::builder().build();
    let stats = pool.restore_postcard(&bytes).unwrap();
    assert_eq!(stats.inserted, 1);
    assert_eq!(pool.get(&7).unwrap().label, "seven");
}

#[tokio::test]
async fn restore_postcard_accepts_entries_only_byte_stream() {
    let donor = Punnu::<E>::builder().build();
    donor
        .insert(E {
            id: 7,
            label: "seven".into(),
        })
        .await
        .unwrap();
    let bytes = donor.snapshot_postcard(SnapshotMode::EntriesOnly).unwrap();

    let pool = Punnu::<E>::builder().build();
    let stats = pool.restore_postcard(&bytes).unwrap();
    assert_eq!(
        stats,
        PunnuRestoreStats {
            inserted: 1,
            updated: 0,
            removed: 0,
        }
    );
    assert_eq!(pool.get(&7).unwrap().label, "seven");
}

#[tokio::test]
async fn restore_entries_postcard_rejects_with_hints_kind() {
    // The lower-level entries-only restore must continue to reject
    // with-hints byte streams as a kind mismatch — the wrapper is the
    // only entry point that auto-dispatches between kinds.
    let donor = Punnu::<E>::builder().build();
    donor
        .insert(E {
            id: 1,
            label: "v".into(),
        })
        .await
        .unwrap();
    let bytes = donor
        .snapshot_postcard(SnapshotMode::WithInternalState)
        .unwrap();

    let pool = Punnu::<E>::builder().build();
    let err = pool.restore_entries_postcard(&bytes).unwrap_err();
    match err {
        PunnuSnapshotError::WireFormat(WireFormatError::KindMismatch { got, expected }) => {
            assert_eq!(got, 0x04, "with-hints kind byte");
            assert_eq!(expected, 0x03, "entries-only kind byte");
        }
        other => panic!("expected kind mismatch, got {other:?}"),
    }
}

// =====================================================================
// WithInternalState mode
// =====================================================================

#[tokio::test]
async fn snapshot_postcard_with_internal_state_round_trips_values() {
    let donor = Punnu::<E>::builder().build();
    donor
        .insert(E {
            id: 1,
            label: "a".into(),
        })
        .await
        .unwrap();
    donor
        .insert(E {
            id: 2,
            label: "b".into(),
        })
        .await
        .unwrap();
    let bytes = donor
        .snapshot_postcard(SnapshotMode::WithInternalState)
        .unwrap();

    let pool = Punnu::<E>::builder().build();
    let stats = pool.restore_postcard(&bytes).unwrap();
    assert_eq!(stats.inserted, 2);
    assert_eq!(pool.get(&1).unwrap().label, "a");
    assert_eq!(pool.get(&2).unwrap().label, "b");
}

#[tokio::test(start_paused = true)]
async fn snapshot_with_internal_state_preserves_remaining_ttl() {
    // EntriesOnly resets TTL to the receiving pool's `default_ttl`.
    // WithInternalState carries the source's remaining TTL across the
    // boundary, so an entry that had a long deadline before snapshot
    // continues to have a long deadline after restore — independent of
    // the receiving pool's own `default_ttl` setting.
    let donor = Punnu::<E>::builder()
        .config(PunnuConfig {
            default_ttl: Some(Duration::from_secs(60)),
            ..Default::default()
        })
        .build();
    donor
        .insert(E {
            id: 1,
            label: "v".into(),
        })
        .await
        .unwrap();

    // Move 10 seconds forward on the donor's clock so the remaining
    // TTL is ~50s.
    tokio::time::advance(Duration::from_secs(10)).await;

    let bytes = donor
        .snapshot_postcard(SnapshotMode::WithInternalState)
        .unwrap();

    let pool = Punnu::<E>::builder()
        .config(PunnuConfig {
            // Receiving pool's default would expire the entry immediately,
            // so any state visible after restore proves that the snapshot's
            // remaining-TTL hint took precedence.
            default_ttl: Some(Duration::from_millis(1)),
            ..Default::default()
        })
        .build();
    pool.restore_postcard(&bytes).unwrap();

    // After restore plus a small wait that exceeds the receiving
    // pool's `default_ttl` but is well under the carried 50s
    // remainder, the entry should still be readable.
    tokio::time::advance(Duration::from_millis(500)).await;
    assert!(
        pool.get(&1).is_some(),
        "with-hints restore must honor the source's remaining TTL, not the receiving pool's default_ttl"
    );

    // After the carried remainder fully elapses, the entry must look
    // absent through the lazy-expiry path.
    tokio::time::advance(Duration::from_secs(60)).await;
    assert!(
        pool.get(&1).is_none(),
        "with-hints entry must expire after its carried remaining TTL"
    );
}

#[tokio::test]
async fn snapshot_with_internal_state_distinguishes_no_ttl_from_short_ttl() {
    // An entry without a TTL on the source should not gain one on
    // restore through the with-hints body. We cannot directly assert
    // "no expiry" on the public surface, but we can verify the entry
    // is still readable far past the receiving pool's tiny default
    // TTL, which the with-hints body must override with `None`.
    let donor = Punnu::<E>::builder().build(); // no default TTL
    donor
        .insert(E {
            id: 1,
            label: "no-ttl".into(),
        })
        .await
        .unwrap();
    let bytes = donor
        .snapshot_postcard(SnapshotMode::WithInternalState)
        .unwrap();

    let pool = Punnu::<E>::builder()
        .config(PunnuConfig {
            default_ttl: Some(Duration::from_millis(1)),
            ..Default::default()
        })
        .build();
    pool.restore_postcard(&bytes).unwrap();

    tokio::time::sleep(Duration::from_millis(50)).await;
    assert!(
        pool.get(&1).is_some(),
        "with-hints restore must preserve 'no TTL' rather than apply the receiving pool's default"
    );
}

#[tokio::test]
async fn snapshot_with_internal_state_preserves_relative_lru_order() {
    // The donor pool touches three entries in a known order; the
    // receiving pool restores under capacity pressure of size 2, so
    // exactly one of the three must be evicted by sampled-LRU. Without
    // hint preservation the receiving pool would issue fresh epochs
    // and any eviction is possible. With hints, the most-recently
    // accessed entry on the source should be the most resistant to
    // eviction on the target.
    let donor = Punnu::<E>::builder().build();
    donor
        .insert(E {
            id: 1,
            label: "a".into(),
        })
        .await
        .unwrap();
    donor
        .insert(E {
            id: 2,
            label: "b".into(),
        })
        .await
        .unwrap();
    donor
        .insert(E {
            id: 3,
            label: "c".into(),
        })
        .await
        .unwrap();

    // Touch id 3 once more to make it the most recent on the donor
    // pool. Sampled LRU is probabilistic on small N, so we use a
    // capacity equal to the entry count and assert the byte stream
    // round-trips.
    let _ = donor.get(&3);

    let bytes = donor
        .snapshot_postcard(SnapshotMode::WithInternalState)
        .unwrap();

    // Restore into a pool of exactly the same size; sampled-LRU has
    // no eviction work to do, but the receiving pool's access clock
    // must advance past the highest restored rank so the next
    // ordinary `insert` still issues an epoch greater than every
    // restored entry's epoch. Verify by inserting a fourth entry into
    // a slightly larger pool and checking it is the freshest.
    let pool = Punnu::<E>::builder()
        .config(PunnuConfig {
            lru_size: 4,
            ..Default::default()
        })
        .build();
    pool.restore_postcard(&bytes).unwrap();
    pool.insert(E {
        id: 4,
        label: "d".into(),
    })
    .await
    .unwrap();
    assert!(pool.get(&4).is_some());
    assert!(pool.get(&3).is_some());
    assert!(pool.get(&2).is_some());
    assert!(pool.get(&1).is_some());
}

#[tokio::test]
async fn restore_postcard_rejects_future_reserved_kind_byte() {
    // Build a byte stream with a wire kind byte beyond the current
    // implementation's understanding (0x05+). The wrapper's kind-peek
    // must reject before any per-body decode work.
    let pool = Punnu::<E>::builder().build();
    let mut bytes = Vec::new();
    encode_test_header(&mut bytes, 0x05, E::cache_type_name());
    bytes.extend_from_slice(&[0xff, 0xff, 0xff, 0xff]);

    let err = pool.restore_postcard(&bytes).unwrap_err();
    match err {
        PunnuSnapshotError::WireFormat(WireFormatError::UnsupportedKind { kind }) => {
            assert_eq!(kind, 0x05);
        }
        other => panic!("expected UnsupportedKind, got {other:?}"),
    }
}

#[tokio::test]
async fn restore_postcard_rejects_unknown_envelope_version() {
    // Build a with-hints byte stream whose envelope version is not the
    // current implementation's. Restore must reject as a wire-format
    // codec error before any L1 mutation.
    let pool = Punnu::<E>::builder().build();

    let mut bytes = Vec::new();
    encode_test_header(&mut bytes, 0x04, E::cache_type_name());
    bytes.extend_from_slice(&999_u16.to_le_bytes()); // bad envelope version
    bytes.extend_from_slice(&0_u32.to_le_bytes()); // count
    let err = pool.restore_postcard(&bytes).unwrap_err();
    match err {
        PunnuSnapshotError::WireFormat(WireFormatError::Codec(message)) => {
            assert!(
                message.contains("envelope version mismatch"),
                "expected envelope version mismatch, got {message}"
            );
        }
        other => panic!("expected wire-format codec error, got {other:?}"),
    }
    assert!(pool.is_empty());
}

#[tokio::test]
async fn restore_postcard_rejects_with_hints_type_mismatch() {
    // A with-hints byte stream produced by donor pool of type F must
    // not restore into a pool of type E.
    let donor: Punnu<F> = Punnu::<F>::builder().build();
    donor.insert(F { id: 1 }).await.unwrap();
    let bytes = donor
        .snapshot_postcard(SnapshotMode::WithInternalState)
        .unwrap();

    let pool: Punnu<E> = Punnu::<E>::builder().build();
    let err = pool.restore_postcard(&bytes).unwrap_err();
    match err {
        PunnuSnapshotError::WireFormat(WireFormatError::TypeNameMismatch { .. }) => {}
        other => panic!("expected type-name mismatch, got {other:?}"),
    }
}

#[tokio::test]
async fn restore_postcard_rejects_with_hints_capacity_overflow() {
    // Build a with-hints byte stream that declares more entries than
    // the receiving pool's capacity. The count guard must reject
    // before any per-entry decode work.
    let pool: Punnu<E> = Punnu::<E>::builder()
        .config(PunnuConfig {
            lru_size: 2,
            ..Default::default()
        })
        .build();

    let mut bytes = Vec::new();
    encode_test_header(&mut bytes, 0x04, E::cache_type_name());
    bytes.extend_from_slice(&1_u16.to_le_bytes()); // current envelope version
    bytes.extend_from_slice(&5_u32.to_le_bytes()); // count exceeds lru_size
    let err = pool.restore_postcard(&bytes).unwrap_err();
    match err {
        PunnuSnapshotError::TooManyEntries { entries, limit } => {
            assert_eq!(entries, 5);
            assert_eq!(limit, 2);
        }
        other => panic!("expected TooManyEntries, got {other:?}"),
    }
}

#[tokio::test]
async fn restore_postcard_rejects_with_hints_duplicate_id() {
    let mut bytes = Vec::new();
    encode_test_header(&mut bytes, 0x04, E::cache_type_name());
    bytes.extend_from_slice(&1_u16.to_le_bytes()); // version
    bytes.extend_from_slice(&2_u32.to_le_bytes()); // count
    // Two entries with the same id.
    let body_a = postcard::to_allocvec(&E {
        id: 1,
        label: "a".into(),
    })
    .unwrap();
    bytes.extend_from_slice(&body_a);
    bytes.push(0); // tag = none
    bytes.extend_from_slice(&0_u64.to_le_bytes()); // ms
    bytes.extend_from_slice(&0_u32.to_le_bytes()); // rank
    let body_b = postcard::to_allocvec(&E {
        id: 1,
        label: "b".into(),
    })
    .unwrap();
    bytes.extend_from_slice(&body_b);
    bytes.push(0);
    bytes.extend_from_slice(&0_u64.to_le_bytes());
    bytes.extend_from_slice(&1_u32.to_le_bytes());

    let pool = Punnu::<E>::builder().build();
    let err = pool.restore_postcard(&bytes).unwrap_err();
    assert!(matches!(err, PunnuSnapshotError::DuplicateId));
    assert!(pool.is_empty());
}

#[tokio::test]
async fn restore_postcard_rejects_with_hints_strict_backend_write_in_flight() {
    let backend = BlockingStrictPutBackend::default();
    let put_entered = backend.put_entered.clone();
    let put_release = backend.put_release.clone();

    let pool: Punnu<E> = Punnu::<E>::builder()
        .config(PunnuConfig {
            backend_failure_mode: BackendFailureMode::Error,
            ..Default::default()
        })
        .backend(backend)
        .build();

    let inserter = {
        let pool = pool.clone();
        tokio::spawn(async move {
            pool.insert(E {
                id: 99,
                label: "blocking".into(),
            })
            .await
        })
    };
    tokio::time::timeout(Duration::from_secs(1), put_entered.notified())
        .await
        .expect("strict put should reach backend");

    let donor: Punnu<E> = Punnu::<E>::builder().build();
    donor
        .insert(E {
            id: 8,
            label: "snap".into(),
        })
        .await
        .unwrap();
    let bytes = donor
        .snapshot_postcard(SnapshotMode::WithInternalState)
        .unwrap();

    let err = pool.restore_postcard(&bytes).unwrap_err();
    match err {
        PunnuSnapshotError::BackendWriteInFlight { reserved } => {
            assert_eq!(reserved, 1);
        }
        other => panic!("expected BackendWriteInFlight, got {other:?}"),
    }
    assert!(pool.get(&8).is_none());

    put_release.notify_one();
    inserter.await.unwrap().unwrap();
}

// =====================================================================
// Helpers
// =====================================================================

const MAGIC: &[u8; 8] = b"SASSI\0W\0";

fn encode_test_header(out: &mut Vec<u8>, kind: u8, type_name: &str) {
    out.extend_from_slice(MAGIC);
    out.extend_from_slice(&wire::WIRE_FORMAT_MAJOR.to_le_bytes());
    out.push(kind);
    out.push(0); // flags
    let name = type_name.as_bytes();
    out.extend_from_slice(&(name.len() as u16).to_le_bytes());
    out.extend_from_slice(name);
}

#[derive(Default)]
struct BlockingStrictPutBackend {
    put_entered: Arc<Notify>,
    put_release: Arc<Notify>,
}

#[async_trait]
impl CacheBackend<E> for BlockingStrictPutBackend {
    async fn get(&self, _keyspace: &BackendKeyspace, _id: &i64) -> Result<Option<E>, BackendError> {
        Ok(None)
    }

    async fn put(
        &self,
        _keyspace: &BackendKeyspace,
        _id: &i64,
        _value: &E,
        _ttl: Option<Duration>,
    ) -> Result<(), BackendError> {
        self.put_entered.notify_one();
        self.put_release.notified().await;
        Ok(())
    }

    async fn invalidate(&self, _keyspace: &BackendKeyspace, _id: &i64) -> Result<(), BackendError> {
        Ok(())
    }

    async fn invalidate_all(&self, _keyspace: &BackendKeyspace) -> Result<(), BackendError> {
        Ok(())
    }

    fn invalidation_stream(&self, _keyspace: BackendKeyspace) -> BackendInvalidationStream<i64> {
        Box::pin(futures::stream::empty())
    }
}