ibapi 2.11.2

A Rust implementation of the Interactive Brokers TWS API, providing a reliable and user friendly interface for TWS and IB Gateway. Designed with a focus on simplicity and performance.
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
//! Asynchronous subscription implementation

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use log::{debug, warn};
use tokio::sync::mpsc;

use super::common::{process_decode_result, DecoderContext, ProcessingResult};
use super::StreamDecoder;
use crate::messages::{OutgoingMessages, RequestMessage, ResponseMessage};
use crate::transport::{AsyncInternalSubscription, AsyncMessageBus};
use crate::Error;

// Type aliases to reduce complexity
type CancelFn = Box<dyn Fn(i32, Option<i32>, Option<&DecoderContext>) -> Result<RequestMessage, Error> + Send + Sync>;
type DecoderFn<T> = Arc<dyn Fn(&DecoderContext, &mut ResponseMessage) -> Result<T, Error> + Send + Sync>;

/// Asynchronous subscription for streaming data
pub struct Subscription<T> {
    inner: SubscriptionInner<T>,
    /// Metadata for cancellation
    request_id: Option<i32>,
    order_id: Option<i32>,
    _message_type: Option<OutgoingMessages>,
    context: DecoderContext,
    cancelled: Arc<AtomicBool>,
    stream_ended: Arc<AtomicBool>,
    message_bus: Option<Arc<dyn AsyncMessageBus>>,
    /// Cancel message generator
    cancel_fn: Option<Arc<CancelFn>>,
}

enum SubscriptionInner<T> {
    /// Subscription with decoder - receives ResponseMessage and decodes to T
    WithDecoder {
        subscription: AsyncInternalSubscription,
        decoder: DecoderFn<T>,
        context: DecoderContext,
    },
    /// Pre-decoded subscription - receives T directly
    PreDecoded { receiver: mpsc::UnboundedReceiver<Result<T, Error>> },
}

impl<T> Clone for SubscriptionInner<T> {
    fn clone(&self) -> Self {
        match self {
            SubscriptionInner::WithDecoder {
                subscription,
                decoder,
                context,
            } => SubscriptionInner::WithDecoder {
                subscription: subscription.clone(),
                decoder: decoder.clone(),
                context: context.clone(),
            },
            SubscriptionInner::PreDecoded { .. } => {
                // Can't clone mpsc receivers
                panic!("Cannot clone pre-decoded subscriptions");
            }
        }
    }
}

impl<T> Clone for Subscription<T> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            request_id: self.request_id,
            order_id: self.order_id,
            _message_type: self._message_type,
            context: self.context.clone(),
            cancelled: self.cancelled.clone(),
            stream_ended: self.stream_ended.clone(),
            message_bus: self.message_bus.clone(),
            cancel_fn: self.cancel_fn.clone(),
        }
    }
}

impl<T> Subscription<T> {
    /// Create a subscription from an internal subscription and a decoder
    #[allow(clippy::too_many_arguments)]
    pub fn with_decoder<D>(
        internal: AsyncInternalSubscription,
        message_bus: Arc<dyn AsyncMessageBus>,
        decoder: D,
        request_id: Option<i32>,
        order_id: Option<i32>,
        message_type: Option<OutgoingMessages>,
        context: DecoderContext,
    ) -> Self
    where
        D: Fn(&DecoderContext, &mut ResponseMessage) -> Result<T, Error> + Send + Sync + 'static,
    {
        Self {
            inner: SubscriptionInner::WithDecoder {
                subscription: internal,
                decoder: Arc::new(decoder),
                context: context.clone(),
            },
            request_id,
            order_id,
            _message_type: message_type,
            context,
            cancelled: Arc::new(AtomicBool::new(false)),
            stream_ended: Arc::new(AtomicBool::new(false)),
            message_bus: Some(message_bus),
            cancel_fn: None,
        }
    }

    /// Create a subscription from an internal subscription with a decoder function
    #[allow(clippy::too_many_arguments)]
    pub fn new_with_decoder<F>(
        internal: AsyncInternalSubscription,
        message_bus: Arc<dyn AsyncMessageBus>,
        decoder: F,
        request_id: Option<i32>,
        order_id: Option<i32>,
        message_type: Option<OutgoingMessages>,
        context: DecoderContext,
    ) -> Self
    where
        F: Fn(&DecoderContext, &mut ResponseMessage) -> Result<T, Error> + Send + Sync + 'static,
    {
        Self::with_decoder(internal, message_bus, decoder, request_id, order_id, message_type, context)
    }

    /// Create a subscription from components and a decoder (alias for with_decoder)
    #[allow(clippy::too_many_arguments)]
    pub fn with_decoder_components<D>(
        internal: AsyncInternalSubscription,
        message_bus: Arc<dyn AsyncMessageBus>,
        decoder: D,
        request_id: Option<i32>,
        order_id: Option<i32>,
        message_type: Option<OutgoingMessages>,
        context: DecoderContext,
    ) -> Self
    where
        D: Fn(&DecoderContext, &mut ResponseMessage) -> Result<T, Error> + Send + Sync + 'static,
    {
        Self::with_decoder(internal, message_bus, decoder, request_id, order_id, message_type, context)
    }

    /// Create a subscription from an internal subscription using the DataStream decoder
    pub(crate) fn new_from_internal<D>(
        internal: AsyncInternalSubscription,
        message_bus: Arc<dyn AsyncMessageBus>,
        request_id: Option<i32>,
        order_id: Option<i32>,
        message_type: Option<OutgoingMessages>,
        context: DecoderContext,
    ) -> Self
    where
        D: StreamDecoder<T> + 'static,
        T: 'static,
    {
        let mut sub = Self::with_decoder_components(internal, message_bus, D::decode, request_id, order_id, message_type, context);
        // Store the cancel function
        sub.cancel_fn = Some(Arc::new(Box::new(D::cancel_message)));
        sub
    }

    /// Create a subscription from internal subscription without explicit metadata
    pub(crate) fn new_from_internal_simple<D>(
        internal: AsyncInternalSubscription,
        context: DecoderContext,
        message_bus: Arc<dyn AsyncMessageBus>,
    ) -> Self
    where
        D: StreamDecoder<T> + 'static,
        T: 'static,
    {
        // The AsyncInternalSubscription already has cleanup logic, so we don't need cancel metadata
        Self::new_from_internal::<D>(internal, message_bus, None, None, None, context)
    }

    /// Create subscription from existing receiver (for backward compatibility)
    pub fn new(receiver: mpsc::UnboundedReceiver<Result<T, Error>>) -> Self {
        // This creates a subscription that expects pre-decoded messages
        // Used for compatibility with existing code that manually decodes
        Self {
            inner: SubscriptionInner::PreDecoded { receiver },
            request_id: None,
            order_id: None,
            _message_type: None,
            context: DecoderContext::default(),
            cancelled: Arc::new(AtomicBool::new(false)),
            stream_ended: Arc::new(AtomicBool::new(false)),
            message_bus: None,
            cancel_fn: None,
        }
    }

    /// Get the next value from the subscription
    pub async fn next(&mut self) -> Option<Result<T, Error>>
    where
        T: 'static,
    {
        if self.stream_ended.load(Ordering::Relaxed) {
            return None;
        }

        match &mut self.inner {
            SubscriptionInner::WithDecoder {
                subscription,
                decoder,
                context,
            } => loop {
                match subscription.next().await {
                    Some(Ok(mut message)) => {
                        let result = decoder(context, &mut message);
                        match process_decode_result(result) {
                            ProcessingResult::Success(val) => return Some(Ok(val)),
                            ProcessingResult::EndOfStream => {
                                self.stream_ended.store(true, Ordering::Relaxed);
                                return None;
                            }
                            ProcessingResult::Skip => {
                                log::trace!("skipping unexpected message on shared channel");
                                continue;
                            }
                            ProcessingResult::Error(err) => return Some(Err(err)),
                        }
                    }
                    Some(Err(e)) => return Some(Err(e)),
                    None => return None,
                }
            },
            SubscriptionInner::PreDecoded { receiver } => receiver.recv().await,
        }
    }

    /// Get the request ID associated with this subscription
    pub fn request_id(&self) -> Option<i32> {
        self.request_id
    }
}

impl<T> Subscription<T> {
    /// Cancel the subscription
    pub async fn cancel(&self) {
        if self.cancelled.load(Ordering::Relaxed) {
            return;
        }

        self.cancelled.store(true, Ordering::Relaxed);

        if let (Some(message_bus), Some(cancel_fn)) = (&self.message_bus, &self.cancel_fn) {
            let id = self.request_id.or(self.order_id);
            if let Ok(message) = cancel_fn(self.context.server_version, id, Some(&self.context)) {
                if let Err(e) = message_bus.send_message(message).await {
                    warn!("error sending cancel message: {e}")
                }
            }
        }

        // The AsyncInternalSubscription's Drop will handle cleanup
    }
}

impl<T> Drop for Subscription<T> {
    fn drop(&mut self) {
        debug!("dropping async subscription");

        // Check if already cancelled
        if self.cancelled.load(Ordering::Relaxed) {
            return;
        }

        self.cancelled.store(true, Ordering::Relaxed);

        // Try to send cancel message if we have the necessary components
        if let (Some(message_bus), Some(cancel_fn)) = (&self.message_bus, &self.cancel_fn) {
            let message_bus = message_bus.clone();
            let id = self.request_id.or(self.order_id);
            let context = self.context.clone();

            // Clone the cancel function for use in the spawned task
            if let Ok(message) = cancel_fn(context.server_version, id, Some(&context)) {
                // Spawn a task to send the cancel message since drop can't be async
                tokio::spawn(async move {
                    if let Err(e) = message_bus.send_message(message).await {
                        warn!("error sending cancel message in drop: {e}");
                    }
                });
            }
        }

        // The AsyncInternalSubscription's Drop will handle channel cleanup
    }
}

// Note: Stream trait implementation removed because tokio's broadcast::Receiver
// doesn't provide poll_recv. Users should use the async next() method instead.
// If Stream is needed, users can convert using futures::stream::unfold.

#[cfg(all(test, feature = "async"))]
mod tests {
    use super::*;
    use crate::market_data::realtime::Bar;
    use crate::messages::OutgoingMessages;
    use crate::stubs::MessageBusStub;
    use std::sync::RwLock;
    use time::OffsetDateTime;
    use tokio::sync::{broadcast, mpsc};

    #[tokio::test]
    async fn test_subscription_with_decoder() {
        let message_bus = Arc::new(MessageBusStub {
            request_messages: RwLock::new(vec![]),
            response_messages: vec!["1|9000|20241231 12:00:00|100.5|101.0|100.0|100.25|1000|100.2|5|0".to_string()],
        });

        let (tx, rx) = broadcast::channel(100);
        let internal = AsyncInternalSubscription::new(rx.resubscribe());

        let subscription: Subscription<Bar> = Subscription::with_decoder(
            internal,
            message_bus,
            |_context, _msg| {
                let bar = Bar {
                    date: OffsetDateTime::now_utc(),
                    open: 100.5,
                    high: 101.0,
                    low: 100.0,
                    close: 100.25,
                    volume: 1000.0,
                    wap: 100.2,
                    count: 5,
                };
                Ok(bar)
            },
            Some(9000),
            None,
            Some(OutgoingMessages::RequestRealTimeBars),
            DecoderContext::default(),
        );

        // Send a test message
        let msg = ResponseMessage::from("1\09000\020241231 12:00:00\0100.5\0101.0\0100.0\0100.25\01000\0100.2\05\00");
        tx.send(msg).unwrap();

        // Test that we can receive the decoded message
        let mut sub = subscription;
        let result = sub.next().await;
        assert!(result.is_some());
        let bar = result.unwrap().unwrap();
        assert_eq!(bar.open, 100.5);
        assert_eq!(bar.high, 101.0);
    }

    #[tokio::test]
    async fn test_subscription_new_with_decoder() {
        let message_bus = Arc::new(MessageBusStub::default());
        let (_tx, rx) = broadcast::channel(100);
        let internal = AsyncInternalSubscription::new(rx);

        let subscription: Subscription<String> = Subscription::new_with_decoder(
            internal,
            message_bus,
            |_context, _msg| Ok("decoded".to_string()),
            Some(1),
            None,
            Some(OutgoingMessages::RequestMarketData),
            DecoderContext::default(),
        );

        assert_eq!(subscription.request_id, Some(1));
        assert_eq!(subscription._message_type, Some(OutgoingMessages::RequestMarketData));
    }

    #[tokio::test]
    async fn test_subscription_with_decoder_components() {
        let message_bus = Arc::new(MessageBusStub::default());
        let (_tx, rx) = broadcast::channel(100);
        let internal = AsyncInternalSubscription::new(rx);

        let subscription: Subscription<i32> = Subscription::with_decoder_components(
            internal,
            message_bus,
            |_context, _msg| Ok(42),
            Some(100),
            Some(200),
            Some(OutgoingMessages::RequestPositions),
            DecoderContext::default(),
        );

        assert_eq!(subscription.request_id, Some(100));
        assert_eq!(subscription.order_id, Some(200));
    }

    #[tokio::test]
    async fn test_subscription_new_from_receiver() {
        let (tx, rx) = mpsc::unbounded_channel();

        let mut subscription = Subscription::new(rx);

        // Send test data
        tx.send(Ok("test".to_string())).unwrap();

        let result = subscription.next().await;
        assert!(result.is_some());
        assert_eq!(result.unwrap().unwrap(), "test");
    }

    #[tokio::test]
    async fn test_subscription_next_with_error() {
        let message_bus = Arc::new(MessageBusStub::default());
        let (tx, rx) = broadcast::channel(100);
        let internal = AsyncInternalSubscription::new(rx);

        let mut subscription: Subscription<String> = Subscription::with_decoder(
            internal,
            message_bus,
            |_context, _msg| Err(Error::Simple("decode error".into())),
            None,
            None,
            None,
            DecoderContext::default(),
        );

        // Send a message that will trigger the error
        let msg = ResponseMessage::from("test\0");
        tx.send(msg).unwrap();

        let result = subscription.next().await;
        assert!(result.is_some());
        assert!(result.unwrap().is_err());
    }

    #[tokio::test]
    async fn test_subscription_next_end_of_stream() {
        let message_bus = Arc::new(MessageBusStub::default());
        let (tx, rx) = broadcast::channel(100);
        let internal = AsyncInternalSubscription::new(rx);

        let mut subscription: Subscription<String> = Subscription::with_decoder(
            internal,
            message_bus,
            |_context, _msg| Err(Error::EndOfStream),
            None,
            None,
            None,
            DecoderContext::default(),
        );

        // Send a message that will trigger end of stream
        let msg = ResponseMessage::from("test\0");
        tx.send(msg).unwrap();

        let result = subscription.next().await;
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_subscription_no_retries_after_end_of_stream() {
        let message_bus = Arc::new(MessageBusStub::default());
        let (tx, rx) = broadcast::channel(100);
        let internal = AsyncInternalSubscription::new(rx);

        let call_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let call_count_clone = call_count.clone();

        let mut subscription: Subscription<String> = Subscription::with_decoder(
            internal,
            message_bus,
            move |_context, _msg| {
                let n = call_count_clone.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                if n == 0 {
                    Err(Error::EndOfStream)
                } else {
                    Err(Error::UnexpectedResponse(ResponseMessage::from("stray\0")))
                }
            },
            None,
            None,
            None,
            DecoderContext::default(),
        );

        // First message triggers EndOfStream
        tx.send(ResponseMessage::from("end\0")).unwrap();
        let result = subscription.next().await;
        assert!(result.is_none());

        // Send stray messages after stream ended
        tx.send(ResponseMessage::from("stray1\0")).unwrap();
        tx.send(ResponseMessage::from("stray2\0")).unwrap();

        // Subsequent calls should return None immediately without invoking decoder
        let result = subscription.next().await;
        assert!(result.is_none());

        // Decoder should have been called only once (for the EndOfStream message)
        assert_eq!(call_count.load(std::sync::atomic::Ordering::Relaxed), 1);
    }

    #[tokio::test]
    async fn test_subscription_skips_unexpected_messages_without_retry_limit() {
        let message_bus = Arc::new(MessageBusStub::default());
        let (tx, rx) = broadcast::channel(100);
        let internal = AsyncInternalSubscription::new(rx);

        let call_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let call_count_clone = call_count.clone();

        // Decoder: returns UnexpectedResponse for the first 20 messages (more than
        // MAX_DECODE_RETRIES=10), then returns a success value. If UnexpectedResponse
        // counted toward the retry limit, the subscription would give up after 10.
        let mut subscription: Subscription<String> = Subscription::with_decoder(
            internal,
            message_bus,
            move |_context, _msg| {
                let n = call_count_clone.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                if n < 20 {
                    Err(Error::UnexpectedResponse(ResponseMessage::from("stray\0")))
                } else {
                    Ok("success".to_string())
                }
            },
            None,
            None,
            None,
            DecoderContext::default(),
        );

        // Send 21 messages — 20 will be "unexpected" (skipped), 1 will succeed
        for _ in 0..21 {
            tx.send(ResponseMessage::from("msg\0")).unwrap();
        }

        let result = subscription.next().await;
        assert!(
            result.is_some(),
            "subscription should not have stopped after skipping unexpected messages"
        );
        assert_eq!(result.unwrap().unwrap(), "success");
        // All 21 messages should have been processed (20 skipped + 1 success)
        assert_eq!(call_count.load(std::sync::atomic::Ordering::Relaxed), 21);
    }

    #[tokio::test]
    async fn test_subscription_cancel() {
        let message_bus = Arc::new(MessageBusStub::default());
        let (_tx, rx) = broadcast::channel(100);
        let internal = AsyncInternalSubscription::new(rx);

        // Mock cancel function
        let cancel_fn: CancelFn = Box::new(|_version, _id, _ctx| {
            let mut msg = RequestMessage::new();
            msg.push_field(&OutgoingMessages::CancelMarketData);
            Ok(msg)
        });

        let mut subscription: Subscription<String> = Subscription::with_decoder(
            internal,
            message_bus.clone(),
            |_context, _msg| Ok("test".to_string()),
            Some(123),
            None,
            Some(OutgoingMessages::RequestMarketData),
            DecoderContext::default(),
        );
        subscription.cancel_fn = Some(Arc::new(cancel_fn));

        // Cancel the subscription
        subscription.cancel().await;

        // Verify cancelled flag is set
        assert!(subscription.cancelled.load(Ordering::Relaxed));

        // Cancel again should be a no-op
        subscription.cancel().await;
    }

    #[tokio::test]
    async fn test_subscription_clone() {
        let message_bus = Arc::new(MessageBusStub::default());
        let (_tx, rx) = broadcast::channel(100);
        let internal = AsyncInternalSubscription::new(rx);

        let subscription: Subscription<String> = Subscription::with_decoder(
            internal,
            message_bus,
            |_context, _msg| Ok("test".to_string()),
            Some(456),
            Some(789),
            Some(OutgoingMessages::RequestPositions),
            DecoderContext::default()
                .with_smart_depth(true)
                .with_request_type(OutgoingMessages::RequestPositions),
        );

        let cloned = subscription.clone();
        assert_eq!(cloned.request_id, Some(456));
        assert_eq!(cloned.order_id, Some(789));
        assert_eq!(cloned._message_type, Some(OutgoingMessages::RequestPositions));
        assert!(cloned.context.is_smart_depth);
    }

    #[tokio::test]
    async fn test_subscription_drop_with_cancel() {
        let message_bus = Arc::new(MessageBusStub::default());
        let (_tx, rx) = broadcast::channel(100);
        let internal = AsyncInternalSubscription::new(rx);

        // Mock cancel function
        let cancel_fn: CancelFn = Box::new(|_version, _id, _ctx| {
            let mut msg = RequestMessage::new();
            msg.push_field(&OutgoingMessages::CancelMarketData);
            Ok(msg)
        });

        {
            let mut subscription: Subscription<String> = Subscription::with_decoder(
                internal,
                message_bus.clone(),
                |_context, _msg| Ok("test".to_string()),
                Some(999),
                None,
                Some(OutgoingMessages::RequestMarketData),
                DecoderContext::default(),
            );
            subscription.cancel_fn = Some(Arc::new(cancel_fn));
            // Subscription will be dropped here and should send cancel message
        }

        // Give async task time to execute
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
    }

    #[tokio::test]
    #[should_panic(expected = "Cannot clone pre-decoded subscriptions")]
    async fn test_subscription_inner_clone_panic() {
        let (_tx, rx) = mpsc::unbounded_channel::<Result<String, Error>>();
        let subscription = Subscription::new(rx);

        // This should panic because PreDecoded subscriptions can't be cloned
        let _ = subscription.inner.clone();
    }

    #[tokio::test]
    async fn test_subscription_with_context() {
        let message_bus = Arc::new(MessageBusStub::default());
        let (_tx, rx) = broadcast::channel(100);
        let internal = AsyncInternalSubscription::new(rx);

        let context = DecoderContext::default()
            .with_smart_depth(true)
            .with_request_type(OutgoingMessages::RequestMarketDepth);

        let subscription: Subscription<String> = Subscription::with_decoder(
            internal,
            message_bus,
            |_context, _msg| Ok("test".to_string()),
            None,
            None,
            None,
            context.clone(),
        );

        assert_eq!(subscription.context, context);
    }

    #[tokio::test]
    async fn test_subscription_new_from_internal_simple() {
        // Define a simple decoder type
        struct TestDecoder;

        impl StreamDecoder<String> for TestDecoder {
            fn decode(_context: &DecoderContext, _msg: &mut ResponseMessage) -> Result<String, Error> {
                Ok("decoded".to_string())
            }

            fn cancel_message(_server_version: i32, _id: Option<i32>, _context: Option<&DecoderContext>) -> Result<RequestMessage, Error> {
                let mut msg = RequestMessage::new();
                msg.push_field(&OutgoingMessages::CancelMarketData);
                Ok(msg)
            }
        }

        let message_bus = Arc::new(MessageBusStub::default());
        let (_tx, rx) = broadcast::channel(100);
        let internal = AsyncInternalSubscription::new(rx);

        let subscription: Subscription<String> =
            Subscription::new_from_internal_simple::<TestDecoder>(internal, DecoderContext::default(), message_bus);

        assert!(subscription.cancel_fn.is_some());
    }
}