Skip to content

Commit

Permalink
feat: enable async test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
LeuisKen committed Jan 18, 2025
1 parent cb29c7b commit e875814
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
2 changes: 2 additions & 0 deletions bridge/core/executing_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,9 @@ void ExecutingContext::SetRunRustFutureTasks(const std::shared_ptr<WebFNativeFun
void ExecutingContext::RunRustFutureTasks() {
if (run_rust_future_tasks_ == nullptr)
return;
dart_isolate_context_->profiler()->StartTrackAsyncEvaluation();
run_rust_future_tasks_->Invoke(this, 0, nullptr);
dart_isolate_context_->profiler()->FinishTrackAsyncEvaluation();
}

void ExecutingContext::DrainPendingPromiseJobs() {
Expand Down
2 changes: 0 additions & 2 deletions bridge/core/native/native_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ static void ExecuteNativeLibrary(PluginLibraryEntryPoint entry_point,
native_library_load_context->context->RunRustFutureTasks();
}

native_library_load_context->promise_resolver->Resolve(JS_UNDEFINED);

delete native_library_load_context;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use webf_sys::ExecutingContext;

pub async fn test_should_work_with_set_item(context: ExecutingContext) {
let storage = context.async_storage();
let exception_state = context.create_exception_state();

storage.clear(&exception_state).await.unwrap();
storage.set_item("keyValue", "12345", &exception_state).await.unwrap();
let value = storage.get_item("keyValue", &exception_state).await.unwrap().unwrap();

assert_eq!(value, "12345");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod async_storage;
40 changes: 38 additions & 2 deletions integration_tests/rust_builder/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::cell::RefCell;
use std::ffi::c_void;
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use webf_sys::executing_context::ExecutingContextRustMethods;
use webf_sys::{initialize_webf_api, ExecutingContext, RustValue};
use webf_sys::{initialize_webf_api, ExecutingContext, FutureRuntime, RustValue};

pub mod async_storage;
pub mod navigator;
pub mod storage;

Expand All @@ -13,9 +18,20 @@ fn webf_test_runner(tests: &[&dyn Fn(ExecutingContext)], context: ExecutingConte
}
}

type TestFn = Box<dyn Fn(ExecutingContext) -> Pin<Box<dyn Future<Output = ()>>>>;

async fn webf_test_runner_async(tests: &[TestFn], context: ExecutingContext) {
println!("Running {} tests", tests.len());
for test in tests {
test(context.clone()).await;
println!("Test passed");
}
}

#[no_mangle]
pub extern "C" fn init_webf_test_app(handle: RustValue<ExecutingContextRustMethods>) -> *mut c_void {
let context = initialize_webf_api(handle);
let context: ExecutingContext = initialize_webf_api(handle);
let context_async = context.clone();

let tests: &[&dyn Fn(ExecutingContext)] = &[
&navigator::navigator::test_user_agent,
Expand All @@ -26,5 +42,25 @@ pub extern "C" fn init_webf_test_app(handle: RustValue<ExecutingContextRustMetho

webf_test_runner(tests, context);

let runtime = Rc::new(RefCell::new(FutureRuntime::new()));

let context_async_runtime = context_async.clone();
runtime.borrow_mut().spawn(async move {
let tests: &[TestFn] = &[
Box::new(|context| {
Box::pin(async_storage::async_storage::test_should_work_with_set_item(context))
}),
];

webf_test_runner_async(tests, context_async_runtime).await;
});

let runtime_run_task_callback = Box::new(move || {
runtime.borrow_mut().run();
});

let exception_state = context_async.create_exception_state();
context_async.set_run_rust_future_tasks(runtime_run_task_callback, &exception_state).unwrap();

std::ptr::null_mut()
}
2 changes: 1 addition & 1 deletion integration_tests/specs/rust/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ describe('Rust', () => {
it('should work with rust native library', async () => {
// @ts-expect-error
await nativeLoader.loadNativeLibrary('rust_native_api_tests', {}).catch(err => console.log(err));
});
}, 10 * 1000);
});

0 comments on commit e875814

Please sign in to comment.