Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/workerd/io/io-context/io-context-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,7 @@ KJ_TEST("an await_js() whose promise GC collects in another request releases its

constexpr kj::StringPtr PROBE_SPECIFIER = "test:io-context-probe"_kj;
constexpr kj::StringPtr FAILING_SPECIFIER = "test:io-context-failing"_kj;
constexpr kj::StringPtr TERMINATING_SPECIFIER = "test:io-context-terminating"_kj;

// Has `registerModules` add Rust modules to a legacy registry, then calls `body` with the
// registry inside a request.
Expand Down Expand Up @@ -2697,6 +2698,20 @@ void withNewRustRegistry(auto&& registerModules, auto&& body) {
fixture.runInIoContext([&](const TestFixture::Environment& env) { body(env); });
}

// Whether calling `fn` throws `JsExceptionThrown` with execution terminating. Cancels the
// termination.
bool terminates(const TestFixture::Environment& env, auto&& fn) {
// NOLINTNEXTLINE(custom-raw-v8-trycatch): the test only checks that the termination stays.
v8::TryCatch tryCatch(env.isolate);
KJ_DEFER(env.isolate->CancelTerminateExecution());
try {
fn();
return false;
} catch (jsg::JsExceptionThrown&) {
return tryCatch.HasTerminated();
}
}

// What calling `fn` throws, as a string.
kj::String thrownBy(jsg::Lock& js, auto&& fn) {
return js.tryCatch([&]() -> kj::String {
Expand Down Expand Up @@ -2744,5 +2759,20 @@ KJ_TEST("the new registry throws the error of a failing Rust module callback") {
});
}

KJ_TEST("the legacy registry keeps the termination of a terminating Rust module callback") {
withLegacyRustRegistry(register_terminating_module, [](const auto& env, auto& registry) {
KJ_EXPECT(terminates(env, [&] {
registry.resolve(env.js, kj::Path::parse(TERMINATING_SPECIFIER), kj::none,
jsg::ModuleRegistry::ResolveOption::INTERNAL_ONLY);
}));
});
}

KJ_TEST("the new registry keeps the termination of a terminating Rust module callback") {
withNewRustRegistry(register_terminating_module, [](const auto& env) {
KJ_EXPECT(terminates(env, [&] { env.js.resolveInternalModule(TERMINATING_SPECIFIER); }));
});
}

} // namespace
} // namespace workerd::io_context::test
12 changes: 12 additions & 0 deletions src/workerd/io/io-context/test_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ mod ffi {
unsafe fn await_and_record_async_context(isolate: usize) -> Result<()>;
fn register_probe_module(registry: Pin<&mut ModuleRegistry>);
fn register_failing_module(registry: Pin<&mut ModuleRegistry>);
fn register_terminating_module(registry: Pin<&mut ModuleRegistry>);
fn take_probe_observation() -> ProbeObservation;
unsafe fn await_released(isolate: usize, ending: AwaitEnding) -> Result<()>;
fn release_await(value: i32);
Expand Down Expand Up @@ -327,6 +328,17 @@ fn register_failing_module(registry: Pin<&mut ffi::ModuleRegistry>) {
);
}

/// Registers the internal module `test:io-context-terminating`, whose callback terminates
/// execution.
fn register_terminating_module(registry: Pin<&mut ffi::ModuleRegistry>) {
jsg::modules::add_builtin(
registry,
"test:io-context-terminating",
|lock| -> jsg::Result<jsg::Rc<probe::Probe>> { Err(lock.terminate_execution_now()) },
jsg::modules::ModuleType::Internal,
);
}

/// What the probe module's closure saw when it last ran, which is then forgotten.
fn take_probe_observation() -> ffi::ProbeObservation {
PROBE_OBSERVATION.with(|cell| cell.replace(ffi::ProbeObservation::NotRun))
Expand Down
Loading