-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathscripting_commands.rs
386 lines (303 loc) · 11.9 KB
/
scripting_commands.rs
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
use crate::{
client::ClientPreparedCommand,
commands::{
CallBuilder, FlushingMode, FunctionListOptions, LibraryInfo, ScriptingCommands,
ServerCommands, StringCommands,
},
error::{Error, RedisErrorKind},
sleep, spawn,
tests::get_test_client,
Result,
};
use serial_test::serial;
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn eval() -> Result<()> {
let client = get_test_client().await?;
let result: String = client
.eval(CallBuilder::script("return ARGV[1]").args("hello"))
.await?;
assert_eq!("hello", result);
client.set("key", "hello").await?;
let result: String = client
.eval(CallBuilder::script("return redis.call('GET', KEYS[1])").keys("key"))
.await?;
assert_eq!("hello", result);
client.set("key", "hello").await?;
let result: String = client
.eval(
CallBuilder::script("return redis.call('GET', KEYS[1])..\" \"..ARGV[1]..\"!\"")
.keys("key")
.args("world"),
)
.await?;
assert_eq!("hello world!", result);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::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(CallBuilder::script(lua_script).args("Hello").args("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(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn evalsha_noscript() -> Result<()> {
let client = get_test_client().await?;
// SHA1("") == da39a3ee5e6b4b0d3255bfef95601890afd80709
let result = client
.evalsha::<()>(CallBuilder::sha1(
"da39a3ee5e6b4b0d3255bfef95601890afd80709",
))
.await
.unwrap_err();
let Error::Redis(error) = result else {
unreachable!();
};
assert_eq!(error.kind, RedisErrorKind::NoScript);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::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(CallBuilder::sha1(sha1).args("hello"))
.await?;
assert_eq!("hello", result);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::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(CallBuilder::function("myfunc").args("hello"))
.await?;
assert_eq!("hello", result);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::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(CallBuilder::function("myfunc").args("Hello").args("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(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::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(CallBuilder::function("myfunc").args("hello"))
.await?;
assert_eq!("hello", result);
client.function_delete("mylib").await?;
let result: Result<String> = client
.fcall(CallBuilder::function("myfunc").args("hello"))
.await;
assert!(result.is_err());
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::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(CallBuilder::function("myfunc").args("hello"))
.await?;
assert_eq!("hello", result);
let serialized_payload = client.function_dump().await?;
assert!(!serialized_payload.0.is_empty());
client.function_delete("mylib").await?;
client
.function_restore(serialized_payload.0, Default::default())
.await?;
let result: String = client
.fcall(CallBuilder::function("myfunc").args("hello"))
.await?;
assert_eq!("hello", result);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::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(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::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(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::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>(CallBuilder::function("myfunc").args("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(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::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(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::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(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::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>(CallBuilder::sha1(sha1).args("hello"))
.await?;
Ok(())
}
let _ = blocking_script(sha1).await;
});
sleep(std::time::Duration::from_millis(100)).await;
client.script_kill().await?;
Ok(())
}