rusmppc 0.4.0

A Rust SMPP client.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
//! Tests in this module test the library's functionality based on the public API.
//!
//! They simulate real scenarios by creating in-memory connections between a test server and a client built using the library's public API.
//!
//! For more in depth tests, see `connection/tests.rs`.

use std::{
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    task::Poll,
    time::{Duration, Instant},
};

use futures::{SinkExt, StreamExt};
use rusmpp::{
    Command, CommandId, CommandStatus, Pdu,
    pdus::{
        AlertNotification, BindReceiverResp, BindTransceiverResp, BindTransmitterResp, SubmitSm,
        SubmitSmResp,
    },
    tokio_codec::CommandCodec,
};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_util::codec::Framed;

use crate::{ConnectionBuilder, Event, error::Error, mock::io::MockIo};

#[derive(Debug)]
pub struct Server {
    bind_delay: Duration,
    enquire_link_delay: Duration,
    response_delay: Duration,
    close_connection_delay: Duration,
}

impl Server {
    pub fn new() -> Self {
        Self {
            bind_delay: Duration::from_millis(500),
            enquire_link_delay: Duration::from_millis(500),
            response_delay: Duration::from_millis(500),
            close_connection_delay: Duration::from_secs(10),
        }
    }

    pub fn bind_delay(mut self, delay: Duration) -> Self {
        self.bind_delay = delay;
        self
    }

    pub fn enquire_link_delay(mut self, delay: Duration) -> Self {
        self.enquire_link_delay = delay;
        self
    }

    pub fn response_delay(mut self, delay: Duration) -> Self {
        self.response_delay = delay;
        self
    }

    pub fn close_connection_delay(mut self, delay: Duration) -> Self {
        self.close_connection_delay = delay;
        self
    }

    pub async fn run<S: AsyncRead + AsyncWrite + Send + Unpin + 'static>(self, stream: S) {
        let mut framed = Framed::new(stream, CommandCodec::new());

        let future = async move {
            while let Some(Ok(command)) = framed.next().await {
                let pdu: Pdu = match command.id() {
                    CommandId::EnquireLink => {
                        tokio::time::sleep(self.enquire_link_delay).await;

                        Pdu::EnquireLinkResp
                    }
                    CommandId::BindTransmitter => {
                        tokio::time::sleep(self.bind_delay).await;

                        BindTransmitterResp::default().into()
                    }
                    CommandId::BindReceiver => {
                        tokio::time::sleep(self.bind_delay).await;

                        BindReceiverResp::default().into()
                    }
                    CommandId::BindTransceiver => {
                        tokio::time::sleep(self.bind_delay).await;

                        BindTransceiverResp::default().into()
                    }
                    CommandId::SubmitSm => {
                        tokio::time::sleep(self.response_delay).await;

                        SubmitSmResp::default().into()
                    }
                    CommandId::Unbind => {
                        tokio::time::sleep(self.response_delay).await;

                        Pdu::UnbindResp
                    }
                    CommandId::GenericNack => {
                        tracing::warn!("Received GenericNack. Crashing");

                        break;
                    }
                    _ => {
                        continue;
                    }
                };

                let response = Command::builder()
                    .status(CommandStatus::EsmeRok)
                    .sequence_number(command.sequence_number())
                    .pdu(pdu);

                framed
                    .send(response)
                    .await
                    .expect("Failed to send response");
            }
        };

        let _ = tokio::time::timeout(self.close_connection_delay, future).await;
    }
}

/// A server that only issues an unbind after a delay.
#[derive(Debug)]
pub struct UnbindServer {
    delay: Duration,
}

impl UnbindServer {
    pub fn new(delay: Duration) -> Self {
        Self { delay }
    }

    pub async fn run<S: AsyncRead + AsyncWrite + Send + Unpin + 'static>(self, stream: S) {
        let mut framed = Framed::new(stream, CommandCodec::new());

        let future = async {
            while let Some(Ok(command)) = framed.next().await {
                let pdu: Pdu = match command.id() {
                    CommandId::EnquireLink => Pdu::EnquireLinkResp,
                    CommandId::BindTransmitter => BindTransmitterResp::default().into(),
                    CommandId::BindReceiver => BindReceiverResp::default().into(),
                    CommandId::BindTransceiver => BindTransceiverResp::default().into(),
                    _ => {
                        continue;
                    }
                };

                let response = Command::builder()
                    .status(CommandStatus::EsmeRok)
                    .sequence_number(command.sequence_number())
                    .pdu(pdu);

                framed
                    .send(response)
                    .await
                    .expect("Failed to send response");
            }
        };

        tokio::select! {
            _ = future => {

            },
            _ = tokio::time::sleep(self.delay) => {
                let unbind = Command::builder()
                    .status(CommandStatus::EsmeRok)
                    .sequence_number(1)
                    .pdu(Pdu::Unbind);

                framed
                    .send(unbind)
                    .await
                    .expect("Failed to send unbind response");
            }
        }
    }
}

pub fn init_tracing() {
    _ = tracing_subscriber::fmt()
        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
        .with_line_number(true)
        .with_ansi(false)
        .try_init();
}

#[tokio::test]
async fn cancel_request_future_should_remove_pending_response() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new().run(server).await;
    });

    let (client, mut events) = ConnectionBuilder::new()
        .response_timeout(Duration::from_millis(1000))
        .connected(client);

    let future = client.submit_sm(SubmitSm::default());

    tokio::select! {
        _ = tokio::time::sleep(Duration::from_millis(100)) => {
            tracing::debug!("Canceling request future");
        }
        _ = future => {}
    }

    let pending_response = client
        .pending_responses()
        .await
        .expect("Failed to get pending responses");

    assert!(
        !pending_response.contains(&1),
        "Pending response was not removed"
    );

    // The submit sm response should be sent to the event stream

    let Some(Event::Incoming(command)) = events.next().await else {
        panic!("Expected command event");
    };

    assert!(matches!(command.id(), CommandId::SubmitSmResp));
    assert_eq!(command.sequence_number(), 1);

    client.close().await.expect("Failed to close connection");

    client.closed().await;

    let _ = events.count().await;
}

#[tokio::test]
async fn request_timeout_should_remove_pending_response() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new()
            .bind_delay(Duration::from_millis(200))
            .response_delay(Duration::from_secs(1))
            .run(server)
            .await;
    });

    let (client, mut events) = ConnectionBuilder::new()
        .response_timeout(Duration::from_millis(500))
        .connected(client);

    let Error::ResponseTimeout {
        sequence_number, ..
    } = client.submit_sm(SubmitSm::default()).await.unwrap_err()
    else {
        panic!("Expected timeout error");
    };

    let pending_response = client
        .pending_responses()
        .await
        .expect("Failed to get pending responses");

    assert!(
        !pending_response.contains(&sequence_number),
        "Pending response was not removed"
    );

    // The submit sm response should be sent to the event stream

    let Some(Event::Incoming(command)) = events.next().await else {
        panic!("Expected command event");
    };

    assert!(matches!(command.id(), CommandId::SubmitSmResp));
    assert_eq!(command.sequence_number(), sequence_number);

    client.close().await.expect("Failed to close connection");

    client.closed().await;

    let _ = events.count().await;
}

#[tokio::test]
async fn no_wait_request_should_pipe_response_through_events() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new().run(server).await;
    });

    let (client, mut events) = ConnectionBuilder::new()
        .response_timeout(Duration::from_millis(1000))
        .connected(client);

    let sequence_number = client
        .no_wait()
        .submit_sm(SubmitSm::default())
        .await
        .expect("Failed to submit SM");

    // The submit sm response should be sent to the event stream

    let Some(Event::Incoming(command)) = events.next().await else {
        panic!("Expected command event");
    };

    assert!(matches!(command.id(), CommandId::SubmitSmResp));
    assert_eq!(command.sequence_number(), sequence_number);

    client.close().await.expect("Failed to close connection");

    client.closed().await;

    let _ = events.count().await;
}

#[tokio::test]
async fn drop_client_should_close_connection() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new().run(server).await;
    });

    let (client, events) = ConnectionBuilder::new().connected(client);

    drop(client);

    let _ = events.count().await;
}

#[tokio::test]
async fn drop_events_should_not_close_connection() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new().run(server).await;
    });

    let (client, _) = ConnectionBuilder::new().connected(client);

    client
        .submit_sm(SubmitSm::default())
        .await
        .expect("Failed to submit SM");

    client.close().await.expect("Failed to close connection");

    client.closed().await;
}

#[tokio::test]
async fn request_after_closing_connection_should_fail() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new().run(server).await;
    });

    let (client, events) = ConnectionBuilder::new().connected(client);

    client.close().await.expect("Failed to close connection");

    let error = client.submit_sm(SubmitSm::default()).await.unwrap_err();

    assert!(matches!(error, Error::ConnectionClosed));

    client.closed().await;

    let _ = events.count().await;
}

#[tokio::test]
async fn close_connection_twice_should_fail() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new().run(server).await;
    });

    let (client, events) = ConnectionBuilder::new().connected(client);

    client.close().await.expect("Failed to close connection");

    let error = client.close().await.unwrap_err();

    assert!(matches!(error, Error::ConnectionClosed));

    let _ = events.count().await;
}

#[tokio::test]
async fn enquire_link_timeout_idle_should_close_connection() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new()
            .enquire_link_delay(Duration::from_secs(5))
            .run(server)
            .await;
    });

    let (_client, events) = ConnectionBuilder::new()
        .enquire_link_interval(Duration::from_secs(2))
        .enquire_link_response_timeout(Duration::from_secs(1))
        .connected(client);

    let now = Instant::now();

    let _ = events.count().await;

    let elapsed = now.elapsed();

    assert!(
        elapsed.as_secs() == 3,
        "Enquire link timeout did not occur as expected"
    );
}

#[tokio::test]
async fn enquire_link_timeout_busy_sequential_should_close_connection() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new()
            .enquire_link_delay(Duration::from_secs(5))
            .response_delay(Duration::from_millis(100))
            .run(server)
            .await;
    });

    let (client, events) = ConnectionBuilder::new()
        .enquire_link_interval(Duration::from_secs(2))
        .enquire_link_response_timeout(Duration::from_secs(1))
        .connected(client);

    let now = Instant::now();

    loop {
        if let Err(Error::ConnectionClosed) = client.submit_sm(SubmitSm::default()).await {
            // Connection closed as expected
            break;
        }
    }

    let _ = events.count().await;

    let elapsed = now.elapsed();

    assert!(
        elapsed.as_secs() == 3,
        "Enquire link timeout did not occur as expected"
    );
}

#[tokio::test]
async fn enquire_link_timeout_busy_concurrent_should_close_connection() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new()
            .enquire_link_delay(Duration::from_secs(5))
            .response_delay(Duration::from_millis(100))
            .run(server)
            .await;
    });

    let (client, events) = ConnectionBuilder::new()
        .enquire_link_interval(Duration::from_secs(2))
        .enquire_link_response_timeout(Duration::from_secs(1))
        .connected(client);

    let now = Instant::now();

    loop {
        if !client.is_active() {
            break;
        }

        let client_clone = client.clone();

        tokio::spawn(async move {
            let _ = client_clone.submit_sm(SubmitSm::default()).await;
        });

        // No sleep => Test hangs
        tokio::time::sleep(Duration::from_nanos(1)).await;
    }

    let _ = events.count().await;

    let elapsed = now.elapsed();

    assert!(
        elapsed.as_secs() == 3,
        "Enquire link timeout did not occur as expected"
    );
}

#[tokio::test]
async fn server_crashes_on_request_should_close_connection() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new().run(server).await;
    });

    let (client, events) = ConnectionBuilder::new().connected(client);

    // Our test server crashes on GenericNack command
    client
        .status(CommandStatus::EsmeRxPAppn)
        .generic_nack(1)
        .await
        .expect("Failed to send generic_nack");

    let _ = events.count().await;
}

#[tokio::test]
async fn connection_lost_should_close_connection() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new()
            .close_connection_delay(Duration::from_secs(1))
            .run(server)
            .await;
    });

    let (client, events) = ConnectionBuilder::new().connected(client);

    tokio::time::sleep(Duration::from_secs(2)).await;

    let error = client.submit_sm(SubmitSm::default()).await.unwrap_err();

    assert!(matches!(error, Error::ConnectionClosed));

    let _ = events.count().await;
}

#[tokio::test]
async fn server_unbinds_and_closes_connection_should_close_connection() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        UnbindServer::new(Duration::from_secs(1)).run(server).await;
    });

    let (client, mut events) = ConnectionBuilder::new().connected(client);

    while let Some(event) = events.next().await {
        if let Event::Incoming(command) = event {
            if command.id() == CommandId::Unbind {
                let error = client
                    .status(CommandStatus::EsmeRok)
                    .unbind_resp(command.sequence_number())
                    .await
                    .unwrap_err();

                assert!(matches!(error, Error::ConnectionClosed));
            }
        }
    }

    let _ = events.count().await;
}

#[tokio::test]
async fn server_sends_an_operation_with_the_same_sequence_number_of_a_pending_response_should_go_through_events()
 {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        let mut framed = Framed::new(server, CommandCodec::new());

        let Some(Ok(command)) = framed.next().await else {
            panic!("Expected command");
        };

        // Out of the blue the server decides to send an AlertNotification
        // with the same sequence number as the pending SubmitSm response
        framed
            .send(
                Command::builder()
                    .status(CommandStatus::EsmeRok)
                    .sequence_number(command.sequence_number())
                    .pdu(AlertNotification::default()),
            )
            .await
            .expect("Failed to send AlertNotification");

        framed
            .send(
                Command::builder()
                    .status(CommandStatus::EsmeRok)
                    .sequence_number(command.sequence_number())
                    .pdu(SubmitSmResp::default()),
            )
            .await
            .expect("Failed to send SubmitSmResp");

        tokio::time::sleep(Duration::from_secs(1)).await;
    });

    let (client, mut events) = ConnectionBuilder::new()
        .response_timeout(Duration::from_millis(500))
        .connected(client);

    let events = tokio::spawn(async move {
        // The server sent an AlertNotification with the same sequence number as the pending response
        let Some(Event::Incoming(command)) = events.next().await else {
            panic!("Expected command event");
        };

        assert!(matches!(command.id(), CommandId::AlertNotification));
        assert_eq!(command.sequence_number(), 1);

        // Server closed the connection

        let _ = events.count().await;
    });

    client
        .submit_sm(SubmitSm::default())
        .await
        .expect("Failed to submit SM");

    let _ = events.await;
}

/// See `server_ddos_client_should_still_send_requests_and_connection_should_still_manage_timeouts` in `connection/tests.rs`` for a more reliable test of the same behavior.
#[tokio::test]
async fn server_ddos_client_should_still_send_requests_and_connection_should_still_manage_timeouts()
{
    // Eventually, the stream poll_next will return pending, after the duplex stream reaches max_buf_size.
    // The loop guards inside the connection do not really have any effect on the connection's ability to handle timeouts in this particular case.
    // They guard against the connection being stuck in the stream poll loop (poll_next never returns pending).
    // I will keep them since they provide a way to predict the connection's behavior.
    let (server, client) = tokio::io::duplex(1024);

    let mut framed = Framed::new(server, CommandCodec::new());

    std::thread::spawn(move || {
        tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("Failed to build runtime")
            .block_on(async move {
                loop {
                    if framed
                        .send(
                            Command::builder()
                                .status(CommandStatus::EsmeRok)
                                .sequence_number(1)
                                .pdu(AlertNotification::default()),
                        )
                        .await
                        .is_err()
                    {
                        break;
                    }
                }
            });
    });

    let (client, events) = ConnectionBuilder::new()
        .enquire_link_interval(Duration::from_secs(1))
        .enquire_link_response_timeout(Duration::from_millis(500))
        .response_timeout(Duration::from_millis(500))
        .connected(client);

    client
        .no_wait() // Server will not respond anyway, so we don't care about the response
        .submit_sm(SubmitSm::default())
        .await
        .expect("Failed to submit SM");

    // After the enquire link timeout, the connection should close
    let _ = events.count().await;
}

/// This test relies on [`enquire_link_timeout_idle_should_close_connection`] to be correct.
///
/// This function uses the same setup as the previous test, but disables the enquire link interval.
#[tokio::test]
async fn enquire_link_interval_none_should_not_send_enquire_link_commands() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new()
            .enquire_link_delay(Duration::from_secs(3))
            .run(server)
            .await;
    });

    let (client, _events) = ConnectionBuilder::new()
        .enquire_link_interval(Duration::from_secs(1))
        .enquire_link_response_timeout(Duration::from_secs(1))
        .no_enquire_link_interval()
        .connected(client);

    tokio::time::sleep(Duration::from_secs(3)).await;

    // after 3 seconds the connection should still be active since no enquire link commands were sent
    assert!(client.is_active(), "Connection was closed unexpectedly");
}

/// The connection should not treat the enquire link response as a response to an enquire link sent by the connection.
///
/// The response should be passed to the client to handle it.
#[tokio::test]
async fn client_sends_enquire_link_connection_should_pass_response_to_client() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        Server::new().run(server).await;
    });

    let (client, events) = ConnectionBuilder::new()
        .enquire_link_interval(Duration::from_millis(10))
        .connected(client);

    // Wait for the automatic enquire link to be sent
    // We can not guarantee that an enquire link with seq (x) was sent before we send our own with seq (y) while the connection is still waiting for the response with seq (x)
    tokio::time::sleep(Duration::from_millis(50)).await;

    client
        .enquire_link()
        .await
        .expect("Failed to send enquire_link");

    client.close().await.expect("Failed to close connection");

    client.closed().await;

    let _ = events.count().await;
}

#[tokio::test]
async fn disabled_auto_enquire_link_response_should_pipe_enquire_link_through_events() {
    init_tracing();

    let (server, client) = tokio::io::duplex(1024);

    tokio::spawn(async move {
        let mut framed = Framed::new(server, CommandCodec::new());

        framed
            .send(
                Command::builder()
                    .status(CommandStatus::EsmeRok)
                    .sequence_number(1)
                    .pdu(Pdu::EnquireLink),
            )
            .await
            .expect("Failed to send EnquireLink");
    });

    let (_client, mut events) = ConnectionBuilder::new()
        .disable_auto_enquire_link_response()
        .connected(client);

    // The enquire link request should be sent to the event stream
    let Some(Event::Incoming(command)) = events.next().await else {
        panic!("Expected command event");
    };

    assert!(matches!(command.id(), CommandId::EnquireLink));
}

#[tokio::test]
async fn stream_shutdown_should_be_called() {
    init_tracing();

    let shutdown_called = Arc::new(AtomicBool::new(false));
    let shutdown_called_clone = shutdown_called.clone();

    let mut io = MockIo::new();

    io.expect_poll_flush_pin()
        .returning(move |_cx| Poll::Ready(Ok(())));

    io.expect_poll_shutdown_pin().returning(move |_cx| {
        shutdown_called_clone.store(true, Ordering::SeqCst);
        Poll::Ready(Ok(()))
    });

    let (client, _) = ConnectionBuilder::new().connected(io);

    client.close().await.expect("Failed to close connection");

    assert!(
        shutdown_called.load(Ordering::SeqCst),
        "Stream shutdown was not called"
    );
}