throttlecrab-server 0.4.37

A high-performance rate limiting server with multiple protocol support
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//! Tests for Redis protocol transport

use super::redis::resp::{RespParser, RespSerializer, RespValue};
use crate::actor::RateLimiterHandle;
use crate::config::StoreType;
use crate::metrics::Metrics;
use crate::store;
use std::sync::Arc;

// Helper function to create a new rate limiter for each test
fn create_test_rate_limiter() -> (RateLimiterHandle, Arc<Metrics>) {
    let metrics = Arc::new(Metrics::new());
    let store_config = crate::config::StoreConfig {
        store_type: StoreType::Periodic,
        capacity: 10000,
        cleanup_interval: 300,
        cleanup_probability: 10000,
        min_interval: 5,
        max_interval: 300,
        max_operations: 1000000,
    };
    let handle = store::create_rate_limiter(&store_config, 10000, metrics.clone());
    (handle, metrics)
}

// Helper to create a THROTTLE command
fn create_throttle_cmd(
    key: &str,
    max_burst: i64,
    count_per_period: i64,
    period: i64,
    quantity: Option<i64>,
) -> RespValue {
    let mut args = vec![
        RespValue::BulkString(Some("THROTTLE".to_string())),
        RespValue::BulkString(Some(key.to_string())),
        RespValue::BulkString(Some(max_burst.to_string())),
        RespValue::BulkString(Some(count_per_period.to_string())),
        RespValue::BulkString(Some(period.to_string())),
    ];
    if let Some(q) = quantity {
        args.push(RespValue::BulkString(Some(q.to_string())));
    }
    RespValue::Array(args)
}

// Helper to create a PING command
fn create_ping_cmd(message: Option<&str>) -> RespValue {
    let mut args = vec![RespValue::BulkString(Some("PING".to_string()))];
    if let Some(msg) = message {
        args.push(RespValue::BulkString(Some(msg.to_string())));
    }
    RespValue::Array(args)
}

// Helper to get throttle response fields
struct ThrottleResponse {
    allowed: bool,
    limit: i64,
    remaining: i64,
    reset_after: i64,
    retry_after: i64,
}

impl ThrottleResponse {
    fn from_resp(response: &RespValue) -> Self {
        match response {
            RespValue::Array(values) => {
                assert_eq!(values.len(), 5, "Throttle response should have 5 elements");
                Self {
                    allowed: match &values[0] {
                        RespValue::Integer(n) => *n == 1,
                        _ => panic!("Expected integer for allowed field"),
                    },
                    limit: match &values[1] {
                        RespValue::Integer(n) => *n,
                        _ => panic!("Expected integer for limit field"),
                    },
                    remaining: match &values[2] {
                        RespValue::Integer(n) => *n,
                        _ => panic!("Expected integer for remaining field"),
                    },
                    reset_after: match &values[3] {
                        RespValue::Integer(n) => *n,
                        _ => panic!("Expected integer for reset_after field"),
                    },
                    retry_after: match &values[4] {
                        RespValue::Integer(n) => *n,
                        _ => panic!("Expected integer for retry_after field"),
                    },
                }
            }
            _ => panic!("Expected array response for throttle command"),
        }
    }
}

#[tokio::test]
async fn test_redis_ping() {
    let (handle, metrics) = create_test_rate_limiter();

    let ping_cmd = create_ping_cmd(None);
    let response = process_command(ping_cmd, &handle, &metrics).await;
    assert_eq!(response, RespValue::SimpleString("PONG".to_string()));
}

#[tokio::test]
async fn test_redis_ping_with_message() {
    let (handle, metrics) = create_test_rate_limiter();

    let ping_cmd = create_ping_cmd(Some("hello"));
    let response = process_command(ping_cmd, &handle, &metrics).await;
    assert_eq!(response, RespValue::BulkString(Some("hello".to_string())));
}

#[tokio::test]
async fn test_redis_throttle_allowed() {
    let (handle, metrics) = create_test_rate_limiter();

    let throttle_cmd = create_throttle_cmd("test_key", 10, 100, 60, None);
    let response = process_command(throttle_cmd, &handle, &metrics).await;

    let throttle_resp = ThrottleResponse::from_resp(&response);
    assert!(throttle_resp.allowed);
    assert_eq!(throttle_resp.limit, 10);
    assert_eq!(throttle_resp.remaining, 9);
    assert_eq!(throttle_resp.reset_after, 5);
    assert_eq!(throttle_resp.retry_after, 0);
}

#[tokio::test]
async fn test_redis_throttle_with_quantity() {
    let (handle, metrics) = create_test_rate_limiter();

    let throttle_cmd = create_throttle_cmd("test_key2", 10, 100, 60, Some(5));
    let response = process_command(throttle_cmd, &handle, &metrics).await;

    let throttle_resp = ThrottleResponse::from_resp(&response);
    assert!(throttle_resp.allowed);
    assert_eq!(throttle_resp.limit, 10);
    assert_eq!(throttle_resp.remaining, 5); // 10-5
    assert_eq!(throttle_resp.reset_after, 7);
    assert_eq!(throttle_resp.retry_after, 0);
}

#[tokio::test]
async fn test_redis_unknown_command() {
    let (handle, metrics) = create_test_rate_limiter();

    let unknown_cmd = create_invalid_cmd("UNKNOWN", vec![]);
    let response = process_command(unknown_cmd, &handle, &metrics).await;
    assert_error_response(&response, "unknown command");
}

#[tokio::test]
async fn test_redis_invalid_throttle_args() {
    let (handle, metrics) = create_test_rate_limiter();

    // Too few arguments
    let throttle_cmd = create_invalid_cmd("THROTTLE", vec!["test_key"]);
    let response = process_command(throttle_cmd, &handle, &metrics).await;
    assert_error_response(&response, "wrong number of arguments");
}

#[tokio::test]
async fn test_resp_parser_partial_data() {
    let mut parser = RespParser::new();

    // Partial simple string
    let data1 = b"+OK";
    assert_eq!(parser.parse(data1).unwrap(), None);

    // Complete it
    let data2 = b"+OK\r\n";
    assert_eq!(
        parser.parse(data2).unwrap(),
        Some((RespValue::SimpleString("OK".to_string()), 5))
    );

    // Partial bulk string
    let data3 = b"$6\r\nfoo";
    assert_eq!(parser.parse(data3).unwrap(), None);

    // Complete bulk string
    let data4 = b"$6\r\nfoobar\r\n";
    assert_eq!(
        parser.parse(data4).unwrap(),
        Some((RespValue::BulkString(Some("foobar".to_string())), 12))
    );
}

#[tokio::test]
async fn test_resp_serializer_roundtrip() {
    let mut parser = RespParser::new();

    // Test various RESP values
    let values = vec![
        RespValue::SimpleString("OK".to_string()),
        RespValue::Error("ERR something".to_string()),
        RespValue::Integer(42),
        RespValue::BulkString(Some("hello world".to_string())),
        RespValue::BulkString(None),
        RespValue::Array(vec![
            RespValue::BulkString(Some("foo".to_string())),
            RespValue::Integer(123),
            RespValue::SimpleString("bar".to_string()),
        ]),
    ];

    for value in values {
        let serialized = RespSerializer::serialize(&value);
        let (parsed, consumed) = parser.parse(&serialized).unwrap().unwrap();
        assert_eq!(parsed, value);
        assert_eq!(consumed, serialized.len());
    }
}

#[tokio::test]
async fn test_resp_edge_cases() {
    let mut parser = RespParser::new();

    // Empty array
    let data = b"*0\r\n";
    assert_eq!(
        parser.parse(data).unwrap(),
        Some((RespValue::Array(vec![]), 4))
    );

    // Nested arrays
    let nested = RespValue::Array(vec![
        RespValue::Array(vec![RespValue::Integer(1), RespValue::Integer(2)]),
        RespValue::BulkString(Some("test".to_string())),
    ]);

    let serialized = RespSerializer::serialize(&nested);
    let (parsed, _) = parser.parse(&serialized).unwrap().unwrap();
    assert_eq!(parsed, nested);
}

// Helper to create invalid command
fn create_invalid_cmd(cmd: &str, args: Vec<&str>) -> RespValue {
    let mut cmd_args = vec![RespValue::BulkString(Some(cmd.to_string()))];
    for arg in args {
        cmd_args.push(RespValue::BulkString(Some(arg.to_string())));
    }
    RespValue::Array(cmd_args)
}

// Helper to assert error response
fn assert_error_response(response: &RespValue, expected_error_substring: &str) {
    match response {
        RespValue::Error(msg) => {
            assert!(
                msg.contains(expected_error_substring),
                "Expected error containing '{expected_error_substring}', got '{msg}'"
            );
        }
        _ => panic!("Expected error response"),
    }
}

// Helper function to test actual commands (exposed for testing)
async fn process_command(
    value: RespValue,
    limiter: &RateLimiterHandle,
    metrics: &Arc<Metrics>,
) -> RespValue {
    super::redis::process_command(value, limiter, metrics).await
}

#[tokio::test]
async fn test_redis_throttle_exhaustion() {
    let (handle, metrics) = create_test_rate_limiter();

    let key = "exhaustion_test";
    let max_burst = 3;
    let throttle_cmd = create_throttle_cmd(key, max_burst, 100, 60, None);

    // First request should succeed
    let response = process_command(throttle_cmd.clone(), &handle, &metrics).await;
    let resp = ThrottleResponse::from_resp(&response);
    assert!(resp.allowed);
    assert_eq!(resp.limit, 3);
    assert_eq!(resp.remaining, 2);

    // Second request should succeed
    let response = process_command(throttle_cmd.clone(), &handle, &metrics).await;
    let resp = ThrottleResponse::from_resp(&response);
    assert!(resp.allowed);
    assert_eq!(resp.remaining, 1);

    // Third request should succeed
    let response = process_command(throttle_cmd.clone(), &handle, &metrics).await;
    let resp = ThrottleResponse::from_resp(&response);
    assert!(resp.allowed);
    assert_eq!(resp.remaining, 0);

    // Fourth request should be denied
    let response = process_command(throttle_cmd, &handle, &metrics).await;
    let resp = ThrottleResponse::from_resp(&response);
    assert!(!resp.allowed);
    assert_eq!(resp.remaining, 0);
    assert!(resp.retry_after >= 0, "retry_after should be non-negative");
}

#[tokio::test]
async fn test_redis_multiple_keys() {
    let (handle, metrics) = create_test_rate_limiter();

    // Test with three different keys
    let keys = vec!["user:123", "user:456", "api:endpoint"];

    for key in &keys {
        let throttle_cmd = create_throttle_cmd(key, 5, 100, 60, None);
        let response = process_command(throttle_cmd, &handle, &metrics).await;
        let resp = ThrottleResponse::from_resp(&response);
        assert!(resp.allowed);
        assert_eq!(resp.limit, 5);
        assert_eq!(resp.remaining, 4);
    }

    // Verify each key maintains its own limit
    for key in &keys {
        let throttle_cmd = create_throttle_cmd(key, 5, 100, 60, None);
        let response = process_command(throttle_cmd, &handle, &metrics).await;
        let resp = ThrottleResponse::from_resp(&response);
        assert!(resp.allowed);
        assert_eq!(resp.remaining, 3); // each has 3 remaining
    }
}

#[tokio::test]
async fn test_redis_different_limits_same_key() {
    let (handle, metrics) = create_test_rate_limiter();

    let key = "dynamic_limit_key";

    // First request with limit of 10
    let throttle_cmd = RespValue::Array(vec![
        RespValue::BulkString(Some("THROTTLE".to_string())),
        RespValue::BulkString(Some(key.to_string())),
        RespValue::BulkString(Some("10".to_string())),
        RespValue::BulkString(Some("100".to_string())),
        RespValue::BulkString(Some("60".to_string())),
    ]);

    let response = process_command(throttle_cmd, &handle, &metrics).await;
    match response {
        RespValue::Array(values) => {
            assert_eq!(values[0], RespValue::Integer(1)); // allowed
            assert_eq!(values[1], RespValue::Integer(10)); // limit
            assert_eq!(values[2], RespValue::Integer(9)); // remaining
        }
        _ => panic!("Expected array response"),
    }

    // Same key but with different limit
    let throttle_cmd = RespValue::Array(vec![
        RespValue::BulkString(Some("THROTTLE".to_string())),
        RespValue::BulkString(Some(key.to_string())),
        RespValue::BulkString(Some("5".to_string())), // smaller limit
        RespValue::BulkString(Some("100".to_string())),
        RespValue::BulkString(Some("60".to_string())),
    ]);

    let response = process_command(throttle_cmd, &handle, &metrics).await;
    match response {
        RespValue::Array(values) => {
            assert_eq!(values[0], RespValue::Integer(1)); // allowed
            assert_eq!(values[1], RespValue::Integer(5)); // new limit
            // The remaining count depends on implementation - just verify it's within bounds
            if let RespValue::Integer(remaining) = &values[2] {
                assert!(
                    *remaining >= 0 && *remaining <= 5,
                    "remaining should be between 0 and 5"
                );
            }
        }
        _ => panic!("Expected array response"),
    }
}

#[tokio::test]
async fn test_redis_large_quantity() {
    let (handle, metrics) = create_test_rate_limiter();

    // Request with quantity larger than limit
    let throttle_cmd = create_throttle_cmd("large_quantity_key", 10, 100, 60, Some(15));
    let response = process_command(throttle_cmd, &handle, &metrics).await;

    let resp = ThrottleResponse::from_resp(&response);
    assert!(!resp.allowed); // denied
    assert_eq!(resp.limit, 10);
    assert_eq!(resp.remaining, 10); // remaining unchanged
}

#[tokio::test]
async fn test_redis_special_characters_in_key() {
    let (handle, metrics) = create_test_rate_limiter();

    // Test keys with special characters
    let special_keys = vec![
        "user:email@example.com",
        "api:v2/users/{id}",
        "rate:limit:user-123",
        "key with spaces",
        "key:with:colons:everywhere",
        "UTF8:测试键",
    ];

    for key in special_keys {
        let throttle_cmd = create_throttle_cmd(key, 5, 100, 60, None);
        let response = process_command(throttle_cmd, &handle, &metrics).await;
        let resp = ThrottleResponse::from_resp(&response);
        assert!(resp.allowed, "Failed for key: {key}");
        assert_eq!(resp.limit, 5);
        assert_eq!(resp.remaining, 4);
    }
}

#[tokio::test]
async fn test_redis_mixed_commands() {
    let (handle, metrics) = create_test_rate_limiter();

    // PING
    let ping_cmd = RespValue::Array(vec![RespValue::BulkString(Some("PING".to_string()))]);
    let response = process_command(ping_cmd, &handle, &metrics).await;
    assert_eq!(response, RespValue::SimpleString("PONG".to_string()));

    // THROTTLE
    let throttle_cmd = RespValue::Array(vec![
        RespValue::BulkString(Some("THROTTLE".to_string())),
        RespValue::BulkString(Some("mixed_key".to_string())),
        RespValue::BulkString(Some("10".to_string())),
        RespValue::BulkString(Some("100".to_string())),
        RespValue::BulkString(Some("60".to_string())),
    ]);
    let response = process_command(throttle_cmd, &handle, &metrics).await;
    match response {
        RespValue::Array(values) => {
            assert_eq!(values[0], RespValue::Integer(1));
        }
        _ => panic!("Expected array response"),
    }

    // PING with message
    let ping_cmd = RespValue::Array(vec![
        RespValue::BulkString(Some("PING".to_string())),
        RespValue::BulkString(Some("test message".to_string())),
    ]);
    let response = process_command(ping_cmd, &handle, &metrics).await;
    assert_eq!(
        response,
        RespValue::BulkString(Some("test message".to_string()))
    );

    // Another THROTTLE
    let throttle_cmd = RespValue::Array(vec![
        RespValue::BulkString(Some("throttle".to_string())), // lowercase
        RespValue::BulkString(Some("mixed_key".to_string())),
        RespValue::BulkString(Some("10".to_string())),
        RespValue::BulkString(Some("100".to_string())),
        RespValue::BulkString(Some("60".to_string())),
    ]);
    let response = process_command(throttle_cmd, &handle, &metrics).await;
    match response {
        RespValue::Array(values) => {
            assert_eq!(values[0], RespValue::Integer(1));
            assert_eq!(values[2], RespValue::Integer(8)); // one less remaining
        }
        _ => panic!("Expected array response"),
    }
}

#[tokio::test]
async fn test_redis_invalid_numeric_args() {
    let (handle, metrics) = create_test_rate_limiter();

    // Test invalid max_burst
    let throttle_cmd =
        create_invalid_cmd("THROTTLE", vec!["test_key", "not_a_number", "100", "60"]);
    let response = process_command(throttle_cmd, &handle, &metrics).await;
    assert_error_response(&response, "invalid max_burst");

    // Test negative values
    let throttle_cmd = create_invalid_cmd("THROTTLE", vec!["test_key", "-5", "100", "60"]);
    let response = process_command(throttle_cmd, &handle, &metrics).await;
    assert_error_response(&response, "ERR");
}

#[tokio::test]
async fn test_redis_zero_quantity() {
    let (handle, metrics) = create_test_rate_limiter();

    // Request with zero quantity
    let throttle_cmd = create_throttle_cmd("zero_quantity_key", 10, 100, 60, Some(0));
    let response = process_command(throttle_cmd, &handle, &metrics).await;

    let resp = ThrottleResponse::from_resp(&response);
    assert!(resp.allowed); // zero quantity always succeeds
    assert_eq!(resp.remaining, 10); // remaining unchanged
}

#[tokio::test]
async fn test_resp_parser_multiple_commands() {
    let mut parser = RespParser::new();

    // Simulate multiple commands in buffer
    let mut data = Vec::new();
    data.extend_from_slice(b"*1\r\n$4\r\nPING\r\n");
    data.extend_from_slice(b"*2\r\n$4\r\nPING\r\n$5\r\nhello\r\n");

    // Parse first command
    let (cmd1, consumed1) = parser.parse(&data).unwrap().unwrap();
    assert_eq!(
        cmd1,
        RespValue::Array(vec![RespValue::BulkString(Some("PING".to_string()))])
    );

    // Remove consumed data
    data.drain(..consumed1);

    // Parse second command
    let (cmd2, consumed2) = parser.parse(&data).unwrap().unwrap();
    assert_eq!(
        cmd2,
        RespValue::Array(vec![
            RespValue::BulkString(Some("PING".to_string())),
            RespValue::BulkString(Some("hello".to_string())),
        ])
    );
    assert_eq!(consumed2, data.len());
}

#[tokio::test]
async fn test_resp_integer_args() {
    let mut parser = RespParser::new();

    // Test command with integer arguments (not bulk strings)
    let data = b"*5\r\n$8\r\nTHROTTLE\r\n$8\r\ntest_key\r\n:10\r\n:100\r\n:60\r\n";
    let (cmd, _) = parser.parse(data).unwrap().unwrap();

    assert_eq!(
        cmd,
        RespValue::Array(vec![
            RespValue::BulkString(Some("THROTTLE".to_string())),
            RespValue::BulkString(Some("test_key".to_string())),
            RespValue::Integer(10),
            RespValue::Integer(100),
            RespValue::Integer(60),
        ])
    );
}

#[tokio::test]
async fn test_redis_concurrent_same_key() {
    let (handle, metrics) = create_test_rate_limiter();

    let key = "concurrent_key";
    let max_burst = 10;

    // Spawn multiple concurrent requests
    let mut handles = vec![];
    for _ in 0..5 {
        let handle_clone = handle.clone();
        let metrics_clone = metrics.clone();
        let key_clone = key.to_string();

        let task = tokio::spawn(async move {
            let throttle_cmd = RespValue::Array(vec![
                RespValue::BulkString(Some("THROTTLE".to_string())),
                RespValue::BulkString(Some(key_clone)),
                RespValue::BulkString(Some(max_burst.to_string())),
                RespValue::BulkString(Some("100".to_string())),
                RespValue::BulkString(Some("60".to_string())),
            ]);

            process_command(throttle_cmd, &handle_clone, &metrics_clone).await
        });
        handles.push(task);
    }

    // Wait for all requests to complete
    let mut results = Vec::new();
    for handle in handles {
        results.push(handle.await.unwrap());
    }

    // All requests should be allowed (we have 10 burst, requesting 5)
    let mut allowed_count = 0;
    for result in &results {
        match result {
            RespValue::Array(values) => {
                assert_eq!(values[0], RespValue::Integer(1)); // all should be allowed
                assert_eq!(values[1], RespValue::Integer(max_burst)); // limit
                allowed_count += 1;
            }
            _ => panic!("Expected array response"),
        }
    }

    // All 5 concurrent requests should be allowed
    assert_eq!(allowed_count, 5);
}

#[tokio::test]
async fn test_redis_rapid_succession() {
    let (handle, metrics) = create_test_rate_limiter();

    let key = "rapid_key";
    let max_burst = 5;
    let mut allowed_count = 0;
    let mut denied_count = 0;

    // Send 10 requests in rapid succession (should exhaust limit)
    for _ in 0..10 {
        let throttle_cmd = create_throttle_cmd(key, max_burst, 100, 60, None);
        let response = process_command(throttle_cmd, &handle, &metrics).await;
        let resp = ThrottleResponse::from_resp(&response);

        if resp.allowed {
            allowed_count += 1;
        } else {
            denied_count += 1;
        }
    }

    assert_eq!(allowed_count, 5, "Expected 5 requests to be allowed");
    assert_eq!(denied_count, 5, "Expected 5 requests to be denied");
}

#[tokio::test]
async fn test_redis_empty_key() {
    let (handle, metrics) = create_test_rate_limiter();

    // Test with empty key
    let throttle_cmd = RespValue::Array(vec![
        RespValue::BulkString(Some("THROTTLE".to_string())),
        RespValue::BulkString(Some("".to_string())), // empty key
        RespValue::BulkString(Some("10".to_string())),
        RespValue::BulkString(Some("100".to_string())),
        RespValue::BulkString(Some("60".to_string())),
    ]);

    let response = process_command(throttle_cmd, &handle, &metrics).await;
    // Empty key should still work
    match response {
        RespValue::Array(values) => {
            assert_eq!(values[0], RespValue::Integer(1)); // allowed
            assert_eq!(values[1], RespValue::Integer(10)); // limit
            assert_eq!(values[2], RespValue::Integer(9)); // remaining
        }
        _ => panic!("Expected array response"),
    }
}

#[tokio::test]
async fn test_redis_null_args() {
    let (handle, metrics) = create_test_rate_limiter();

    // Test with null bulk string
    let throttle_cmd = RespValue::Array(vec![
        RespValue::BulkString(Some("THROTTLE".to_string())),
        RespValue::BulkString(None), // null key
        RespValue::BulkString(Some("10".to_string())),
        RespValue::BulkString(Some("100".to_string())),
        RespValue::BulkString(Some("60".to_string())),
    ]);

    let response = process_command(throttle_cmd, &handle, &metrics).await;
    match response {
        RespValue::Error(msg) => assert!(msg.contains("invalid key")),
        _ => panic!("Expected error response for null key"),
    }
}

#[tokio::test]
async fn test_redis_boundary_values() {
    let (handle, metrics) = create_test_rate_limiter();

    // Test with max i64 values
    let throttle_cmd = RespValue::Array(vec![
        RespValue::BulkString(Some("THROTTLE".to_string())),
        RespValue::BulkString(Some("boundary_key".to_string())),
        RespValue::BulkString(Some(i64::MAX.to_string())),
        RespValue::BulkString(Some(i64::MAX.to_string())),
        RespValue::BulkString(Some(i64::MAX.to_string())),
    ]);

    let response = process_command(throttle_cmd, &handle, &metrics).await;
    match response {
        RespValue::Array(values) => {
            assert_eq!(values[0], RespValue::Integer(1)); // allowed
            assert_eq!(values[1], RespValue::Integer(i64::MAX)); // limit
        }
        _ => panic!("Expected array response"),
    }

    // Test with very small values
    let throttle_cmd = RespValue::Array(vec![
        RespValue::BulkString(Some("THROTTLE".to_string())),
        RespValue::BulkString(Some("tiny_key".to_string())),
        RespValue::BulkString(Some("1".to_string())),
        RespValue::BulkString(Some("1".to_string())),
        RespValue::BulkString(Some("1".to_string())),
    ]);

    let response = process_command(throttle_cmd, &handle, &metrics).await;
    match response {
        RespValue::Array(values) => {
            assert_eq!(values[0], RespValue::Integer(1)); // allowed
            assert_eq!(values[1], RespValue::Integer(1)); // limit
            assert_eq!(values[2], RespValue::Integer(0)); // remaining
        }
        _ => panic!("Expected array response"),
    }
}

#[tokio::test]
async fn test_redis_command_case_insensitive() {
    let (handle, metrics) = create_test_rate_limiter();

    // Test various case combinations
    let commands = vec!["ping", "PING", "Ping", "PiNg"];

    for cmd in commands {
        let ping_cmd = RespValue::Array(vec![RespValue::BulkString(Some(cmd.to_string()))]);

        let response = process_command(ping_cmd, &handle, &metrics).await;
        assert_eq!(
            response,
            RespValue::SimpleString("PONG".to_string()),
            "Failed for command: {cmd}"
        );
    }

    // Test throttle command case variations
    let throttle_commands = vec!["throttle", "THROTTLE", "Throttle", "ThRoTtLe"];

    for cmd in throttle_commands {
        let throttle_cmd = RespValue::Array(vec![
            RespValue::BulkString(Some(cmd.to_string())),
            RespValue::BulkString(Some("case_test_key".to_string())),
            RespValue::BulkString(Some("10".to_string())),
            RespValue::BulkString(Some("100".to_string())),
            RespValue::BulkString(Some("60".to_string())),
        ]);

        let response = process_command(throttle_cmd, &handle, &metrics).await;
        match response {
            RespValue::Array(values) => {
                assert_eq!(
                    values[0],
                    RespValue::Integer(1),
                    "Failed for command: {cmd}"
                );
            }
            _ => panic!("Expected array response for command: {cmd}"),
        }
    }
}

#[tokio::test]
async fn test_redis_very_long_key() {
    let (handle, metrics) = create_test_rate_limiter();

    // Test with a very long key (1000 characters)
    let long_key = "x".repeat(1000);

    let throttle_cmd = RespValue::Array(vec![
        RespValue::BulkString(Some("THROTTLE".to_string())),
        RespValue::BulkString(Some(long_key.clone())),
        RespValue::BulkString(Some("10".to_string())),
        RespValue::BulkString(Some("100".to_string())),
        RespValue::BulkString(Some("60".to_string())),
    ]);

    let response = process_command(throttle_cmd, &handle, &metrics).await;
    match response {
        RespValue::Array(values) => {
            assert_eq!(values[0], RespValue::Integer(1)); // allowed
            assert_eq!(values[1], RespValue::Integer(10)); // limit
            assert_eq!(values[2], RespValue::Integer(9)); // remaining
        }
        _ => panic!("Expected array response"),
    }

    // Verify the same key works again
    let throttle_cmd = RespValue::Array(vec![
        RespValue::BulkString(Some("THROTTLE".to_string())),
        RespValue::BulkString(Some(long_key)),
        RespValue::BulkString(Some("10".to_string())),
        RespValue::BulkString(Some("100".to_string())),
        RespValue::BulkString(Some("60".to_string())),
    ]);

    let response = process_command(throttle_cmd, &handle, &metrics).await;
    match response {
        RespValue::Array(values) => {
            assert_eq!(values[0], RespValue::Integer(1)); // allowed
            assert_eq!(values[2], RespValue::Integer(8)); // one less remaining
        }
        _ => panic!("Expected array response"),
    }
}