rivven-cdc 0.0.2

Change Data Capture for Rivven - PostgreSQL, MySQL, MariaDB
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
//! # Parallel CDC Source
//!
//! Multi-table parallel CDC processing with work distribution.
//!
//! ## Features
//!
//! - **Table Parallelism**: Process multiple tables concurrently
//! - **Work Stealing**: Dynamic load balancing across workers
//! - **Backpressure**: Per-table rate limiting
//! - **Coordinated Snapshots**: Consistent multi-table snapshots
//!
//! ## Usage
//!
//! ```ignore
//! use rivven_cdc::common::{ParallelSource, ParallelConfig};
//!
//! let config = ParallelConfig::builder()
//!     .concurrency(4)
//!     .per_table_buffer(1000)
//!     .build();
//!
//! let source = ParallelSource::new(config, sources);
//! source.start().await?;
//!
//! while let Some(event) = source.next().await {
//!     process(event).await?;
//! }
//! ```

use crate::common::{CdcError, CdcEvent, Result};
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::mpsc::{self, Receiver, Sender};
use tokio::sync::{Notify, RwLock, Semaphore};
use tokio::task::JoinHandle;
use tracing::{debug, error, info, warn};

/// Configuration for parallel CDC source.
#[derive(Debug, Clone)]
pub struct ParallelConfig {
    /// Maximum concurrent table workers
    pub concurrency: usize,
    /// Buffer size per table
    pub per_table_buffer: usize,
    /// Output channel buffer size
    pub output_buffer: usize,
    /// Enable work stealing between workers
    pub work_stealing: bool,
    /// Maximum events per second per table
    pub per_table_rate_limit: Option<u64>,
    /// Timeout for worker shutdown
    pub shutdown_timeout: Duration,
}

impl Default for ParallelConfig {
    fn default() -> Self {
        Self {
            concurrency: 4,
            per_table_buffer: 1000,
            output_buffer: 10_000,
            work_stealing: true,
            per_table_rate_limit: None,
            shutdown_timeout: Duration::from_secs(30),
        }
    }
}

impl ParallelConfig {
    pub fn builder() -> ParallelConfigBuilder {
        ParallelConfigBuilder::new()
    }

    /// High-throughput preset.
    pub fn high_throughput() -> Self {
        Self {
            concurrency: 8,
            per_table_buffer: 5000,
            output_buffer: 50_000,
            work_stealing: true,
            per_table_rate_limit: None,
            shutdown_timeout: Duration::from_secs(60),
        }
    }

    /// Low-latency preset.
    pub fn low_latency() -> Self {
        Self {
            concurrency: 2,
            per_table_buffer: 100,
            output_buffer: 1000,
            work_stealing: false,
            per_table_rate_limit: Some(10_000),
            shutdown_timeout: Duration::from_secs(10),
        }
    }
}

/// Builder for ParallelConfig.
pub struct ParallelConfigBuilder {
    config: ParallelConfig,
}

impl ParallelConfigBuilder {
    pub fn new() -> Self {
        Self {
            config: ParallelConfig::default(),
        }
    }

    pub fn concurrency(mut self, n: usize) -> Self {
        self.config.concurrency = n;
        self
    }

    pub fn per_table_buffer(mut self, size: usize) -> Self {
        self.config.per_table_buffer = size;
        self
    }

    pub fn output_buffer(mut self, size: usize) -> Self {
        self.config.output_buffer = size;
        self
    }

    pub fn work_stealing(mut self, enabled: bool) -> Self {
        self.config.work_stealing = enabled;
        self
    }

    pub fn per_table_rate_limit(mut self, limit: u64) -> Self {
        self.config.per_table_rate_limit = Some(limit);
        self
    }

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

    pub fn build(self) -> ParallelConfig {
        self.config
    }
}

impl Default for ParallelConfigBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Statistics for a table worker.
#[derive(Debug, Default)]
pub struct TableWorkerStats {
    pub events_processed: AtomicU64,
    pub events_dropped: AtomicU64,
    pub last_event_time: RwLock<Option<Instant>>,
    pub avg_latency_us: AtomicU64,
}

impl TableWorkerStats {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn record_event(&self, latency: Duration) {
        self.events_processed.fetch_add(1, Ordering::Relaxed);

        // Exponential moving average for latency
        let latency_us = latency.as_micros() as u64;
        let current = self.avg_latency_us.load(Ordering::Relaxed);
        let new_avg = if current == 0 {
            latency_us
        } else {
            (current * 7 + latency_us) / 8 // EMA with alpha = 0.125
        };
        self.avg_latency_us.store(new_avg, Ordering::Relaxed);
    }

    pub fn record_dropped(&self) {
        self.events_dropped.fetch_add(1, Ordering::Relaxed);
    }

    pub fn snapshot(&self) -> TableWorkerStatsSnapshot {
        TableWorkerStatsSnapshot {
            events_processed: self.events_processed.load(Ordering::Relaxed),
            events_dropped: self.events_dropped.load(Ordering::Relaxed),
            avg_latency_us: self.avg_latency_us.load(Ordering::Relaxed),
        }
    }
}

/// Snapshot of table worker statistics.
#[derive(Debug, Clone)]
pub struct TableWorkerStatsSnapshot {
    pub events_processed: u64,
    pub events_dropped: u64,
    pub avg_latency_us: u64,
}

/// Overall parallel source statistics.
#[derive(Debug, Default)]
pub struct ParallelSourceStats {
    pub total_events: AtomicU64,
    pub total_dropped: AtomicU64,
    pub active_workers: AtomicU64,
    pub tables: RwLock<HashMap<String, Arc<TableWorkerStats>>>,
}

impl ParallelSourceStats {
    pub fn new() -> Self {
        Self::default()
    }

    pub async fn register_table(&self, table: &str) -> Arc<TableWorkerStats> {
        let stats = Arc::new(TableWorkerStats::new());
        self.tables
            .write()
            .await
            .insert(table.to_string(), stats.clone());
        stats
    }

    pub fn record_event(&self) {
        self.total_events.fetch_add(1, Ordering::Relaxed);
    }

    pub fn record_dropped(&self) {
        self.total_dropped.fetch_add(1, Ordering::Relaxed);
    }

    pub fn worker_started(&self) {
        self.active_workers.fetch_add(1, Ordering::Relaxed);
    }

    pub fn worker_stopped(&self) {
        self.active_workers.fetch_sub(1, Ordering::Relaxed);
    }

    pub async fn snapshot(&self) -> ParallelSourceStatsSnapshot {
        let tables = self.tables.read().await;
        let table_stats: HashMap<String, TableWorkerStatsSnapshot> = tables
            .iter()
            .map(|(k, v)| (k.clone(), v.snapshot()))
            .collect();

        ParallelSourceStatsSnapshot {
            total_events: self.total_events.load(Ordering::Relaxed),
            total_dropped: self.total_dropped.load(Ordering::Relaxed),
            active_workers: self.active_workers.load(Ordering::Relaxed),
            tables: table_stats,
        }
    }
}

/// Snapshot of parallel source statistics.
#[derive(Debug, Clone)]
pub struct ParallelSourceStatsSnapshot {
    pub total_events: u64,
    pub total_dropped: u64,
    pub active_workers: u64,
    pub tables: HashMap<String, TableWorkerStatsSnapshot>,
}

impl ParallelSourceStatsSnapshot {
    /// Calculate throughput (events/sec) given duration.
    pub fn throughput(&self, duration: Duration) -> f64 {
        if duration.as_secs_f64() == 0.0 {
            return 0.0;
        }
        self.total_events as f64 / duration.as_secs_f64()
    }

    /// Calculate drop rate (0.0 - 1.0).
    pub fn drop_rate(&self) -> f64 {
        let total = self.total_events + self.total_dropped;
        if total == 0 {
            return 0.0;
        }
        self.total_dropped as f64 / total as f64
    }
}

/// A table assignment for parallel processing.
#[derive(Debug, Clone)]
pub struct TableAssignment {
    pub schema: String,
    pub table: String,
    pub priority: u32,
}

impl TableAssignment {
    pub fn new(schema: &str, table: &str) -> Self {
        Self {
            schema: schema.to_string(),
            table: table.to_string(),
            priority: 0,
        }
    }

    pub fn with_priority(mut self, priority: u32) -> Self {
        self.priority = priority;
        self
    }

    pub fn full_name(&self) -> String {
        format!("{}.{}", self.schema, self.table)
    }
}

/// Event producer trait for table-specific CDC sources.
#[async_trait::async_trait]
pub trait TableEventProducer: Send + Sync {
    /// Start producing events for a table.
    async fn start(&mut self, table: &TableAssignment) -> Result<()>;

    /// Get next event (None = no more events).
    async fn next_event(&mut self) -> Result<Option<CdcEvent>>;

    /// Stop producing events.
    async fn stop(&mut self) -> Result<()>;
}

/// Worker handle for managing table processing.
struct TableWorker {
    table: TableAssignment,
    handle: JoinHandle<()>,
    shutdown: Arc<Notify>,
}

/// Coordinator for parallel CDC processing.
pub struct ParallelCoordinator {
    config: ParallelConfig,
    stats: Arc<ParallelSourceStats>,
    workers: RwLock<Vec<TableWorker>>,
    semaphore: Arc<Semaphore>,
    output_tx: Sender<CdcEvent>,
    output_rx: RwLock<Option<Receiver<CdcEvent>>>,
    running: AtomicBool,
    start_time: RwLock<Option<Instant>>,
}

impl ParallelCoordinator {
    /// Create a new parallel coordinator.
    pub fn new(config: ParallelConfig) -> Self {
        let (tx, rx) = mpsc::channel(config.output_buffer);
        Self {
            semaphore: Arc::new(Semaphore::new(config.concurrency)),
            stats: Arc::new(ParallelSourceStats::new()),
            workers: RwLock::new(Vec::new()),
            output_tx: tx,
            output_rx: RwLock::new(Some(rx)),
            running: AtomicBool::new(false),
            start_time: RwLock::new(None),
            config,
        }
    }

    /// Add a table for parallel processing.
    pub async fn add_table<F, P>(&self, table: TableAssignment, producer_factory: F) -> Result<()>
    where
        F: FnOnce() -> P + Send + 'static,
        P: TableEventProducer + 'static,
    {
        let permit = self
            .semaphore
            .clone()
            .acquire_owned()
            .await
            .map_err(|_| CdcError::replication("Semaphore closed"))?;

        let table_name = table.full_name();
        let table_for_worker = table.clone();
        let table_stats = self.stats.register_table(&table_name).await;
        let output_tx = self.output_tx.clone();
        let stats = self.stats.clone();
        let shutdown = Arc::new(Notify::new());
        let shutdown_clone = shutdown.clone();
        let rate_limit = self.config.per_table_rate_limit;
        let buffer_size = self.config.per_table_buffer;

        let handle = tokio::spawn(async move {
            let _permit = permit; // Hold permit until worker completes
            stats.worker_started();

            let mut producer = producer_factory();
            if let Err(e) = producer.start(&table_for_worker).await {
                error!(
                    "Failed to start producer for {}: {}",
                    table_for_worker.full_name(),
                    e
                );
                stats.worker_stopped();
                return;
            }

            let mut last_send = Instant::now();
            let mut events_this_second = 0u64;
            let mut buffer: Vec<CdcEvent> = Vec::with_capacity(buffer_size);

            loop {
                tokio::select! {
                    _ = shutdown_clone.notified() => {
                        debug!("Worker for {} received shutdown signal", table_for_worker.full_name());
                        break;
                    }
                    result = producer.next_event() => {
                        match result {
                            Ok(Some(event)) => {
                                let event_start = Instant::now();

                                // Rate limiting
                                if let Some(limit) = rate_limit {
                                    let now = Instant::now();
                                    if now.duration_since(last_send) >= Duration::from_secs(1) {
                                        last_send = now;
                                        events_this_second = 0;
                                    }
                                    if events_this_second >= limit {
                                        tokio::time::sleep(Duration::from_millis(10)).await;
                                        continue;
                                    }
                                    events_this_second += 1;
                                }

                                // Buffer events
                                buffer.push(event);

                                // Flush when buffer is full or enough time has passed
                                if buffer.len() >= buffer_size {
                                    for e in buffer.drain(..) {
                                        if output_tx.try_send(e).is_err() {
                                            table_stats.record_dropped();
                                            stats.record_dropped();
                                        } else {
                                            table_stats.record_event(event_start.elapsed());
                                            stats.record_event();
                                        }
                                    }
                                }
                            }
                            Ok(None) => {
                                debug!("Producer for {} completed", table_for_worker.full_name());
                                break;
                            }
                            Err(e) => {
                                error!("Error from producer {}: {}", table_for_worker.full_name(), e);
                                break;
                            }
                        }
                    }
                }
            }

            // Flush remaining buffered events
            for e in buffer.drain(..) {
                let _ = output_tx.try_send(e);
            }

            if let Err(e) = producer.stop().await {
                warn!(
                    "Error stopping producer for {}: {}",
                    table_for_worker.full_name(),
                    e
                );
            }

            stats.worker_stopped();
            info!("Worker for {} stopped", table_for_worker.full_name());
        });

        self.workers.write().await.push(TableWorker {
            table,
            handle,
            shutdown,
        });

        Ok(())
    }

    /// Start processing (if not already started).
    pub async fn start(&self) -> Result<()> {
        if self.running.swap(true, Ordering::SeqCst) {
            return Ok(()); // Already running
        }
        *self.start_time.write().await = Some(Instant::now());
        info!("Parallel coordinator started");
        Ok(())
    }

    /// Take the output receiver (can only be called once).
    pub async fn take_receiver(&self) -> Option<Receiver<CdcEvent>> {
        self.output_rx.write().await.take()
    }

    /// Get next event from any table.
    pub async fn next(&self) -> Option<CdcEvent> {
        if let Some(ref mut rx) = *self.output_rx.write().await {
            rx.recv().await
        } else {
            None
        }
    }

    /// Stop all workers.
    pub async fn stop(&self) -> Result<()> {
        if !self.running.swap(false, Ordering::SeqCst) {
            return Ok(()); // Already stopped
        }

        info!("Stopping parallel coordinator");

        let workers = std::mem::take(&mut *self.workers.write().await);

        // Notify all workers to shutdown
        for worker in &workers {
            worker.shutdown.notify_one();
        }

        // Wait for workers with timeout
        let deadline = Instant::now() + self.config.shutdown_timeout;
        for worker in workers {
            let remaining = deadline.saturating_duration_since(Instant::now());
            if remaining.is_zero() {
                worker.handle.abort();
                warn!(
                    "Aborted worker for {} due to timeout",
                    worker.table.full_name()
                );
            } else {
                match tokio::time::timeout(remaining, worker.handle).await {
                    Ok(Ok(())) => {
                        debug!("Worker for {} stopped gracefully", worker.table.full_name())
                    }
                    Ok(Err(e)) => warn!("Worker for {} panicked: {}", worker.table.full_name(), e),
                    Err(_) => {
                        warn!("Worker for {} timed out", worker.table.full_name());
                    }
                }
            }
        }

        info!("Parallel coordinator stopped");
        Ok(())
    }

    /// Get statistics.
    pub async fn stats(&self) -> ParallelSourceStatsSnapshot {
        self.stats.snapshot().await
    }

    /// Check if running.
    pub fn is_running(&self) -> bool {
        self.running.load(Ordering::SeqCst)
    }

    /// Get uptime.
    pub async fn uptime(&self) -> Duration {
        self.start_time
            .read()
            .await
            .map(|t| t.elapsed())
            .unwrap_or_default()
    }
}

/// Simple in-memory event producer for testing.
pub struct MemoryEventProducer {
    events: Vec<CdcEvent>,
    index: usize,
}

impl MemoryEventProducer {
    pub fn new(events: Vec<CdcEvent>) -> Self {
        Self { events, index: 0 }
    }
}

#[async_trait::async_trait]
impl TableEventProducer for MemoryEventProducer {
    async fn start(&mut self, _table: &TableAssignment) -> Result<()> {
        Ok(())
    }

    async fn next_event(&mut self) -> Result<Option<CdcEvent>> {
        if self.index < self.events.len() {
            let event = self.events[self.index].clone();
            self.index += 1;
            Ok(Some(event))
        } else {
            Ok(None)
        }
    }

    async fn stop(&mut self) -> Result<()> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::CdcOp;

    fn make_event(table: &str, id: i64) -> CdcEvent {
        CdcEvent {
            source_type: "postgres".to_string(),
            database: "testdb".to_string(),
            schema: "public".to_string(),
            table: table.to_string(),
            op: CdcOp::Insert,
            before: None,
            after: Some(serde_json::json!({ "id": id })),
            timestamp: chrono::Utc::now().timestamp(),
            transaction: None,
        }
    }

    #[test]
    fn test_config_builder() {
        let config = ParallelConfig::builder()
            .concurrency(8)
            .per_table_buffer(2000)
            .output_buffer(20000)
            .build();

        assert_eq!(config.concurrency, 8);
        assert_eq!(config.per_table_buffer, 2000);
        assert_eq!(config.output_buffer, 20000);
    }

    #[test]
    fn test_config_presets() {
        let ht = ParallelConfig::high_throughput();
        assert_eq!(ht.concurrency, 8);

        let ll = ParallelConfig::low_latency();
        assert_eq!(ll.concurrency, 2);
    }

    #[test]
    fn test_table_assignment() {
        let table = TableAssignment::new("public", "users").with_priority(10);
        assert_eq!(table.full_name(), "public.users");
        assert_eq!(table.priority, 10);
    }

    #[tokio::test]
    async fn test_parallel_coordinator_basic() {
        let config = ParallelConfig::default();
        let coordinator = ParallelCoordinator::new(config);

        let events = vec![make_event("users", 1), make_event("users", 2)];

        let table = TableAssignment::new("public", "users");
        let events_clone = events.clone();
        coordinator
            .add_table(table, move || MemoryEventProducer::new(events_clone))
            .await
            .unwrap();

        coordinator.start().await.unwrap();

        // Receive events
        let mut received = Vec::new();
        let mut rx = coordinator.take_receiver().await.unwrap();

        while let Ok(Some(event)) =
            tokio::time::timeout(Duration::from_millis(100), rx.recv()).await
        {
            received.push(event);
        }

        coordinator.stop().await.unwrap();

        assert_eq!(received.len(), 2);
    }

    #[tokio::test]
    async fn test_parallel_coordinator_multiple_tables() {
        let config = ParallelConfig::builder().concurrency(4).build();
        let coordinator = ParallelCoordinator::new(config);

        // Add multiple tables
        for i in 0..3 {
            let table_name = format!("table_{}", i);
            let events = vec![make_event(&table_name, 1), make_event(&table_name, 2)];
            let table = TableAssignment::new("public", &table_name);
            coordinator
                .add_table(table, move || MemoryEventProducer::new(events))
                .await
                .unwrap();
        }

        coordinator.start().await.unwrap();

        let mut rx = coordinator.take_receiver().await.unwrap();
        let mut count = 0;

        while let Ok(Some(_)) = tokio::time::timeout(Duration::from_millis(200), rx.recv()).await {
            count += 1;
        }

        coordinator.stop().await.unwrap();

        assert_eq!(count, 6); // 3 tables * 2 events
    }

    #[tokio::test]
    async fn test_worker_stats() {
        let stats = TableWorkerStats::new();

        stats.record_event(Duration::from_micros(100));
        stats.record_event(Duration::from_micros(200));
        stats.record_dropped();

        let snapshot = stats.snapshot();
        assert_eq!(snapshot.events_processed, 2);
        assert_eq!(snapshot.events_dropped, 1);
    }

    #[tokio::test]
    async fn test_parallel_source_stats() {
        let stats = ParallelSourceStats::new();

        stats.register_table("users").await;
        stats.record_event();
        stats.record_event();
        stats.record_dropped();

        let snapshot = stats.snapshot().await;
        assert_eq!(snapshot.total_events, 2);
        assert_eq!(snapshot.total_dropped, 1);
        assert!(snapshot.tables.contains_key("users"));
    }

    #[test]
    fn test_stats_calculations() {
        let stats = ParallelSourceStatsSnapshot {
            total_events: 1000,
            total_dropped: 100,
            active_workers: 4,
            tables: HashMap::new(),
        };

        // 1100 total, 100 dropped = ~9.09% drop rate
        assert!((stats.drop_rate() - 0.0909).abs() < 0.01);

        // 1000 events in 10 seconds = 100 events/sec
        assert!((stats.throughput(Duration::from_secs(10)) - 100.0).abs() < 0.1);
    }

    #[tokio::test]
    async fn test_memory_event_producer() {
        let events = vec![make_event("users", 1), make_event("users", 2)];
        let mut producer = MemoryEventProducer::new(events);

        let table = TableAssignment::new("public", "users");
        producer.start(&table).await.unwrap();

        let e1 = producer.next_event().await.unwrap();
        assert!(e1.is_some());

        let e2 = producer.next_event().await.unwrap();
        assert!(e2.is_some());

        let e3 = producer.next_event().await.unwrap();
        assert!(e3.is_none());

        producer.stop().await.unwrap();
    }
}