<div dir="ltr"><div><br clear="all"></div>Hi all,<br><br>We were testing the JEP 499: Structured Concurrency (Third Preview) and found a challenge. <br><br>Context:<br><br>We currently use Executor to create concurrent tasks, and when we create integration tests using Spring Boot, we want to exercise the code in two ways:<br><br>a. Executing the tasks sequentially, to validate that the use cases behave as we expected. These tests are easy to setup.<br>b. Executing the tasks concurrently, to validate the logic is prepared to handle concurrent behavior. These tests are not that easy to setup<br><br>The a. point will have many tests for all the possible branches, while b. will have a few tests, focused only on the concurrent part.<br><br>For b. point, what we do today is to mock the Executor interface, as follows:<br><br>Executor executor = new Executor() {<br>            @Override<br>            public void execute(Runnable command) {<br>                command.run(); // Directly runs the command in the calling thread<br>            }<br>}<br><br>That implementation guarantees that any tasks running over that executor will run in the same thread, and therefore, sequentially. <br><br>Things get more complicated when you need to use ExecutorService. as the interface is more cumbersome, so, creating a mock object from it is not easy.<br><br>There is also the option to use Executors.newSingleThreadExecutor(), which will have only one thread to run all the tasks, and therefore, it will need to queue them in order. However, the downside of this approach is when you want to run concurrent tasks but not join them to the current thread.<br><br>Challenge:<br><br>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?<br><br>Would like to know what you think about it. Thanks in advance<div><br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13px;border-collapse:collapse;color:rgb(136,136,136)">Daniel Andrés Pelaez López</span><div><span style="font-family:arial,sans-serif;font-size:13px;border-collapse:collapse;color:rgb(136,136,136)">e. <a href="mailto:estigma88@gmail.com" target="_blank">estigma88@gmail.com</a></span></div></div></div></div></div></div></div>