|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package example.springdata.jpa.simple; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.BlockingQueue; |
| 22 | +import java.util.concurrent.CompletableFuture; |
| 23 | +import java.util.concurrent.LinkedBlockingQueue; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.condition.EnabledOnJre; |
| 29 | +import org.junit.jupiter.api.condition.JRE; |
| 30 | + |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.boot.test.context.SpringBootTest; |
| 33 | +import org.springframework.core.task.SimpleAsyncTaskExecutor; |
| 34 | +import org.springframework.transaction.annotation.Propagation; |
| 35 | +import org.springframework.transaction.annotation.Transactional; |
| 36 | + |
| 37 | +/** |
| 38 | + * Integration test showing the basic usage of {@link SimpleUserRepository} with Virtual Threads. |
| 39 | + * |
| 40 | + * @author Mark Paluch |
| 41 | + */ |
| 42 | +@Transactional |
| 43 | +@SpringBootTest(properties = "spring.threads.virtual.enabled=true") |
| 44 | +@EnabledOnJre(JRE.JAVA_21) |
| 45 | +class VirtualThreadsTests { |
| 46 | + |
| 47 | + @Autowired SimpleUserRepository repository; |
| 48 | + private User user; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + void setUp() { |
| 52 | + |
| 53 | + user = new User(); |
| 54 | + user.setUsername("foobar"); |
| 55 | + user.setFirstname("firstname"); |
| 56 | + user.setLastname("lastname"); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * This repository invocation runs on a dedicated virtual thread. |
| 61 | + */ |
| 62 | + @Test |
| 63 | + @Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 64 | + void supportsVirtualThreads() throws Exception { |
| 65 | + |
| 66 | + BlockingQueue<String> thread = new LinkedBlockingQueue<>(); |
| 67 | + repository.save(new User("Customer1", "Foo")); |
| 68 | + repository.save(new User("Customer2", "Bar")); |
| 69 | + |
| 70 | + try (SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor()) { |
| 71 | + executor.setVirtualThreads(true); |
| 72 | + |
| 73 | + var future = executor.submit(() -> { |
| 74 | + thread.add(Thread.currentThread().toString()); |
| 75 | + return repository.findAll(); |
| 76 | + }); |
| 77 | + |
| 78 | + List<User> users = future.get(); |
| 79 | + String threadName = thread.poll(1, TimeUnit.SECONDS); |
| 80 | + |
| 81 | + assertThat(threadName).contains("VirtualThread"); |
| 82 | + assertThat(users).hasSize(2); |
| 83 | + } |
| 84 | + |
| 85 | + repository.deleteAll(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Here we demonstrate the usage of {@link CompletableFuture} as a result wrapper for asynchronous repository query |
| 90 | + * methods running on Virtual Threads. Note, that we need to disable the surrounding transaction to be able to |
| 91 | + * asynchronously read the written data from another thread within the same test method. |
| 92 | + */ |
| 93 | + @Test |
| 94 | + @Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 95 | + void asyncUsesVirtualThreads() throws Exception { |
| 96 | + |
| 97 | + BlockingQueue<String> thread = new LinkedBlockingQueue<>(); |
| 98 | + repository.save(new User("Customer1", "Foo")); |
| 99 | + repository.save(new User("Customer2", "Bar")); |
| 100 | + |
| 101 | + var future = repository.readAllBy().thenAccept(users -> { |
| 102 | + |
| 103 | + assertThat(users).hasSize(2); |
| 104 | + thread.add(Thread.currentThread().toString()); |
| 105 | + }); |
| 106 | + |
| 107 | + future.join(); |
| 108 | + String threadName = thread.poll(1, TimeUnit.SECONDS); |
| 109 | + |
| 110 | + assertThat(threadName).contains("VirtualThread"); |
| 111 | + |
| 112 | + repository.deleteAll(); |
| 113 | + } |
| 114 | +} |
0 commit comments