diff --git a/ylong_runtime/src/io/stdio.rs b/ylong_runtime/src/io/stdio.rs index 82e3dda0930f9faef89e7e052fe96b5f89704a38..8c493e3a58fd413fa4cb71e710344ecb8f09576c 100644 --- a/ylong_runtime/src/io/stdio.rs +++ b/ylong_runtime/src/io/stdio.rs @@ -194,11 +194,8 @@ pub(crate) use std_async_write; #[cfg(test)] mod test { - #[cfg(unix)] - use std::os::fd::{AsFd, AsRawFd}; - use crate::io::stdio::BufInner; - use crate::io::{AsyncWriteExt, ReadBuf}; + use crate::io::ReadBuf; /// UT test cases for `stdout` and `stderr``. /// @@ -223,40 +220,4 @@ mod test { let n = buf_inner.clone_into(&mut read_buf); assert_eq!(n, 10); } - - /// UT test cases for `stdout` and `stderr``. - /// - /// # Brief - /// 1. create a `stdout` and a `stderr`. - /// 2. write something into `stdout` and `stderr`. - /// 3. check operation is ok. - #[test] - fn ut_test_stdio_write() { - let handle = crate::spawn(async { - let mut stdout = crate::io::stdout(); - #[cfg(unix)] - assert!(stdout.as_fd().as_raw_fd() >= 0); - #[cfg(unix)] - assert!(stdout.as_raw_fd() >= 0); - let res = stdout.write_all(b"something").await; - assert!(res.is_ok()); - let res = stdout.flush().await; - assert!(res.is_ok()); - let res = stdout.shutdown().await; - assert!(res.is_ok()); - - let mut stderr = crate::io::stderr(); - #[cfg(unix)] - assert!(stderr.as_fd().as_raw_fd() >= 0); - #[cfg(unix)] - assert!(stderr.as_raw_fd() >= 0); - let res = stderr.write_all(b"something").await; - assert!(res.is_ok()); - let res = stderr.flush().await; - assert!(res.is_ok()); - let res = stderr.shutdown().await; - assert!(res.is_ok()); - }); - let _ = crate::block_on(handle); - } } diff --git a/ylong_runtime/src/net/sys/unix/datagram.rs b/ylong_runtime/src/net/sys/unix/datagram.rs index ab3990a905274379326a82834a2736c91d39c72b..af3078ce1a03414109101782eb1fcbdd97bc8fcb 100644 --- a/ylong_runtime/src/net/sys/unix/datagram.rs +++ b/ylong_runtime/src/net/sys/unix/datagram.rs @@ -493,21 +493,15 @@ mod test { /// otherwise the next bind operation will fail. #[test] fn ut_uds_datagram_read_write_test() { - const PATH: &str = "/tmp/uds_datagram_test_path1"; - let _ = std::fs::remove_file(PATH); - let handle2 = crate::spawn(async { - let socket = UnixDatagram::bind(PATH).unwrap(); - - let handle = crate::spawn(async { - let socket = UnixDatagram::unbound().unwrap(); - socket.connect(PATH).unwrap(); + let (server, client) = UnixDatagram::pair().unwrap(); - socket.send(b"hello world").await.expect("send failed"); + let handle = crate::spawn(async move { + client.send(b"hello world").await.expect("send failed"); }); let mut buf = vec![0; 11]; - socket.recv(buf.as_mut_slice()).await.expect("recv failed"); + server.recv(buf.as_mut_slice()).await.expect("recv failed"); assert_eq!( std::str::from_utf8(&buf).unwrap(), "hello world".to_string() @@ -516,7 +510,6 @@ mod test { handle.await.unwrap(); }); crate::block_on(handle2).unwrap(); - let _ = std::fs::remove_file(PATH); } /// Uds UnixDatagram try_xxx() test case. diff --git a/ylong_runtime/src/net/sys/unix/listener.rs b/ylong_runtime/src/net/sys/unix/listener.rs index a61027762816affd38e43bb9e7e0cb9ca68fe37c..e0465b09221dbab892e28d10db66a7377e0af44a 100644 --- a/ylong_runtime/src/net/sys/unix/listener.rs +++ b/ylong_runtime/src/net/sys/unix/listener.rs @@ -125,87 +125,3 @@ impl AsFd for UnixListener { unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } } } - -#[cfg(test)] -mod test { - use std::os::fd::{AsFd, AsRawFd}; - - use crate::io::{AsyncReadExt, AsyncWriteExt}; - use crate::net::{UnixListener, UnixStream}; - - /// Uds UnixListener test case. - /// - /// # Title - /// ut_uds_listener_baisc_test - /// - /// # Brief - /// 1. Create a std UnixListener with `bind()`. - /// 2. Convert std UnixListener to Ylong_runtime UnixListener. - /// 3. Check result is correct. - #[test] - fn ut_uds_listener_baisc_test() { - const PATH: &str = "/tmp/uds_listener_path1"; - let _ = std::fs::remove_file(PATH); - let listener = std::os::unix::net::UnixListener::bind(PATH).unwrap(); - let handle = crate::spawn(async { - let res = UnixListener::from_std(listener); - assert!(res.is_ok()); - let listener = res.unwrap(); - assert!(listener.as_fd().as_raw_fd() >= 0); - assert!(listener.as_raw_fd() >= 0); - assert!(listener.take_error().is_ok()); - }); - crate::block_on(handle).unwrap(); - let _ = std::fs::remove_file(PATH); - } - - /// Uds UnixListener test case. - /// - /// # Title - /// ut_uds_listener_read_write_test - /// - /// # Brief - /// 1. Create a server with `bind()` and `accept()`. - /// 2. Create a client with `connect()`. - /// 3. Server Sends message and client recv it. - #[test] - fn ut_uds_listener_read_write_test() { - const PATH: &str = "/tmp/uds_listener_path2"; - let _ = std::fs::remove_file(PATH); - let client_msg = "hello client"; - let server_msg = "hello server"; - - crate::block_on(async { - let mut read_buf = [0_u8; 12]; - let listener = UnixListener::bind(PATH).unwrap(); - - let handle = crate::spawn(async { - let mut stream = UnixStream::connect(PATH).await; - while stream.is_err() { - stream = UnixStream::connect(PATH).await; - } - let mut stream = stream.unwrap(); - let mut read_buf = [0_u8; 12]; - stream.read_exact(&mut read_buf).await.unwrap(); - assert_eq!( - std::str::from_utf8(&read_buf).unwrap(), - client_msg.to_string() - ); - stream.write_all(server_msg.as_bytes()).await.unwrap(); - }); - - let (mut stream, _) = listener.accept().await.unwrap(); - stream.write_all(client_msg.as_bytes()).await.unwrap(); - - stream.read_exact(&mut read_buf).await.unwrap(); - assert_eq!( - std::str::from_utf8(&read_buf).unwrap(), - server_msg.to_string() - ); - - handle.await.unwrap(); - }); - - let _ = std::fs::remove_file(PATH); - } -} diff --git a/ylong_runtime/tests/async_buf_write.rs b/ylong_runtime/tests/async_buf_write.rs index fdaac5a17c0afbc3202eeb50be5846c05c2b1f64..40d0bce8ca36973561a51197502946dcbb545013 100644 --- a/ylong_runtime/tests/async_buf_write.rs +++ b/ylong_runtime/tests/async_buf_write.rs @@ -193,33 +193,3 @@ fn sdv_buf_writer_write_vectored_2() { ylong_runtime::block_on(server).unwrap(); ylong_runtime::block_on(client).unwrap(); } - -/// SDV test cases for `stdout` and `stderr``. -/// -/// # Brief -/// 1. create a `stdout` and a `stderr`. -/// 2. write something into `stdout` and `stderr`. -/// 3. check operation is ok. -#[test] -fn sdv_buf_writer_stdio_write() { - let handle = ylong_runtime::spawn(async { - let stdout = ylong_runtime::io::stdout(); - let mut buf_writer = AsyncBufWriter::new(stdout); - let res = buf_writer.write_all(b"something").await; - assert!(res.is_ok()); - let res = buf_writer.flush().await; - assert!(res.is_ok()); - let res = buf_writer.shutdown().await; - assert!(res.is_ok()); - - let stderr = ylong_runtime::io::stderr(); - let mut buf_writer = AsyncBufWriter::new(stderr); - let res = buf_writer.write_all(b"something").await; - assert!(res.is_ok()); - let res = buf_writer.flush().await; - assert!(res.is_ok()); - let res = buf_writer.shutdown().await; - assert!(res.is_ok()); - }); - ylong_runtime::block_on(handle).unwrap(); -} diff --git a/ylong_runtime/tests/entry.rs b/ylong_runtime/tests/entry.rs index a525e248b832ae78bd1199b5d74f7e0f392682c9..860d91e31004e61fd652474350f3e94f2e5983be 100644 --- a/ylong_runtime/tests/entry.rs +++ b/ylong_runtime/tests/entry.rs @@ -22,6 +22,7 @@ mod async_fs; mod async_pool; mod async_read; mod block_on; +mod builder; mod error; mod join_set; mod mpsc_test; @@ -29,6 +30,7 @@ mod mutex; mod par_iter; mod process; mod pty_process; +mod select; mod semaphore_test; mod signal; mod singleton_runtime; @@ -39,3 +41,4 @@ mod task_cancel; mod tcp_test; mod timer_test; mod udp_test; +mod uds_test; diff --git a/ylong_runtime/tests/stdio_cargo_test.rs b/ylong_runtime/tests/stdio_cargo_test.rs new file mode 100644 index 0000000000000000000000000000000000000000..a3280a5d7b205c1a947f7967ff07a3472de0644e --- /dev/null +++ b/ylong_runtime/tests/stdio_cargo_test.rs @@ -0,0 +1,84 @@ +// Copyright (c) 2023 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! This test can only run in cargo. + +#[cfg(unix)] +use std::os::fd::{AsFd, AsRawFd}; + +use ylong_runtime::io::{AsyncBufWriter, AsyncWriteExt}; + +/// UT test cases for `stdout` and `stderr``. +/// +/// # Brief +/// 1. create a `stdout` and a `stderr`. +/// 2. write something into `stdout` and `stderr`. +/// 3. check operation is ok. +#[test] +fn sdv_stdio_write() { + ylong_runtime::block_on(async { + let mut stdout = ylong_runtime::io::stdout(); + #[cfg(unix)] + assert!(stdout.as_fd().as_raw_fd() >= 0); + #[cfg(unix)] + assert!(stdout.as_raw_fd() >= 0); + let res = stdout.write_all(b"something").await; + assert!(res.is_ok()); + let res = stdout.flush().await; + assert!(res.is_ok()); + let res = stdout.shutdown().await; + assert!(res.is_ok()); + + let mut stderr = ylong_runtime::io::stderr(); + #[cfg(unix)] + assert!(stderr.as_fd().as_raw_fd() >= 0); + #[cfg(unix)] + assert!(stderr.as_raw_fd() >= 0); + let res = stderr.write_all(b"something").await; + assert!(res.is_ok()); + let res = stderr.flush().await; + assert!(res.is_ok()); + let res = stderr.shutdown().await; + assert!(res.is_ok()); + }); +} + +/// SDV test cases for `stdout` and `stderr``. +/// +/// # Brief +/// 1. create a `stdout` and a `stderr`. +/// 2. write something into `stdout` and `stderr`. +/// 3. check operation is ok. +#[test] +fn sdv_stdio_buf_writer_write() { + let handle = ylong_runtime::spawn(async { + let stdout = ylong_runtime::io::stdout(); + let mut buf_writer = AsyncBufWriter::new(stdout); + let res = buf_writer.write_all(b"something").await; + assert!(res.is_ok()); + let res = buf_writer.flush().await; + assert!(res.is_ok()); + let res = buf_writer.shutdown().await; + assert!(res.is_ok()); + + let stderr = ylong_runtime::io::stderr(); + let mut buf_writer = AsyncBufWriter::new(stderr); + let res = buf_writer.write_all(b"something").await; + assert!(res.is_ok()); + let res = buf_writer.flush().await; + assert!(res.is_ok()); + let res = buf_writer.shutdown().await; + assert!(res.is_ok()); + }); + ylong_runtime::block_on(handle).unwrap(); +} diff --git a/ylong_runtime/tests/uds_cargo_test.rs b/ylong_runtime/tests/uds_cargo_test.rs new file mode 100644 index 0000000000000000000000000000000000000000..7734312b600f65ae19592a63b2b1044b21013d8e --- /dev/null +++ b/ylong_runtime/tests/uds_cargo_test.rs @@ -0,0 +1,91 @@ +// Copyright (c) 2023 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! This test can only run in cargo. + +#![cfg(all(target_family = "unix", feature = "net"))] + +use std::os::fd::{AsFd, AsRawFd}; + +use ylong_runtime::io::{AsyncReadExt, AsyncWriteExt}; +use ylong_runtime::net::{UnixListener, UnixStream}; + +/// Uds UnixListener test case. +/// +/// # Brief +/// 1. Create a std UnixListener with `bind()`. +/// 2. Convert std UnixListener to Ylong_runtime UnixListener. +/// 3. Check result is correct. +#[test] +fn sdv_uds_listener_baisc_test() { + const PATH: &str = "/tmp/uds_listener_path1"; + let _ = std::fs::remove_file(PATH); + let listener = std::os::unix::net::UnixListener::bind(PATH).unwrap(); + let handle = ylong_runtime::spawn(async { + let res = UnixListener::from_std(listener); + assert!(res.is_ok()); + let listener = res.unwrap(); + assert!(listener.as_fd().as_raw_fd() >= 0); + assert!(listener.as_raw_fd() >= 0); + assert!(listener.take_error().is_ok()); + }); + ylong_runtime::block_on(handle).unwrap(); + let _ = std::fs::remove_file(PATH); +} + +/// Uds UnixListener test case. +/// +/// # Brief +/// 1. Create a server with `bind()` and `accept()`. +/// 2. Create a client with `connect()`. +/// 3. Server Sends message and client recv it. +#[test] +fn sdv_uds_listener_read_write_test() { + const PATH: &str = "/tmp/uds_listener_path2"; + let _ = std::fs::remove_file(PATH); + let client_msg = "hello client"; + let server_msg = "hello server"; + + ylong_runtime::block_on(async { + let mut read_buf = [0_u8; 12]; + let listener = UnixListener::bind(PATH).unwrap(); + + let handle = ylong_runtime::spawn(async { + let mut stream = UnixStream::connect(PATH).await; + while stream.is_err() { + stream = UnixStream::connect(PATH).await; + } + let mut stream = stream.unwrap(); + let mut read_buf = [0_u8; 12]; + stream.read_exact(&mut read_buf).await.unwrap(); + assert_eq!( + std::str::from_utf8(&read_buf).unwrap(), + client_msg.to_string() + ); + stream.write_all(server_msg.as_bytes()).await.unwrap(); + }); + + let (mut stream, _) = listener.accept().await.unwrap(); + stream.write_all(client_msg.as_bytes()).await.unwrap(); + + stream.read_exact(&mut read_buf).await.unwrap(); + assert_eq!( + std::str::from_utf8(&read_buf).unwrap(), + server_msg.to_string() + ); + + handle.await.unwrap(); + }); + + let _ = std::fs::remove_file(PATH); +} diff --git a/ylong_runtime/tests/uds_test.rs b/ylong_runtime/tests/uds_test.rs index 346bc7f38e9ece9d6f642ba5e7ca2a1640f2f469..9ae9e1cd9003a3d69a196c14eb481a8b57b154e1 100644 --- a/ylong_runtime/tests/uds_test.rs +++ b/ylong_runtime/tests/uds_test.rs @@ -204,31 +204,6 @@ fn sdv_uds_datagram_try_test() { ylong_runtime::block_on(handle).unwrap(); } -/// Uds UnixListener test case. -/// -/// # Title -/// sdv_uds_listener_baisc_test -/// -/// # Brief -/// 1. Create a std UnixListener with `bind()`. -/// 2. Convert std UnixListener to Ylong_runtime UnixListener. -/// 3. Check result is correct. -#[test] -fn sdv_uds_listener_baisc_test() { - const PATH: &str = "/tmp/uds_path3"; - let _ = std::fs::remove_file(PATH); - let listener = std::os::unix::net::UnixListener::bind(PATH).unwrap(); - let handle = ylong_runtime::spawn(async { - let res = UnixListener::from_std(listener); - assert!(res.is_ok()); - let listener = res.unwrap(); - assert!(listener.as_raw_fd() >= 0); - assert!(listener.take_error().is_ok()); - }); - ylong_runtime::block_on(handle).unwrap(); - let _ = std::fs::remove_file(PATH); -} - /// Uds UnixStream test case. /// /// # Title