|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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 | + * https://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 | + |
| 17 | +package org.springframework.web.reactive.result.method.annotation |
| 18 | + |
| 19 | +import kotlinx.coroutines.Deferred |
| 20 | +import kotlinx.coroutines.FlowPreview |
| 21 | +import kotlinx.coroutines.GlobalScope |
| 22 | +import kotlinx.coroutines.async |
| 23 | +import kotlinx.coroutines.delay |
| 24 | +import kotlinx.coroutines.flow.Flow |
| 25 | +import kotlinx.coroutines.flow.flow |
| 26 | +import org.junit.Assert.assertEquals |
| 27 | +import org.junit.Test |
| 28 | +import org.springframework.context.ApplicationContext |
| 29 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext |
| 30 | +import org.springframework.context.annotation.ComponentScan |
| 31 | +import org.springframework.context.annotation.Configuration |
| 32 | +import org.springframework.http.HttpHeaders |
| 33 | +import org.springframework.http.HttpStatus |
| 34 | +import org.springframework.web.bind.annotation.GetMapping |
| 35 | +import org.springframework.web.bind.annotation.RestController |
| 36 | +import org.springframework.web.client.HttpServerErrorException |
| 37 | +import org.springframework.web.reactive.config.EnableWebFlux |
| 38 | + |
| 39 | +@FlowPreview |
| 40 | +class CoroutinesIntegrationTests : AbstractRequestMappingIntegrationTests() { |
| 41 | + |
| 42 | + override fun initApplicationContext(): ApplicationContext { |
| 43 | + val context = AnnotationConfigApplicationContext() |
| 44 | + context.register(WebConfig::class.java) |
| 45 | + context.refresh() |
| 46 | + return context |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `Suspending handler method`() { |
| 51 | + val entity = performGet<String>("/suspend", HttpHeaders.EMPTY, String::class.java) |
| 52 | + assertEquals(HttpStatus.OK, entity.statusCode) |
| 53 | + assertEquals("foo", entity.body) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun `Handler method returning Deferred`() { |
| 58 | + val entity = performGet<String>("/deferred", HttpHeaders.EMPTY, String::class.java) |
| 59 | + assertEquals(HttpStatus.OK, entity.statusCode) |
| 60 | + assertEquals("foo", entity.body) |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun `Handler method returning Flow`() { |
| 65 | + val entity = performGet<String>("/flow", HttpHeaders.EMPTY, String::class.java) |
| 66 | + assertEquals(HttpStatus.OK, entity.statusCode) |
| 67 | + assertEquals("foobar", entity.body) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `Suspending handler method returning Flow`() { |
| 72 | + val entity = performGet<String>("/suspending-flow", HttpHeaders.EMPTY, String::class.java) |
| 73 | + assertEquals(HttpStatus.OK, entity.statusCode) |
| 74 | + assertEquals("foobar", entity.body) |
| 75 | + } |
| 76 | + |
| 77 | + @Test(expected = HttpServerErrorException.InternalServerError::class) |
| 78 | + fun `Suspending handler method throwing exception`() { |
| 79 | + performGet<String>("/error", HttpHeaders.EMPTY, String::class.java) |
| 80 | + } |
| 81 | + |
| 82 | + @Test(expected = HttpServerErrorException.InternalServerError::class) |
| 83 | + fun `Handler method returning Flow throwing exception`() { |
| 84 | + performGet<String>("/flow-error", HttpHeaders.EMPTY, String::class.java) |
| 85 | + } |
| 86 | + |
| 87 | + @Configuration |
| 88 | + @EnableWebFlux |
| 89 | + @ComponentScan(resourcePattern = "**/CoroutinesIntegrationTests*") |
| 90 | + open class WebConfig |
| 91 | + |
| 92 | + @RestController |
| 93 | + class CoroutinesController { |
| 94 | + |
| 95 | + @GetMapping("/suspend") |
| 96 | + suspend fun suspendingEndpoint(): String { |
| 97 | + delay(1) |
| 98 | + return "foo" |
| 99 | + } |
| 100 | + |
| 101 | + @GetMapping("/deferred") |
| 102 | + fun deferredEndpoint(): Deferred<String> = GlobalScope.async { |
| 103 | + delay(1) |
| 104 | + "foo" |
| 105 | + } |
| 106 | + |
| 107 | + @GetMapping("/flow") |
| 108 | + fun flowEndpoint()= flow { |
| 109 | + emit("foo") |
| 110 | + delay(1) |
| 111 | + emit("bar") |
| 112 | + delay(1) |
| 113 | + } |
| 114 | + |
| 115 | + @GetMapping("/suspending-flow") |
| 116 | + suspend fun suspendingFlowEndpoint(): Flow<String> { |
| 117 | + delay(10) |
| 118 | + return flow { |
| 119 | + emit("foo") |
| 120 | + delay(1) |
| 121 | + emit("bar") |
| 122 | + delay(1) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @GetMapping("/error") |
| 127 | + suspend fun error() { |
| 128 | + delay(1) |
| 129 | + throw IllegalStateException() |
| 130 | + } |
| 131 | + |
| 132 | + @GetMapping("/flow-error") |
| 133 | + suspend fun flowError() = flow<String> { |
| 134 | + delay(1) |
| 135 | + throw IllegalStateException() |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | +} |
0 commit comments