Skip to content

Commit a6ac3d9

Browse files
committed
tests: Use tokio UnixStream::pair() directly
Simplify socket pair creation by using tokio's UnixStream::pair() instead of manually creating std sockets, setting non-blocking mode, and converting to tokio. The tokio method handles all of this internally.
1 parent 72cbca7 commit a6ac3d9

1 file changed

Lines changed: 8 additions & 18 deletions

File tree

tests/jsonrpsee_interop.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,12 @@ use jsonrpc_fdpass::{
1111
UnixSocketTransport,
1212
};
1313
use serde_json::Value;
14-
use std::io;
15-
use std::os::unix::net::UnixStream as StdUnixStream;
1614
use tokio::net::UnixStream;
1715

18-
/// Create a connected pair of Unix streams using socketpair.
19-
fn create_socket_pair() -> io::Result<(UnixStream, UnixStream)> {
20-
let (a, b) = StdUnixStream::pair()?;
21-
a.set_nonblocking(true)?;
22-
b.set_nonblocking(true)?;
23-
Ok((UnixStream::from_std(a)?, UnixStream::from_std(b)?))
24-
}
25-
2616
/// Test round-trip: our client -> our server, verifying wire format compatibility.
2717
#[tokio::test]
2818
async fn test_wire_format_round_trip() -> Result<()> {
29-
let (client_stream, server_stream) = create_socket_pair().unwrap();
19+
let (client_stream, server_stream) = UnixStream::pair().unwrap();
3020

3121
// Start our server
3222
let server_handle = tokio::spawn(async move {
@@ -79,7 +69,7 @@ async fn test_wire_format_round_trip() -> Result<()> {
7969
/// Test that notifications work correctly (no response expected).
8070
#[tokio::test]
8171
async fn test_notification_no_response() -> Result<()> {
82-
let (client_stream, server_stream) = create_socket_pair().unwrap();
72+
let (client_stream, server_stream) = UnixStream::pair().unwrap();
8373

8474
let received = std::sync::Arc::new(std::sync::Mutex::new(None));
8575
let received_clone = received.clone();
@@ -134,7 +124,7 @@ async fn test_notification_no_response() -> Result<()> {
134124
/// Test error responses are correctly formatted per JSON-RPC 2.0 spec.
135125
#[tokio::test]
136126
async fn test_error_response_format() -> Result<()> {
137-
let (client_stream, server_stream) = create_socket_pair().unwrap();
127+
let (client_stream, server_stream) = UnixStream::pair().unwrap();
138128

139129
// Start our server
140130
let server_handle = tokio::spawn(async move {
@@ -180,7 +170,7 @@ async fn test_error_response_format() -> Result<()> {
180170
/// Test that batch requests work (send multiple messages in sequence).
181171
#[tokio::test]
182172
async fn test_sequential_requests() -> Result<()> {
183-
let (client_stream, server_stream) = create_socket_pair().unwrap();
173+
let (client_stream, server_stream) = UnixStream::pair().unwrap();
184174

185175
// Start our server
186176
let server_handle = tokio::spawn(async move {
@@ -244,7 +234,7 @@ async fn test_sequential_requests() -> Result<()> {
244234
async fn test_parse_jsonrpsee_format_request() -> Result<()> {
245235
use tokio::io::{AsyncReadExt, AsyncWriteExt};
246236

247-
let (mut client_stream, server_stream) = create_socket_pair().unwrap();
237+
let (mut client_stream, server_stream) = UnixStream::pair().unwrap();
248238

249239
// Spawn server to receive and echo
250240
let server_handle = tokio::spawn(async move {
@@ -345,7 +335,7 @@ async fn test_our_format_is_valid_jsonrpc() -> Result<()> {
345335
/// Test string ID handling (JSON-RPC allows string or number IDs).
346336
#[tokio::test]
347337
async fn test_string_id_handling() -> Result<()> {
348-
let (client_stream, server_stream) = create_socket_pair().unwrap();
338+
let (client_stream, server_stream) = UnixStream::pair().unwrap();
349339

350340
let server_handle = tokio::spawn(async move {
351341
let mut server = jsonrpc_fdpass::Server::new();
@@ -392,7 +382,7 @@ async fn test_string_id_handling() -> Result<()> {
392382
/// Test null params handling.
393383
#[tokio::test]
394384
async fn test_null_params() -> Result<()> {
395-
let (client_stream, server_stream) = create_socket_pair().unwrap();
385+
let (client_stream, server_stream) = UnixStream::pair().unwrap();
396386

397387
let server_handle = tokio::spawn(async move {
398388
let mut server = jsonrpc_fdpass::Server::new();
@@ -438,7 +428,7 @@ async fn test_null_params() -> Result<()> {
438428
/// Test array params (positional parameters as per JSON-RPC 2.0 spec).
439429
#[tokio::test]
440430
async fn test_array_params() -> Result<()> {
441-
let (client_stream, server_stream) = create_socket_pair().unwrap();
431+
let (client_stream, server_stream) = UnixStream::pair().unwrap();
442432

443433
let server_handle = tokio::spawn(async move {
444434
let mut server = jsonrpc_fdpass::Server::new();

0 commit comments

Comments
 (0)