How to "mock" the StructuredTaskScope for integration tests where we want to run every task sequentially instead of concurrently

Alan Bateman alan.bateman at oracle.com
Wed Mar 26 08:37:43 UTC 2025


On 25/03/2025 20:35, Daniel Andres Pelaez Lopez wrote:
>
> :
>
> Challenge:
>
> Testing JEP 499: Structured Concurrency (Third Preview) we couldn't 
> find an easy way to do this, perhaps we need to create a new 
> StructuredTaskScope implementation as we do with Executor interface? 
> or should we pass a custom ThreadFactory that only creates one thread?

A new virtual thread is started to execute each subtask. So no 
equivalent of a "caller runs" policy. A ThreadFactory can decorate the 
Runnable to use a Semaphore to limit concurrency and a single permit 
would ensure the subtasks execute sequentially. Would that help what you 
are doing?

-Alan


More information about the loom-dev mailing list