valkey-module 0.1.14

A toolkit for building valkey modules in Rust
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
use super::call::TestCallExpectation;
use super::context::TestContext;
use crate::context::thread_safe::{DetachedFromClient, ThreadSafeContext};
use crate::{raw, ValkeyValue};
use std::cell::RefCell;
use std::collections::HashMap;
use std::ops::Deref;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, OnceLock};

pub(super) fn install() {
    // SAFETY: `setup_test_shims` calls this once after verifying the real API is uninitialized.
    unsafe {
        raw::RedisModule_GetThreadSafeContext = Some(get_thread_safe_context);
        raw::RedisModule_ThreadSafeContextLock = Some(thread_safe_context_lock);
        raw::RedisModule_ThreadSafeContextUnlock = Some(thread_safe_context_unlock);
        raw::RedisModule_FreeThreadSafeContext = Some(free_thread_safe_context);
    }
}

// Maps live base context tokens to synchronized fixture state so expectations can be configured
// on one thread and installed into an ordinary test context on the thread that locks it.
static THREAD_SAFE_CONTEXTS: OnceLock<Mutex<HashMap<usize, SharedContextData>>> = OnceLock::new();
static NEXT_TEST_CONTEXT_TOKEN: AtomicUsize = AtomicUsize::new(1);

thread_local! {
    /// Associates a lock operation with the next guard context created on the same thread.
    static PENDING_LOCK_STATE: RefCell<PendingLockState> = RefCell::default();
    /// Keeps each ordinary test context alive until its corresponding guard is freed.
    static LIVE_GUARD_CONTEXTS: RefCell<HashMap<usize, TestContext>> = RefCell::default();
}

impl ThreadSafeContext<DetachedFromClient> {
    /// Creates a configurable thread-safe context for tests without a running Valkey server.
    ///
    /// The test shim supports only thread-safe contexts detached from blocked clients. It does
    /// not simulate a thread-safe context associated with a [`crate::BlockedClient`].
    #[must_use]
    pub fn test() -> TestThreadSafeContext {
        TestThreadSafeContext::new()
    }
}

/// Stores expectations owned by one test thread-safe context.
#[derive(Clone, Default)]
struct ThreadSafeContextData {
    client_id: Option<u64>,
    call_expectations: Vec<TestCallExpectation>,
}

/// Provides synchronized ownership of context expectations across test threads.
type SharedContextData = Arc<Mutex<ThreadSafeContextData>>;

/// Distinguishes a standalone context request from valid and invalid lock requests.
#[derive(Default)]
enum PendingLockState {
    #[default]
    Unlocked,
    Valid(SharedContextData),
    Invalid,
}

/// Owns a configurable test-only detached [`ThreadSafeContext`] backed by synchronized state.
pub struct TestThreadSafeContext {
    context: ThreadSafeContext<DetachedFromClient>,
    data: SharedContextData,
}

// Constructs thread-safe test contexts and configures their shared callback expectations.
impl TestThreadSafeContext {
    fn new() -> Self {
        super::setup_test_shims();

        let context = ThreadSafeContext::new();
        let data = state(context.ctx).expect("new test thread-safe context should be registered");
        Self { context, data }
    }

    /// Configures the value returned by [`crate::Context::get_client_id`].
    pub fn expect_get_client_id(&mut self, client_id: u64) -> &mut Self {
        lock_state(&self.data).client_id = Some(client_id);
        self
    }

    /// Configures a reply for an exact command and argument list.
    ///
    /// Expectations form a reusable fixture template. Each lock receives a fresh copy, so the
    /// configured reply remains available across repeated and concurrent locks.
    pub fn expect_call<T: AsRef<[u8]>>(
        &mut self,
        command: impl AsRef<[u8]>,
        args: &[T],
        reply: ValkeyValue,
    ) -> &mut Self {
        let expectation = TestCallExpectation::new(command, args, reply)
            .expect("unsupported reply type configured for test-shim call");
        lock_state(&self.data).call_expectations.push(expectation);
        self
    }

    /// Configures the value returned by [`crate::Context::config_get`].
    pub fn expect_config_get(
        &mut self,
        config: impl Into<String>,
        value: impl Into<String>,
    ) -> &mut Self {
        let config = config.into();
        self.expect_call(
            "CONFIG",
            &["GET", config.as_str()],
            ValkeyValue::Array(vec![
                ValkeyValue::SimpleString(config.clone()),
                ValkeyValue::SimpleString(value.into()),
            ]),
        )
    }
}

impl Deref for TestThreadSafeContext {
    type Target = ThreadSafeContext<DetachedFromClient>;

    fn deref(&self) -> &Self::Target {
        &self.context
    }
}

/// Implements detached test contexts only; the blocked-client association is not shimmed.
pub(super) extern "C" fn get_thread_safe_context(
    _blocked_client: *mut raw::RedisModuleBlockedClient,
) -> *mut raw::RedisModuleCtx {
    let pending = PENDING_LOCK_STATE.with(|pending| std::mem::take(&mut *pending.borrow_mut()));
    match pending {
        PendingLockState::Unlocked => register_new(),
        PendingLockState::Valid(data) => create_guard_context(data),
        PendingLockState::Invalid => std::ptr::null_mut(),
    }
}

pub(super) extern "C" fn thread_safe_context_lock(ctx: *mut raw::RedisModuleCtx) {
    PENDING_LOCK_STATE.with(|pending| {
        *pending.borrow_mut() = state(ctx)
            .map(PendingLockState::Valid)
            .unwrap_or(PendingLockState::Invalid);
    });
}

pub(super) extern "C" fn thread_safe_context_unlock(_ctx: *mut raw::RedisModuleCtx) {
    PENDING_LOCK_STATE.with(|pending| {
        *pending.borrow_mut() = PendingLockState::Unlocked;
    });
}

pub(super) extern "C" fn free_thread_safe_context(ctx: *mut raw::RedisModuleCtx) {
    let guard = LIVE_GUARD_CONTEXTS.with(|contexts| contexts.borrow_mut().remove(&(ctx as usize)));
    if guard.is_none() {
        unregister(ctx);
    }
}

/// Allocates a new opaque thread-safe context token with default synchronized state.
fn register_new() -> *mut raw::RedisModuleCtx {
    register_shared(Arc::new(Mutex::new(ThreadSafeContextData::default())))
}

/// Issues a unique opaque thread-safe context token that resolves to existing state.
fn register_shared(data: SharedContextData) -> *mut raw::RedisModuleCtx {
    // The token is never dereferenced. Odd monotonic values cannot collide with aligned ordinary
    // test-context pointers and prevent stale tokens from resolving to replacement contexts.
    let token = NEXT_TEST_CONTEXT_TOKEN
        .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |token| {
            token.checked_add(2)
        })
        .expect("thread-safe test context token space exhausted");
    let ctx = token as *mut raw::RedisModuleCtx;
    lock_registry().insert(ctx as usize, data);
    ctx
}

/// Resolves a live opaque thread-safe context token to its synchronized state.
fn state(ctx: *mut raw::RedisModuleCtx) -> Option<SharedContextData> {
    if ctx.is_null() {
        return None;
    }
    lock_registry().get(&(ctx as usize)).cloned()
}

/// Unregisters a thread-safe context and retires its opaque token.
fn unregister(ctx: *mut raw::RedisModuleCtx) {
    if !ctx.is_null() {
        lock_registry().remove(&(ctx as usize));
    }
}

fn registry() -> &'static Mutex<HashMap<usize, SharedContextData>> {
    THREAD_SAFE_CONTEXTS.get_or_init(|| Mutex::new(HashMap::new()))
}

fn lock_registry() -> MutexGuard<'static, HashMap<usize, SharedContextData>> {
    registry()
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

fn lock_state(data: &SharedContextData) -> MutexGuard<'_, ThreadSafeContextData> {
    data.lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

/// Creates an ordinary test context on the locking thread and replays all configured expectations.
fn create_guard_context(data: SharedContextData) -> *mut raw::RedisModuleCtx {
    let data = lock_state(&data).clone();
    let mut context = TestContext::new_thread_safe_guard();

    if let Some(client_id) = data.client_id {
        context.expect_get_client_id(client_id);
    }
    for expectation in data.call_expectations {
        context.expect_call_reply(expectation);
    }

    let ctx = context.ctx;
    let replaced =
        LIVE_GUARD_CONTEXTS.with(|contexts| contexts.borrow_mut().insert(ctx as usize, context));
    assert!(
        replaced.is_none(),
        "new guard context should have a unique pointer"
    );
    ctx
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Context, RedisModuleClientInfo, ValkeyError, ValkeyValue};
    use std::ptr::null_mut;
    use std::sync::Barrier;

    #[test]
    fn creates_registered_test_context_for_guard() {
        let base = get_thread_safe_context(null_mut());
        thread_safe_context_lock(base);
        let guard = get_thread_safe_context(null_mut());

        assert!(state(base).is_some());
        assert!(state(guard).is_none());
        assert!(
            LIVE_GUARD_CONTEXTS.with(|contexts| contexts.borrow().contains_key(&(guard as usize)))
        );

        thread_safe_context_unlock(guard);
        free_thread_safe_context(guard);
        assert!(
            !LIVE_GUARD_CONTEXTS.with(|contexts| contexts.borrow().contains_key(&(guard as usize)))
        );

        free_thread_safe_context(base);
        assert!(state(base).is_none());
    }

    #[test]
    fn retired_context_token_is_not_reused() {
        let retired = get_thread_safe_context(null_mut());
        free_thread_safe_context(retired);

        let replacement = get_thread_safe_context(null_mut());

        assert_ne!(retired, replacement);
        assert!(state(retired).is_none());
        free_thread_safe_context(replacement);
    }

    #[test]
    fn invalid_lock_for_null_handle_is_rejected() {
        assert_invalid_lock_is_rejected(null_mut());
    }

    #[test]
    fn invalid_lock_for_foreign_handle_is_rejected() {
        let foreign = 2usize as *mut raw::RedisModuleCtx;

        assert_invalid_lock_is_rejected(foreign);
    }

    #[test]
    fn invalid_lock_for_retired_handle_is_rejected() {
        let retired = get_thread_safe_context(null_mut());
        free_thread_safe_context(retired);

        assert_invalid_lock_is_rejected(retired);
    }

    #[test]
    fn invalid_lock_for_double_freed_handle_is_rejected() {
        let retired = get_thread_safe_context(null_mut());
        free_thread_safe_context(retired);
        free_thread_safe_context(retired);

        assert_invalid_lock_is_rejected(retired);
    }

    #[test]
    fn test_thread_safe_context_configures_locked_context() {
        let mut context = ThreadSafeContext::test();
        context
            .expect_get_client_id(42)
            .expect_call("INCR", &["counter"], ValkeyValue::Integer(1))
            .expect_config_get("hz", "10");

        let guard = context.lock();

        assert_eq!(guard.get_client_id(), 42);
        assert!(matches!(
            guard.call("INCR", &["counter"]),
            Ok(ValkeyValue::Integer(1))
        ));
        assert_eq!(
            guard
                .config_get("hz".to_owned())
                .expect("configured config value should be returned")
                .as_slice(),
            b"10"
        );
    }

    #[test]
    fn independent_thread_safe_contexts_do_not_share_fixture_state() {
        let mut first = ThreadSafeContext::test();
        first
            .expect_get_client_id(11)
            .expect_call("FIRST", &["one"], ValkeyValue::Integer(1));
        let mut second = ThreadSafeContext::test();
        second
            .expect_get_client_id(22)
            .expect_call("SECOND", &["two"], ValkeyValue::Integer(2));

        let first_guard = first.lock();
        assert_eq!(first_guard.get_client_id(), 11);
        assert_eq!(
            first_guard
                .call("FIRST", &["one"])
                .expect("first fixture should return its configured reply"),
            ValkeyValue::Integer(1)
        );

        let second_guard = second.lock();
        assert_eq!(second_guard.get_client_id(), 22);
        assert_eq!(
            second_guard
                .call("SECOND", &["two"])
                .expect("second fixture should return its configured reply"),
            ValkeyValue::Integer(2)
        );
        assert!(matches!(
            first_guard.call("SECOND", &["two"]),
            Err(ValkeyError::String(message)) if message == "unexpected call: SECOND two"
        ));
        assert!(matches!(
            second_guard.call("FIRST", &["one"]),
            Err(ValkeyError::String(message)) if message == "unexpected call: FIRST one"
        ));

        drop(second_guard);
        assert_eq!(first_guard.get_client_id(), 11);
    }

    #[test]
    fn dropping_public_thread_safe_context_retires_registry_entry() {
        let context = ThreadSafeContext::test();
        let ctx = context.context.ctx;
        assert!(state(ctx).is_some());

        drop(context);

        assert!(state(ctx).is_none());
    }

    #[test]
    fn guards_are_cleaned_up_independently_in_reverse_order() {
        let mut first = ThreadSafeContext::test();
        first.expect_get_client_id(11);
        let mut second = ThreadSafeContext::test();
        second.expect_get_client_id(22);
        let first_guard = first.lock();
        let second_guard = second.lock();
        let first_ctx = first_guard.ctx;
        let second_ctx = second_guard.ctx;

        assert!(guard_is_live(first_ctx));
        assert!(guard_is_live(second_ctx));

        drop(second_guard);
        assert!(guard_is_live(first_ctx));
        assert!(!guard_is_live(second_ctx));
        assert_eq!(first_guard.get_client_id(), 11);

        drop(first_guard);
        assert!(!guard_is_live(first_ctx));
    }

    #[test]
    fn locking_thread_safe_context_preserves_ordinary_context_expectations() {
        let mut ordinary_context = Context::test();
        ordinary_context
            .expect_get_server_version(8, 1, 2)
            .expect_get_client_name_by_id(42, "alice")
            .expect_get_client_info_by_id(RedisModuleClientInfo {
                id: 42,
                port: 6379,
                ..RedisModuleClientInfo::default()
            });
        let thread_safe_context = ThreadSafeContext::test();

        drop(thread_safe_context.lock());

        assert_eq!(
            ordinary_context
                .get_server_version()
                .expect("configured server version should be preserved"),
            raw::Version {
                major: 8,
                minor: 1,
                patch: 2,
            }
        );
        assert_eq!(
            ordinary_context
                .get_client_name_by_id(42)
                .expect("configured client name should be preserved")
                .as_slice(),
            b"alice"
        );
        let client_info = ordinary_context
            .get_client_info_by_id(42)
            .expect("configured client info should be preserved");
        assert_eq!((client_info.id, client_info.port), (42, 6379));
    }

    #[test]
    fn with_lock_exposes_configured_context_to_closure() {
        let mut context = ThreadSafeContext::test();
        context.expect_call("INCR", &["counter"], ValkeyValue::Integer(1));

        let reply = context.with_lock(|guard| guard.call("INCR", &["counter"]));

        assert!(matches!(reply, Ok(ValkeyValue::Integer(1))));
    }

    #[test]
    fn configured_call_expectation_is_replayed_for_each_lock() {
        let mut context = ThreadSafeContext::test();
        context.expect_call("INCR", &["counter"], ValkeyValue::Integer(1));

        for _ in 0..2 {
            assert_eq!(
                context
                    .with_lock(|guard| guard.call("INCR", &["counter"]))
                    .expect("each lock should receive the configured call expectation"),
                ValkeyValue::Integer(1)
            );
        }
    }

    #[test]
    fn with_lock_releases_the_guard_when_the_closure_panics() {
        let mut context = ThreadSafeContext::test();
        context.expect_call("INCR", &["counter"], ValkeyValue::Integer(1));

        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            context.with_lock(|_| panic!("test panic"));
        }));

        assert!(result.is_err());
        assert!(LIVE_GUARD_CONTEXTS.with(|contexts| contexts.borrow().is_empty()));
        assert!(matches!(
            context.with_lock(|guard| guard.call("INCR", &["counter"])),
            Ok(ValkeyValue::Integer(1))
        ));
    }

    #[test]
    fn thread_safe_context_replays_bulk_valkey_string_call_reply() {
        let mut context = ThreadSafeContext::test();
        context.expect_call(
            "ECHO",
            &[] as &[&str],
            ValkeyValue::BulkValkeyString(crate::ValkeyString::test("value")),
        );

        let reply = context.with_lock(|guard| guard.call("ECHO", &[] as &[&str]));

        assert_eq!(
            reply.expect("configured call reply should be returned"),
            ValkeyValue::SimpleString("value".to_owned())
        );
    }

    #[test]
    fn thread_safe_context_can_be_reused_sequentially_across_threads() {
        let mut context = ThreadSafeContext::test();
        context
            .expect_get_client_id(42)
            .expect_call("INCR", &["counter"], ValkeyValue::Integer(1));
        let context = Arc::new(context);

        for _ in 0..2 {
            let context = Arc::clone(&context);
            std::thread::spawn(move || assert_configured_fixture(&context))
                .join()
                .expect("test thread should complete");
        }

        assert_configured_fixture(&context);
    }

    #[test]
    fn concurrent_locks_create_and_clean_up_independent_guards() {
        let mut context = ThreadSafeContext::test();
        context
            .expect_get_client_id(42)
            .expect_call("INCR", &["counter"], ValkeyValue::Integer(1));
        let context = Arc::new(context);
        let barrier = Arc::new(Barrier::new(2));

        let workers = (0..2)
            .map(|_| {
                let context = Arc::clone(&context);
                let barrier = Arc::clone(&barrier);
                std::thread::spawn(move || {
                    let guard = context.lock();
                    let guard_ctx = guard.ctx;
                    assert!(guard_is_live(guard_ctx));

                    barrier.wait();
                    assert_eq!(guard.get_client_id(), 42);
                    assert_eq!(
                        guard
                            .call("INCR", &["counter"])
                            .expect("each concurrent guard should receive the fixture template"),
                        ValkeyValue::Integer(1)
                    );

                    drop(guard);
                    assert!(!guard_is_live(guard_ctx));
                    guard_ctx as usize
                })
            })
            .collect::<Vec<_>>();

        let guard_contexts = workers
            .into_iter()
            .map(|worker| worker.join().expect("test thread should complete"))
            .collect::<Vec<_>>();

        assert_ne!(guard_contexts[0], guard_contexts[1]);
    }

    #[test]
    fn thread_safe_context_matches_test_context_call_errors() {
        let ordinary = Context::test();
        let thread_safe = ThreadSafeContext::test();
        let ordinary_unconfigured = ordinary
            .call("MISSING", &["argument"])
            .expect_err("ordinary context should reject an unconfigured call")
            .to_string();
        let thread_safe_unconfigured = thread_safe
            .with_lock(|guard| guard.call("MISSING", &["argument"]))
            .expect_err("thread-safe context should reject an unconfigured call")
            .to_string();
        assert_eq!(thread_safe_unconfigured, ordinary_unconfigured);

        let expected = b"\0\xff".as_slice();
        let actual = b"\0\xfe".as_slice();
        let mut ordinary = Context::test();
        ordinary.expect_call("ECHO", &[expected], ValkeyValue::Integer(1));
        let mut thread_safe = ThreadSafeContext::test();
        thread_safe.expect_call("ECHO", &[expected], ValkeyValue::Integer(1));
        let ordinary_mismatch = ordinary
            .call("ECHO", &[actual])
            .expect_err("ordinary context should reject a mismatched binary argument")
            .to_string();
        let thread_safe_mismatch = thread_safe
            .with_lock(|guard| guard.call("ECHO", &[actual]))
            .expect_err("thread-safe context should reject a mismatched binary argument")
            .to_string();

        assert_eq!(thread_safe_mismatch, ordinary_mismatch);
    }

    fn assert_configured_fixture(context: &TestThreadSafeContext) {
        let guard = context.lock();
        assert_eq!(guard.get_client_id(), 42);
        assert_eq!(
            guard
                .call("INCR", &["counter"])
                .expect("configured call should be replayed on every thread"),
            ValkeyValue::Integer(1)
        );
    }

    fn guard_is_live(ctx: *mut raw::RedisModuleCtx) -> bool {
        LIVE_GUARD_CONTEXTS.with(|contexts| contexts.borrow().contains_key(&(ctx as usize)))
    }

    fn assert_invalid_lock_is_rejected(ctx: *mut raw::RedisModuleCtx) {
        thread_safe_context_lock(ctx);
        let guard = get_thread_safe_context(null_mut());

        thread_safe_context_unlock(guard);
        if !guard.is_null() {
            free_thread_safe_context(guard);
        }
        assert!(guard.is_null());
    }
}