rs2-stream 0.3.3

A high-performance, production-ready async streaming library 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
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
use futures::StreamExt;
use rs2_stream::state::config::StateConfigs;
use rs2_stream::state::stream_ext::StateAccess;
use rs2_stream::state::traits::StateStorageType;
use rs2_stream::state::{
    CustomKeyExtractor, InMemoryState, KeyExtractor, StateStorage, StatefulStreamExt,
};
use serde::{Deserialize, Serialize};
use serde_json;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex as TokioMutex;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct TestEvent {
    id: u64,
    value: String,
    amount: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct TestState {
    count: u64,
    total_amount: f64,
    last_value: String,
}

#[tokio::test]
async fn test_stateful_map_basic() {
    let events = futures::stream::iter(vec![
        TestEvent {
            id: 1,
            value: "test1".to_string(),
            amount: 10.0,
        },
        TestEvent {
            id: 2,
            value: "test2".to_string(),
            amount: 20.0,
        },
        TestEvent {
            id: 1,
            value: "test3".to_string(),
            amount: 15.0,
        },
    ]);

    let config = rs2_stream::state::StateConfig::new();

    let results: Vec<_> = events
        .stateful_map_rs2(
            config,
            CustomKeyExtractor::new(|event: &TestEvent| event.id.to_string()),
            |event, state_access| {
                Box::pin(async move {
                    // Get current state or create new one
                    let mut state = if let Some(bytes) = state_access.get().await {
                        serde_json::from_slice(&bytes).unwrap_or(TestState {
                            count: 0,
                            total_amount: 0.0,
                            last_value: String::new(),
                        })
                    } else {
                        TestState {
                            count: 0,
                            total_amount: 0.0,
                            last_value: String::new(),
                        }
                    };

                    // Update state
                    state.count += 1;
                    state.total_amount += event.amount;
                    state.last_value = event.value.clone();

                    // Save state
                    let bytes = serde_json::to_vec(&state).unwrap();
                    state_access.set(&bytes).await.unwrap();

                    Ok(state)
                })
            },
        )
        .collect::<Vec<_>>()
        .await;

    let results: Vec<TestState> = results.into_iter().map(|r| r.unwrap()).collect();

    assert_eq!(results.len(), 3);
    assert_eq!(results[0].count, 1);
    assert_eq!(results[0].total_amount, 10.0);
    assert_eq!(results[1].count, 1);
    assert_eq!(results[1].total_amount, 20.0);
    assert_eq!(results[2].count, 2);
    assert_eq!(results[2].total_amount, 25.0);
}

#[tokio::test]
async fn test_stateful_filter() {
    let events = futures::stream::iter(vec![
        TestEvent {
            id: 1,
            value: "test1".to_string(),
            amount: 10.0,
        },
        TestEvent {
            id: 2,
            value: "test2".to_string(),
            amount: 20.0,
        },
        TestEvent {
            id: 1,
            value: "test3".to_string(),
            amount: 15.0,
        },
        TestEvent {
            id: 3,
            value: "test4".to_string(),
            amount: 5.0,
        },
    ]);

    let config = rs2_stream::state::StateConfig::new();

    let results: Vec<_> = events
        .stateful_filter_rs2(
            config,
            CustomKeyExtractor::new(|event: &TestEvent| event.id.to_string()),
            |event, state_access| {
                Box::pin(async move {
                    // Get current count
                    let count = if let Some(bytes) = state_access.get().await {
                        let state: TestState =
                            serde_json::from_slice(&bytes).unwrap_or(TestState {
                                count: 0,
                                total_amount: 0.0,
                                last_value: String::new(),
                            });
                        state.count
                    } else {
                        0
                    };

                    // Update count
                    let new_state = TestState {
                        count: count + 1,
                        total_amount: 0.0,
                        last_value: String::new(),
                    };
                    let bytes = serde_json::to_vec(&new_state).unwrap();
                    state_access.set(&bytes).await.unwrap();

                    // Only allow events with count <= 2
                    Ok(count < 2)
                })
            },
        )
        .collect::<Vec<_>>()
        .await;

    let results: Vec<TestEvent> = results.into_iter().map(|r| r.unwrap()).collect();

    assert_eq!(results.len(), 4);
    assert_eq!(results[0].id, 1);
    assert_eq!(results[1].id, 2);
    assert_eq!(results[2].id, 1);
    assert_eq!(results[3].id, 3);
}

#[tokio::test]
async fn test_stateful_fold() {
    let events = futures::stream::iter(vec![
        TestEvent {
            id: 1,
            value: "test1".to_string(),
            amount: 10.0,
        },
        TestEvent {
            id: 2,
            value: "test2".to_string(),
            amount: 20.0,
        },
        TestEvent {
            id: 1,
            value: "test3".to_string(),
            amount: 15.0,
        },
    ]);

    let config = rs2_stream::state::StateConfig::new();

    let results: Vec<_> = events
        .stateful_fold_rs2(
            config,
            CustomKeyExtractor::new(|event: &TestEvent| event.id.to_string()),
            TestState {
                count: 0,
                total_amount: 0.0,
                last_value: String::new(),
            },
            |state, event, state_access| {
                Box::pin(async move {
                    // Get current state
                    let mut state = if let Some(bytes) = state_access.get().await {
                        serde_json::from_slice(&bytes).unwrap_or(TestState {
                            count: 0,
                            total_amount: 0.0,
                            last_value: String::new(),
                        })
                    } else {
                        TestState {
                            count: 0,
                            total_amount: 0.0,
                            last_value: String::new(),
                        }
                    };

                    // Update state
                    state.count += 1;
                    state.total_amount += event.amount;
                    state.last_value = event.value.clone();

                    // Save state
                    let bytes = serde_json::to_vec(&state).unwrap();
                    state_access.set(&bytes).await.unwrap();

                    Ok(state)
                })
            },
        )
        .collect::<Vec<_>>()
        .await;

    let results: Vec<TestState> = results.into_iter().map(|r| r.unwrap()).collect();

    assert_eq!(results.len(), 3);
    assert_eq!(results[0].count, 1);
    assert_eq!(results[0].total_amount, 10.0);
    assert_eq!(results[1].count, 1);
    assert_eq!(results[1].total_amount, 20.0);
    assert_eq!(results[2].count, 2);
    assert_eq!(results[2].total_amount, 25.0);
}

#[tokio::test]
async fn test_in_memory_storage() {
    let storage = InMemoryState::new(Duration::from_secs(60));

    // Test basic operations
    let key = "test_key";
    let value = TestState {
        count: 42,
        total_amount: 100.0,
        last_value: "test".to_string(),
    };

    // Set value
    storage
        .set(&key, &serde_json::to_vec(&value).unwrap())
        .await
        .unwrap();

    // Get value
    let bytes = storage.get(&key).await.unwrap();
    let retrieved: TestState = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(retrieved, value);

    // Check exists
    assert!(storage.exists(&key).await);

    // Delete value
    storage.delete(&key).await.unwrap();
    assert!(!storage.exists(&key).await);
}

#[tokio::test]
async fn test_state_config_validation() {
    // Valid config
    let valid_config = StateConfigs::high_performance();
    assert!(valid_config.validate().is_ok());

    // Invalid config - zero TTL
    let mut invalid_config = StateConfigs::high_performance();
    invalid_config.ttl = Duration::from_secs(0);
    assert!(invalid_config.validate().is_err());

    // Invalid config - cleanup interval > TTL
    let mut invalid_config = StateConfigs::high_performance();
    invalid_config.cleanup_interval = Duration::from_secs(2 * 60 * 60); // 2 hours
    invalid_config.ttl = Duration::from_secs(60 * 60); // 1 hour
    assert!(invalid_config.validate().is_err());
}

#[tokio::test]
async fn test_state_config_builder() {
    let config = StateConfigBuilder::new()
        .in_memory()
        .ttl(Duration::from_secs(2 * 60 * 60)) // 2 hours
        .cleanup_interval(Duration::from_secs(10 * 60)) // 10 minutes
        .max_size(1000)
        .build()
        .unwrap();

    assert!(matches!(config.storage_type, StateStorageType::InMemory));
    assert_eq!(config.ttl, Duration::from_secs(2 * 60 * 60)); // 2 hours
    assert_eq!(config.cleanup_interval, Duration::from_secs(10 * 60)); // 10 minutes
    assert_eq!(config.max_size, Some(1000));
}

#[tokio::test]
async fn test_state_access_interface() {
    let config = rs2_stream::state::StateConfig::new();
    let storage = config.create_storage_arc();
    let state_access = StateAccess::new(storage, "test_key".to_string());

    // Test basic get/set operations
    let initial_state = TestState {
        count: 0,
        total_amount: 0.0,
        last_value: String::new(),
    };

    // Set initial state
    let state_bytes = serde_json::to_vec(&initial_state).unwrap();
    state_access.set(&state_bytes).await.unwrap();

    // Get and update state
    let state_bytes = state_access.get().await.unwrap_or(Vec::new());
    let mut state: TestState = if state_bytes.is_empty() {
        TestState {
            count: 0,
            total_amount: 0.0,
            last_value: String::new(),
        }
    } else {
        serde_json::from_slice(&state_bytes).unwrap()
    };

    state.count += 1;
    state.total_amount += 50.0;

    // Save updated state
    let state_bytes = serde_json::to_vec(&state).unwrap();
    state_access.set(&state_bytes).await.unwrap();

    assert_eq!(state.count, 1);

    // Verify the update by getting the state again
    let state_bytes = state_access.get().await.unwrap_or(Vec::new());
    let retrieved: TestState = if state_bytes.is_empty() {
        TestState {
            count: 0,
            total_amount: 0.0,
            last_value: String::new(),
        }
    } else {
        serde_json::from_slice(&state_bytes).unwrap()
    };

    assert_eq!(retrieved.count, 1);
    assert_eq!(retrieved.total_amount, 50.0);
}

#[tokio::test]
async fn test_stateful_window_processing() {
    let events = futures::stream::iter(vec![
        TestEvent {
            id: 1,
            value: "test1".to_string(),
            amount: 10.0,
        },
        TestEvent {
            id: 2,
            value: "test2".to_string(),
            amount: 20.0,
        },
        TestEvent {
            id: 1,
            value: "test3".to_string(),
            amount: 15.0,
        },
        TestEvent {
            id: 3,
            value: "test4".to_string(),
            amount: 25.0,
        },
    ]);

    let config = StateConfigs::high_performance();

    let results: Vec<_> = events
        .stateful_window_rs2(
            config,
            CustomKeyExtractor::new(|event: &TestEvent| event.id.to_string()),
            2,
            |window_events, _state_access| {
                Box::pin(async move {
                    let total_amount: f64 = window_events.iter().map(|e| e.amount).sum();
                    let count = window_events.len();
                    Ok((count, total_amount))
                })
            },
        )
        .collect::<Vec<_>>()
        .await;

    let results: Vec<(usize, f64)> = results.into_iter().map(|r| r.unwrap()).collect();

    assert_eq!(results.len(), 1);
    assert_eq!(results[0], (2, 25.0)); // Only one window: id=1, 10.0 + 15.0
}

#[tokio::test]
async fn test_state_ttl_expiration() {
    // Use a very short TTL for testing
    let storage = InMemoryState::new(Duration::from_millis(10));

    let key = "expiring_key";
    let value = TestState {
        count: 1,
        total_amount: 100.0,
        last_value: "test".to_string(),
    };

    // Set the value
    storage
        .set(&key, &serde_json::to_vec(&value).unwrap())
        .await
        .unwrap();

    // Verify it exists immediately
    assert!(storage.exists(&key).await);
    assert!(storage.get(&key).await.is_some());

    // Wait for expiration with a timeout
    let timeout = tokio::time::timeout(Duration::from_millis(100), async {
        loop {
            tokio::time::sleep(Duration::from_millis(5)).await;
            if !storage.exists(&key).await {
                break;
            }
        }
    })
    .await;

    // Ensure we didn't timeout
    assert!(timeout.is_ok(), "TTL expiration timed out");

    // After expiration, exists() should return false and get() should return None
    assert!(!storage.exists(&key).await);
    assert!(storage.get(&key).await.is_none());
}

#[tokio::test]
async fn test_state_max_size_enforcement() {
    let mut config = StateConfigs::high_performance();
    config.max_size = Some(2);

    let storage = InMemoryState::new(config.ttl).with_max_size(2);

    // Add more items than max_size
    for i in 0..5 {
        let key = format!("key_{}", i);
        let value = TestState {
            count: i,
            total_amount: i as f64 * 10.0,
            last_value: format!("value_{}", i),
        };
        storage
            .set(&key, &serde_json::to_vec(&value).unwrap())
            .await
            .unwrap();
    }

    // Should only have the last 2 items
    assert!(storage.exists("key_3").await);
    assert!(storage.exists("key_4").await);
    assert!(!storage.exists("key_0").await);
    assert!(!storage.exists("key_1").await);
    assert!(!storage.exists("key_2").await);
}

#[tokio::test]
async fn test_custom_key_extractor() {
    let extractor = CustomKeyExtractor::new(|event: &TestEvent| format!("user_{}", event.id));

    let event = TestEvent {
        id: 42,
        value: "test".to_string(),
        amount: 100.0,
    };

    let key = extractor.extract_key(&event);
    assert_eq!(key, "user_42");
}

#[tokio::test]
async fn test_state_error_handling() {
    let storage = InMemoryState::new(Duration::from_secs(60));

    // Test with invalid JSON
    let invalid_json = b"invalid json";
    let result = storage.set("test_key", invalid_json).await;
    assert!(result.is_ok()); // Storage should accept any bytes

    // Test get with non-existent key
    let result = storage.get("non_existent").await;
    assert!(result.is_none());
}

// Builder pattern for StateConfig
struct StateConfigBuilder {
    config: rs2_stream::state::StateConfig,
}

impl StateConfigBuilder {
    fn new() -> Self {
        Self {
            config: rs2_stream::state::StateConfig::default(),
        }
    }

    fn in_memory(mut self) -> Self {
        self.config.storage_type = StateStorageType::InMemory;
        self
    }

    fn ttl(mut self, ttl: Duration) -> Self {
        self.config.ttl = ttl;
        self
    }

    fn cleanup_interval(mut self, interval: Duration) -> Self {
        self.config.cleanup_interval = interval;
        self
    }

    fn max_size(mut self, max_size: usize) -> Self {
        self.config.max_size = Some(max_size);
        self
    }

    fn build(self) -> Result<rs2_stream::state::StateConfig, String> {
        self.config.validate().map_err(|e| e.to_string())?;
        Ok(self.config)
    }
}