Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EXEC without MULTI when using coroutines over async #1441

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ import kotlinx.coroutines.future.await
* @since 6.0
*/
@ExperimentalLettuceCoroutinesApi
suspend inline fun <K, V> RedisAsyncCommands<K, V>.multi(action: RedisAsyncCommands<K, V>.() -> Unit): TransactionResult {
suspend inline fun <K, V> RedisAsyncCommands<K, V>.multi(action: RedisAsyncCommands<K, V>.() -> Unit): TransactionResult = try {
multi().await()
runCatching {
action.invoke(this)
}.onFailure {
discard()
}
return exec().await()
action.invoke(this)
exec().await()
} catch (thr: Throwable) {
discard().await()
throw thr
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ import io.lettuce.core.TransactionResult
* @since 6.0
*/
@ExperimentalLettuceCoroutinesApi
suspend inline fun <K : Any, V : Any> RedisCoroutinesCommands<K, V>.multi(action: RedisCoroutinesCommands<K, V>.() -> Unit): TransactionResult? {
suspend inline fun <K : Any, V : Any> RedisCoroutinesCommands<K, V>.multi(action: RedisCoroutinesCommands<K, V>.() -> Unit): TransactionResult? = try {
multi()
runCatching {
action.invoke(this)
}.onFailure {
discard()
}
return exec()
action.invoke(this)
exec()
} catch (thr: Throwable) {
discard()
throw thr
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ import io.lettuce.core.TransactionResult
* @since 6.0
*/
@ExperimentalLettuceCoroutinesApi
inline fun <K, V> RedisCommands<K, V>.multi(action: RedisCommands<K, V>.() -> Unit): TransactionResult {
inline fun <K, V> RedisCommands<K, V>.multi(action: RedisCommands<K, V>.() -> Unit): TransactionResult = try {
multi()
runCatching {
action.invoke(this)
}.onFailure {
discard()
}
return exec()
action.invoke(this)
exec()
} catch (thr: Throwable) {
discard()
throw thr
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.lettuce.core.api.StatefulRedisConnection
import io.lettuce.core.api.async.multi
import io.lettuce.core.api.sync.multi
import io.lettuce.test.LettuceExtension
import kotlinx.coroutines.future.await
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -62,4 +63,22 @@ class TransactionExtensionsIntegrationTests : TestSupport() {
}
}

@Test
@Inject
internal fun shouldDiscardMultiClosureOverAsync(connection: StatefulRedisConnection<String, String>) {

runBlocking {
val transactionResult = runCatching {
connection.async().multi {
set("key", "value")
throw RedisCommandExecutionException("oops")
}
}

assertThat(transactionResult.isFailure).isTrue()
assertThat(transactionResult.exceptionOrNull()).isInstanceOf(RedisCommandExecutionException::class.java)
assertThat(connection.async().get("key").await()).isNull()
}
}

}