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
20 changes: 9 additions & 11 deletions web-virtual-threads/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ We include standard Spring Web, Spring Data JPA, alongside PostgreSQL and Testco
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-webmvc")
implementation("org.springframework.boot:spring-boot-restclient")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.springframework.boot:spring-boot-starter-data-jpa-test")
testImplementation("org.springframework.boot:spring-boot-resttestclient")
Expand All @@ -49,7 +48,6 @@ Configure the application to enable virtual threads in `src/main/resources/appli
spring.application.name=web-virtual-threads

spring.threads.virtual.enabled=true
spring.jpa.hibernate.ddl-auto=create-drop
----

Setting `spring.threads.virtual.enabled=true` instructs Tomcat (or your chosen embedded web server) to use an `Executor` backed by virtual threads for handling HTTP requests.
Expand Down Expand Up @@ -161,27 +159,27 @@ public class TestcontainersConfiguration {

[source,java]
----
@AutoConfigureTestRestTemplate
@AutoConfigureRestTestClient
@Import(TestcontainersConfiguration.class)
@SpringBootTest(properties = "spring.jpa.hibernate.ddl-auto=create-drop", webEnvironment = RANDOM_PORT)
class UserControllerTests {

@Autowired
private TestRestTemplate restClient;
private RestTestClient restClient;

@Autowired
private UserRepository em;
private UserRepository users;

@Test
@DisplayName("Should process web request and database query on a Virtual Thread")
void virtualThreadEnabled() {
em.save(new User("Rashidi"));
users.save(new User("Rashidi"));

var response = restClient.getForEntity("/thread-info", Map.class);

assertThat(response)
.extracting("statusCode", "body.isVirtual", "body.userCount")
.containsOnly(OK, true, 1);
restClient.get().uri("/thread-info").exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.isVirtual").isEqualTo(true)
.jsonPath("$.userCount").isEqualTo(1);
}

}
Expand Down
1 change: 0 additions & 1 deletion web-virtual-threads/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ repositories {
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-webmvc")
implementation("org.springframework.boot:spring-boot-restclient")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.springframework.boot:spring-boot-starter-data-jpa-test")
testImplementation("org.springframework.boot:spring-boot-resttestclient")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,35 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.resttestclient.TestRestTemplate;
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.client.RestTestClient;
import zin.rashidi.boot.web.virtualthreads.TestcontainersConfiguration;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.http.HttpStatus.OK;

@AutoConfigureTestRestTemplate
@AutoConfigureRestTestClient
@Import(TestcontainersConfiguration.class)
@SpringBootTest(properties = "spring.jpa.hibernate.ddl-auto=create-drop", webEnvironment = RANDOM_PORT)
class UserControllerTests {

@Autowired
private TestRestTemplate restClient;
private RestTestClient restClient;

@Autowired
private UserRepository em;
private UserRepository users;

@Test
@DisplayName("Should process web request and database query on a Virtual Thread")
void virtualThreadEnabled() {
em.save(new User("Rashidi"));

var response = restClient.getForEntity("/thread-info", Map.class);
users.save(new User("Rashidi"));

assertThat(response)
.extracting("statusCode", "body.isVirtual", "body.userCount")
.containsOnly(OK, true, 1);
restClient.get().uri("/thread-info").exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.isVirtual").isEqualTo(true)
.jsonPath("$.userCount").isEqualTo(1);
}

}
Loading