kimojio 0.16.2

A thread-per-core Linux io_uring async runtime optimized for latency.
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use crate::{
    CompletionResources, Errno, OwnedFd,
    io_type::IOType,
    operations,
    pointer_buffer::{IdPointerMsg, pointer_from_buffer_ref},
    pointer_from_buffer, pointer_to_buffer,
};
use futures::future::FusedFuture;
use std::{cell::RefCell, future::Future, marker::PhantomData, rc::Rc, time::Duration};

const POINTER_SIZE: usize = std::mem::size_of::<*const ()>();

type MessagePipePair<T1, T2> = (MessagePipe<T1, T2>, MessagePipe<T2, T1>);

/// Create a pair of message pipes that can be used to send messages
/// across threads within the same process.
///
/// NOTE: both pipes can be used to send and receive, so you may not
/// get the drop detection you expect. Use make_message_pipe_oneway
/// if you require drop detection.
pub fn make_message_pipe<T1: Send, T2: Send>() -> MessagePipePair<T1, T2> {
    let (pipe1, pipe2) = crate::pipe::bipipe();
    (MessagePipe::new(pipe1), MessagePipe::new(pipe2))
}

/// Create a message pipe that can be used to send messages across threads
/// within the same process.
pub async fn make_message_pipe_oneway<T: Send>()
-> Result<(MessagePipeSender<T>, MessagePipeReceiver<T>), Errno> {
    let (pipe1, pipe2) = crate::pipe::bipipe();
    Ok((
        MessagePipeSender::new(pipe1).await?,
        MessagePipeReceiver::new(pipe2).await?,
    ))
}

/// Create a message pipe that can be used to send messages across threads
/// within the same process.
pub fn make_message_pipe_oneway_sync<T: Send>()
-> Result<(MessagePipeSender<T>, MessagePipeReceiver<T>), Errno> {
    let (pipe1, pipe2) = crate::pipe::bipipe();
    Ok((
        MessagePipeSender::new_sync(pipe1)?,
        MessagePipeReceiver::new_sync(pipe2)?,
    ))
}

/// An in process pipe for sending messages of type T and receiving
/// messages of type R. Can be used across threads but not between
/// processes.
///
/// `send_message` and `recv_message` must be called from uringruntime
/// threads and can be used to send messages between tasks in that
/// therad.
///
/// `send_message_sync` and `recv_message_sync` are blocking calls that
/// can be used to send or receive a message from any thread.  To send
/// messages from another thread to a uringruntime thread, use these
/// methods from the non-uringruntime thread and use `recv_message` and
/// `send_message` on the recipient uringruntime thread.
#[derive(Debug)]
pub struct MessagePipe<T: Send, R: Send> {
    pipe: OwnedFd,

    _marker: PhantomData<(T, R)>,
}

impl<T: Send, R: Send> MessagePipe<T, R> {
    pub fn new(pipe: OwnedFd) -> Self {
        Self {
            pipe,
            _marker: PhantomData,
        }
    }

    pub fn into_inner(self) -> OwnedFd {
        self.pipe
    }

    /// Send a message on the pipe from a uringruntime thread. Message must be
    /// boxed as it is sent in-process as a pointer.
    pub async fn send_message(&self, message: Box<T>) -> Result<(), Errno> {
        let buffer = pointer_to_buffer(message);
        match operations::write(&self.pipe, &buffer).await {
            Ok(amount) => {
                assert!(amount == buffer.len());
                Ok(())
            }
            Err(e) => {
                // Reconstitute Box to prevent leaking on write failure
                unsafe {
                    // SAFETY: since write failed, buffer is only copy of the pointer
                    pointer_from_buffer::<T>(buffer);
                }
                Err(e)
            }
        }
    }

    /// Receive a message on the pipe. This must be called from a uringruntime thread.
    pub fn recv_message(&self) -> RecvMessageFuture<'_, R> {
        self.recv_message_with_timeout(None)
    }

    /// Receive a message on the pipe with an optional timeout.
    /// This must be called from a uringruntime thread.
    pub fn recv_message_with_timeout(&self, timeout: Option<Duration>) -> RecvMessageFuture<'_, R> {
        use std::os::fd::AsRawFd;
        let buffer = Rc::new(RefCell::new([0u8; POINTER_SIZE]));
        let fd = self.pipe.as_raw_fd();
        let entry = rustix_uring::opcode::Read::new(
            rustix_uring::types::Fd(fd),
            buffer.borrow_mut().as_mut_ptr(),
            POINTER_SIZE as u32,
        )
        .offset(u64::MAX)
        .build();
        let fut = crate::ring_future::UsizeFuture::with_polled(
            entry,
            fd,
            timeout,
            IOType::Read,
            false,
            CompletionResources::Rc(buffer.clone()),
        );

        RecvMessageFuture {
            fut,
            buffer,
            _marker: PhantomData,
        }
    }

    /// Send a message on the pipe from any thread. `message` must be boxed as
    /// it is sent in-process as a pointer. This is a blocking call and should
    /// never be called from an async task.
    pub fn send_message_sync(&self, message: Box<T>) -> Result<(), Errno> {
        let buffer = pointer_to_buffer(message);
        match rustix::io::write(&self.pipe, &buffer) {
            Ok(amount) => {
                assert!(amount == buffer.len());
                Ok(())
            }
            Err(e) => {
                // Reconstitute Box to prevent leaking on write failure
                unsafe {
                    // SAFETY: since write failed, buffer is only copy of the pointer
                    pointer_from_buffer::<T>(buffer);
                }
                Err(e)
            }
        }
    }

    /// Receive a message on the pipe. This is a blocking call and should never
    /// be called from an async task.
    pub fn recv_message_sync(&self) -> Result<Box<R>, Errno> {
        let mut buffer = [0u8; POINTER_SIZE];
        let amount = rustix::io::read(&self.pipe, &mut buffer)?;
        if amount == 0 {
            return Err(Errno::from_raw_os_error(libc::EPIPE));
        }
        assert_eq!(amount, buffer.len());
        unsafe {
            // SAFETY: The read bytes are the only copy of the pointer
            Ok(pointer_from_buffer(buffer))
        }
    }
}

#[derive(Debug)]
pub struct MessagePipeReceiver<R: Send> {
    pipe: OwnedFd,

    _marker: PhantomData<R>,
}

impl<R: Send> MessagePipeReceiver<R> {
    pub async fn new(pipe: OwnedFd) -> Result<Self, Errno> {
        operations::shutdown(&pipe, rustix::net::Shutdown::Write as i32).await?;
        Ok(Self {
            pipe,
            _marker: PhantomData,
        })
    }

    pub fn new_sync(pipe: OwnedFd) -> Result<Self, Errno> {
        rustix::net::shutdown(&pipe, rustix::net::Shutdown::Write)?;
        Ok(Self {
            pipe,
            _marker: PhantomData,
        })
    }

    pub fn into_inner(self) -> OwnedFd {
        self.pipe
    }

    /// Receive a message on the pipe. This must be called from a uringruntime thread.
    pub fn recv_message(&self) -> RecvMessageFuture<'_, R> {
        self.recv_message_with_timeout(None)
    }

    /// Receive a message on the pipe with an optional timeout.
    /// This must be called from a uringruntime thread.
    pub fn recv_message_with_timeout(&self, timeout: Option<Duration>) -> RecvMessageFuture<'_, R> {
        use std::os::fd::AsRawFd;
        let buffer = Rc::new(RefCell::new([0u8; POINTER_SIZE]));
        let fd = self.pipe.as_raw_fd();
        let entry = rustix_uring::opcode::Read::new(
            rustix_uring::types::Fd(fd),
            buffer.borrow_mut().as_mut_ptr(),
            POINTER_SIZE as u32,
        )
        .offset(u64::MAX)
        .build();
        let fut = crate::ring_future::UsizeFuture::with_polled(
            entry,
            fd,
            timeout,
            IOType::Read,
            false,
            CompletionResources::Rc(buffer.clone()),
        );

        RecvMessageFuture {
            fut,
            buffer,
            _marker: PhantomData,
        }
    }
}

impl<R: Send> Clone for MessagePipeReceiver<R> {
    fn clone(&self) -> Self {
        Self {
            pipe: self.pipe.try_clone().unwrap(),
            _marker: Default::default(),
        }
    }
}

#[derive(Debug)]
pub struct MessagePipeSender<T: Send> {
    pipe: OwnedFd,

    _marker: PhantomData<T>,
}

impl<T: Send> MessagePipeSender<T> {
    pub async fn new(pipe: OwnedFd) -> Result<Self, Errno> {
        operations::shutdown(&pipe, rustix::net::Shutdown::Read as i32).await?;
        Ok(Self {
            pipe,
            _marker: PhantomData,
        })
    }

    pub fn new_sync(pipe: OwnedFd) -> Result<Self, Errno> {
        rustix::net::shutdown(&pipe, rustix::net::Shutdown::Read)?;
        Ok(Self {
            pipe,
            _marker: PhantomData,
        })
    }

    pub fn into_inner(self) -> OwnedFd {
        self.pipe
    }

    /// Send a message on the pipe from a uringruntime thread. Message must be
    /// boxed as it is sent in-process as a pointer.
    pub async fn send_message(&self, message: Box<T>) -> Result<(), Errno> {
        let buffer = pointer_to_buffer(message);
        match operations::write(&self.pipe, &buffer).await {
            Ok(amount) => {
                assert!(amount == buffer.len());
                Ok(())
            }
            Err(e) => {
                // Reconstitute Box to prevent leaking on write failure
                unsafe {
                    // SAFETY
                    //
                    // since write failed, buffer is only copy of the pointer
                    pointer_from_buffer::<T>(buffer);
                }
                Err(e)
            }
        }
    }

    /// Send a message on the pipe from any thread. `message` must be boxed as
    /// it is sent in-process as a pointer. This is a blocking call and should
    /// never be called from an async task.
    pub fn send_message_sync(&self, message: Box<T>) -> Result<(), Errno> {
        let buffer = pointer_to_buffer(message);
        match rustix::io::write(&self.pipe, &buffer) {
            Ok(amount) => {
                assert!(amount == buffer.len());
                Ok(())
            }
            Err(e) => {
                // Reconstitute Box to prevent leaking on write failure
                unsafe {
                    // SAFETY
                    // since write failed, buffer is only copy of the pointer
                    pointer_from_buffer::<T>(buffer);
                }
                Err(e)
            }
        }
    }
}

impl<T: Send> Clone for MessagePipeSender<T> {
    fn clone(&self) -> Self {
        Self {
            pipe: self.pipe.try_clone().unwrap(),
            _marker: Default::default(),
        }
    }
}

pin_project_lite::pin_project! {
    pub struct RecvMessageFuture<'a, T> {
        #[pin]
        fut: crate::ring_future::UsizeFuture<'a>,
        buffer: Rc<RefCell<[u8; POINTER_SIZE]>>,
        _marker: PhantomData<T>,
    }
}

impl<'a, T> Future for RecvMessageFuture<'a, T> {
    type Output = Result<Box<T>, Errno>;

    fn poll(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        use std::task::Poll;

        let this = self.project();
        match this.fut.poll(cx) {
            Poll::Ready(Ok(0)) => Poll::Ready(Err(Errno::PIPE)),
            Poll::Ready(Ok(POINTER_SIZE)) => {
                let result = unsafe {
                    // SAFETY: The read bytes are the only copy of the pointer
                    pointer_from_buffer_ref(&this.buffer.borrow())
                };
                Poll::Ready(Ok(result))
            }
            Poll::Ready(Ok(amount)) => panic!("amount should be POINTER_SIZE, not {amount}"),
            Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
            Poll::Pending => Poll::Pending,
        }
    }
}

impl<'a, T> FusedFuture for RecvMessageFuture<'a, T> {
    fn is_terminated(&self) -> bool {
        self.fut.is_terminated()
    }
}

// Same as MessagePipe, but send and receive has an id param.
pub struct IdMessagePipe<T: Send, R: Send> {
    pipe: OwnedFd,

    _marker: PhantomData<(T, R)>,
}

impl<T: Send, R: Send> IdMessagePipe<T, R> {
    pub fn new(pipe: OwnedFd) -> Self {
        Self {
            pipe,
            _marker: PhantomData,
        }
    }

    pub fn into_inner(self) -> OwnedFd {
        self.pipe
    }

    /// Send a message on the pipe from a uringruntime thread. Message must be
    /// boxed as it is sent in-process as a pointer.
    pub async fn send_message(&self, id: u64, message: Box<T>) -> Result<(), Errno> {
        let buffer = IdPointerMsg::from_id_box(id, message);
        let buffer_ref = buffer.as_ref();
        match operations::write(&self.pipe, buffer_ref).await {
            Ok(amount) => {
                assert!(amount == buffer_ref.len());
                // If we succeeded in writing to pipe, then we no longer own the pointer and should
                // not drop it.
                std::mem::forget(buffer);
                Ok(())
            }
            Err(e) => Err(e),
        }
    }

    /// Receive a message on the pipe. This must be called from a uringruntime thread.
    pub async fn recv_message(&self) -> Result<(u64, Box<R>), Errno> {
        let mut buffer = [0u8; 2 * POINTER_SIZE];
        let amount = operations::read(&self.pipe, &mut buffer).await?;
        if amount == 0 {
            return Err(Errno::from_raw_os_error(libc::EPIPE));
        }
        assert_eq!(amount, buffer.len());
        Ok(IdPointerMsg::<R>::new(buffer).into_id_box())
    }

    /// Send a message on the pipe from any thread. `message` must be boxed as
    /// it is sent in-process as a pointer. This is a blocking call and should
    /// never be called from an async task.
    pub fn send_message_sync(&self, id: u64, message: Box<T>) -> Result<(), Errno> {
        let buffer = IdPointerMsg::<T>::from_id_box(id, message);
        let buffer_ref = buffer.as_ref();
        match rustix::io::write(&self.pipe, buffer_ref) {
            Ok(amount) => {
                assert!(amount == buffer_ref.len());
                // If we succeeded in writing to pipe, then we no longer own the pointer and should
                // not drop it.
                std::mem::forget(buffer);
                Ok(())
            }
            Err(e) => Err(e),
        }
    }

    /// Receive a message on the pipe. This is a blocking call and should never
    /// be called from an async task.
    pub fn recv_message_sync(&self) -> Result<(u64, Box<R>), Errno> {
        let mut buffer = [0u8; 2 * POINTER_SIZE];
        let amount = rustix::io::read(&self.pipe, &mut buffer)?;
        if amount == 0 {
            return Err(Errno::from_raw_os_error(libc::EPIPE));
        }
        assert_eq!(amount, buffer.len());
        Ok(IdPointerMsg::<R>::new(buffer).into_id_box())
    }
}

impl<T: Send, R: Send> Clone for MessagePipe<T, R> {
    fn clone(&self) -> Self {
        Self {
            pipe: self.pipe.try_clone().unwrap(),
            _marker: Default::default(),
        }
    }
}

#[cfg(test)]
mod test {
    use super::{IdMessagePipe, MessagePipe, make_message_pipe, make_message_pipe_oneway_sync};
    use crate::{MessagePipeReceiver, MessagePipeSender, make_message_pipe_oneway};
    use futures::{FutureExt, future::FusedFuture};
    use std::time::Duration;

    #[crate::test]
    async fn test_message_pipe_behavior_send() {
        let (tx, rx): (MessagePipeSender<String>, MessagePipeReceiver<String>) =
            make_message_pipe_oneway().await.unwrap();

        let rx2 = rx.clone();
        drop(rx);
        drop(rx2);

        match tx.send_message(Box::new("message".to_string())).await {
            Ok(_) => panic!("Successfully sent message, should have errored out"),
            Err(err) => println!("Errored out {err:?} while sending message, as expected"),
        }
    }

    #[crate::test]
    async fn test_message_pipe_behavior_recv() {
        let (tx, rx): (MessagePipeSender<String>, MessagePipeReceiver<String>) =
            make_message_pipe_oneway().await.unwrap();

        let tx2 = tx.clone();

        drop(tx);
        drop(tx2);

        match rx.recv_message().await {
            Ok(_) => panic!("Successfully received message, should have errored out"),
            Err(err) => println!("Errored out {err:?} while receiving message, as expected"),
        }
    }

    #[crate::test]
    async fn test_make_message_pipe() {
        let (pipe1, pipe2): (MessagePipe<String, i32>, MessagePipe<i32, String>) =
            make_message_pipe();

        // Test bidirectional communication
        let send_task = crate::operations::spawn_task(async move {
            pipe1
                .send_message(Box::new("hello".to_string()))
                .await
                .unwrap();
            let response = pipe1.recv_message().await.unwrap();
            assert_eq!(*response, 42);
        });

        let recv_task = crate::operations::spawn_task(async move {
            let message = pipe2.recv_message().await.unwrap();
            assert_eq!(*message, "hello");
            pipe2.send_message(Box::new(42)).await.unwrap();
        });

        send_task.await.unwrap();
        recv_task.await.unwrap();
    }

    #[crate::test]
    async fn test_make_message_pipe_oneway_sync() {
        let (tx, rx): (MessagePipeSender<String>, MessagePipeReceiver<String>) =
            make_message_pipe_oneway_sync().unwrap();

        let send_task = crate::operations::spawn_task(async move {
            tx.send_message(Box::new("sync test".to_string()))
                .await
                .unwrap();
        });

        let recv_task = crate::operations::spawn_task(async move {
            let message = rx.recv_message().await.unwrap();
            assert_eq!(*message, "sync test");
        });

        send_task.await.unwrap();
        recv_task.await.unwrap();
    }

    #[crate::test]
    async fn test_message_pipe_new_and_into_inner() {
        let (pipe1, pipe2) = crate::pipe::bipipe();
        let msg_pipe: MessagePipe<String, i32> = MessagePipe::new(pipe1);

        // Test into_inner
        let fd = msg_pipe.into_inner();

        // Create new message pipe and test basic communication
        let msg_pipe2: MessagePipe<i32, String> = MessagePipe::new(pipe2);
        let msg_pipe1: MessagePipe<String, i32> = MessagePipe::new(fd);

        let send_task = crate::operations::spawn_task(async move {
            msg_pipe1
                .send_message(Box::new("test".to_string()))
                .await
                .unwrap();
        });

        let recv_task = crate::operations::spawn_task(async move {
            let message = msg_pipe2.recv_message().await.unwrap();
            assert_eq!(*message, "test");
        });

        send_task.await.unwrap();
        recv_task.await.unwrap();
    }

    #[crate::test]
    async fn test_message_pipe_recv_with_timeout() {
        let (pipe1, pipe2): (MessagePipe<String, i32>, MessagePipe<i32, String>) =
            make_message_pipe();

        // Test timeout
        let timeout_result = pipe1
            .recv_message_with_timeout(Some(Duration::from_millis(10)))
            .await;
        assert!(timeout_result.is_err());

        // Test successful receive with timeout
        let send_task = crate::operations::spawn_task(async move {
            pipe2.send_message(Box::new(123)).await.unwrap();
        });

        let recv_task = crate::operations::spawn_task(async move {
            let message = pipe1
                .recv_message_with_timeout(Some(Duration::from_millis(100)))
                .await
                .unwrap();
            assert_eq!(*message, 123);
        });

        send_task.await.unwrap();
        recv_task.await.unwrap();
    }

    #[crate::test]
    async fn test_message_pipe_sync_operations() {
        let (pipe1, pipe2): (MessagePipe<String, i32>, MessagePipe<i32, String>) =
            make_message_pipe();

        let sync_task = std::thread::spawn(move || {
            // Test sync send
            pipe1
                .send_message_sync(Box::new("sync message".to_string()))
                .unwrap();

            // Test sync receive
            let response = pipe1.recv_message_sync().unwrap();
            assert_eq!(*response, 456);
        });

        let async_task = crate::operations::spawn_task(async move {
            // Receive sync message
            let message = pipe2.recv_message().await.unwrap();
            assert_eq!(*message, "sync message");

            // Send response
            pipe2.send_message(Box::new(456)).await.unwrap();
        });

        async_task.await.unwrap();
        sync_task.join().unwrap();
    }

    #[crate::test]
    async fn test_message_pipe_sender_new_sync_and_into_inner() {
        let (pipe1, pipe2) = crate::pipe::bipipe();
        let sender: MessagePipeSender<String> = MessagePipeSender::new_sync(pipe1).unwrap();

        // Test into_inner
        let fd = sender.into_inner();

        // Create receiver and test communication
        let receiver: MessagePipeReceiver<String> = MessagePipeReceiver::new(pipe2).await.unwrap();
        let sender: MessagePipeSender<String> = MessagePipeSender::new(fd).await.unwrap();

        let send_task = crate::operations::spawn_task(async move {
            sender
                .send_message(Box::new("test sender".to_string()))
                .await
                .unwrap();
        });

        let recv_task = crate::operations::spawn_task(async move {
            let message = receiver.recv_message().await.unwrap();
            assert_eq!(*message, "test sender");
        });

        send_task.await.unwrap();
        recv_task.await.unwrap();
    }

    #[crate::test]
    async fn test_message_pipe_sender_sync_operations() {
        let (tx, rx): (MessagePipeSender<String>, MessagePipeReceiver<String>) =
            make_message_pipe_oneway_sync().unwrap();

        let sync_task = std::thread::spawn(move || {
            tx.send_message_sync(Box::new("sync sender test".to_string()))
                .unwrap();
        });

        let async_task = crate::operations::spawn_task(async move {
            let message = rx.recv_message().await.unwrap();
            assert_eq!(*message, "sync sender test");
        });

        sync_task.join().unwrap();
        async_task.await.unwrap();
    }

    #[crate::test]
    async fn test_message_pipe_receiver_new_sync_and_into_inner() {
        let (pipe1, pipe2) = crate::pipe::bipipe();
        let receiver: MessagePipeReceiver<String> = MessagePipeReceiver::new_sync(pipe2).unwrap();

        // Test into_inner
        let fd = receiver.into_inner();

        // Create sender and test communication
        let sender: MessagePipeSender<String> = MessagePipeSender::new(pipe1).await.unwrap();
        let receiver: MessagePipeReceiver<String> = MessagePipeReceiver::new(fd).await.unwrap();

        let send_task = crate::operations::spawn_task(async move {
            sender
                .send_message(Box::new("test receiver".to_string()))
                .await
                .unwrap();
        });

        let recv_task = crate::operations::spawn_task(async move {
            let message = receiver.recv_message().await.unwrap();
            assert_eq!(*message, "test receiver");
        });

        send_task.await.unwrap();
        recv_task.await.unwrap();
    }

    #[crate::test]
    async fn test_message_pipe_receiver_recv_with_timeout() {
        let (tx, rx): (MessagePipeSender<String>, MessagePipeReceiver<String>) =
            make_message_pipe_oneway().await.unwrap();

        // Test timeout
        let timeout_result = rx
            .recv_message_with_timeout(Some(Duration::from_millis(10)))
            .await;
        assert!(timeout_result.is_err());

        // Test successful receive with timeout
        let send_task = crate::operations::spawn_task(async move {
            tx.send_message(Box::new("timeout test".to_string()))
                .await
                .unwrap();
        });

        let recv_task = crate::operations::spawn_task(async move {
            let message = rx
                .recv_message_with_timeout(Some(Duration::from_millis(100)))
                .await
                .unwrap();
            assert_eq!(*message, "timeout test");
        });

        send_task.await.unwrap();
        recv_task.await.unwrap();
    }

    #[crate::test]
    async fn test_id_message_pipe_new_and_into_inner() {
        let (pipe1, pipe2) = crate::pipe::bipipe();
        let id_pipe: IdMessagePipe<String, i32> = IdMessagePipe::new(pipe1);

        // Test into_inner
        let fd = id_pipe.into_inner();

        // Create new id message pipes and test communication
        let id_pipe1: IdMessagePipe<String, i32> = IdMessagePipe::new(fd);
        let id_pipe2: IdMessagePipe<i32, String> = IdMessagePipe::new(pipe2);

        let send_task = crate::operations::spawn_task(async move {
            id_pipe1
                .send_message(123, Box::new("id test".to_string()))
                .await
                .unwrap();
        });

        let recv_task = crate::operations::spawn_task(async move {
            let (id, message) = id_pipe2.recv_message().await.unwrap();
            assert_eq!(id, 123);
            assert_eq!(*message, "id test");
        });

        send_task.await.unwrap();
        recv_task.await.unwrap();
    }

    #[crate::test]
    async fn test_id_message_pipe_async_operations() {
        let (pipe1, pipe2) = crate::pipe::bipipe();
        let id_pipe1: IdMessagePipe<String, i32> = IdMessagePipe::new(pipe1);
        let id_pipe2: IdMessagePipe<i32, String> = IdMessagePipe::new(pipe2);

        let send_task = crate::operations::spawn_task(async move {
            id_pipe1
                .send_message(456, Box::new("async id test".to_string()))
                .await
                .unwrap();
            let (response_id, response) = id_pipe1.recv_message().await.unwrap();
            assert_eq!(response_id, 789);
            assert_eq!(*response, 999);
        });

        let recv_task = crate::operations::spawn_task(async move {
            let (id, message) = id_pipe2.recv_message().await.unwrap();
            assert_eq!(id, 456);
            assert_eq!(*message, "async id test");

            id_pipe2.send_message(789, Box::new(999)).await.unwrap();
        });

        send_task.await.unwrap();
        recv_task.await.unwrap();
    }

    #[crate::test]
    async fn test_id_message_pipe_sync_operations() {
        let (pipe1, pipe2) = crate::pipe::bipipe();
        let id_pipe1: IdMessagePipe<String, i32> = IdMessagePipe::new(pipe1);
        let id_pipe2: IdMessagePipe<i32, String> = IdMessagePipe::new(pipe2);

        let sync_task = std::thread::spawn(move || {
            // Test sync send
            id_pipe1
                .send_message_sync(321, Box::new("sync id test".to_string()))
                .unwrap();

            // Test sync receive
            let (response_id, response) = id_pipe1.recv_message_sync().unwrap();
            assert_eq!(response_id, 654);
            assert_eq!(*response, 888);
        });

        let async_task = crate::operations::spawn_task(async move {
            // Receive sync message
            let (id, message) = id_pipe2.recv_message().await.unwrap();
            assert_eq!(id, 321);
            assert_eq!(*message, "sync id test");

            // Send response
            id_pipe2.send_message(654, Box::new(888)).await.unwrap();
        });

        async_task.await.unwrap();
        sync_task.join().unwrap();
    }

    #[crate::test]
    async fn test_id_message_pipe_multiple_instances() {
        let (pipe1, pipe2) = crate::pipe::bipipe();
        let (pipe3, pipe4) = crate::pipe::bipipe();

        let id_pipe1: IdMessagePipe<String, i32> = IdMessagePipe::new(pipe1);
        let id_pipe2: IdMessagePipe<i32, String> = IdMessagePipe::new(pipe2);
        let id_pipe3: IdMessagePipe<String, i32> = IdMessagePipe::new(pipe3);
        let id_pipe4: IdMessagePipe<i32, String> = IdMessagePipe::new(pipe4);

        let send_task1 = crate::operations::spawn_task(async move {
            id_pipe1
                .send_message(111, Box::new("test 1".to_string()))
                .await
                .unwrap();
        });

        let send_task2 = crate::operations::spawn_task(async move {
            id_pipe3
                .send_message(222, Box::new("test 2".to_string()))
                .await
                .unwrap();
        });

        let recv_task1 = crate::operations::spawn_task(async move {
            let (id, message) = id_pipe2.recv_message().await.unwrap();
            assert_eq!(id, 111);
            assert_eq!(*message, "test 1");
        });

        let recv_task2 = crate::operations::spawn_task(async move {
            let (id, message) = id_pipe4.recv_message().await.unwrap();
            assert_eq!(id, 222);
            assert_eq!(*message, "test 2");
        });

        send_task1.await.unwrap();
        send_task2.await.unwrap();
        recv_task1.await.unwrap();
        recv_task2.await.unwrap();
    }

    #[crate::test]
    async fn test_recv_message_future_is_terminated() {
        let (tx, rx): (MessagePipeSender<String>, MessagePipeReceiver<String>) =
            make_message_pipe_oneway().await.unwrap();

        let future = rx.recv_message().fuse();

        // Check that future is not terminated initially
        assert!(!future.is_terminated());

        // Send a message
        tx.send_message(Box::new("terminate test".to_string()))
            .await
            .unwrap();

        // Complete the future
        let message = future.await.unwrap();
        assert_eq!(*message, "terminate test");
    }

    #[crate::test]
    async fn test_message_pipe_clone() {
        let (pipe1, pipe2): (MessagePipe<String, i32>, MessagePipe<i32, String>) =
            make_message_pipe();
        let pipe1_clone = pipe1.clone();

        let send_task = crate::operations::spawn_task(async move {
            pipe1_clone
                .send_message(Box::new("clone test".to_string()))
                .await
                .unwrap();
        });

        let recv_task = crate::operations::spawn_task(async move {
            let message = pipe2.recv_message().await.unwrap();
            assert_eq!(*message, "clone test");
        });

        send_task.await.unwrap();
        recv_task.await.unwrap();
    }

    #[crate::test]
    async fn test_message_pipe_error_handling() {
        let (pipe1, _pipe2): (MessagePipe<String, i32>, MessagePipe<i32, String>) =
            make_message_pipe();

        // Drop pipe2 to close the connection
        drop(_pipe2);

        // Sending should fail
        let result = pipe1
            .send_message(Box::new("should fail".to_string()))
            .await;
        assert!(result.is_err());

        // Receiving should fail
        let result = pipe1.recv_message().await;
        assert!(result.is_err());
    }

    #[crate::test]
    async fn test_id_message_pipe_error_handling() {
        let (pipe1, _pipe2) = crate::pipe::bipipe();
        let id_pipe: IdMessagePipe<String, i32> = IdMessagePipe::new(pipe1);

        // Drop pipe2 to close the connection
        drop(_pipe2);

        // Sending should fail
        let result = id_pipe
            .send_message(123, Box::new("should fail".to_string()))
            .await;
        assert!(result.is_err());

        // Receiving should fail
        let result = id_pipe.recv_message().await;
        assert!(result.is_err());
    }
}