CoroutinesAnnotationTransactionInterceptorTests.kt (spring-framework-5.3.23) | : | CoroutinesAnnotationTransactionInterceptorTests.kt (spring-framework-5.3.24) | ||
---|---|---|---|---|
/* | /* | |||
* Copyright 2002-2020 the original author or authors. | * Copyright 2002-2022 the original author or authors. | |||
* | * | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | * You may obtain a copy of the License at | |||
* | * | |||
* https://www.apache.org/licenses/LICENSE-2.0 | * https://www.apache.org/licenses/LICENSE-2.0 | |||
* | * | |||
* Unless required by applicable law or agreed to in writing, software | * Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | * distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
skipping to change at line 24 | skipping to change at line 24 | |||
* limitations under the License. | * limitations under the License. | |||
*/ | */ | |||
package org.springframework.transaction.annotation | package org.springframework.transaction.annotation | |||
import kotlinx.coroutines.delay | import kotlinx.coroutines.delay | |||
import kotlinx.coroutines.flow.Flow | import kotlinx.coroutines.flow.Flow | |||
import kotlinx.coroutines.flow.flow | import kotlinx.coroutines.flow.flow | |||
import kotlinx.coroutines.flow.toList | import kotlinx.coroutines.flow.toList | |||
import kotlinx.coroutines.runBlocking | import kotlinx.coroutines.runBlocking | |||
import org.assertj.core.api.Assertions | import org.assertj.core.api.Assertions.assertThat | |||
import org.assertj.core.api.Assertions.fail | ||||
import org.junit.jupiter.api.Test | import org.junit.jupiter.api.Test | |||
import org.springframework.aop.framework.ProxyFactory | import org.springframework.aop.framework.ProxyFactory | |||
import org.springframework.transaction.interceptor.TransactionInterceptor | import org.springframework.transaction.interceptor.TransactionInterceptor | |||
import org.springframework.transaction.testfixture.ReactiveCallCountingTransacti onManager | import org.springframework.transaction.testfixture.ReactiveCallCountingTransacti onManager | |||
/** | /** | |||
* @author Sebastien Deleuze | * @author Sebastien Deleuze | |||
*/ | */ | |||
class CoroutinesAnnotationTransactionInterceptorTests { | class CoroutinesAnnotationTransactionInterceptorTests { | |||
skipping to change at line 63 | skipping to change at line 64 | |||
val proxyFactory = ProxyFactory() | val proxyFactory = ProxyFactory() | |||
proxyFactory.setTarget(TestWithCoroutines()) | proxyFactory.setTarget(TestWithCoroutines()) | |||
proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) | proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) | |||
val proxy = proxyFactory.proxy as TestWithCoroutines | val proxy = proxyFactory.proxy as TestWithCoroutines | |||
runBlocking { | runBlocking { | |||
try { | try { | |||
proxy.suspendingNoValueFailure() | proxy.suspendingNoValueFailure() | |||
} | } | |||
catch (ex: IllegalStateException) { | catch (ex: IllegalStateException) { | |||
} | } | |||
} | } | |||
assertReactiveGetTransactionAndRollbackCount(1) | assertReactiveGetTransactionAndRollbackCount(1) | |||
} | } | |||
@Test | @Test | |||
fun suspendingValueSuccess() { | fun suspendingValueSuccess() { | |||
val proxyFactory = ProxyFactory() | val proxyFactory = ProxyFactory() | |||
proxyFactory.setTarget(TestWithCoroutines()) | proxyFactory.setTarget(TestWithCoroutines()) | |||
proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) | proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) | |||
val proxy = proxyFactory.proxy as TestWithCoroutines | val proxy = proxyFactory.proxy as TestWithCoroutines | |||
runBlocking { | runBlocking { | |||
Assertions.assertThat(proxy.suspendingValueSuccess()).isE qualTo("foo") | assertThat(proxy.suspendingValueSuccess()).isEqualTo("foo ") | |||
} | } | |||
assertReactiveGetTransactionAndCommitCount(1) | assertReactiveGetTransactionAndCommitCount(1) | |||
} | } | |||
@Test | @Test | |||
fun suspendingValueFailure() { | fun suspendingValueFailure() { | |||
val proxyFactory = ProxyFactory() | val proxyFactory = ProxyFactory() | |||
proxyFactory.setTarget(TestWithCoroutines()) | proxyFactory.setTarget(TestWithCoroutines()) | |||
proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) | proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) | |||
val proxy = proxyFactory.proxy as TestWithCoroutines | val proxy = proxyFactory.proxy as TestWithCoroutines | |||
runBlocking { | runBlocking { | |||
try { | try { | |||
proxy.suspendingValueFailure() | proxy.suspendingValueFailure() | |||
Assertions.fail("No exception thrown as expected" ) | fail("No exception thrown as expected") | |||
} | } | |||
catch (ex: IllegalStateException) { | catch (ex: IllegalStateException) { | |||
} | } | |||
} | } | |||
assertReactiveGetTransactionAndRollbackCount(1) | assertReactiveGetTransactionAndRollbackCount(1) | |||
} | } | |||
@Test | @Test | |||
fun suspendingFlowSuccess() { | fun suspendingFlowSuccess() { | |||
val proxyFactory = ProxyFactory() | val proxyFactory = ProxyFactory() | |||
proxyFactory.setTarget(TestWithCoroutines()) | proxyFactory.setTarget(TestWithCoroutines()) | |||
proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) | proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) | |||
val proxy = proxyFactory.proxy as TestWithCoroutines | val proxy = proxyFactory.proxy as TestWithCoroutines | |||
runBlocking { | runBlocking { | |||
Assertions.assertThat(proxy.suspendingFlowSuccess().toLis t()).containsExactly("foo", "foo") | assertThat(proxy.suspendingFlowSuccess().toList()).contai nsExactly("foo", "foo") | |||
} | } | |||
assertReactiveGetTransactionAndCommitCount(1) | assertReactiveGetTransactionAndCommitCount(1) | |||
} | } | |||
@Test | @Test | |||
fun flowSuccess() { | fun flowSuccess() { | |||
val proxyFactory = ProxyFactory() | val proxyFactory = ProxyFactory() | |||
proxyFactory.setTarget(TestWithCoroutines()) | proxyFactory.setTarget(TestWithCoroutines()) | |||
proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) | proxyFactory.addAdvice(TransactionInterceptor(rtm, source)) | |||
val proxy = proxyFactory.proxy as TestWithCoroutines | val proxy = proxyFactory.proxy as TestWithCoroutines | |||
runBlocking { | runBlocking { | |||
Assertions.assertThat(proxy.flowSuccess().toList()).conta insExactly("foo", "foo") | assertThat(proxy.flowSuccess().toList()).containsExactly( "foo", "foo") | |||
} | } | |||
assertReactiveGetTransactionAndCommitCount(1) | assertReactiveGetTransactionAndCommitCount(1) | |||
} | } | |||
private fun assertReactiveGetTransactionAndCommitCount(expectedCount: Int ) { | private fun assertReactiveGetTransactionAndCommitCount(expectedCount: Int ) { | |||
Assertions.assertThat(rtm.begun).isEqualTo(expectedCount) | assertThat(rtm.begun).isEqualTo(expectedCount) | |||
Assertions.assertThat(rtm.commits).isEqualTo(expectedCount) | assertThat(rtm.commits).isEqualTo(expectedCount) | |||
} | } | |||
private fun assertReactiveGetTransactionAndRollbackCount(expectedCount: I nt) { | private fun assertReactiveGetTransactionAndRollbackCount(expectedCount: I nt) { | |||
Assertions.assertThat(rtm.begun).isEqualTo(expectedCount) | assertThat(rtm.begun).isEqualTo(expectedCount) | |||
Assertions.assertThat(rtm.rollbacks).isEqualTo(expectedCount) | assertThat(rtm.rollbacks).isEqualTo(expectedCount) | |||
} | } | |||
@Transactional | @Transactional | |||
open class TestWithCoroutines { | open class TestWithCoroutines { | |||
open suspend fun suspendingNoValueSuccess() { | open suspend fun suspendingNoValueSuccess() { | |||
delay(10) | delay(10) | |||
} | } | |||
open suspend fun suspendingNoValueFailure() { | open suspend fun suspendingNoValueFailure() { | |||
End of changes. 9 change blocks. | ||||
11 lines changed or deleted | 11 lines changed or added |