Skip to content

Commit

Permalink
test(runtime): perform block in run loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Feb 25, 2024
1 parent f1a11ec commit dc12962
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
3 changes: 2 additions & 1 deletion compio-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ libc = { workspace = true }
windows-sys = { workspace = true, features = ["Win32_UI_WindowsAndMessaging"] }

[target.'cfg(target_os = "macos")'.dev-dependencies]
core-foundation = "0.9"
core-foundation = "0.9.4"
block2 = "0.4.0"

[features]
event = ["dep:cfg-if", "compio-buf/arrayvec"]
Expand Down
27 changes: 21 additions & 6 deletions compio-runtime/tests/custom_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
fn cf_run_loop() {
use std::{future::Future, os::raw::c_void, time::Duration};

use block2::{Block, ConcreteBlock};
use compio_driver::AsRawFd;
use compio_runtime::Runtime;
use compio_runtime::{event::Event, Runtime};
use core_foundation::{
base::TCFType,
filedescriptor::{kCFFileDescriptorReadCallBack, CFFileDescriptor, CFFileDescriptorRef},
runloop::{kCFRunLoopDefaultMode, CFRunLoop},
runloop::{kCFRunLoopDefaultMode, CFRunLoop, CFRunLoopRef},
string::CFStringRef,
};

struct CFRunLoopRuntime {
Expand Down Expand Up @@ -73,11 +75,24 @@ fn cf_run_loop() {

let runtime = CFRunLoopRuntime::new();

let res = runtime.block_on(async {
compio_runtime::time::sleep(Duration::from_secs(1)).await;
1
runtime.block_on(async {
let event = Event::new();
let block = ConcreteBlock::new(|| {
event.handle().notify();
});
extern "C" {
fn CFRunLoopPerformBlock(rl: CFRunLoopRef, mode: CFStringRef, block: &Block<(), ()>);
}
let run_loop = CFRunLoop::get_current();
unsafe {
CFRunLoopPerformBlock(
run_loop.as_concrete_TypeRef(),
kCFRunLoopDefaultMode,
&block,
);
}
event.wait().await;
});
assert_eq!(res, 1);
}

#[cfg(windows)]
Expand Down

0 comments on commit dc12962

Please sign in to comment.