rustis 0.22.0

Redis async driver for 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
use crate::{
    Result,
    client::ClientPreparedCommand,
    commands::{
        FlushingMode, FunctionListOptions, LibraryInfo, ScriptDebugMode, ScriptingCommands,
        ServerCommands, StringCommands,
    },
    error::{Error, RedisErrorKind},
    sleep, spawn,
    tests::get_test_client,
};
use serial_test::serial;

#[tokio::test]
#[serial]
async fn eval() -> Result<()> {
    let client = get_test_client().await?;

    let result: String = client.eval("return ARGV[1]", (), "hello").await?;
    assert_eq!("hello", result);

    client.set("key", "hello").await?;
    let result: String = client
        .eval("return redis.call('GET', KEYS[1])", "key", ())
        .await?;
    assert_eq!("hello", result);

    client.set("key", "hello").await?;
    let result: String = client
        .eval(
            "return redis.call('GET', KEYS[1])..\" \"..ARGV[1]..\"!\"",
            "key",
            "world",
        )
        .await?;
    assert_eq!("hello world!", result);

    Ok(())
}

#[tokio::test]
#[serial]
async fn eval_tuple_response() -> Result<()> {
    let client = get_test_client().await?;

    let lua_script = r#"
redis.call("DEL", "key");
redis.call("SADD", "key", 1, 2, 3, 4);
local arr = redis.call("SMEMBERS", "key");
redis.call("DEL", "key");
return { ARGV[1], ARGV[2], 42, arr }
    "#;
    let result: (String, String, i32, Vec<i64>) =
        client.eval(lua_script, (), ["Hello", "world"]).await?;

    assert_eq!(result.0, "Hello");
    assert_eq!(result.1, "world");
    assert_eq!(result.2, 42);
    assert_eq!(result.3, vec![1, 2, 3, 4]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn evalsha_noscript() -> Result<()> {
    let client = get_test_client().await?;

    // SHA1("") == da39a3ee5e6b4b0d3255bfef95601890afd80709
    let result = client
        .evalsha::<()>("da39a3ee5e6b4b0d3255bfef95601890afd80709", (), ())
        .await
        .unwrap_err();

    let Error::Redis(error) = result else {
        unreachable!();
    };

    assert_eq!(error.kind, RedisErrorKind::NoScript);

    Ok(())
}

#[tokio::test]
#[serial]
async fn evalsha() -> Result<()> {
    let client = get_test_client().await?;

    let sha1: String = client.script_load("return ARGV[1]").await?;

    let result: String = client.evalsha(sha1, (), "hello").await?;
    assert_eq!("hello", result);

    Ok(())
}

#[tokio::test]
#[serial]
async fn fcall() -> Result<()> {
    let client = get_test_client().await?;

    let library: String = client.function_load(true, "#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)").await?;
    assert_eq!("mylib", library);

    let result: String = client.fcall("myfunc", (), "hello").await?;
    assert_eq!("hello", result);

    Ok(())
}

#[tokio::test]
#[serial]
async fn eval_readonly() -> Result<()> {
    let client = get_test_client().await?;

    client.set("key", "hello").await?;

    let result: String = client
        .eval_readonly("return redis.call('GET', KEYS[1])", "key", ())
        .await?;
    assert_eq!("hello", result);

    let result: String = client
        .eval_readonly(
            "return redis.call('GET', KEYS[1])..\" \"..ARGV[1]..\"!\"",
            "key",
            "world",
        )
        .await?;
    assert_eq!("hello world!", result);

    // A write from a read-only script is rejected by the server.
    let result: Result<()> = client
        .eval_readonly("return redis.call('SET', KEYS[1], 'other')", "key", ())
        .await;
    assert!(result.is_err());

    Ok(())
}

#[tokio::test]
#[serial]
async fn evalsha_readonly() -> Result<()> {
    let client = get_test_client().await?;

    client.set("key", "hello").await?;

    let sha1: String = client
        .script_load("return redis.call('GET', KEYS[1])")
        .await?;

    let result: String = client.evalsha_readonly(sha1.clone(), "key", ()).await?;
    assert_eq!("hello", result);

    let sha1: String = client
        .script_load("return redis.call('SET', KEYS[1], 'other')")
        .await?;

    let result: Result<()> = client.evalsha_readonly(sha1, "key", ()).await;
    assert!(result.is_err());

    Ok(())
}

#[tokio::test]
#[serial]
async fn fcall_readonly() -> Result<()> {
    let client = get_test_client().await?;

    client.function_flush(FlushingMode::Sync).await?;

    // Only a function registered with the `no-writes` flag is callable through
    // FCALL_RO.
    let library: String = client
        .function_load(
            true,
            "#!lua name=mylib \n redis.register_function{function_name='myfunc', callback=function(keys, args) return args[1] end, flags={'no-writes'}}",
        )
        .await?;
    assert_eq!("mylib", library);

    let result: String = client.fcall_readonly("myfunc", (), "hello").await?;
    assert_eq!("hello", result);

    let library: String = client
        .function_load(
            true,
            "#!lua name=writelib \n redis.register_function('writefunc', function(keys, args) return redis.call('SET', keys[1], args[1]) end)",
        )
        .await?;
    assert_eq!("writelib", library);

    let result: Result<()> = client.fcall_readonly("writefunc", "key", "value").await;
    assert!(result.is_err());

    client.function_flush(FlushingMode::Sync).await?;

    Ok(())
}

#[tokio::test]
#[serial]
async fn fcall_tuple_response() -> Result<()> {
    let client = get_test_client().await?;

    let lua_lib = r#"#!lua name=mylib
redis.register_function('myfunc', function(keys, args) 
    redis.call("DEL", "key");
    redis.call("SADD", "key", 1, 2, 3, 4);
    local arr = redis.call("SMEMBERS", "key");
    redis.call("DEL", "key");
    return { args[1], args[2], 42, arr }
end)
    "#;
    let library: String = client.function_load(true, lua_lib).await?;
    assert_eq!("mylib", library);
    let result: (String, String, i32, Vec<i64>) =
        client.fcall("myfunc", (), ["Hello", "world"]).await?;

    assert_eq!(result.0, "Hello");
    assert_eq!(result.1, "world");
    assert_eq!(result.2, 42);
    assert_eq!(result.3, vec![1, 2, 3, 4]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn function_delete() -> Result<()> {
    let client = get_test_client().await?;

    let library: String = client.function_load(true, "#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)").await?;
    assert_eq!("mylib", library);

    let result: String = client.fcall("myfunc", (), "hello").await?;
    assert_eq!("hello", result);

    client.function_delete("mylib").await?;

    let result: Result<String> = client.fcall("myfunc", (), "hello").await;
    assert!(result.is_err());

    Ok(())
}

#[tokio::test]
#[serial]
async fn function_dump() -> Result<()> {
    let client = get_test_client().await?;

    client.flushdb(FlushingMode::Sync).await?;

    let library: String = client.function_load(true, "#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)").await?;
    assert_eq!("mylib", library);

    let result: String = client.fcall("myfunc", (), "hello").await?;
    assert_eq!("hello", result);

    let serialized_payload = client.function_dump().await?;
    assert!(!serialized_payload.is_empty());

    client.function_delete("mylib").await?;

    client.function_restore(&serialized_payload, None).await?;

    let result: String = client.fcall("myfunc", (), "hello").await?;
    assert_eq!("hello", result);

    Ok(())
}

#[tokio::test]
#[serial]
async fn function_flush() -> Result<()> {
    let client = get_test_client().await?;

    let library: String = client.function_load(true, "#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)").await?;
    assert_eq!("mylib", library);

    client.function_flush(FlushingMode::Sync).await?;

    let list: Vec<LibraryInfo> = client.function_list(FunctionListOptions::default()).await?;
    assert_eq!(0, list.len());

    Ok(())
}

#[tokio::test]
#[serial]
async fn function_list() -> Result<()> {
    let client = get_test_client().await?;

    client.function_flush(FlushingMode::Sync).await?;

    let code = "#!lua name=mylib \n redis.register_function{function_name='myfunc', callback=function(keys, args) return args[1] end, flags={ 'no-writes' }, description='My description'}";
    let library: String = client.function_load(true, code).await?;
    assert_eq!("mylib", library);

    let libs: Vec<LibraryInfo> = client.function_list(FunctionListOptions::default()).await?;
    assert_eq!(1, libs.len());
    assert_eq!("mylib", libs[0].library_name);
    assert_eq!("LUA", libs[0].engine);
    assert_eq!(1, libs[0].functions.len());
    assert_eq!("myfunc", libs[0].functions[0].name);
    assert_eq!("My description", libs[0].functions[0].description);
    assert_eq!(1, libs[0].functions[0].flags.len());
    assert_eq!("no-writes", libs[0].functions[0].flags[0]);
    assert_eq!(None, libs[0].library_code);

    let libs: Vec<LibraryInfo> = client
        .function_list(FunctionListOptions::default().with_code())
        .await?;
    assert_eq!(1, libs.len());
    assert_eq!("mylib", libs[0].library_name);
    assert_eq!("LUA", libs[0].engine);
    assert_eq!(1, libs[0].functions.len());
    assert_eq!("myfunc", libs[0].functions[0].name);
    assert_eq!("My description", libs[0].functions[0].description);
    assert_eq!(1, libs[0].functions[0].flags.len());
    assert_eq!("no-writes", libs[0].functions[0].flags[0]);
    assert_eq!(Some(code.to_owned()), libs[0].library_code);

    Ok(())
}

#[tokio::test]
#[serial]
async fn function_stats() -> Result<()> {
    let client = get_test_client().await?;

    client.function_kill().forget()?;

    client.function_flush(FlushingMode::Sync).await?;

    let code = "#!lua name=mylib \n redis.register_function{function_name='myfunc', callback=function(keys, args) while (true) do end return args[1] end, flags={ 'no-writes' }, description='My description'}";
    let library: String = client.function_load(true, code).await?;
    assert_eq!("mylib", library);

    spawn(async move {
        async fn blocking_fcall() -> Result<()> {
            let client = get_test_client().await?;

            let _ = client.fcall::<String>("myfunc", (), "hello").await?;

            Ok(())
        }

        let _ = blocking_fcall().await;
    });

    sleep(std::time::Duration::from_millis(100)).await;

    let function_stat = client.function_stats().await?;
    assert!(function_stat.running_script.is_some());
    if let Some(running_script) = function_stat.running_script {
        assert_eq!("myfunc", running_script.name);
        assert_eq!(4, running_script.command.len());
        assert_eq!("FCALL", running_script.command[0]);
        assert_eq!("myfunc", running_script.command[1]);
        assert_eq!("0", running_script.command[2]);
        assert_eq!("hello", running_script.command[3]);
        assert!(running_script.duration_ms > 100);
    }
    assert!(function_stat.engines.contains_key("LUA"));
    assert_eq!(1, function_stat.engines["LUA"].libraries_count);
    assert_eq!(1, function_stat.engines["LUA"].functions_count);

    client.function_kill().await?;

    Ok(())
}

#[tokio::test]
#[serial]
async fn script_exists() -> Result<()> {
    let client = get_test_client().await?;

    let sha11: String = client.script_load("return ARGV[1]").await?;
    let sha12: String = client
        .script_load("return redis.call('GET', KEYS[1])")
        .await?;

    let result = client
        .script_exists([sha11, sha12, "unknwon".to_owned()])
        .await?;
    assert_eq!([true, true, false], &result[..]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn script_flush() -> Result<()> {
    let client = get_test_client().await?;

    let sha11: String = client.script_load("return ARGV[1]").await?;
    let sha12: String = client
        .script_load("return redis.call('GET', KEYS[1])")
        .await?;

    client.script_flush(FlushingMode::Sync).await?;

    let result = client.script_exists([sha11, sha12]).await?;
    assert_eq!([false, false], &result[..]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn script_kill() -> Result<()> {
    let client = get_test_client().await?;

    let _ = client.script_kill().await;

    let sha1: String = client
        .script_load("while (true) do end return ARGV[1]")
        .await?;

    spawn(async move {
        async fn blocking_script(sha1: String) -> Result<()> {
            let client = get_test_client().await?;

            let _ = client.evalsha::<String>(sha1, (), "hello").await?;

            Ok(())
        }

        let _ = blocking_script(sha1).await;
    });

    sleep(std::time::Duration::from_millis(100)).await;

    client.script_kill().await?;

    Ok(())
}

/// `FUNCTION HELP` answers the subcommand list as a flat array of text lines,
/// which is the shape the declared return type claims.
#[tokio::test]
#[serial]
async fn function_help() -> Result<()> {
    let client = get_test_client().await?;

    let help = client.function_help().await?;

    assert!(help.iter().any(|line| line.contains("LOAD")));

    Ok(())
}

/// `SCRIPT DEBUG` takes one of three modes. `No` is the server default, so it
/// is the one mode a test can send without leaving the connection in a state
/// that stalls every script the rest of the suite runs.
#[tokio::test]
#[serial]
async fn script_debug() -> Result<()> {
    let client = get_test_client().await?;

    client.script_debug(ScriptDebugMode::No).await?;

    let result: String = client.eval("return ARGV[1]", (), "hello").await?;
    assert_eq!("hello", result);

    Ok(())
}

/// `FUNCTION LIST [LIBRARYNAME library-name] [WITHCODE]`. LIBRARYNAME is an exact
/// library name, so a name that exists selects one library and any other selects
/// none.
#[tokio::test]
#[serial]
async fn function_list_library_name_pattern() -> Result<()> {
    let client = get_test_client().await?;

    client.function_flush(FlushingMode::Sync).await?;

    let code = "#!lua name=mylib \n redis.register_function{function_name='myfunc', callback=function(keys, args) return args[1] end, flags={ 'no-writes' }}";
    let library: String = client.function_load(true, code).await?;
    assert_eq!("mylib", library);

    let libs: Vec<LibraryInfo> = client
        .function_list(FunctionListOptions::default().library_name_pattern("mylib"))
        .await?;
    assert_eq!(1, libs.len());
    assert_eq!("mylib", libs[0].library_name);

    let libs: Vec<LibraryInfo> = client
        .function_list(FunctionListOptions::default().library_name_pattern("otherlib"))
        .await?;
    assert!(libs.is_empty());

    Ok(())
}