-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65dfa5a
commit c48cef0
Showing
6 changed files
with
303 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod common; | ||
pub mod ringbuf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod mpsc; | ||
pub mod spsc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use std::{sync::Arc, time::Duration}; | ||
|
||
use crate::common::{ | ||
msg_num, start_producer, MsgForward, StartProducerOptions, | ||
}; | ||
use shm_ringbuf::{ | ||
consumer::{settings::ConsumerSettingsBuilder, RingbufConsumer}, | ||
producer::{settings::ProducerSettingsBuilder, RingbufProducer}, | ||
}; | ||
use tokio::sync::mpsc; | ||
|
||
#[tokio::test] | ||
async fn test_ringbuf_mpsc_base() { | ||
do_test_ringbuf_mpsc(false, Duration::from_millis(10), None, 0, 1000).await; | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_ringbuf_mpsc_with_notify() { | ||
// Set too long interval for testing notify. | ||
do_test_ringbuf_mpsc(false, Duration::from_secs(100), Some(500), 501, 1000) | ||
.await; | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_ringbuf_mpsc_with_wait_result() { | ||
do_test_ringbuf_mpsc(true, Duration::from_millis(10), None, 0, 1000).await; | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_ringbuf_mpsc_with_wait_result_and_notify() { | ||
// Set too long interval for testing notify. | ||
do_test_ringbuf_mpsc(true, Duration::from_secs(100), Some(500), 501, 1000) | ||
.await; | ||
} | ||
|
||
async fn do_test_ringbuf_mpsc( | ||
wait_result: bool, | ||
process_interval: Duration, | ||
notify_limit: Option<u32>, | ||
min_msg_len: usize, | ||
max_msg_len: usize, | ||
) { | ||
tracing_subscriber::fmt::init(); | ||
|
||
let (send_msgs, mut recv_msgs) = mpsc::channel(100); | ||
|
||
let num_producers = 4; | ||
|
||
let mut expected_sends = Vec::with_capacity(4); | ||
let mut expected_recvs = Vec::with_capacity(4); | ||
|
||
for _ in 0..num_producers { | ||
let (send, recv) = mpsc::channel(100); | ||
expected_sends.push(send); | ||
expected_recvs.push(recv); | ||
} | ||
|
||
let dir = tempfile::tempdir().unwrap(); | ||
let grpc_sock_path = dir.path().join("control.sock"); | ||
let fdpass_sock_path = dir.path().join("sendfd.sock"); | ||
|
||
let settings = ConsumerSettingsBuilder::new() | ||
.grpc_sock_path(grpc_sock_path.clone()) | ||
.fdpass_sock_path(fdpass_sock_path.clone()) | ||
.process_interval(process_interval) | ||
.build(); | ||
|
||
tokio::spawn(async move { | ||
let string_print = MsgForward { sender: send_msgs }; | ||
RingbufConsumer::new(settings).run(string_print).await; | ||
}); | ||
|
||
// Wait for the consumer to start. | ||
tokio::time::sleep(Duration::from_millis(10)).await; | ||
|
||
let settings = ProducerSettingsBuilder::new() | ||
.grpc_sock_path(grpc_sock_path.clone()) | ||
.fdpass_sock_path(fdpass_sock_path.clone()) | ||
.build(); | ||
|
||
let msg_num = msg_num(); | ||
|
||
let producer = | ||
Arc::new(RingbufProducer::connect_lazy(settings).await.unwrap()); | ||
|
||
for (i, expected_send) in expected_sends.into_iter().enumerate() { | ||
let options = StartProducerOptions { | ||
producer: producer.clone(), | ||
msg_num, | ||
expected_send, | ||
wait_result, | ||
min_msg_len, | ||
max_msg_len, | ||
notify_limit, | ||
msg_prefix: Some(format!("{}-", i)), | ||
}; | ||
|
||
start_producer(options).await; | ||
} | ||
|
||
for _ in 0..num_producers * msg_num { | ||
let actual = recv_msgs.recv().await.unwrap(); | ||
let index = actual.split('-').nth(0).unwrap().parse::<usize>().unwrap(); | ||
let expected = expected_recvs[index].recv().await.unwrap(); | ||
|
||
assert_eq!(expected, actual); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use std::{sync::Arc, time::Duration}; | ||
|
||
use crate::common::{ | ||
msg_num, start_producer, MsgForward, StartProducerOptions, | ||
}; | ||
use shm_ringbuf::{ | ||
consumer::{settings::ConsumerSettingsBuilder, RingbufConsumer}, | ||
producer::{settings::ProducerSettingsBuilder, RingbufProducer}, | ||
}; | ||
use tokio::sync::mpsc; | ||
|
||
#[tokio::test] | ||
async fn test_ringbuf_spsc_base() { | ||
do_test_ringbuf_spsc(false, Duration::from_millis(10), None, 0, 1000).await; | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_ringbuf_spsc_with_notify() { | ||
// Set too long interval for testing notify. | ||
do_test_ringbuf_spsc(false, Duration::from_secs(100), Some(500), 501, 1000) | ||
.await; | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_ringbuf_spsc_with_wait_result() { | ||
do_test_ringbuf_spsc(true, Duration::from_millis(10), None, 0, 1000).await; | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_ringbuf_spsc_with_wait_result_and_notify() { | ||
// Set too long interval for testing notify. | ||
do_test_ringbuf_spsc(true, Duration::from_secs(100), Some(500), 501, 1000) | ||
.await; | ||
} | ||
|
||
async fn do_test_ringbuf_spsc( | ||
wait_result: bool, | ||
process_interval: Duration, | ||
notify_limit: Option<u32>, | ||
min_msg_len: usize, | ||
max_msg_len: usize, | ||
) { | ||
tracing_subscriber::fmt::init(); | ||
|
||
let (send_msgs, mut recv_msgs) = mpsc::channel(100); | ||
|
||
let (expected_send, mut expected_recv) = mpsc::channel(100); | ||
|
||
let dir = tempfile::tempdir().unwrap(); | ||
let grpc_sock_path = dir.path().join("control.sock"); | ||
let fdpass_sock_path = dir.path().join("sendfd.sock"); | ||
|
||
let settings = ConsumerSettingsBuilder::new() | ||
.grpc_sock_path(grpc_sock_path.clone()) | ||
.fdpass_sock_path(fdpass_sock_path.clone()) | ||
.process_interval(process_interval) | ||
.build(); | ||
|
||
tokio::spawn(async move { | ||
let string_print = MsgForward { sender: send_msgs }; | ||
RingbufConsumer::new(settings).run(string_print).await; | ||
}); | ||
|
||
// Wait for the consumer to start. | ||
tokio::time::sleep(Duration::from_millis(10)).await; | ||
|
||
let settings = ProducerSettingsBuilder::new() | ||
.grpc_sock_path(grpc_sock_path.clone()) | ||
.fdpass_sock_path(fdpass_sock_path.clone()) | ||
.build(); | ||
|
||
let msg_num = msg_num(); | ||
|
||
let producer = | ||
Arc::new(RingbufProducer::connect_lazy(settings).await.unwrap()); | ||
|
||
let options = StartProducerOptions { | ||
producer, | ||
msg_num, | ||
expected_send, | ||
wait_result, | ||
min_msg_len, | ||
max_msg_len, | ||
notify_limit, | ||
msg_prefix: None, | ||
}; | ||
|
||
start_producer(options).await; | ||
|
||
for _ in 0..msg_num { | ||
let expected = expected_recv.recv().await.unwrap(); | ||
let actual = recv_msgs.recv().await.unwrap(); | ||
|
||
assert_eq!(expected, actual); | ||
} | ||
} |
Oops, something went wrong.