Skip to content

Commit c67170a

Browse files
committed
Simplify API
1 parent 8d9358e commit c67170a

File tree

2 files changed

+14
-56
lines changed

2 files changed

+14
-56
lines changed

extensions/arc/deployment/src/test/kotlin/io/quarkus/arc/kotlin/RequestContextCoroutineContextTest.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import jakarta.enterprise.context.RequestScoped
66
import jakarta.inject.Inject
77
import kotlinx.coroutines.CompletableDeferred
88
import kotlinx.coroutines.Dispatchers
9+
import kotlinx.coroutines.async
910
import kotlinx.coroutines.coroutineScope
1011
import kotlinx.coroutines.delay
1112
import kotlinx.coroutines.launch
1213
import kotlinx.coroutines.test.runTest
14+
import kotlinx.coroutines.withContext
1315
import org.jboss.shrinkwrap.api.spec.JavaArchive
1416
import org.junit.jupiter.api.AfterEach
1517
import org.junit.jupiter.api.Assertions
@@ -60,7 +62,7 @@ class RequestContextCoroutineContextTest {
6062

6163
// WHEN we run a block
6264
runTest {
63-
withPropagatedContext(Dispatchers.IO) {
65+
withContext(Dispatchers.IO.withCdiContext()) {
6466
// THEN the request context should not be active
6567
Assertions.assertFalse(
6668
Arc.container().requestContext().isActive,
@@ -83,7 +85,7 @@ class RequestContextCoroutineContextTest {
8385
// WHEN we run a block with async
8486
runTest {
8587
coroutineScope {
86-
asyncWithPropagatedContext(Dispatchers.IO) {
88+
async(Dispatchers.IO.withCdiContext()) {
8789
// THEN the request context should not be active
8890
Assertions.assertFalse(
8991
Arc.container().requestContext().isActive,
@@ -115,7 +117,7 @@ class RequestContextCoroutineContextTest {
115117

116118
// WHEN we run a block with the request context
117119
runTest {
118-
withPropagatedContext(Dispatchers.IO) {
120+
withContext(Dispatchers.IO.withCdiContext()) {
119121
// THEN the request context should be active
120122
Assertions.assertTrue(
121123
Arc.container().requestContext().isActive,
@@ -169,7 +171,7 @@ class RequestContextCoroutineContextTest {
169171
// WHEN we run a block with async
170172
runTest {
171173
coroutineScope {
172-
asyncWithPropagatedContext(Dispatchers.IO) {
174+
async(Dispatchers.IO.withCdiContext()) {
173175
// THEN the request context should be active
174176
Assertions.assertTrue(
175177
Arc.container().requestContext().isActive,
@@ -225,7 +227,7 @@ class RequestContextCoroutineContextTest {
225227
// WHEN we run a block with async
226228
runTest {
227229
coroutineScope {
228-
asyncWithPropagatedContext(Dispatchers.IO) {
230+
async(Dispatchers.IO.withCdiContext()) {
229231
coroutineScope {
230232
// THEN the request context should be active
231233
Assertions.assertTrue(
@@ -289,7 +291,7 @@ class RequestContextCoroutineContextTest {
289291
// WHEN we run a block with async
290292
runTest {
291293
val job = launch {
292-
asyncWithPropagatedContext(Dispatchers.IO) {
294+
async(Dispatchers.IO.withCdiContext()) {
293295
// THEN the request context should be active
294296
Assertions.assertTrue(
295297
Arc.container().requestContext().isActive,
@@ -368,7 +370,7 @@ class RequestContextCoroutineContextTest {
368370
runTest {
369371
val jobFirstRequest = launch {
370372
Arc.container().requestContext().activate(firstRequestState)
371-
asyncWithPropagatedContext(Dispatchers.IO) {
373+
async(Dispatchers.IO.withCdiContext()) {
372374
// THEN the request context should be active
373375
Assertions.assertTrue(
374376
Arc.container().requestContext().isActive,
@@ -402,7 +404,7 @@ class RequestContextCoroutineContextTest {
402404

403405
val jobSecondRequest = launch {
404406
Arc.container().requestContext().activate(secondRequestState)
405-
asyncWithPropagatedContext(Dispatchers.IO) {
407+
async(Dispatchers.IO.withCdiContext()) {
406408
// THEN the request context should be active
407409
Assertions.assertTrue(
408410
Arc.container().requestContext().isActive,

extensions/arc/kotlin/src/main/kotlin/io/quarkus/arc/kotlin/RequestContextCoroutineContext.kt

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,18 @@ import io.quarkus.arc.Arc
44
import io.quarkus.arc.InjectableContext
55
import io.quarkus.arc.ManagedContext
66
import kotlin.coroutines.CoroutineContext
7-
import kotlin.coroutines.EmptyCoroutineContext
8-
import kotlinx.coroutines.CoroutineScope
9-
import kotlinx.coroutines.CoroutineStart
10-
import kotlinx.coroutines.Deferred
117
import kotlinx.coroutines.ThreadContextElement
12-
import kotlinx.coroutines.async
13-
import kotlinx.coroutines.withContext
148

159
/**
16-
* A suspending function that executes a block of code within the Quarkus Request Context.
10+
* This function extends the CoroutineContext to include the Quarkus Request Context if it is
11+
* active.
1712
*
18-
* This function captures the current request context and ensures it is activated when the coroutine
19-
* resumes on a thread.
20-
*
21-
* If the request context is finalized before the block completes, it results in undefined behavior.
22-
*
23-
* Will not start a request context if there is none active at the time of invocation.
24-
*
25-
* @param context The CoroutineContext to use for the coroutine.
26-
* @param block The block of code to execute within the request context.
27-
* @return The result of the block execution.
28-
*/
29-
suspend fun <T> withPropagatedContext(
30-
context: CoroutineContext,
31-
block: suspend CoroutineScope.() -> T,
32-
): T {
33-
return withContext(context = context.appendRequestContextToCoroutineContext(), block = block)
34-
}
35-
36-
/**
37-
* An async function that executes a block of code within the Quarkus Request Context.
38-
*
39-
* This function captures the current request context and ensures it is activated when the coroutine
40-
* resumes on a thread.
41-
*
42-
* If the caller finalizes the request context before the block is executed, results in undefined
13+
* If the caller finalizes the request context before the coroutine resumes, it results in undefined
4314
* behavior.
4415
*
4516
* Will not start a request context if there is none active at the time of invocation.
46-
*
47-
* @param context The CoroutineContext to use for the coroutine.
48-
* @param block The block of code to execute within the request context.
4917
*/
50-
fun <T> CoroutineScope.asyncWithPropagatedContext(
51-
context: CoroutineContext = EmptyCoroutineContext,
52-
start: CoroutineStart = CoroutineStart.DEFAULT,
53-
block: suspend CoroutineScope.() -> T,
54-
): Deferred<T> {
55-
return async(
56-
context = context.appendRequestContextToCoroutineContext(),
57-
start = start,
58-
block = block,
59-
)
60-
}
61-
62-
fun CoroutineContext.appendRequestContextToCoroutineContext(): CoroutineContext {
18+
fun CoroutineContext.withCdiContext(): CoroutineContext {
6319
val requestContext: ManagedContext? = Arc.container()?.requestContext()
6420
return if (requestContext == null) {
6521
this

0 commit comments

Comments
 (0)