-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Eval EXEC <execvy@gmail.com>
- Loading branch information
Showing
8 changed files
with
158 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,98 @@ | ||
use crate::{ | ||
broadcast_exit_signals, new_crossbeam_exit_rx, new_tokio_exit_rx, register_thread, | ||
register_tokio, wait_all_ckb_services_exit, | ||
}; | ||
use ckb_async_runtime::{new_global_runtime, Handle}; | ||
use ckb_channel::select; | ||
use rand::Rng; | ||
use std::time::Duration; | ||
|
||
fn send_ctrlc_later(duration: Duration) { | ||
std::thread::spawn(move || { | ||
std::thread::sleep(duration); | ||
// send SIGINT to myself | ||
unsafe { | ||
libc::raise(libc::SIGINT); | ||
println!("[ $$ sent SIGINT to myself $$ ]"); | ||
} | ||
}); | ||
} | ||
|
||
fn start_many_threads() { | ||
for i in 0..5 { | ||
let join = std::thread::spawn(move || { | ||
let ticker = ckb_channel::tick(Duration::from_millis(500)); | ||
let deadline = ckb_channel::after(Duration::from_millis( | ||
(rand::thread_rng().gen_range(1.0..5.0) * 1000.0) as u64, | ||
)); | ||
|
||
let stop = new_crossbeam_exit_rx(); | ||
|
||
loop { | ||
select! { | ||
recv(ticker) -> _ => { | ||
println!("thread {} received tick signal", i); | ||
}, | ||
recv(stop) -> _ => { | ||
println!("thread {} received crossbeam exit signal", i); | ||
return; | ||
}, | ||
recv(deadline) -> _ =>{ | ||
println!("thread {} finish its job", i); | ||
return | ||
} | ||
} | ||
} | ||
}); | ||
register_thread(join); | ||
} | ||
} | ||
|
||
fn start_many_tokio_tasks(handle: Handle) { | ||
for i in 0..5 { | ||
let mut stop = new_tokio_exit_rx(); | ||
|
||
let join = handle.spawn(async move { | ||
let mut interval = tokio::time::interval(Duration::from_millis(500)); | ||
|
||
let duration = | ||
Duration::from_millis((rand::thread_rng().gen_range(1.0..5.0) * 1000.0) as u64); | ||
let deadline = tokio::time::sleep(duration); | ||
tokio::pin!(deadline); | ||
|
||
loop { | ||
tokio::select! { | ||
_ = &mut deadline =>{ | ||
println!("tokio task {} finish its job", i); | ||
break; | ||
} | ||
_ = interval.tick()=> { | ||
println!("tokio task {} received tick signal", i); | ||
}, | ||
_ = stop.changed() => { | ||
println!("tokio task {} receive exit signal", i); | ||
break | ||
}, | ||
else => break, | ||
} | ||
} | ||
}); | ||
register_tokio(join); | ||
} | ||
} | ||
|
||
#[test] | ||
fn basic() { | ||
let (handle, _runtime) = new_global_runtime(); | ||
|
||
ctrlc::set_handler(move || { | ||
broadcast_exit_signals(); | ||
}); | ||
|
||
send_ctrlc_later(Duration::from_secs(3)); | ||
|
||
start_many_threads(); | ||
start_many_tokio_tasks(handle.clone()); | ||
|
||
wait_all_ckb_services_exit(handle); | ||
} |