├── .gitignore ├── README.adoc ├── build.gradle.kts ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle.kts └── src ├── main ├── kotlin │ └── com │ │ └── example │ │ ├── CoroutinesApplication.kt │ │ └── CoroutinesController.kt └── resources │ ├── application.properties │ └── templates │ └── index.mustache └── test └── kotlin └── com └── example ├── CoroutinesApplicationTests.kt └── CoroutinesControllerTests.kt /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | !gradle/wrapper/gradle-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | /out/ 20 | 21 | ### NetBeans ### 22 | /nbproject/private/ 23 | /nbbuild/ 24 | /dist/ 25 | /nbdist/ 26 | /.nb-gradle/ 27 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | = Spring Boot Coroutines demo 2 | 3 | This project demonstrates how to use https://docs.spring.io/spring-framework/docs/current/reference/html/languages.html#coroutines[WebFlux Coroutines API] from a Spring Boot application. See https://github.com/sdeleuze/spring-boot-coroutines-demo/blob/master/src/main/kotlin/com/example/CoroutinesApplication.kt[CoroutinesApplication] and https://github.com/sdeleuze/spring-boot-coroutines-demo/blob/master/src/main/kotlin/com/example/CoroutinesController.kt[CoroutinesController] for more details. 4 | 5 | 6 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 2 | 3 | plugins { 4 | id("org.springframework.boot") version "2.4.4" 5 | id("io.spring.dependency-management") version "1.0.9.RELEASE" 6 | kotlin("jvm") version "1.4.32" 7 | kotlin("plugin.spring") version "1.4.32" 8 | } 9 | 10 | group = "com.example" 11 | version = "0.0.1-SNAPSHOT" 12 | java.sourceCompatibility = JavaVersion.VERSION_1_8 13 | 14 | repositories { 15 | mavenCentral() 16 | maven { url = uri("https://repo.spring.io/milestone") } 17 | } 18 | 19 | dependencies { 20 | implementation("org.springframework.boot:spring-boot-starter-mustache") 21 | implementation("org.springframework.boot:spring-boot-starter-webflux") 22 | implementation("com.fasterxml.jackson.module:jackson-module-kotlin") 23 | implementation("org.jetbrains.kotlin:kotlin-reflect") 24 | implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") 25 | implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor") 26 | testImplementation("org.springframework.boot:spring-boot-starter-test") 27 | testImplementation("io.projectreactor:reactor-test") 28 | } 29 | 30 | tasks.withType { 31 | useJUnitPlatform() 32 | } 33 | 34 | tasks.withType { 35 | kotlinOptions { 36 | freeCompilerArgs = listOf("-Xjsr305=strict") 37 | jvmTarget = "1.8" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sdeleuze/spring-boot-coroutines-demo/2703fab2efb7b441c61aee5933ea70c0950052c8/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | maven { url = uri("https://repo.spring.io/milestone") } 4 | gradlePluginPortal() 5 | } 6 | resolutionStrategy { 7 | eachPlugin { 8 | if (requested.id.id == "org.springframework.boot") { 9 | useModule("org.springframework.boot:spring-boot-gradle-plugin:${requested.version}") 10 | } 11 | } 12 | } 13 | } 14 | rootProject.name = "spring-boot-coroutines-demo" 15 | -------------------------------------------------------------------------------- /src/main/kotlin/com/example/CoroutinesApplication.kt: -------------------------------------------------------------------------------- 1 | package com.example 2 | 3 | import kotlinx.coroutines.flow.flatMapMerge 4 | import kotlinx.coroutines.flow.flow 5 | import org.springframework.boot.autoconfigure.SpringBootApplication 6 | import org.springframework.boot.runApplication 7 | import org.springframework.context.annotation.Bean 8 | import org.springframework.http.MediaType 9 | import org.springframework.stereotype.Component 10 | import org.springframework.web.reactive.function.client.WebClient 11 | import org.springframework.web.reactive.function.client.awaitBody 12 | import org.springframework.web.reactive.function.server.* 13 | 14 | @SpringBootApplication 15 | class CoroutinesApplication { 16 | 17 | @Bean 18 | fun routes(handlers: Handlers) = coRouter { 19 | GET("/", handlers::index) 20 | GET("/suspend", handlers::suspending) 21 | GET("/sequential-flow", handlers::sequentialFlow) 22 | GET("/concurrent-flow", handlers::concurrentFlow) 23 | GET("/error", handlers::error) 24 | } 25 | } 26 | 27 | data class Banner(val title: String, val message: String) 28 | 29 | @Suppress("DuplicatedCode") 30 | @Component 31 | class Handlers(builder: WebClient.Builder) { 32 | 33 | private val banner = Banner("title", "Lorem ipsum dolor sit amet, consectetur adipiscing elit.") 34 | 35 | private val client = builder.baseUrl("http://localhost:8080").build() 36 | 37 | suspend fun index(request: ServerRequest) = 38 | ServerResponse 39 | .ok() 40 | .renderAndAwait("index", mapOf("banner" to banner)) 41 | 42 | suspend fun suspending(request: ServerRequest) = 43 | ServerResponse 44 | .ok() 45 | .contentType(MediaType.APPLICATION_JSON) 46 | .bodyValueAndAwait(banner) 47 | 48 | suspend fun sequentialFlow(request: ServerRequest) = flow { 49 | for (i in 1..4) { 50 | emit(client 51 | .get() 52 | .uri("/suspend") 53 | .accept(MediaType.APPLICATION_JSON) 54 | .retrieve() 55 | .awaitBody())} 56 | }.let { ServerResponse 57 | .ok() 58 | .contentType(MediaType.APPLICATION_JSON) 59 | .bodyAndAwait(it) } 60 | 61 | // TODO Improve when https://github.com/Kotlin/kotlinx.coroutines/issues/1147 will be fixed 62 | suspend fun concurrentFlow(request: ServerRequest): ServerResponse = flow { 63 | for (i in 1..4) emit("/suspend") 64 | }.flatMapMerge { 65 | flow { 66 | emit(client 67 | .get() 68 | .uri(it) 69 | .accept(MediaType.APPLICATION_JSON) 70 | .retrieve() 71 | .awaitBody()) 72 | } 73 | }.let { ServerResponse 74 | .ok() 75 | .contentType(MediaType.APPLICATION_JSON) 76 | .bodyAndAwait(it) } 77 | 78 | suspend fun error(request: ServerRequest): ServerResponse { 79 | throw IllegalStateException() 80 | } 81 | } 82 | 83 | fun main(args: Array) { 84 | runApplication(*args) 85 | } 86 | -------------------------------------------------------------------------------- /src/main/kotlin/com/example/CoroutinesController.kt: -------------------------------------------------------------------------------- 1 | package com.example 2 | 3 | import kotlinx.coroutines.* 4 | import kotlinx.coroutines.flow.flatMapMerge 5 | import kotlinx.coroutines.flow.flow 6 | import org.springframework.http.MediaType 7 | import org.springframework.stereotype.Controller 8 | import org.springframework.ui.Model 9 | import org.springframework.ui.set 10 | import org.springframework.web.bind.annotation.GetMapping 11 | import org.springframework.web.bind.annotation.RequestMapping 12 | import org.springframework.web.bind.annotation.ResponseBody 13 | import org.springframework.web.reactive.function.client.WebClient 14 | import org.springframework.web.reactive.function.client.awaitBody 15 | import org.springframework.web.reactive.function.client.bodyToFlow 16 | 17 | @Controller 18 | @RequestMapping("/controller") 19 | class CoroutinesController(builder: WebClient.Builder) { 20 | 21 | private val banner = Banner("title", "Lorem ipsum dolor sit amet, consectetur adipiscing elit.") 22 | 23 | private val client = builder.baseUrl("http://localhost:8080/controller").build() 24 | 25 | @GetMapping("/suspend") @ResponseBody 26 | suspend fun suspendingEndpoint(): Banner { 27 | delay(10) 28 | return banner 29 | } 30 | 31 | @GetMapping("/deferred") @ResponseBody 32 | fun deferredEndpoint(): Deferred = GlobalScope.async { 33 | delay(10) 34 | banner 35 | } 36 | 37 | @GetMapping("/") 38 | suspend fun render(model: Model): String { 39 | delay(10) 40 | model["banner"] = banner 41 | return "index" 42 | } 43 | 44 | @GetMapping("/sequential-flow") @ResponseBody 45 | suspend fun sequentialFlow() = flow { 46 | for (i in 1..4) { 47 | emit(client 48 | .get() 49 | .uri("/suspend") 50 | .accept(MediaType.APPLICATION_JSON) 51 | .retrieve() 52 | .awaitBody())} 53 | } 54 | 55 | // TODO Improve when https://github.com/Kotlin/kotlinx.coroutines/issues/1147 will be fixed 56 | @GetMapping("/concurrent-flow") @ResponseBody 57 | suspend fun concurrentFlow() = flow { 58 | for (i in 1..4) emit("/suspend") 59 | }.flatMapMerge { 60 | flow { 61 | emit(client 62 | .get() 63 | .uri(it) 64 | .accept(MediaType.APPLICATION_JSON) 65 | .retrieve() 66 | .awaitBody()) 67 | } 68 | } 69 | 70 | 71 | @GetMapping("/flow-via-webclient") 72 | @ResponseBody 73 | suspend fun flowViaWebClient() = 74 | client.get() 75 | .uri("/concurrent-flow") 76 | .accept(MediaType.APPLICATION_JSON) 77 | .retrieve() 78 | .bodyToFlow() 79 | 80 | @GetMapping("/error") 81 | @ResponseBody 82 | suspend fun error() { 83 | throw IllegalStateException() 84 | } 85 | 86 | } -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sdeleuze/spring-boot-coroutines-demo/2703fab2efb7b441c61aee5933ea70c0950052c8/src/main/resources/application.properties -------------------------------------------------------------------------------- /src/main/resources/templates/index.mustache: -------------------------------------------------------------------------------- 1 | 2 | 3 |

{{banner.title}}

4 |

{{banner.message}}

5 | 6 | -------------------------------------------------------------------------------- /src/test/kotlin/com/example/CoroutinesApplicationTests.kt: -------------------------------------------------------------------------------- 1 | package com.example 2 | 3 | import org.junit.jupiter.api.Disabled 4 | import org.junit.jupiter.api.Test 5 | import org.springframework.beans.factory.annotation.Autowired 6 | import org.springframework.boot.test.context.SpringBootTest 7 | import org.springframework.boot.test.context.SpringBootTest.* 8 | import org.springframework.http.MediaType 9 | import org.springframework.test.web.reactive.server.WebTestClient 10 | import org.springframework.test.web.reactive.server.expectBody 11 | import org.springframework.test.web.reactive.server.expectBodyList 12 | 13 | @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) 14 | class CoroutinesApplicationTests(@Autowired val client: WebTestClient) { 15 | 16 | private val banner = Banner("title", "Lorem ipsum dolor sit amet, consectetur adipiscing elit.") 17 | 18 | @Test 19 | fun index() { 20 | client.get().uri("/").exchange().expectStatus().is2xxSuccessful.expectBody() 21 | } 22 | 23 | @Test 24 | fun suspending() { 25 | client.get() 26 | .uri("/suspend") 27 | .accept(MediaType.APPLICATION_JSON) 28 | .exchange() 29 | .expectStatus() 30 | .is2xxSuccessful 31 | .expectBody() 32 | .isEqualTo(banner) 33 | } 34 | 35 | @Test 36 | fun sequentialFlow() { 37 | client.get() 38 | .uri("/sequential-flow") 39 | .accept(MediaType.APPLICATION_JSON) 40 | .exchange() 41 | .expectStatus() 42 | .is2xxSuccessful 43 | .expectBodyList().contains(banner, banner, banner, banner) 44 | } 45 | 46 | @Test 47 | fun concurrentFlow() { 48 | client.get() 49 | .uri("/concurrent-flow") 50 | .accept(MediaType.APPLICATION_JSON) 51 | .exchange() 52 | .expectStatus() 53 | .is2xxSuccessful 54 | .expectBodyList().contains(banner, banner, banner, banner) 55 | } 56 | 57 | @Test 58 | fun error() { 59 | client.get().uri("/error").exchange().expectStatus().is5xxServerError 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/test/kotlin/com/example/CoroutinesControllerTests.kt: -------------------------------------------------------------------------------- 1 | package com.example 2 | 3 | import org.junit.jupiter.api.Test 4 | import org.springframework.beans.factory.annotation.Autowired 5 | import org.springframework.boot.test.context.SpringBootTest 6 | import org.springframework.boot.test.context.SpringBootTest.* 7 | import org.springframework.http.MediaType 8 | import org.springframework.test.web.reactive.server.WebTestClient 9 | import org.springframework.test.web.reactive.server.expectBody 10 | import org.springframework.test.web.reactive.server.expectBodyList 11 | 12 | @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) 13 | class CoroutinesControllerTests(@Autowired val client: WebTestClient) { 14 | 15 | private val banner = Banner("title", "Lorem ipsum dolor sit amet, consectetur adipiscing elit.") 16 | 17 | @Test 18 | fun index() { 19 | client.get().uri("/controller/").exchange().expectStatus().is2xxSuccessful.expectBody() 20 | } 21 | 22 | @Test 23 | fun suspending() { 24 | client.get() 25 | .uri("/controller/suspend") 26 | .accept(MediaType.APPLICATION_JSON) 27 | .exchange() 28 | .expectStatus() 29 | .is2xxSuccessful 30 | .expectBody() 31 | .isEqualTo(banner) 32 | } 33 | 34 | @Test 35 | fun sequentialFlow() { 36 | client.get() 37 | .uri("/controller/sequential-flow") 38 | .accept(MediaType.APPLICATION_JSON) 39 | .exchange() 40 | .expectStatus() 41 | .is2xxSuccessful 42 | .expectBodyList().contains(banner, banner, banner, banner) 43 | } 44 | 45 | @Test 46 | fun parallelFlow() { 47 | client.get() 48 | .uri("/controller/concurrent-flow") 49 | .accept(MediaType.APPLICATION_JSON) 50 | .exchange() 51 | .expectStatus() 52 | .is2xxSuccessful 53 | .expectBodyList().contains(banner, banner, banner, banner) 54 | } 55 | 56 | @Test 57 | fun flowViaWebClient() { 58 | client.get() 59 | .uri("/controller/flow-via-webclient") 60 | .accept(MediaType.APPLICATION_JSON) 61 | .exchange() 62 | .expectStatus() 63 | .is2xxSuccessful 64 | .expectBodyList().contains(banner, banner, banner, banner) 65 | } 66 | 67 | @Test 68 | fun error() { 69 | client.get().uri("/controller/error").exchange().expectStatus().is5xxServerError 70 | } 71 | 72 | } --------------------------------------------------------------------------------