|
17 | 17 | package org.springframework.transaction.annotation |
18 | 18 |
|
19 | 19 | import kotlinx.coroutines.delay |
| 20 | +import kotlinx.coroutines.flow.Flow |
| 21 | +import kotlinx.coroutines.flow.flow |
| 22 | +import kotlinx.coroutines.flow.toList |
20 | 23 | import kotlinx.coroutines.runBlocking |
21 | 24 | import org.assertj.core.api.Assertions |
22 | 25 | import org.junit.jupiter.api.Test |
@@ -83,14 +86,38 @@ class CoroutinesAnnotationTransactionInterceptorTests { |
83 | 86 | runBlocking { |
84 | 87 | try { |
85 | 88 | proxy.suspendingValueFailure() |
| 89 | + Assertions.fail("No exception thrown as expected") |
86 | 90 | } |
87 | 91 | catch (ex: IllegalStateException) { |
88 | 92 | } |
89 | | - |
90 | 93 | } |
91 | 94 | assertReactiveGetTransactionAndRollbackCount(1) |
92 | 95 | } |
93 | 96 |
|
| 97 | + @Test |
| 98 | + fun suspendingFlowSuccess() { |
| 99 | + val proxyFactory = ProxyFactory() |
| 100 | + proxyFactory.setTarget(TestWithCoroutines()) |
| 101 | + proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) |
| 102 | + val proxy = proxyFactory.proxy as TestWithCoroutines |
| 103 | + runBlocking { |
| 104 | + Assertions.assertThat(proxy.suspendingFlowSuccess().toList()).containsExactly("foo", "foo") |
| 105 | + } |
| 106 | + assertReactiveGetTransactionAndCommitCount(1) |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun flowSuccess() { |
| 111 | + val proxyFactory = ProxyFactory() |
| 112 | + proxyFactory.setTarget(TestWithCoroutines()) |
| 113 | + proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) |
| 114 | + val proxy = proxyFactory.proxy as TestWithCoroutines |
| 115 | + runBlocking { |
| 116 | + Assertions.assertThat(proxy.flowSuccess().toList()).containsExactly("foo", "foo") |
| 117 | + } |
| 118 | + assertReactiveGetTransactionAndCommitCount(1) |
| 119 | + } |
| 120 | + |
94 | 121 | private fun assertReactiveGetTransactionAndCommitCount(expectedCount: Int) { |
95 | 122 | Assertions.assertThat(rtm.begun).isEqualTo(expectedCount) |
96 | 123 | Assertions.assertThat(rtm.commits).isEqualTo(expectedCount) |
@@ -122,5 +149,22 @@ class CoroutinesAnnotationTransactionInterceptorTests { |
122 | 149 | delay(10) |
123 | 150 | throw IllegalStateException() |
124 | 151 | } |
| 152 | + |
| 153 | + open fun flowSuccess(): Flow<String> { |
| 154 | + return flow { |
| 155 | + emit("foo") |
| 156 | + delay(10) |
| 157 | + emit("foo") |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + open suspend fun suspendingFlowSuccess(): Flow<String> { |
| 162 | + delay(10) |
| 163 | + return flow { |
| 164 | + emit("foo") |
| 165 | + delay(10) |
| 166 | + emit("foo") |
| 167 | + } |
| 168 | + } |
125 | 169 | } |
126 | 170 | } |
0 commit comments