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
72 changes: 32 additions & 40 deletions jooby/src/main/kotlin/io/jooby/CoroutineRouter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,67 @@
*/
package io.jooby

import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import io.jooby.Router.*
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext

internal class RouterCoroutineScope(coroutineContext: CoroutineContext) : CoroutineScope {
override val coroutineContext = coroutineContext
}
internal class RouterCoroutineScope(override val coroutineContext: CoroutineContext) : CoroutineScope

class CoroutineRouter(val coroutineStart: CoroutineStart, val router: Router) {

val coroutineScope: CoroutineScope by lazy {
RouterCoroutineScope(router.worker.asCoroutineDispatcher())
}

@RouterDsl
fun get(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
return route(Router.GET, pattern, handler)
private var extendCoroutineContext: (CoroutineContext) -> CoroutineContext = { it }
fun launchContext(block: (CoroutineContext) -> CoroutineContext) {
extendCoroutineContext = block
}

@RouterDsl
fun post(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
return route(Router.POST, pattern, handler)
}
fun get(pattern: String, handler: suspend HandlerContext.() -> Any) =
route(GET, pattern, handler)

@RouterDsl
fun put(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
return route(Router.PUT, pattern, handler)
}
fun post(pattern: String, handler: suspend HandlerContext.() -> Any) =
route(POST, pattern, handler)

@RouterDsl
fun delete(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
return route(Router.DELETE, pattern, handler)
}
fun put(pattern: String, handler: suspend HandlerContext.() -> Any) =
route(PUT, pattern, handler)

@RouterDsl
fun patch(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
return route(Router.PATCH, pattern, handler)
}
fun delete(pattern: String, handler: suspend HandlerContext.() -> Any) =
route(DELETE, pattern, handler)

@RouterDsl
fun head(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
return route(Router.HEAD, pattern, handler)
}
fun patch(pattern: String, handler: suspend HandlerContext.() -> Any) =
route(PATCH, pattern, handler)

@RouterDsl
fun trace(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
return route(Router.TRACE, pattern, handler)
}
fun head(pattern: String, handler: suspend HandlerContext.() -> Any) =
route(HEAD, pattern, handler)

@RouterDsl
fun options(pattern: String, handler: suspend HandlerContext.() -> Any): Route {
return route(Router.OPTIONS, pattern, handler)
}
fun trace(pattern: String, handler: suspend HandlerContext.() -> Any) =
route(TRACE, pattern, handler)

fun route(method: String, pattern: String, handler: suspend HandlerContext.() -> Any): Route {
return router.route(method, pattern) { ctx ->
val xhandler = CoroutineExceptionHandler { _, x ->
ctx.sendError(x)
}
coroutineScope.launch(xhandler, coroutineStart) {
@RouterDsl
fun options(pattern: String, handler: suspend HandlerContext.() -> Any) =
route(OPTIONS, pattern, handler)

fun route(method: String, pattern: String, handler: suspend HandlerContext.() -> Any): Route =
router.route(method, pattern) { ctx ->
launch(ctx) {
val result = handler(HandlerContext(ctx))
if (result != ctx) {
ctx.render(result)
}
}
}.setHandle(handler)
.attribute("coroutine", true)
}.setHandle(handler).attribute("coroutine", true)

internal fun launch(ctx: Context, block: suspend CoroutineScope.() -> Unit) {
val exceptionHandler = CoroutineExceptionHandler { _, x -> ctx.sendError(x) }
coroutineScope.launch(extendCoroutineContext(exceptionHandler), coroutineStart, block)
}
}
15 changes: 6 additions & 9 deletions jooby/src/main/kotlin/io/jooby/internal/mvc/CoroutineLauncher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,22 @@ package io.jooby.internal.mvc
import io.jooby.Context
import io.jooby.CoroutineRouter
import io.jooby.Route
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.launch
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn

/**
* Used by compiled MVC-style routes with suspend functions
*/
class CoroutineLauncher(val next: Route.Handler) : Route.Handler {
override fun apply(ctx: Context): Any {
override fun apply(ctx: Context) = ctx.also {
val router = ctx.router.attribute<CoroutineRouter>("coroutineRouter")
val exceptionHandler = CoroutineExceptionHandler { _, x ->
ctx.sendError(x)
}
router.coroutineScope.launch(exceptionHandler, router.coroutineStart) {
router.launch(ctx) {
val result = suspendCoroutineUninterceptedOrReturn<Any> {
ctx.attribute("___continuation", it)
next.apply(ctx)
}
if (!ctx.isResponseStarted) {
ctx.render(result!!)
ctx.render(result)
}
}
return ctx
}
}
51 changes: 51 additions & 0 deletions jooby/src/test/kotlin/io/jooby/CoroutineRouterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.jooby

import io.jooby.Router.GET
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineStart
import org.junit.jupiter.api.Test
import org.mockito.ArgumentCaptor
import org.mockito.Mockito.*
import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.CoroutineContext

class CoroutineRouterTest {
private val router = mock(Router::class.java, RETURNS_DEEP_STUBS)
private val ctx = mock(Context::class.java)

@Test
fun withoutLaunchContext() {
CoroutineRouter(CoroutineStart.UNDISPATCHED, router).apply {
get("/path") { "Result" }
}

val handlerCaptor = ArgumentCaptor.forClass(Route.Handler::class.java)
verify(router).route(eq(GET), eq("/path"), handlerCaptor.capture())
handlerCaptor.value.apply(ctx)

verify(ctx).render("Result")
}

@Test
fun launchContext_isRunEveryTime() {
val mockCoroutineContext = mock(CoroutineContext::class.java)
`when`(mockCoroutineContext.plus(any() ?: mockCoroutineContext)).thenReturn(mockCoroutineContext, ExtraContext())

CoroutineRouter(CoroutineStart.DEFAULT, router).apply {
launchContext { mockCoroutineContext + it + ExtraContext() }
get("/path") { "Result" }
}

val handlerCaptor = ArgumentCaptor.forClass(Route.Handler::class.java)
verify(router).route(eq(GET), eq("/path"), handlerCaptor.capture())
verifyNoInteractions(mockCoroutineContext)

handlerCaptor.value.apply(ctx)
verify(mockCoroutineContext).plus(argThat { it is CoroutineExceptionHandler } ?: mockCoroutineContext)
verify(mockCoroutineContext).plus(argThat { it is ExtraContext } ?: mockCoroutineContext)
}

class ExtraContext : AbstractCoroutineContextElement(Key) {
companion object Key : CoroutineContext.Key<ExtraContext>
}
}