kevy 3.0.0

kevy — a pure-Rust, zero-dependency, Redis-compatible KV server.
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
//! `BLPOP` / `BRPOP` / `XREAD BLOCK` / `XREADGROUP BLOCK` —
//! end-to-end tests against a real reactor + socket. Verifies the
//! v2-7d BLOCK reactor's three core paths per command:
//!
//! 1. **Hit immediately** — non-empty list / fresh stream entry already
//!    available → the command returns at once without parking the conn.
//! 2. **Timeout** — empty, `BLOCK ms` elapses → the reactor's tick fires
//!    a nil reply (shape per command: `*-1` for BLPOP/BRPOP, `$-1` for
//!    XREAD/XREADGROUP) and unblocks the conn.
//! 3. **Wake** — empty + a sibling conn pushes / XADDs the watched key
//!    → the oldest waiter is popped, its command replays, and the
//!    reply lands on the parked conn.

use std::io::{Read, Write};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

static START_GATE: Mutex<()> = Mutex::new(());

fn free_port() -> u16 {
    let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    l.local_addr().unwrap().port()
}

fn req(parts: &[&[u8]]) -> Vec<u8> {
    let mut v = format!("*{}\r\n", parts.len()).into_bytes();
    for p in parts {
        v.extend_from_slice(format!("${}\r\n", p.len()).as_bytes());
        v.extend_from_slice(p);
        v.extend_from_slice(b"\r\n");
    }
    v
}

fn read_n(s: &mut std::net::TcpStream, n: usize) -> Vec<u8> {
    let mut buf = vec![0u8; n];
    s.read_exact(&mut buf).unwrap();
    buf
}

fn read_line(s: &mut std::net::TcpStream, out: &mut Vec<u8>) {
    loop {
        let b = read_n(s, 1);
        out.extend_from_slice(&b);
        if out.ends_with(b"\r\n") {
            break;
        }
    }
}

fn read_len(s: &mut std::net::TcpStream, out: &mut Vec<u8>) -> i64 {
    let start = out.len();
    read_line(s, out);
    let line = &out[start..out.len() - 2];
    std::str::from_utf8(line).unwrap().parse().unwrap()
}

fn read_reply(s: &mut std::net::TcpStream) -> Vec<u8> {
    let head = read_n(s, 1);
    let mut out = head.clone();
    match head[0] {
        b'+' | b'-' | b':' => read_line(s, &mut out),
        b'$' => {
            let len = read_len(s, &mut out);
            if len < 0 {
                return out;
            }
            out.extend_from_slice(&read_n(s, len as usize + 2));
        }
        b'*' => {
            let n = read_len(s, &mut out);
            if n < 0 {
                return out;
            }
            for _ in 0..n {
                out.extend_from_slice(&read_reply(s));
            }
        }
        other => panic!("unknown reply prefix {other:?}"),
    }
    out
}

struct Server {
    port: u16,
    dir: std::path::PathBuf,
    stop: Arc<AtomicBool>,
    handle: Option<std::thread::JoinHandle<()>>,
}

impl Server {
    fn start(nshards: usize) -> Self {
        let _gate = START_GATE.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
        let port = free_port();
        let dir = std::env::temp_dir().join(format!(
            "kevy-blocking-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        let stop = Arc::new(AtomicBool::new(false));
        let stop_thread = stop.clone();
        let dir_thread = dir.clone();
        let handle = std::thread::spawn(move || {
            let rt = kevy_rt::Runtime::new([127, 0, 0, 1], port, nshards, kevy::KevyCommands)
                .with_data_dir(dir_thread);
            rt.run(stop_thread).unwrap();
        });
        for _ in 0..200 {
            if std::net::TcpStream::connect(("127.0.0.1", port)).is_ok() {
                break;
            }
            std::thread::sleep(std::time::Duration::from_millis(5));
        }
        Self {
            port,
            dir,
            stop,
            handle: Some(handle),
        }
    }

    fn connect(&self) -> std::net::TcpStream {
        let s = std::net::TcpStream::connect(("127.0.0.1", self.port)).unwrap();
        s.set_read_timeout(Some(std::time::Duration::from_secs(5)))
            .unwrap();
        s
    }
}

impl Drop for Server {
    fn drop(&mut self) {
        self.stop.store(true, Ordering::Relaxed);
        if let Some(h) = self.handle.take() {
            let _ = h.join();
        }
        let _ = std::fs::remove_dir_all(&self.dir);
    }
}

// ───────────── BLPOP ─────────────

#[test]
fn blpop_returns_immediately_when_list_has_value() {
    let srv = Server::start(1);
    let mut c = srv.connect();
    c.write_all(&req(&[b"RPUSH", b"k", b"v"])).unwrap();
    let _ = read_reply(&mut c); // :1
    c.write_all(&req(&[b"BLPOP", b"k", b"5"])).unwrap();
    // Expect: *2\r\n$1\r\nk\r\n$1\r\nv\r\n
    let reply = read_reply(&mut c);
    assert_eq!(reply, b"*2\r\n$1\r\nk\r\n$1\r\nv\r\n");
}

#[test]
fn blpop_times_out_with_nil_array_when_list_empty() {
    let srv = Server::start(1);
    let mut c = srv.connect();
    // 100ms = 0.1s — short enough that the test stays fast.
    c.write_all(&req(&[b"BLPOP", b"empty", b"0.1"])).unwrap();
    let t0 = std::time::Instant::now();
    let reply = read_reply(&mut c);
    let elapsed = t0.elapsed();
    assert_eq!(reply, b"*-1\r\n", "BLPOP timeout must return nil array");
    assert!(
        elapsed >= std::time::Duration::from_millis(80),
        "BLPOP should block at least the requested timeout (~100ms), got {elapsed:?}",
    );
    assert!(
        elapsed < std::time::Duration::from_secs(2),
        "BLPOP timeout fired far too late ({elapsed:?})",
    );
}

#[test]
fn blpop_woken_by_concurrent_push() {
    let srv = Server::start(1);
    let mut consumer = srv.connect();
    let mut producer = srv.connect();
    // Park the consumer with a generous timeout — wake must come first.
    consumer
        .write_all(&req(&[b"BLPOP", b"wakeable", b"5"]))
        .unwrap();
    std::thread::sleep(std::time::Duration::from_millis(50));
    producer
        .write_all(&req(&[b"LPUSH", b"wakeable", b"hello"]))
        .unwrap();
    let _push_reply = read_reply(&mut producer); // :1
    let reply = read_reply(&mut consumer);
    assert_eq!(reply, b"*2\r\n$8\r\nwakeable\r\n$5\r\nhello\r\n");
}

// ───────────── BRPOP ─────────────

#[test]
fn brpop_returns_immediately_when_list_has_value() {
    let srv = Server::start(1);
    let mut c = srv.connect();
    c.write_all(&req(&[b"RPUSH", b"k", b"v"])).unwrap();
    let _ = read_reply(&mut c);
    c.write_all(&req(&[b"BRPOP", b"k", b"5"])).unwrap();
    let reply = read_reply(&mut c);
    assert_eq!(reply, b"*2\r\n$1\r\nk\r\n$1\r\nv\r\n");
}

#[test]
fn brpop_woken_by_concurrent_rpush() {
    let srv = Server::start(1);
    let mut consumer = srv.connect();
    let mut producer = srv.connect();
    consumer.write_all(&req(&[b"BRPOP", b"q", b"5"])).unwrap();
    std::thread::sleep(std::time::Duration::from_millis(50));
    producer.write_all(&req(&[b"RPUSH", b"q", b"x"])).unwrap();
    let _ = read_reply(&mut producer);
    let reply = read_reply(&mut consumer);
    assert_eq!(reply, b"*2\r\n$1\r\nq\r\n$1\r\nx\r\n");
}

// ───────────── XREAD BLOCK ─────────────

#[test]
fn xread_block_returns_immediately_when_stream_has_entry() {
    let srv = Server::start(1);
    let mut c = srv.connect();
    c.write_all(&req(&[b"XADD", b"s", b"1-0", b"f", b"v"])).unwrap();
    let _ = read_reply(&mut c); // $3 1-0
    c.write_all(&req(&[
        b"XREAD", b"BLOCK", b"5000", b"STREAMS", b"s", b"0",
    ])).unwrap();
    // Expect *1 [*2 s [*1 [*2 1-0 [*2 f v]]]]
    let reply = read_reply(&mut c);
    // Quick sanity: starts with *1 and contains the entry payload.
    assert!(reply.starts_with(b"*1\r\n"), "expected one stream in reply, got {reply:?}");
    assert!(reply.windows(3).any(|w| w == b"1-0"), "expected entry id 1-0 in reply");
    assert!(reply.windows(1).any(|w| w == b"v"), "expected value v in reply");
}

#[test]
fn xread_block_times_out_with_nil_bulk_when_no_entries() {
    let srv = Server::start(1);
    let mut c = srv.connect();
    // Create the stream so `$` resolves; otherwise xread_dollar_last_id
    // errors out before BLOCK can engage.
    c.write_all(&req(&[b"XADD", b"s", b"1-0", b"f", b"v"])).unwrap();
    let _ = read_reply(&mut c);
    c.write_all(&req(&[
        b"XREAD", b"BLOCK", b"100", b"STREAMS", b"s", b"$",
    ])).unwrap();
    let t0 = std::time::Instant::now();
    let reply = read_reply(&mut c);
    let elapsed = t0.elapsed();
    assert_eq!(reply, b"$-1\r\n", "XREAD BLOCK timeout must return nil bulk");
    assert!(elapsed >= std::time::Duration::from_millis(80));
}

#[test]
fn xread_block_woken_by_concurrent_xadd() {
    // Wake path with an explicit cursor (no `$`). Companion to
    // `xread_block_dollar_id_wakes` below, which exercises the same
    // path with the `$` cursor that needs park-time rewriting.
    let srv = Server::start(1);
    let mut consumer = srv.connect();
    let mut producer = srv.connect();
    producer
        .write_all(&req(&[b"XADD", b"stream", b"1-0", b"f", b"v"]))
        .unwrap();
    let _ = read_reply(&mut producer);
    consumer
        .write_all(&req(&[
            b"XREAD", b"BLOCK", b"5000", b"STREAMS", b"stream", b"1-0",
        ]))
        .unwrap();
    std::thread::sleep(std::time::Duration::from_millis(50));
    producer
        .write_all(&req(&[b"XADD", b"stream", b"2-0", b"f", b"v2"]))
        .unwrap();
    let _ = read_reply(&mut producer);
    let reply = read_reply(&mut consumer);
    assert!(reply.starts_with(b"*1\r\n"));
    assert!(reply.windows(3).any(|w| w == b"2-0"));
    assert!(reply.windows(2).any(|w| w == b"v2"));
}

#[test]
fn xread_block_dollar_id_wakes() {
    // `$` cursor: park-time rewrite (Commands::resolve_block_argv on
    // BlockKind::XReadBlock) must snapshot the stream's last_id when
    // the conn is registered, so the wake retry sees the original
    // cursor — not the post-XADD last_id, which would mean "0 entries
    // > last_id" and a timeout. Regression test for v2-7e.
    let srv = Server::start(1);
    let mut consumer = srv.connect();
    let mut producer = srv.connect();
    // Pre-populate so `$` resolves to a real ID (xread_dollar_last_id
    // errors on a missing key, which would prevent registration).
    producer
        .write_all(&req(&[b"XADD", b"stream", b"1-0", b"f", b"v"]))
        .unwrap();
    let _ = read_reply(&mut producer);
    consumer
        .write_all(&req(&[
            b"XREAD", b"BLOCK", b"5000", b"STREAMS", b"stream", b"$",
        ]))
        .unwrap();
    std::thread::sleep(std::time::Duration::from_millis(50));
    producer
        .write_all(&req(&[b"XADD", b"stream", b"2-0", b"f", b"v2"]))
        .unwrap();
    let _ = read_reply(&mut producer);
    let reply = read_reply(&mut consumer);
    assert!(
        reply.starts_with(b"*1\r\n"),
        "expected one stream in reply, got {:?}",
        std::str::from_utf8(&reply).unwrap_or("<non-utf8>")
    );
    assert!(reply.windows(3).any(|w| w == b"2-0"));
    assert!(reply.windows(2).any(|w| w == b"v2"));
}

// ───────────── XREADGROUP BLOCK ─────────────

#[test]
fn xreadgroup_block_times_out_when_no_new_entries() {
    let srv = Server::start(1);
    let mut c = srv.connect();
    // Set up: create stream + group.
    c.write_all(&req(&[b"XADD", b"s", b"1-0", b"f", b"v"])).unwrap();
    let _ = read_reply(&mut c);
    c.write_all(&req(&[b"XGROUP", b"CREATE", b"s", b"g", b"$"])).unwrap();
    let _ = read_reply(&mut c); // +OK
    c.write_all(&req(&[
        b"XREADGROUP",
        b"GROUP",
        b"g",
        b"alice",
        b"BLOCK",
        b"100",
        b"STREAMS",
        b"s",
        b">",
    ])).unwrap();
    let t0 = std::time::Instant::now();
    let reply = read_reply(&mut c);
    let elapsed = t0.elapsed();
    assert_eq!(reply, b"$-1\r\n", "XREADGROUP BLOCK timeout returns nil bulk");
    assert!(elapsed >= std::time::Duration::from_millis(80));
}

#[test]
fn xreadgroup_block_woken_by_concurrent_xadd() {
    let srv = Server::start(1);
    let mut consumer = srv.connect();
    let mut producer = srv.connect();
    producer.write_all(&req(&[b"XADD", b"stream2", b"1-0", b"f", b"v"])).unwrap();
    let _ = read_reply(&mut producer);
    producer.write_all(&req(&[b"XGROUP", b"CREATE", b"stream2", b"g", b"$"])).unwrap();
    let _ = read_reply(&mut producer);
    consumer.write_all(&req(&[
        b"XREADGROUP",
        b"GROUP",
        b"g",
        b"bob",
        b"BLOCK",
        b"5000",
        b"STREAMS",
        b"stream2",
        b">",
    ])).unwrap();
    std::thread::sleep(std::time::Duration::from_millis(50));
    producer.write_all(&req(&[b"XADD", b"stream2", b"2-0", b"f", b"v2"])).unwrap();
    let _ = read_reply(&mut producer);
    let reply = read_reply(&mut consumer);
    assert!(reply.starts_with(b"*1\r\n"));
    assert!(reply.windows(3).any(|w| w == b"2-0"));
    assert!(reply.windows(2).any(|w| w == b"v2"));
}

// ───────────── Multi-key (v2-7e) ─────────────
//
// Multi-key BLPOP runs through the cross-shard arbiter even on one shard
// (every key is a self-target), so these cover the arbiter's park / wake /
// timeout paths without needing a multi-shard harness — the dedicated
// cross-shard layout lives in `blocking_cross_shard.rs`.

#[test]
fn blpop_multi_key_times_out_when_all_empty() {
    let srv = Server::start(1);
    let mut c = srv.connect();
    c.write_all(&req(&[b"BLPOP", b"a", b"b", b"0.1"])).unwrap();
    let t0 = std::time::Instant::now();
    let reply = read_reply(&mut c);
    assert_eq!(reply, b"*-1\r\n", "multi-key BLPOP timeout returns nil array");
    assert!(t0.elapsed() >= std::time::Duration::from_millis(80));
}

#[test]
fn blpop_multi_key_woken_on_second_key() {
    let srv = Server::start(1);
    let mut consumer = srv.connect();
    let mut producer = srv.connect();
    consumer
        .write_all(&req(&[b"BLPOP", b"k1", b"k2", b"5"]))
        .unwrap();
    std::thread::sleep(std::time::Duration::from_millis(50));
    // Push to the *second* watched key — the arbiter must serve k2.
    producer.write_all(&req(&[b"LPUSH", b"k2", b"v2"])).unwrap();
    let _ = read_reply(&mut producer); // :1
    let reply = read_reply(&mut consumer);
    assert_eq!(reply, b"*2\r\n$2\r\nk2\r\n$2\r\nv2\r\n");
}

#[test]
fn blpop_multi_key_immediate_hit_first_available() {
    let srv = Server::start(1);
    let mut c = srv.connect();
    c.write_all(&req(&[b"RPUSH", b"ready", b"x"])).unwrap();
    let _ = read_reply(&mut c); // :1
    // `missing` is empty, `ready` has data → resolves at once via the arm
    // readiness peek (no parking).
    c.write_all(&req(&[b"BLPOP", b"missing", b"ready", b"5"]))
        .unwrap();
    let reply = read_reply(&mut c);
    assert_eq!(reply, b"*2\r\n$5\r\nready\r\n$1\r\nx\r\n");
}