Skip to content

Commit

Permalink
Fix EXEC without MULTI when using coroutines over async #1441
Browse files Browse the repository at this point in the history
  • Loading branch information
sokomishalov authored and mp911de committed Oct 26, 2020
1 parent c619497 commit 30076e9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
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()
}
}

}

0 comments on commit 30076e9

Please sign in to comment.