|
| 1 | +package org.springframework.transaction.annotation |
| 2 | + |
| 3 | +import kotlinx.coroutines.delay |
| 4 | +import kotlinx.coroutines.runBlocking |
| 5 | +import org.assertj.core.api.Assertions |
| 6 | +import org.junit.jupiter.api.Disabled |
| 7 | +import org.junit.jupiter.api.Test |
| 8 | +import org.springframework.aop.framework.ProxyFactory |
| 9 | +import org.springframework.transaction.TransactionManager |
| 10 | +import org.springframework.transaction.interceptor.TransactionInterceptor |
| 11 | +import org.springframework.transaction.testfixture.CallCountingTransactionManager |
| 12 | +import org.springframework.transaction.testfixture.ReactiveCallCountingTransactionManager |
| 13 | + |
| 14 | +class CoroutinesAnnotationTransactionInterceptorTests { |
| 15 | + |
| 16 | + private val ptm = CallCountingTransactionManager() |
| 17 | + |
| 18 | + private val rtm = ReactiveCallCountingTransactionManager() |
| 19 | + |
| 20 | + private val source = AnnotationTransactionAttributeSource() |
| 21 | + |
| 22 | + private val ti = TransactionInterceptor((ptm as TransactionManager), source) |
| 23 | + |
| 24 | + @Test |
| 25 | + fun suspendingNoValueSuccess() { |
| 26 | + val proxyFactory = ProxyFactory() |
| 27 | + proxyFactory.setTarget(TestWithCoroutines()) |
| 28 | + proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) |
| 29 | + val proxy = proxyFactory.proxy as TestWithCoroutines |
| 30 | + runBlocking { |
| 31 | + proxy.suspendingNoValueSuccess() |
| 32 | + } |
| 33 | + assertReactiveGetTransactionAndCommitCount(1) |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun suspendingNoValueFailure() { |
| 38 | + val proxyFactory = ProxyFactory() |
| 39 | + proxyFactory.setTarget(TestWithCoroutines()) |
| 40 | + proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) |
| 41 | + val proxy = proxyFactory.proxy as TestWithCoroutines |
| 42 | + runBlocking { |
| 43 | + try { |
| 44 | + proxy.suspendingNoValueFailure() |
| 45 | + } |
| 46 | + catch (ex: IllegalStateException) { |
| 47 | + } |
| 48 | + |
| 49 | + } |
| 50 | + assertReactiveGetTransactionAndRollbackCount(1) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @Disabled("Currently fails due to gh-25998") |
| 55 | + fun suspendingValueSuccess() { |
| 56 | + val proxyFactory = ProxyFactory() |
| 57 | + proxyFactory.setTarget(TestWithCoroutines()) |
| 58 | + proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) |
| 59 | + val proxy = proxyFactory.proxy as TestWithCoroutines |
| 60 | + runBlocking { |
| 61 | + Assertions.assertThat(proxy.suspendingValueSuccess()).isEqualTo("foo") |
| 62 | + } |
| 63 | + assertReactiveGetTransactionAndCommitCount(1) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun suspendingValueFailure() { |
| 68 | + val proxyFactory = ProxyFactory() |
| 69 | + proxyFactory.setTarget(TestWithCoroutines()) |
| 70 | + proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) |
| 71 | + val proxy = proxyFactory.proxy as TestWithCoroutines |
| 72 | + runBlocking { |
| 73 | + try { |
| 74 | + proxy.suspendingValueFailure() |
| 75 | + } |
| 76 | + catch (ex: IllegalStateException) { |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + assertReactiveGetTransactionAndRollbackCount(1) |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + private fun assertReactiveGetTransactionAndCommitCount(expectedCount: Int) { |
| 86 | + Assertions.assertThat(rtm.begun).isEqualTo(expectedCount) |
| 87 | + Assertions.assertThat(rtm.commits).isEqualTo(expectedCount) |
| 88 | + } |
| 89 | + |
| 90 | + private fun assertReactiveGetTransactionAndRollbackCount(expectedCount: Int) { |
| 91 | + Assertions.assertThat(rtm.begun).isEqualTo(expectedCount) |
| 92 | + Assertions.assertThat(rtm.rollbacks).isEqualTo(expectedCount) |
| 93 | + } |
| 94 | + |
| 95 | + @Transactional |
| 96 | + open class TestWithCoroutines { |
| 97 | + |
| 98 | + open suspend fun suspendingNoValueSuccess() { |
| 99 | + delay(10) |
| 100 | + } |
| 101 | + |
| 102 | + open suspend fun suspendingNoValueFailure() { |
| 103 | + delay(10) |
| 104 | + throw IllegalStateException() |
| 105 | + } |
| 106 | + |
| 107 | + open suspend fun suspendingValueSuccess(): String { |
| 108 | + delay(10) |
| 109 | + return "foo" |
| 110 | + } |
| 111 | + |
| 112 | + open suspend fun suspendingValueFailure(): String { |
| 113 | + delay(10) |
| 114 | + throw IllegalStateException() |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments