opendeviationbar-streaming 13.70.3

Real-time streaming engine for open deviation bar processing
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
use futures::Stream;
/// Production-ready streaming architecture with bounded memory and backpressure
/// # FILE-SIZE-OK
///
/// This module implements true infinite streaming capabilities addressing critical failures:
/// - Eliminates Vec<OpenDeviationBar> accumulation (unbounded memory growth)
/// - Implements proper backpressure with bounded channels
/// - Provides circuit breaker resilience patterns
/// - Maintains temporal integrity for financial data
use opendeviationbar_core::processor::ExportOpenDeviationBarProcessor;
use opendeviationbar_core::{Tick, OpenDeviationBar};
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::task::{Context, Poll};
use tokio::sync::mpsc;
use tokio::time::{Duration, Instant};

/// Configuration for production streaming
#[derive(Debug, Clone)]
pub struct StreamingProcessorConfig {
    /// Channel capacity for trade input
    pub trade_channel_capacity: usize,
    /// Channel capacity for completed bars
    pub bar_channel_capacity: usize,
    /// Memory usage threshold in bytes
    pub memory_threshold_bytes: usize,
    /// Backpressure timeout
    pub backpressure_timeout: Duration,
    /// Circuit breaker error rate threshold (0.0-1.0)
    pub circuit_breaker_threshold: f64,
    /// Circuit breaker timeout before retry
    pub circuit_breaker_timeout: Duration,
}

impl StreamingProcessorConfig {
    /// Get bar channel capacity from environment or use default (10K)
    /// Issue #96 Task #6: OPENDEVIATIONBAR_MAX_PENDING_BARS env var support
    fn get_bar_channel_capacity() -> usize {
        std::env::var("OPENDEVIATIONBAR_MAX_PENDING_BARS")
            .ok()
            .and_then(|v| v.parse::<usize>().ok())
            .unwrap_or(10_000)
    }
}

impl Default for StreamingProcessorConfig {
    fn default() -> Self {
        Self {
            trade_channel_capacity: 5_000, // Based on consensus analysis
            bar_channel_capacity: StreamingProcessorConfig::get_bar_channel_capacity(), // Issue #96: 10K backpressure bound
            memory_threshold_bytes: 100_000_000, // 100MB limit
            backpressure_timeout: Duration::from_millis(100),
            circuit_breaker_threshold: 0.5, // 50% error rate
            circuit_breaker_timeout: Duration::from_secs(30),
        }
    }
}

/// Production streaming processor with bounded memory
pub struct StreamingProcessor {
    /// Open deviation bar processor (single instance, no accumulation)
    processor: ExportOpenDeviationBarProcessor,

    /// Threshold in decimal basis points for recreating processor
    _threshold_decimal_bps: u32,

    /// Bounded channel for incoming trades
    trade_sender: Option<mpsc::Sender<Tick>>,
    trade_receiver: mpsc::Receiver<Tick>,

    /// Bounded channel for outgoing bars
    bar_sender: mpsc::Sender<OpenDeviationBar>,
    bar_receiver: Option<mpsc::Receiver<OpenDeviationBar>>,

    /// Configuration
    config: StreamingProcessorConfig,

    /// Metrics
    metrics: Arc<StreamingMetrics>,

    /// Circuit breaker state
    circuit_breaker: CircuitBreaker,
}

/// Circuit breaker implementation
#[derive(Debug)]
struct CircuitBreaker {
    state: CircuitBreakerState,
    failure_count: u64,
    success_count: u64,
    last_failure_time: Option<Instant>,
    threshold: f64,
    timeout: Duration,
}

#[derive(Debug, PartialEq)]
enum CircuitBreakerState {
    Closed,
    Open,
    HalfOpen,
}

/// Streaming metrics for observability
/// Issue #96 Task #6: Extended with queue depth and block time tracking
#[derive(Debug, Default)]
pub struct StreamingMetrics {
    pub trades_processed: AtomicU64,
    pub bars_generated: AtomicU64,
    pub errors_total: AtomicU64,
    pub backpressure_events: AtomicU64,
    pub circuit_breaker_trips: AtomicU64,
    pub memory_usage_bytes: AtomicU64,
    pub max_queue_depth: AtomicU64, // Issue #96 Task #6: Max observed queue depth
    pub total_block_time_ms: AtomicU64, // Issue #96 Task #6: Accumulated block time
}

impl StreamingProcessor {
    /// Create new production streaming processor
    pub fn new(
        threshold_decimal_bps: u32,
    ) -> Result<Self, opendeviationbar_core::processor::ProcessingError> {
        Self::with_config(threshold_decimal_bps, StreamingProcessorConfig::default())
    }

    /// Create with custom configuration
    pub fn with_config(
        threshold_decimal_bps: u32,
        config: StreamingProcessorConfig,
    ) -> Result<Self, opendeviationbar_core::processor::ProcessingError> {
        let (trade_sender, trade_receiver) = mpsc::channel(config.trade_channel_capacity);
        let (bar_sender, bar_receiver) = mpsc::channel(config.bar_channel_capacity);

        let circuit_breaker_threshold = config.circuit_breaker_threshold;
        let circuit_breaker_timeout = config.circuit_breaker_timeout;

        Ok(Self {
            processor: ExportOpenDeviationBarProcessor::new(threshold_decimal_bps)?,
            _threshold_decimal_bps: threshold_decimal_bps,
            trade_sender: Some(trade_sender),
            trade_receiver,
            bar_sender,
            bar_receiver: Some(bar_receiver),
            config,
            metrics: Arc::new(StreamingMetrics::default()),
            circuit_breaker: CircuitBreaker::new(
                circuit_breaker_threshold,
                circuit_breaker_timeout,
            ),
        })
    }

    /// Get trade sender for external components
    pub fn trade_sender(&mut self) -> Option<mpsc::Sender<Tick>> {
        self.trade_sender.take()
    }

    /// Get bar receiver for external components
    pub fn bar_receiver(&mut self) -> Option<mpsc::Receiver<OpenDeviationBar>> {
        self.bar_receiver.take()
    }

    /// Start processing loop (bounded memory, infinite capability)
    pub async fn start_processing(&mut self) -> Result<(), StreamingError> {
        loop {
            // Check circuit breaker state
            if !self.circuit_breaker.can_process() {
                tokio::time::sleep(Duration::from_millis(100)).await;
                continue;
            }

            // Receive trade with timeout (prevents blocking forever)
            let trade = match tokio::time::timeout(
                self.config.backpressure_timeout,
                self.trade_receiver.recv(),
            )
            .await
            {
                Ok(Some(trade)) => trade,
                Ok(None) => {
                    // Channel closed - send final incomplete bar if exists
                    if let Some(final_bar) = self.processor.get_incomplete_bar()
                        && let Err(e) = self.send_bar_with_backpressure(final_bar).await
                    {
                        println!("Failed to send final incomplete bar: {:?}", e);
                    }
                    break;
                }
                Err(_) => continue, // Timeout, check circuit breaker again
            };

            // Process single trade (use borrowed reference per Issue #96 Task #78)
            match self.process_single_trade(&trade).await {
                Ok(bar_opt) => {
                    self.circuit_breaker.record_success();

                    // If bar completed, send with backpressure handling
                    if let Some(bar) = bar_opt
                        && let Err(e) = self.send_bar_with_backpressure(bar).await
                    {
                        println!("Failed to send bar: {:?}", e);
                        self.circuit_breaker.record_failure();
                    }
                }
                Err(e) => {
                    println!("Trade processing error: {:?}", e);
                    self.circuit_breaker.record_failure();
                    self.metrics.errors_total.fetch_add(1, Ordering::Relaxed);
                }
            }
        }

        Ok(())
    }

    /// Process single trade - extracts completed bars without accumulation
    // Issue #96 Task #78: Accept borrowed Tick reference
    async fn process_single_trade(
        &mut self,
        trade: &Tick,
    ) -> Result<Option<OpenDeviationBar>, StreamingError> {
        // Update metrics
        self.metrics
            .trades_processed
            .fetch_add(1, Ordering::Relaxed);

        // Process trade using existing algorithm (single trade at a time)
        self.processor
            .process_trades_continuously(std::slice::from_ref(trade));

        // Extract completed bars immediately (prevents accumulation)
        let mut completed_bars = self.processor.get_all_completed_bars();

        if !completed_bars.is_empty() {
            // Bounded memory: only return first completed bar
            // Additional bars would be rare edge cases but must be handled
            let completed_bar = completed_bars.remove(0);

            // Handle rare case of multiple completions
            if !completed_bars.is_empty() {
                println!(
                    "Warning: {} additional bars completed, dropping for bounded memory",
                    completed_bars.len()
                );
                self.metrics
                    .backpressure_events
                    .fetch_add(completed_bars.len() as u64, Ordering::Relaxed);
            }

            self.metrics.bars_generated.fetch_add(1, Ordering::Relaxed);
            Ok(Some(completed_bar))
        } else {
            Ok(None)
        }
    }

    /// Send bar with backpressure handling
    async fn send_bar_with_backpressure(
        &self,
        bar: OpenDeviationBar,
    ) -> Result<(), StreamingError> {
        // Use try_send for immediate check, then send for blocking
        match self.bar_sender.try_send(bar.clone()) {
            Ok(()) => Ok(()),
            Err(mpsc::error::TrySendError::Full(_)) => {
                // Apply backpressure - channel is full
                println!("Bar channel full, applying backpressure");
                self.metrics
                    .backpressure_events
                    .fetch_add(1, Ordering::Relaxed);

                // Wait for capacity with blocking send
                self.bar_sender
                    .send(bar)
                    .await
                    .map_err(|_| StreamingError::ChannelClosed)
            }
            Err(mpsc::error::TrySendError::Closed(_)) => Err(StreamingError::ChannelClosed),
        }
    }

    /// Get current metrics
    pub fn metrics(&self) -> &StreamingMetrics {
        &self.metrics
    }

    /// Extract final incomplete bar when stream ends (for algorithmic consistency)
    pub fn get_final_incomplete_bar(&mut self) -> Option<OpenDeviationBar> {
        self.processor.get_incomplete_bar()
    }

    /// Check memory usage against threshold
    pub fn check_memory_usage(&self) -> bool {
        let current_usage = self.metrics.memory_usage_bytes.load(Ordering::Relaxed);
        current_usage < self.config.memory_threshold_bytes as u64
    }
}

impl CircuitBreaker {
    fn new(threshold: f64, timeout: Duration) -> Self {
        Self {
            state: CircuitBreakerState::Closed,
            failure_count: 0,
            success_count: 0,
            last_failure_time: None,
            threshold,
            timeout,
        }
    }

    fn can_process(&mut self) -> bool {
        match self.state {
            CircuitBreakerState::Closed => true,
            CircuitBreakerState::Open => {
                if let Some(last_failure) = self.last_failure_time {
                    if last_failure.elapsed() >= self.timeout {
                        self.state = CircuitBreakerState::HalfOpen;
                        true
                    } else {
                        false
                    }
                } else {
                    true
                }
            }
            CircuitBreakerState::HalfOpen => true,
        }
    }

    fn record_success(&mut self) {
        self.success_count += 1;

        if self.state == CircuitBreakerState::HalfOpen {
            // Successful request in half-open, close circuit
            self.state = CircuitBreakerState::Closed;
            self.failure_count = 0;
        }
    }

    fn record_failure(&mut self) {
        self.failure_count += 1;
        self.last_failure_time = Some(Instant::now());

        let total_requests = self.failure_count + self.success_count;
        if total_requests >= 10 {
            // Minimum sample size
            let failure_rate = self.failure_count as f64 / total_requests as f64;

            if failure_rate >= self.threshold {
                self.state = CircuitBreakerState::Open;
            }
        }
    }
}

/// Stream implementation for open deviation bars (true streaming)
pub struct OpenDeviationBarStream {
    receiver: mpsc::Receiver<OpenDeviationBar>,
}

impl OpenDeviationBarStream {
    pub fn new(receiver: mpsc::Receiver<OpenDeviationBar>) -> Self {
        Self { receiver }
    }
}

impl Stream for OpenDeviationBarStream {
    type Item = Result<OpenDeviationBar, StreamingError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match self.receiver.poll_recv(cx) {
            Poll::Ready(Some(bar)) => Poll::Ready(Some(Ok(bar))),
            Poll::Ready(None) => Poll::Ready(None),
            Poll::Pending => Poll::Pending,
        }
    }
}

/// Streaming errors
#[derive(Debug, thiserror::Error)]
pub enum StreamingError {
    #[error("Channel closed")]
    ChannelClosed,

    #[error("Backpressure timeout")]
    BackpressureTimeout,

    #[error("Circuit breaker open")]
    CircuitBreakerOpen,

    #[error("Memory threshold exceeded")]
    MemoryThresholdExceeded,

    #[error("Processing error: {0}")]
    ProcessingError(String),
}

impl StreamingMetrics {
    /// Get metrics summary
    pub fn summary(&self) -> MetricsSummary {
        MetricsSummary {
            trades_processed: self.trades_processed.load(Ordering::Relaxed),
            bars_generated: self.bars_generated.load(Ordering::Relaxed),
            errors_total: self.errors_total.load(Ordering::Relaxed),
            backpressure_events: self.backpressure_events.load(Ordering::Relaxed),
            circuit_breaker_trips: self.circuit_breaker_trips.load(Ordering::Relaxed),
            memory_usage_bytes: self.memory_usage_bytes.load(Ordering::Relaxed),
        }
    }
}

/// Metrics snapshot
#[derive(Debug, Clone)]
pub struct MetricsSummary {
    pub trades_processed: u64,
    pub bars_generated: u64,
    pub errors_total: u64,
    pub backpressure_events: u64,
    pub circuit_breaker_trips: u64,
    pub memory_usage_bytes: u64,
}

impl MetricsSummary {
    /// Calculate bars per aggTrade ratio
    pub fn bars_per_aggtrade(&self) -> f64 {
        if self.trades_processed > 0 {
            self.bars_generated as f64 / self.trades_processed as f64
        } else {
            0.0
        }
    }

    /// Calculate error rate
    pub fn error_rate(&self) -> f64 {
        if self.trades_processed > 0 {
            self.errors_total as f64 / self.trades_processed as f64
        } else {
            0.0
        }
    }

    /// Format memory usage
    pub fn memory_usage_mb(&self) -> f64 {
        self.memory_usage_bytes as f64 / 1_000_000.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use opendeviationbar_core::FixedPoint;

    fn create_test_trade(id: u64, price: f64, timestamp: u64) -> Tick {
        let price_str = format!("{:.8}", price);
        Tick {
            ref_id: id as i64,
            price: FixedPoint::from_str(&price_str).unwrap(),
            volume: FixedPoint::from_str("1.0").unwrap(),
            first_sub_id: id as i64,
            last_sub_id: id as i64,
            timestamp: timestamp as i64,
            is_buyer_maker: false,
            is_best_match: None,
            best_bid: None,
            best_ask: None,
        }
    }

    #[tokio::test]
    async fn test_bounded_memory_streaming() {
        let mut processor = StreamingProcessor::new(25).unwrap(); // 0.25% threshold

        // Test that memory remains bounded
        let initial_metrics = processor.metrics().summary();

        // Send 1000 trades
        for i in 0..1000 {
            let trade = create_test_trade(i, 23000.0 + (i as f64), 1659312000000 + i);
            if let Ok(bar_opt) = processor.process_single_trade(&trade).await {
                // Verify no accumulation - at most one bar per aggTrade
                assert!(bar_opt.is_none() || bar_opt.is_some());
            }
        }

        let final_metrics = processor.metrics().summary();
        assert!(final_metrics.trades_processed >= initial_metrics.trades_processed);
        assert!(final_metrics.trades_processed <= 1000);
    }

    #[tokio::test]
    async fn test_circuit_breaker() {
        let mut circuit_breaker = CircuitBreaker::new(0.5, Duration::from_millis(100));

        // Initially closed
        assert!(circuit_breaker.can_process());

        // Record failures
        for _ in 0..20 {
            circuit_breaker.record_failure();
        }

        // Should open after 50% failure rate
        assert_eq!(circuit_breaker.state, CircuitBreakerState::Open);
        assert!(!circuit_breaker.can_process());

        // Wait for timeout
        tokio::time::sleep(Duration::from_millis(150)).await;

        // Should transition to half-open
        assert!(circuit_breaker.can_process());

        // Record success
        circuit_breaker.record_success();

        // Should close
        assert_eq!(circuit_breaker.state, CircuitBreakerState::Closed);
    }

    // === Circuit Breaker State Machine Tests ===

    #[test]
    fn test_circuit_breaker_stays_closed_below_threshold() {
        let mut cb = CircuitBreaker::new(0.5, Duration::from_secs(10));

        // 8 successes, 2 failures = 20% failure rate, below 50% threshold
        for _ in 0..8 {
            cb.record_success();
        }
        for _ in 0..2 {
            cb.record_failure();
        }

        // Should remain closed (20% < 50%)
        assert_eq!(cb.state, CircuitBreakerState::Closed);
        assert!(cb.can_process());
    }

    #[test]
    fn test_circuit_breaker_minimum_sample_size() {
        let mut cb = CircuitBreaker::new(0.5, Duration::from_secs(10));

        // 9 failures, 0 successes = 100% failure rate, but only 9 requests (< 10 minimum)
        for _ in 0..9 {
            cb.record_failure();
        }

        // Should remain closed (minimum sample size not met)
        assert_eq!(cb.state, CircuitBreakerState::Closed);
        assert!(cb.can_process());

        // 10th failure triggers open
        cb.record_failure();
        assert_eq!(cb.state, CircuitBreakerState::Open);
    }

    #[test]
    fn test_circuit_breaker_halfopen_failure_reopens() {
        let mut cb = CircuitBreaker::new(0.5, Duration::from_secs(0));

        // Trip the breaker: 10 failures opens it
        for _ in 0..10 {
            cb.record_failure();
        }
        assert_eq!(cb.state, CircuitBreakerState::Open);

        // Zero-second timeout → immediately transitions to HalfOpen on can_process
        assert!(cb.can_process());
        assert_eq!(cb.state, CircuitBreakerState::HalfOpen);

        // Record failure in HalfOpen → should re-open
        // (failure_count accumulates, total >= 10, rate >= threshold)
        cb.record_failure();
        assert_eq!(cb.state, CircuitBreakerState::Open);
    }

    #[test]
    fn test_circuit_breaker_closed_resets_failure_count() {
        let mut cb = CircuitBreaker::new(0.5, Duration::from_secs(0));

        // Trip the breaker
        for _ in 0..10 {
            cb.record_failure();
        }
        assert_eq!(cb.state, CircuitBreakerState::Open);

        // Transition to HalfOpen
        assert!(cb.can_process());
        assert_eq!(cb.state, CircuitBreakerState::HalfOpen);

        // Record success → closes and resets failure_count
        cb.record_success();
        assert_eq!(cb.state, CircuitBreakerState::Closed);
        assert_eq!(cb.failure_count, 0);
    }

    #[test]
    fn test_circuit_breaker_open_blocks_until_timeout() {
        let mut cb = CircuitBreaker::new(0.5, Duration::from_secs(3600)); // 1 hour timeout

        // Trip the breaker
        for _ in 0..10 {
            cb.record_failure();
        }

        // Should be blocked — timeout hasn't elapsed
        assert!(!cb.can_process());
        assert_eq!(cb.state, CircuitBreakerState::Open);
    }

    #[test]
    fn test_metrics_zero_trades() {
        let metrics = MetricsSummary {
            trades_processed: 0,
            bars_generated: 0,
            errors_total: 0,
            backpressure_events: 0,
            circuit_breaker_trips: 0,
            memory_usage_bytes: 0,
        };

        // Division by zero guarded
        assert_eq!(metrics.bars_per_aggtrade(), 0.0);
        assert_eq!(metrics.error_rate(), 0.0);
        assert_eq!(metrics.memory_usage_mb(), 0.0);
    }

    #[test]
    fn test_metrics_calculations() {
        let metrics = MetricsSummary {
            trades_processed: 1000,
            bars_generated: 50,
            errors_total: 5,
            backpressure_events: 2,
            circuit_breaker_trips: 1,
            memory_usage_bytes: 50_000_000,
        };

        assert_eq!(metrics.bars_per_aggtrade(), 0.05);
        assert_eq!(metrics.error_rate(), 0.005);
        assert_eq!(metrics.memory_usage_mb(), 50.0);
    }

    // === Metrics Snapshot & Take-Once Tests (Issue #96 Task #114) ===

    #[test]
    fn test_streaming_metrics_summary_snapshot() {
        let metrics = StreamingMetrics::default();
        metrics.trades_processed.store(500, Ordering::Relaxed);
        metrics.bars_generated.store(25, Ordering::Relaxed);
        metrics.errors_total.store(3, Ordering::Relaxed);
        metrics.backpressure_events.store(1, Ordering::Relaxed);
        metrics.circuit_breaker_trips.store(0, Ordering::Relaxed);
        metrics
            .memory_usage_bytes
            .store(42_000_000, Ordering::Relaxed);

        let summary = metrics.summary();

        assert_eq!(summary.trades_processed, 500);
        assert_eq!(summary.bars_generated, 25);
        assert_eq!(summary.errors_total, 3);
        assert_eq!(summary.backpressure_events, 1);
        assert_eq!(summary.circuit_breaker_trips, 0);
        assert_eq!(summary.memory_usage_bytes, 42_000_000);
    }

    #[test]
    fn test_memory_usage_mb_conversion() {
        // Exact MB boundary
        let m1 = MetricsSummary {
            trades_processed: 0,
            bars_generated: 0,
            errors_total: 0,
            backpressure_events: 0,
            circuit_breaker_trips: 0,
            memory_usage_bytes: 1_000_000,
        };
        assert_eq!(m1.memory_usage_mb(), 1.0);

        // Fractional MB
        let m2 = MetricsSummary {
            trades_processed: 0,
            bars_generated: 0,
            errors_total: 0,
            backpressure_events: 0,
            circuit_breaker_trips: 0,
            memory_usage_bytes: 1_500_000,
        };
        assert_eq!(m2.memory_usage_mb(), 1.5);

        // Large value (4 GB)
        let m3 = MetricsSummary {
            trades_processed: 0,
            bars_generated: 0,
            errors_total: 0,
            backpressure_events: 0,
            circuit_breaker_trips: 0,
            memory_usage_bytes: 4_000_000_000,
        };
        assert_eq!(m3.memory_usage_mb(), 4000.0);
    }

    #[test]
    fn test_trade_sender_take_once() {
        let mut processor = StreamingProcessor::new(25).unwrap();

        // First call returns Some
        let sender = processor.trade_sender();
        assert!(
            sender.is_some(),
            "First trade_sender() call must return Some"
        );

        // Second call returns None (already taken)
        let sender2 = processor.trade_sender();
        assert!(
            sender2.is_none(),
            "Second trade_sender() call must return None"
        );
    }

    #[test]
    fn test_bar_receiver_take_once() {
        let mut processor = StreamingProcessor::new(25).unwrap();

        // First call returns Some
        let receiver = processor.bar_receiver();
        assert!(
            receiver.is_some(),
            "First bar_receiver() call must return Some"
        );

        // Second call returns None (already taken)
        let receiver2 = processor.bar_receiver();
        assert!(
            receiver2.is_none(),
            "Second bar_receiver() call must return None"
        );
    }

    #[test]
    fn test_check_memory_usage_below_threshold() {
        let processor = StreamingProcessor::new(25).unwrap();

        // Default: memory_usage_bytes = 0, threshold = 100MB → within bounds
        assert!(
            processor.check_memory_usage(),
            "Zero memory usage should be within threshold"
        );
    }

    #[test]
    fn test_check_memory_usage_above_threshold() {
        let processor = StreamingProcessor::new(25).unwrap();

        // Simulate exceeding threshold (100MB default)
        processor
            .metrics
            .memory_usage_bytes
            .store(200_000_000, Ordering::Relaxed);
        assert!(
            !processor.check_memory_usage(),
            "200MB should exceed 100MB threshold"
        );
    }

    #[test]
    fn test_get_final_incomplete_bar_empty() {
        let mut processor = StreamingProcessor::new(25).unwrap();

        // No trades processed → no incomplete bar
        let bar = processor.get_final_incomplete_bar();
        assert!(bar.is_none(), "No incomplete bar before any trades");
    }

    #[test]
    fn test_bars_per_aggtrade_ratio() {
        let metrics = MetricsSummary {
            trades_processed: 200,
            bars_generated: 10,
            errors_total: 0,
            backpressure_events: 0,
            circuit_breaker_trips: 0,
            memory_usage_bytes: 0,
        };

        assert_eq!(metrics.bars_per_aggtrade(), 0.05);
        assert_eq!(metrics.error_rate(), 0.0);
    }
}