From 62042a5d6dc1a4ac369138da064de0326a47b10c Mon Sep 17 00:00:00 2001 From: wonwoo Date: Sun, 1 Mar 2020 13:44:31 +0900 Subject: [PATCH] DATAREDIS-1111 - Add missing list and zset coroutine extensions. Original Pull Request: #514 --- .../core/ReactiveListOperationsExtensions.kt | 18 ++++++ .../core/ReactiveZSetOperationsExtensions.kt | 27 +++++++++ ...activeListOperationsExtensionsUnitTests.kt | 30 ++++++++++ ...activeZSetOperationsExtensionsUnitTests.kt | 60 +++++++++++++++++++ 4 files changed, 135 insertions(+) diff --git a/src/main/kotlin/org/springframework/data/redis/core/ReactiveListOperationsExtensions.kt b/src/main/kotlin/org/springframework/data/redis/core/ReactiveListOperationsExtensions.kt index 33ce27ec4..4ce0dc3de 100644 --- a/src/main/kotlin/org/springframework/data/redis/core/ReactiveListOperationsExtensions.kt +++ b/src/main/kotlin/org/springframework/data/redis/core/ReactiveListOperationsExtensions.kt @@ -201,6 +201,24 @@ suspend fun ReactiveListOperations.rightPopAndAwait(key suspend fun ReactiveListOperations.rightPopAndAwait(key: K, timeout: Duration): V? = rightPop(key, timeout).awaitFirstOrNull() +/** + * Coroutines variant of [ReactiveListOperations.rightPopAndLeftPush]. + * + * @author Wonwoo Lee + * @since 2.3 + */ +suspend fun ReactiveListOperations.rightPopAndLeftPushAndAwait(key: K, destinationKey: K): V? = + rightPopAndLeftPush(key, destinationKey).awaitFirstOrNull() + +/** + * Coroutines variant of [ReactiveListOperations.rightPopAndLeftPush]. + * + * @author Wonwoo Lee + * @since 2.3 + */ +suspend fun ReactiveListOperations.rightPopAndLeftPushAndAwait(key: K, destinationKey: K, timeout: Duration): V? = + rightPopAndLeftPush(key, destinationKey, timeout).awaitFirstOrNull() + /** * Coroutines variant of [ReactiveListOperations.delete]. * diff --git a/src/main/kotlin/org/springframework/data/redis/core/ReactiveZSetOperationsExtensions.kt b/src/main/kotlin/org/springframework/data/redis/core/ReactiveZSetOperationsExtensions.kt index 7a83d9ef5..b7c55e149 100644 --- a/src/main/kotlin/org/springframework/data/redis/core/ReactiveZSetOperationsExtensions.kt +++ b/src/main/kotlin/org/springframework/data/redis/core/ReactiveZSetOperationsExtensions.kt @@ -159,6 +159,15 @@ fun ReactiveZSetOperations.reverseRangeByScoreWithScore suspend fun ReactiveZSetOperations.countAndAwait(key: K, range: Range): Long = count(key, range).awaitSingle() +/** + * Coroutines variant of [ReactiveZSetOperations.size]. + * + * @author Wonwoo Lee + * @since 2.3 + */ +suspend fun ReactiveZSetOperations.sizeAndAwait(key: K): Long = + size(key).awaitSingle() + /** * Coroutines variant of [ReactiveZSetOperations.score]. * @@ -258,6 +267,24 @@ suspend fun ReactiveZSetOperations.intersectAndStoreAnd suspend fun ReactiveZSetOperations.intersectAndStoreAndAwait(key: K, otherKeys: Collection, destKey: K, aggregate: RedisZSetCommands.Aggregate, weights: RedisZSetCommands.Weights): Long = intersectAndStore(key, otherKeys, destKey, aggregate, weights).awaitSingle() +/** + * Coroutines variant of [ReactiveZSetOperations.rangeByLex]. + * + * @author Wonwoo Lee + * @since 2.3 + */ +fun ReactiveZSetOperations.rangeByLexAndAwait(key: K, range: Range, limit: Limit? = null): Flow = + (if (limit == null) rangeByLex(key, range) else rangeByLex(key, range, limit)).asFlow() + +/** + * Coroutines variant of [ReactiveZSetOperations.reverseRangeByLex]. + * + * @author Wonwoo Lee + * @since 2.3 + */ +fun ReactiveZSetOperations.reverseRangeByLexAndAwait(key: K, range: Range, limit: Limit? = null): Flow = + (if (limit == null) reverseRangeByLex(key, range) else reverseRangeByLex(key, range, limit)).asFlow() + /** * Coroutines variant of [ReactiveZSetOperations.delete]. * diff --git a/src/test/kotlin/org/springframework/data/redis/core/ReactiveListOperationsExtensionsUnitTests.kt b/src/test/kotlin/org/springframework/data/redis/core/ReactiveListOperationsExtensionsUnitTests.kt index 03a48ed0b..1203a0cdc 100644 --- a/src/test/kotlin/org/springframework/data/redis/core/ReactiveListOperationsExtensionsUnitTests.kt +++ b/src/test/kotlin/org/springframework/data/redis/core/ReactiveListOperationsExtensionsUnitTests.kt @@ -334,6 +334,36 @@ class ReactiveListOperationsExtensionsUnitTests { } } + @Test + fun rightPopAndLeftPush() { + + val operations = mockk>() + every { operations.rightPopAndLeftPush(any(), any()) } returns Mono.just("foo") + + runBlocking { + assertThat(operations.rightPopAndLeftPushAndAwait("foo", "bar")).isEqualTo("foo") + } + + verify { + operations.rightPopAndLeftPush("foo", "bar") + } + } + + @Test + fun blockingRightPopAndLeftPush() { + + val operations = mockk>() + every { operations.rightPopAndLeftPush(any(), any(), Duration.ofDays(1)) } returns Mono.just("foo") + + runBlocking { + assertThat(operations.rightPopAndLeftPushAndAwait("foo", "bar", Duration.ofDays(1))).isEqualTo("foo") + } + + verify { + operations.rightPopAndLeftPush("foo", "bar", Duration.ofDays(1)) + } + } + @Test // DATAREDIS-937 fun delete() { diff --git a/src/test/kotlin/org/springframework/data/redis/core/ReactiveZSetOperationsExtensionsUnitTests.kt b/src/test/kotlin/org/springframework/data/redis/core/ReactiveZSetOperationsExtensionsUnitTests.kt index 5a897d941..f33b21c10 100644 --- a/src/test/kotlin/org/springframework/data/redis/core/ReactiveZSetOperationsExtensionsUnitTests.kt +++ b/src/test/kotlin/org/springframework/data/redis/core/ReactiveZSetOperationsExtensionsUnitTests.kt @@ -304,6 +304,21 @@ class ReactiveZSetOperationsExtensionsUnitTests { } } + @Test + fun size() { + + val operations = mockk>() + every { operations.size(any()) } returns Mono.just(1) + + runBlocking { + assertThat(operations.sizeAndAwait("foo")).isEqualTo(1) + } + + verify { + operations.size("foo") + } + } + @Test // DATAREDIS-937 fun score() { @@ -483,4 +498,49 @@ class ReactiveZSetOperationsExtensionsUnitTests { operations.intersectAndStore("foo", listOf("bar"), "baz", Aggregate.MAX, Weights.fromSetCount(1)) } } + + @Test + fun rangeByLex() { + + val operations = mockk>() + every { operations.rangeByLex(any(), any()) } returns Flux.just("bar") + + runBlocking { + assertThat(operations.rangeByLexAndAwait("foo", Range.just("bar")).toList()).contains("bar") + } + + verify { + operations.rangeByLex("foo", Range.just("bar")) + } + } + + @Test + fun reverseRangeByLexAndAwait() { + + val operations = mockk>() + every { operations.reverseRangeByLex(any(), any()) } returns Flux.just("bar") + + runBlocking { + assertThat(operations.reverseRangeByLexAndAwait("foo", Range.just("bar")).toList()).contains("bar") + } + + verify { + operations.reverseRangeByLex("foo", Range.just("bar")) + } + } + + @Test + fun delete() { + + val operations = mockk>() + every { operations.delete(any()) } returns Mono.just(true) + + runBlocking { + assertThat(operations.deleteAndAwait("foo")).isTrue() + } + + verify { + operations.delete("foo") + } + } }