Skip to content

Commit c11c664

Browse files
Vaider7frostyplanet
authored andcommitted
feat: compio test suite
1 parent e625ead commit c11c664

File tree

5 files changed

+190
-47
lines changed

5 files changed

+190
-47
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: cron-master-compio-arm
2+
3+
on:
4+
schedule: [cron: "0 */5 * * *"]
5+
workflow_dispatch:
6+
7+
env:
8+
CARGO_TERM_COLOR: always
9+
10+
jobs:
11+
build_and_test:
12+
13+
runs-on: "macos-15"
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
18+
# We use cargo-nextest because cargo test does not forward cancellation signal
19+
- uses: taiki-e/install-action@nextest
20+
21+
- name: Run tests with smol
22+
run: env WORKFLOW=1 make test_compio
23+
24+
- name: Run test with compio release
25+
run: env WORKFLOW=1 make test_compio_release
26+
27+
- name: Run test with compio release and trace_log
28+
run: WORKFLOW=1 exec cargo nextest run -F="compio,trace_log" --hide-progress-bar -j 1 --no-capture -r
29+
30+
- name: Run tests with smol
31+
run: env WORKFLOW=1 make test_compio_dispatcher
32+
33+
- name: Run test with compio_dispatcher release
34+
run: env WORKFLOW=1 make test_compio_dispatcher_release
35+
36+
- name: Run test with compio_dispatcher release and trace_log
37+
run: WORKFLOW=1 exec cargo nextest run -F="compio_dispatcher,trace_log" --hide-progress-bar -j 1 --no-capture -r
38+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: cron-master-compio-x86
2+
3+
on:
4+
schedule: [cron: "0 */5 * * *"]
5+
workflow_dispatch:
6+
7+
env:
8+
CARGO_TERM_COLOR: always
9+
10+
jobs:
11+
build_and_test:
12+
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
18+
# We use cargo-nextest because cargo test does not forward cancellation signal
19+
- uses: taiki-e/install-action@nextest
20+
21+
- name: Run tests with smol
22+
run: env WORKFLOW=1 make test_compio
23+
24+
- name: Run test with compio release
25+
run: env WORKFLOW=1 make test_compio_release
26+
27+
- name: Run test with compio release and trace_log
28+
run: WORKFLOW=1 exec cargo nextest run -F="compio,trace_log" --hide-progress-bar -j 1 --no-capture -r
29+
30+
- name: Run tests with smol
31+
run: env WORKFLOW=1 make test_compio_dispatcher
32+
33+
- name: Run test with compio_dispatcher release
34+
run: env WORKFLOW=1 make test_compio_dispatcher_release
35+
36+
- name: Run test with compio_dispatcher release and trace_log
37+
run: WORKFLOW=1 exec cargo nextest run -F="compio_dispatcher,trace_log" --hide-progress-bar -j 1 --no-capture -r

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ tokio = { version = "1", features = ["time", "rt"], optional=true }
3030
async-std = {version = "1", optional=true}
3131
log = { version="0", optional=true}
3232
smol = {version = "2", optional=true }
33+
compio = { version = "0.17", optional = true, features = ["runtime", "time", "dispatcher", "polling"], default-features = false}
3334

3435
[dev-dependencies]
3536
tokio = { version = "1", features = ["time", "sync", "rt-multi-thread", "rt", "macros"] }
@@ -59,6 +60,12 @@ async_std = ["dep:async-std"]
5960
# This only switch runtime in benchmark and test workflow.
6061
smol = ["dep:smol"]
6162

63+
# This only switch runtime in benchmark and test workflow.
64+
compio = ["dep:compio"]
65+
66+
# This only switch runtime in benchmark and test workflow.
67+
compio_dispatcher = ["dep:compio"]
68+
6269
# for test workflow debugging
6370
trace_log = ["dep:log"]
6471

Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,39 @@ test_smol_release:
104104
test_log_smol_release:
105105
@${RUNRELEASECASE}; FEATURE_FLAG="-F smol,trace_log"; _run_test_release_case
106106

107+
.PHONY: test_compio
108+
test_compio:
109+
@${RUNTESTCASE}; FEATURE_FLAG="-F compio"; _run_test_case
110+
111+
.PHONY: test_log_compio
112+
test_log_compio:
113+
@${RUNTESTCASE}; FEATURE_FLAG="-F compio,trace_log"; _run_test_case
114+
115+
.PHONY: test_compio_release
116+
test_compio_release:
117+
@${RUNTESTCASE}; FEATURE_FLAG="-F compio"; _run_test_release_case
118+
119+
.PHONY: test_log_compio_release
120+
test_log_compio_release:
121+
@${RUNTESTCASE}; FEATURE_FLAG="-F compio,trace_log"; _run_test_release_case
122+
123+
.PHONY: test_compio_dispatcher
124+
test_compio_dispatcher:
125+
@${RUNTESTCASE}; FEATURE_FLAG="-F compio_dispatcher"; _run_test_case
126+
127+
.PHONY: test_log_compio_dispatcher
128+
test_log_compio_dispatcher:
129+
@${RUNTESTCASE}; FEATURE_FLAG="-F compio_dispatcher,trace_log"; _run_test_case
130+
131+
.PHONY: test_compio_dispatcher_release
132+
test_compio_dispatcher_release:
133+
@${RUNTESTCASE}; FEATURE_FLAG="-F compio_dispatcher"; _run_test_release_case
134+
135+
.PHONY: test_log_compio_dispatcher_release
136+
test_log_compio_dispatcher_release:
137+
@${RUNTESTCASE}; FEATURE_FLAG="-F compio_dispatcher,trace_log"; _run_test_release_case
138+
139+
107140
.PHONY: build
108141
build: init
109142
cargo build

src/tests/common.rs

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ pub const ROUND: usize = 10000;
66
#[cfg(miri)]
77
pub const ROUND: usize = 20;
88

9+
#[cfg(feature = "compio_dispatcher")]
10+
use std::sync::OnceLock;
11+
12+
#[cfg(feature = "compio_dispatcher")]
13+
use compio::dispatcher::Dispatcher;
14+
15+
#[cfg(feature = "compio_dispatcher")]
16+
pub static COMPIO_DISPATCHER: OnceLock<Dispatcher> = OnceLock::new();
17+
918
pub fn _setup_log() {
1019
#[cfg(feature = "trace_log")]
1120
{
@@ -57,25 +66,29 @@ macro_rules! runtime_block_on {
5766
log::info!("run with smol");
5867
smol::block_on($f)
5968
}
60-
#[cfg(not(feature = "smol"))]
69+
#[cfg(feature = "async_std")]
6170
{
62-
#[cfg(feature = "async_std")]
63-
{
64-
log::info!("run with async_std");
65-
async_std::task::block_on($f)
66-
}
67-
#[cfg(any(feature = "tokio", not(feature = "async_std")))]
68-
{
69-
let runtime_flag = std::env::var("SINGLE_THREAD_RUNTIME").unwrap_or("".to_string());
70-
let mut rt = if runtime_flag.len() > 0 {
71-
log::info!("run with tokio current thread");
72-
tokio::runtime::Builder::new_current_thread()
73-
} else {
74-
log::info!("run with tokio multi thread");
75-
tokio::runtime::Builder::new_multi_thread()
76-
};
77-
rt.enable_all().build().unwrap().block_on($f)
78-
}
71+
log::info!("run with async_std");
72+
async_std::task::block_on($f)
73+
}
74+
#[cfg(feature = "tokio")]
75+
{
76+
let runtime_flag = std::env::var("SINGLE_THREAD_RUNTIME").unwrap_or("".to_string());
77+
let mut rt = if runtime_flag.len() > 0 {
78+
log::info!("run with tokio current thread");
79+
tokio::runtime::Builder::new_current_thread()
80+
} else {
81+
log::info!("run with tokio multi thread");
82+
tokio::runtime::Builder::new_multi_thread()
83+
};
84+
rt.enable_all().build().unwrap().block_on($f)
85+
}
86+
#[cfg(any(feature = "compio", feature = "compio_dispatcher"))]
87+
{
88+
log::info!("run with compio");
89+
90+
let rt = compio::runtime::Runtime::new().unwrap();
91+
rt.block_on($f)
7992
}
8093
}};
8194
}
@@ -88,16 +101,27 @@ macro_rules! async_spawn {
88101
{
89102
smol::spawn($f)
90103
}
91-
#[cfg(not(feature = "smol"))]
104+
#[cfg(feature = "async_std")]
105+
{
106+
async_std::task::spawn($f)
107+
}
108+
#[cfg(feature = "tokio")]
109+
{
110+
tokio::spawn($f)
111+
}
112+
#[cfg(feature = "compio")]
113+
{
114+
compio::runtime::spawn($f)
115+
}
116+
#[cfg(feature = "compio_dispatcher")]
92117
{
93-
#[cfg(feature = "async_std")]
94-
{
95-
async_std::task::spawn($f)
96-
}
97-
#[cfg(any(feature = "tokio", not(feature = "async_std")))]
98-
{
99-
tokio::spawn($f)
100-
}
118+
let disp = crate::tests::common::COMPIO_DISPATCHER.get_or_init(|| {
119+
compio::dispatcher::DispatcherBuilder::new()
120+
.worker_threads(std::num::NonZero::new(8).unwrap())
121+
.build()
122+
.expect("create dispatcher")
123+
});
124+
disp.dispatch(move || $f).expect("dispatch")
101125
}
102126
}};
103127
}
@@ -106,20 +130,17 @@ pub(super) use async_spawn;
106130
#[allow(dead_code)]
107131
macro_rules! async_join_result {
108132
($th: expr) => {{
109-
#[cfg(feature = "smol")]
133+
#[cfg(any(feature = "async_std", feature = "smol"))]
110134
{
111135
$th.await
112136
}
113-
#[cfg(not(feature = "smol"))]
137+
#[cfg(any(feature = "compio", feature = "tokio"))]
114138
{
115-
#[cfg(feature = "async_std")]
116-
{
117-
$th.await
118-
}
119-
#[cfg(not(feature = "async_std"))]
120-
{
121-
$th.await.expect("join")
122-
}
139+
$th.await.expect("join")
140+
}
141+
#[cfg(feature = "compio_dispatcher")]
142+
{
143+
$th.await.expect("join")
123144
}
124145
}};
125146
}
@@ -182,16 +203,17 @@ pub async fn sleep(duration: std::time::Duration) {
182203
{
183204
smol::Timer::after(duration).await;
184205
}
185-
#[cfg(not(feature = "smol"))]
206+
#[cfg(feature = "async_std")]
186207
{
187-
#[cfg(feature = "async_std")]
188-
{
189-
async_std::task::sleep(duration).await;
190-
}
191-
#[cfg(not(feature = "async_std"))]
192-
{
193-
tokio::time::sleep(duration).await;
194-
}
208+
async_std::task::sleep(duration).await;
209+
}
210+
#[cfg(feature = "tokio")]
211+
{
212+
tokio::time::sleep(duration).await;
213+
}
214+
#[cfg(any(feature = "compio", feature = "compio_dispatcher"))]
215+
{
216+
compio::time::sleep(duration).await;
195217
}
196218
}
197219

@@ -206,10 +228,16 @@ where
206228
.await
207229
.map_err(|_| format!("Test timed out after {:?}", duration))
208230
}
209-
#[cfg(not(feature = "async_std"))]
231+
#[cfg(feature = "tokio")]
210232
{
211233
tokio::time::timeout(duration, future)
212234
.await
213235
.map_err(|_| format!("Test timed out after {:?}", duration))
214236
}
237+
#[cfg(any(feature = "compio", feature = "compio_dispatcher"))]
238+
{
239+
compio::time::timeout(duration, future)
240+
.await
241+
.map_err(|_| format!("Test timed out after {:?}", duration))
242+
}
215243
}

0 commit comments

Comments
 (0)