redis-oxide 0.2.3

High-performance async Redis client for Rust with automatic cluster support, multiplexing, and advanced features
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
//! Integration tests for RESP3 protocol support

#![allow(clippy::uninlined_format_args)]
#![allow(clippy::similar_names)]
#![allow(clippy::approx_constant)]
#![allow(clippy::float_cmp)]

use redis_oxide::{Client, ConnectionConfig, ProtocolVersion, Resp3Value};
use std::collections::{HashMap, HashSet};

fn redis_url() -> String {
    std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string())
}

#[allow(dead_code)]
async fn setup_client_resp3() -> Result<Client, redis_oxide::RedisError> {
    let config =
        ConnectionConfig::new(redis_url().as_str()).with_protocol_version(ProtocolVersion::Resp3);
    Client::connect(config).await
}

#[allow(dead_code)]
async fn setup_client_resp2() -> Result<Client, redis_oxide::RedisError> {
    let config =
        ConnectionConfig::new(redis_url().as_str()).with_protocol_version(ProtocolVersion::Resp2);
    Client::connect(config).await
}

#[tokio::test]
async fn test_resp3_basic_data_types() -> Result<(), Box<dyn std::error::Error>> {
    use redis_oxide::protocol::resp3::{Resp3Decoder, Resp3Encoder};

    let mut encoder = Resp3Encoder::new();
    let mut decoder = Resp3Decoder::new();

    // Test Boolean
    let bool_val = Resp3Value::Boolean(true);
    let encoded = encoder.encode(&bool_val)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(bool_val, decoded);

    // Test Double
    let double_val = Resp3Value::Double(3.14159);
    let encoded = encoder.encode(&double_val)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(double_val, decoded);

    // Test Map
    let mut map = HashMap::new();
    map.insert(
        "key1".to_string(),
        Resp3Value::SimpleString("value1".to_string()),
    );
    map.insert("key2".to_string(), Resp3Value::Number(42));
    let map_val = Resp3Value::Map(map);
    let encoded = encoder.encode(&map_val)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(map_val, decoded);

    // Test Set
    let mut set = HashSet::new();
    set.insert(Resp3Value::SimpleString("item1".to_string()));
    set.insert(Resp3Value::SimpleString("item2".to_string()));
    set.insert(Resp3Value::Number(123));
    let set_val = Resp3Value::Set(set);
    let encoded = encoder.encode(&set_val)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(set_val, decoded);

    Ok(())
}

#[tokio::test]
async fn test_resp3_verbatim_string() -> Result<(), Box<dyn std::error::Error>> {
    use redis_oxide::protocol::resp3::{Resp3Decoder, Resp3Encoder};

    let mut encoder = Resp3Encoder::new();
    let mut decoder = Resp3Decoder::new();

    // Test VerbatimString
    let verbatim_val = Resp3Value::VerbatimString {
        encoding: "txt".to_string(),
        data: "Hello, World!".to_string(),
    };
    let encoded = encoder.encode(&verbatim_val)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(verbatim_val, decoded);

    // Test different encoding
    let markdown_val = Resp3Value::VerbatimString {
        encoding: "mkd".to_string(),
        data: "# Markdown Title\n\nSome **bold** text.".to_string(),
    };
    let encoded = encoder.encode(&markdown_val)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(markdown_val, decoded);

    Ok(())
}

#[tokio::test]
async fn test_resp3_big_number() -> Result<(), Box<dyn std::error::Error>> {
    use redis_oxide::protocol::resp3::{Resp3Decoder, Resp3Encoder};

    let mut encoder = Resp3Encoder::new();
    let mut decoder = Resp3Decoder::new();

    // Test BigNumber
    let big_num = Resp3Value::BigNumber("123456789012345678901234567890".to_string());
    let encoded = encoder.encode(&big_num)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(big_num, decoded);

    Ok(())
}

#[tokio::test]
async fn test_resp3_attribute() -> Result<(), Box<dyn std::error::Error>> {
    use redis_oxide::protocol::resp3::{Resp3Decoder, Resp3Encoder};

    let mut encoder = Resp3Encoder::new();
    let mut decoder = Resp3Decoder::new();

    // Test Attribute
    let mut attrs = HashMap::new();
    attrs.insert("ttl".to_string(), Resp3Value::Number(3600));
    attrs.insert(
        "type".to_string(),
        Resp3Value::SimpleString("string".to_string()),
    );

    let attr_val = Resp3Value::Attribute {
        attrs,
        data: Box::new(Resp3Value::BlobString("actual_data".to_string())),
    };

    let encoded = encoder.encode(&attr_val)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(attr_val, decoded);

    Ok(())
}

#[tokio::test]
async fn test_resp3_push_type() -> Result<(), Box<dyn std::error::Error>> {
    use redis_oxide::protocol::resp3::{Resp3Decoder, Resp3Encoder};

    let mut encoder = Resp3Encoder::new();
    let mut decoder = Resp3Decoder::new();

    // Test Push (server-initiated message)
    let push_val = Resp3Value::Push(vec![
        Resp3Value::SimpleString("pubsub".to_string()),
        Resp3Value::SimpleString("message".to_string()),
        Resp3Value::SimpleString("channel1".to_string()),
        Resp3Value::BlobString("Hello from channel!".to_string()),
    ]);

    let encoded = encoder.encode(&push_val)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(push_val, decoded);

    Ok(())
}

#[tokio::test]
async fn test_resp3_null_handling() -> Result<(), Box<dyn std::error::Error>> {
    use redis_oxide::protocol::resp3::{Resp3Decoder, Resp3Encoder};

    let mut encoder = Resp3Encoder::new();
    let mut decoder = Resp3Decoder::new();

    // Test explicit Null
    let null_val = Resp3Value::Null;
    let encoded = encoder.encode(&null_val)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(null_val, decoded);
    assert!(decoded.is_null());

    Ok(())
}

#[tokio::test]
async fn test_resp3_value_conversions() -> Result<(), Box<dyn std::error::Error>> {
    // Test string conversion
    let str_val = Resp3Value::BlobString("hello".to_string());
    assert_eq!(str_val.as_string()?, "hello");

    let simple_str = Resp3Value::SimpleString("world".to_string());
    assert_eq!(simple_str.as_string()?, "world");

    let verbatim_str = Resp3Value::VerbatimString {
        encoding: "txt".to_string(),
        data: "test".to_string(),
    };
    assert_eq!(verbatim_str.as_string()?, "test");

    // Test integer conversion
    let num_val = Resp3Value::Number(42);
    assert_eq!(num_val.as_int()?, 42);

    let double_val = Resp3Value::Double(3.14);
    assert_eq!(double_val.as_int()?, 3); // Truncated

    // Test float conversion
    assert_eq!(double_val.as_float()?, 3.14);
    assert_eq!(num_val.as_float()?, 42.0);

    // Test boolean conversion
    let bool_true = Resp3Value::Boolean(true);
    let bool_false = Resp3Value::Boolean(false);
    assert!(bool_true.as_bool()?);
    assert!(!bool_false.as_bool()?);

    let num_one = Resp3Value::Number(1);
    let num_zero = Resp3Value::Number(0);
    assert!(num_one.as_bool()?);
    assert!(!num_zero.as_bool()?);

    Ok(())
}

#[tokio::test]
async fn test_resp3_resp2_compatibility() -> Result<(), Box<dyn std::error::Error>> {
    use redis_oxide::core::value::RespValue;

    // Test RESP3 to RESP2 conversion
    let resp3_bool = Resp3Value::Boolean(true);
    let resp2_val: RespValue = resp3_bool.into();
    match resp2_val {
        RespValue::Integer(1) => {} // Boolean true becomes integer 1
        _ => panic!("Expected integer 1"),
    }

    let resp3_map = {
        let mut map = HashMap::new();
        map.insert(
            "key".to_string(),
            Resp3Value::SimpleString("value".to_string()),
        );
        Resp3Value::Map(map)
    };
    let resp2_val: RespValue = resp3_map.into();
    match resp2_val {
        RespValue::Array(arr) => {
            assert_eq!(arr.len(), 2); // key-value pair becomes array
        }
        _ => panic!("Expected array"),
    }

    // Test RESP2 to RESP3 conversion
    let resp2_str = RespValue::SimpleString("test".to_string());
    let resp3_val: Resp3Value = resp2_str.into();
    match resp3_val {
        Resp3Value::SimpleString(s) => assert_eq!(s, "test"),
        _ => panic!("Expected simple string"),
    }

    Ok(())
}

#[tokio::test]
async fn test_resp3_type_names() -> Result<(), Box<dyn std::error::Error>> {
    assert_eq!(
        Resp3Value::SimpleString("test".to_string()).type_name(),
        "simple-string"
    );
    assert_eq!(Resp3Value::Number(42).type_name(), "number");
    assert_eq!(Resp3Value::Boolean(true).type_name(), "boolean");
    assert_eq!(Resp3Value::Double(3.14).type_name(), "double");
    assert_eq!(Resp3Value::Null.type_name(), "null");

    let map = HashMap::new();
    assert_eq!(Resp3Value::Map(map).type_name(), "map");

    let set = HashSet::new();
    assert_eq!(Resp3Value::Set(set).type_name(), "set");

    assert_eq!(
        Resp3Value::BigNumber("123".to_string()).type_name(),
        "big-number"
    );

    let verbatim = Resp3Value::VerbatimString {
        encoding: "txt".to_string(),
        data: "test".to_string(),
    };
    assert_eq!(verbatim.type_name(), "verbatim-string");

    Ok(())
}

#[tokio::test]
async fn test_resp3_complex_nested_structures() -> Result<(), Box<dyn std::error::Error>> {
    use redis_oxide::protocol::resp3::{Resp3Decoder, Resp3Encoder};

    let mut encoder = Resp3Encoder::new();
    let mut decoder = Resp3Decoder::new();

    // Create a complex nested structure
    let mut inner_map = HashMap::new();
    inner_map.insert("nested_key".to_string(), Resp3Value::Boolean(true));
    inner_map.insert(
        "nested_number".to_string(),
        Resp3Value::Double(std::f64::consts::E),
    );

    let mut inner_set = HashSet::new();
    inner_set.insert(Resp3Value::SimpleString("set_item1".to_string()));
    inner_set.insert(Resp3Value::Number(999));

    let mut outer_map = HashMap::new();
    outer_map.insert("inner_map".to_string(), Resp3Value::Map(inner_map));
    outer_map.insert("inner_set".to_string(), Resp3Value::Set(inner_set));
    outer_map.insert(
        "simple_value".to_string(),
        Resp3Value::BlobString("simple".to_string()),
    );

    let complex_val = Resp3Value::Array(vec![
        Resp3Value::Map(outer_map),
        Resp3Value::VerbatimString {
            encoding: "json".to_string(),
            data: r#"{"json": "data"}"#.to_string(),
        },
        Resp3Value::Push(vec![
            Resp3Value::SimpleString("push_type".to_string()),
            Resp3Value::Number(12345),
        ]),
    ]);

    // Test encoding and decoding
    let encoded = encoder.encode(&complex_val)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(complex_val, decoded);

    Ok(())
}

#[tokio::test]
async fn test_resp3_error_types() -> Result<(), Box<dyn std::error::Error>> {
    use redis_oxide::protocol::resp3::{Resp3Decoder, Resp3Encoder};

    let mut encoder = Resp3Encoder::new();
    let mut decoder = Resp3Decoder::new();

    // Test SimpleError
    let simple_error = Resp3Value::SimpleError("ERR something went wrong".to_string());
    let encoded = encoder.encode(&simple_error)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(simple_error, decoded);

    // Test BlobError
    let blob_error = Resp3Value::BlobError("SYNTAX invalid command syntax".to_string());
    let encoded = encoder.encode(&blob_error)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(blob_error, decoded);

    Ok(())
}

// Note: The following tests would require a Redis 6.0+ server with RESP3 support
// For now, they test the protocol implementation without actual Redis integration

#[tokio::test]
async fn test_protocol_version_configuration() -> Result<(), Box<dyn std::error::Error>> {
    // Test RESP2 configuration (default)
    let config_resp2 = ConnectionConfig::new("redis://localhost:6379")
        .with_protocol_version(ProtocolVersion::Resp2);
    assert_eq!(config_resp2.protocol_version, ProtocolVersion::Resp2);

    // Test RESP3 configuration
    let config_resp3 = ConnectionConfig::new("redis://localhost:6379")
        .with_protocol_version(ProtocolVersion::Resp3);
    assert_eq!(config_resp3.protocol_version, ProtocolVersion::Resp3);

    // Test default is RESP2
    let config_default = ConnectionConfig::new("redis://localhost:6379");
    assert_eq!(config_default.protocol_version, ProtocolVersion::Resp2);

    Ok(())
}

#[tokio::test]
async fn test_resp3_encoding_edge_cases() -> Result<(), Box<dyn std::error::Error>> {
    use redis_oxide::protocol::resp3::{Resp3Decoder, Resp3Encoder};

    let mut encoder = Resp3Encoder::new();
    let mut decoder = Resp3Decoder::new();

    // Test empty string
    let empty_str = Resp3Value::BlobString(String::new());
    let encoded = encoder.encode(&empty_str)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(empty_str, decoded);

    // Test empty array
    let empty_array = Resp3Value::Array(vec![]);
    let encoded = encoder.encode(&empty_array)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(empty_array, decoded);

    // Test empty map
    let empty_map = Resp3Value::Map(HashMap::new());
    let encoded = encoder.encode(&empty_map)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(empty_map, decoded);

    // Test empty set
    let empty_set = Resp3Value::Set(HashSet::new());
    let encoded = encoder.encode(&empty_set)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(empty_set, decoded);

    // Test zero and negative numbers
    let zero = Resp3Value::Number(0);
    let encoded = encoder.encode(&zero)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(zero, decoded);

    let negative = Resp3Value::Number(-42);
    let encoded = encoder.encode(&negative)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(negative, decoded);

    // Test special float values
    let zero_float = Resp3Value::Double(0.0);
    let encoded = encoder.encode(&zero_float)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(zero_float, decoded);

    let negative_float = Resp3Value::Double(-3.14);
    let encoded = encoder.encode(&negative_float)?;
    let decoded = decoder.decode(&encoded)?;
    assert_eq!(negative_float, decoded);

    Ok(())
}

#[tokio::test]
async fn test_resp3_hash_and_equality() -> Result<(), Box<dyn std::error::Error>> {
    use std::collections::HashSet;

    // Test that equal values have the same hash
    let val1 = Resp3Value::SimpleString("test".to_string());
    let val2 = Resp3Value::SimpleString("test".to_string());
    assert_eq!(val1, val2);

    // Test in HashSet
    let mut set = HashSet::new();
    set.insert(val1);
    set.insert(val2); // Should not increase size due to equality
    assert_eq!(set.len(), 1);

    // Test different types with same content
    let simple_str = Resp3Value::SimpleString("hello".to_string());
    let blob_str = Resp3Value::BlobString("hello".to_string());
    assert_ne!(simple_str, blob_str); // Different types should not be equal

    // Test boolean values
    let bool_true1 = Resp3Value::Boolean(true);
    let bool_true2 = Resp3Value::Boolean(true);
    let bool_false = Resp3Value::Boolean(false);
    assert_eq!(bool_true1, bool_true2);
    assert_ne!(bool_true1, bool_false);

    Ok(())
}