Skip to content

Commit

Permalink
enhance test cases (#16)
Browse files Browse the repository at this point in the history
* Change lua script return types

* enhance test cases
  • Loading branch information
himadieievsv authored Jan 1, 2024
1 parent e5846fc commit 096dd66
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class RedLockTest {

@Test
fun `lock throws exception`() {
every { backend.setLock(eq("test"), any(), any()) } throws RuntimeException()
every { backend.setLock(eq("test"), any(), any()) } throws RuntimeException("test exception")

val redLock = RedLock(listOf(backend))
val permit = redLock.lock("test")
Expand All @@ -76,7 +76,7 @@ class RedLockTest {

@Test
fun `lock throws cancellation exception`() {
every { backend.setLock(eq("test"), any(), any()) } throws CancellationException()
every { backend.setLock(eq("test"), any(), any()) } throws CancellationException("test exception")
every { backend.removeLock(eq("test"), any()) } returns "OK"

val redLock = RedLock(listOf(backend))
Expand All @@ -90,7 +90,7 @@ class RedLockTest {

@Test
fun `unlock throws exception`() {
every { backend.removeLock(eq("test"), any()) } throws RuntimeException()
every { backend.removeLock(eq("test"), any()) } throws RuntimeException("test exception")

val redLock = RedLock(listOf(backend))
redLock.unlock("test")
Expand All @@ -101,7 +101,7 @@ class RedLockTest {

@Test
fun `unlock throws cancellation exception`() {
every { backend.removeLock(eq("test"), any()) } throws CancellationException()
every { backend.removeLock(eq("test"), any()) } throws CancellationException("test exception")

val redLock = RedLock(listOf(backend))
redLock.unlock("test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ class SemaphoreTest {
@ParameterizedTest(name = "lock acquired with ttl - {0}")
@ValueSource(ints = [-123, -1, 0, 1, 2, 5, 7, 10])
fun `validate ttl`(ttl: Int) {
// every { redis.eval(any(), any<List<String>>(), any<List<String>>()) } returns "OK"
every {
backend.setSemaphoreLock(
eq("semaphore:leasers:test"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class JedisLocksBackendTest {
it.equalsTo(SetParams().nx().px(200.milliseconds.inWholeMilliseconds))
},
)
} throws IOException()
} throws IOException("test exception")
val permit = lock.setLock("test", clientId, 200.milliseconds)

assertNull(permit)
Expand Down Expand Up @@ -121,7 +121,7 @@ class JedisLocksBackendTest {
@Test
fun `remove lock throws exception`() {
val clientId = "uuid"
every { redis.eval(any(), eq(listOf("test")), eq(listOf(clientId))) } throws IOException()
every { redis.eval(any(), eq(listOf("test")), eq(listOf(clientId))) } throws IOException("test exception")
val permit = lock.removeLock("test", clientId)

assertNull(permit)
Expand Down Expand Up @@ -163,7 +163,7 @@ class JedisLocksBackendTest {
val clientId = "uuid"
every {
redis.eval(any<String>(), eq(listOf("test-key1", "test-key2")), eq(listOf(clientId, "10", "100")))
} throws IOException()
} throws IOException("test exception")
val permit = lock.setSemaphoreLock("test-key1", "test-key2", clientId, 10, 100.milliseconds)

assertNull(permit)
Expand Down Expand Up @@ -202,7 +202,8 @@ class JedisLocksBackendTest {
every { redis.pipelined() } returns pipelined
every { pipelined.srem(any<String>(), any()) } returns mockk()
every { pipelined.del(any<String>()) } returns mockk()
every { pipelined.sync() } throws IOException()
every { pipelined.sync() } throws IOException("test exception")
every { pipelined.close() } returns Unit
val permit = lock.removeSemaphoreLock("test-key1", "test-key2", clientId)

assertNull(permit)
Expand Down Expand Up @@ -241,7 +242,13 @@ class JedisLocksBackendTest {

@Test
fun `clean up semaphore locks throws exception`() {
every { redis.eval(any(), eq(listOf("test-key")), eq(listOf("test-key-prefix"))) } throws IOException()
every {
redis.eval(
any(),
eq(listOf("test-key")),
eq(listOf("test-key-prefix")),
)
} throws IOException("test exception")
val permit = lock.cleanUpExpiredSemaphoreLocks("test-key", "test-key-prefix")

assertNull(permit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ internal class LettuceLocksBackend(private val redis: LettucePooled<String, Stri
if redis.call("get", KEYS[1]) == ARGV[1] then
return redis.call("del", KEYS[1])
end
return nil
return 0
""".trimIndent()
return failsafe(null) {
convertToString(
redis.sync { sync -> sync.eval(luaScript, ScriptOutputType.VALUE, arrayOf(resourceName), clientId) },
redis.sync { sync -> sync.eval(luaScript, ScriptOutputType.INTEGER, arrayOf(resourceName), clientId) },
)
}
}
Expand Down Expand Up @@ -99,13 +99,14 @@ internal class LettuceLocksBackend(private val redis: LettucePooled<String, Stri
redis.call("srem", leasersKey, leaser)
end
end
return "OK"
""".trimIndent()
return failsafe(null) {
convertToString(
redis.sync { sync ->
sync.eval(
luaScript,
ScriptOutputType.VALUE,
ScriptOutputType.STATUS,
arrayOf(leasersKey),
leaserValidityKeyPrefix,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ class LettuceLocksBackendTest {
fun `set lock failed`() {
val clientId = "uuid"
every {
sync.set(eq("test"), eq(clientId), eq(SetArgs().nx().px(10.seconds.inWholeMilliseconds)))
sync.set(
eq("test"),
eq(clientId),
match { it.equalsTo(SetArgs().nx().px(10.seconds.inWholeMilliseconds)) },
)
} returns null
val permit = backend.setLock("test", clientId, 10.seconds)

Expand All @@ -78,8 +82,12 @@ class LettuceLocksBackendTest {
fun `set lock throws exception`() {
val clientId = "uuid"
every {
sync.set(eq("test"), eq(clientId), eq(SetArgs().nx().px(200.milliseconds.inWholeMilliseconds)))
} throws IOException()
sync.set(
eq("test"),
eq(clientId),
match { it.equalsTo(SetArgs().nx().px(200.milliseconds.inWholeMilliseconds)) },
)
} throws IOException("test exception")
val permit = backend.setLock("test", clientId, 200.milliseconds)

assertNull(permit)
Expand All @@ -92,13 +100,13 @@ class LettuceLocksBackendTest {
fun `remove lock successful`() {
val clientId = "uuid"
every {
sync.eval<String>(any<String>(), eq(ScriptOutputType.VALUE), eq(arrayOf("test")), eq(clientId))
sync.eval<String>(any<String>(), eq(ScriptOutputType.INTEGER), eq(arrayOf("test")), eq(clientId))
} returns "OK"
val permit = backend.removeLock("test", clientId)

assertEquals("OK", permit)
verify(exactly = 1) {
sync.eval<String>(any<String>(), eq(ScriptOutputType.VALUE), eq(arrayOf("test")), eq(clientId))
sync.eval<String>(any<String>(), eq(ScriptOutputType.INTEGER), eq(arrayOf("test")), eq(clientId))
}
verify(exactly = 0) {
sync.set(any<String>(), any(), any())
Expand All @@ -109,7 +117,7 @@ class LettuceLocksBackendTest {
fun `remove lock failed`() {
val clientId = "uuid"
every {
sync.eval<String>(any<String>(), eq(ScriptOutputType.VALUE), eq(arrayOf("test")), eq(clientId))
sync.eval<String>(any<String>(), eq(ScriptOutputType.INTEGER), eq(arrayOf("test")), eq(clientId))
} returns null
val permit = backend.removeLock("test", clientId)

Expand All @@ -120,8 +128,8 @@ class LettuceLocksBackendTest {
fun `remove lock throws exception`() {
val clientId = "uuid"
every {
sync.eval<String>(any<String>(), eq(ScriptOutputType.VALUE), eq(arrayOf("test")), eq(clientId))
} throws IOException()
sync.eval<String>(any<String>(), eq(ScriptOutputType.INTEGER), eq(arrayOf("test")), eq(clientId))
} throws IOException("test exception")
val permit = backend.removeLock("test", clientId)

assertNull(permit)
Expand Down Expand Up @@ -198,7 +206,7 @@ class LettuceLocksBackendTest {
eq("10"),
eq("100"),
)
} throws IOException()
} throws IOException("test exception")
val permit = backend.setSemaphoreLock("test-key1", "test-key2", clientId, 10, 100.milliseconds)

assertNull(permit)
Expand Down Expand Up @@ -235,7 +243,7 @@ class LettuceLocksBackendTest {
fun `remove semaphore lock failed`() {
val clientId = "uuid"
every { sync.srem(eq("test-key1"), eq(clientId)) } returns 1
every { sync.del(eq("test-key2")) } throws IOException()
every { sync.del(eq("test-key2")) } throws IOException("test exception")
val permit = backend.removeSemaphoreLock("test-key1", "test-key2", clientId)

assertNull(permit)
Expand All @@ -253,7 +261,7 @@ class LettuceLocksBackendTest {
every {
sync.eval<String>(
any<String>(),
eq(ScriptOutputType.VALUE),
eq(ScriptOutputType.STATUS),
eq(arrayOf("test-key")),
eq("test-key-prefix"),
)
Expand All @@ -264,7 +272,7 @@ class LettuceLocksBackendTest {
verify(exactly = 1) {
sync.eval<String>(
any<String>(),
eq(ScriptOutputType.VALUE),
eq(ScriptOutputType.STATUS),
eq(arrayOf("test-key")),
eq("test-key-prefix"),
)
Expand All @@ -276,7 +284,7 @@ class LettuceLocksBackendTest {
every {
sync.eval<String>(
any<String>(),
eq(ScriptOutputType.VALUE),
eq(ScriptOutputType.STATUS),
eq(arrayOf("test-key")),
eq("test-key-prefix"),
)
Expand All @@ -287,7 +295,7 @@ class LettuceLocksBackendTest {
verify(exactly = 1) {
sync.eval<String>(
any<String>(),
eq(ScriptOutputType.VALUE),
eq(ScriptOutputType.STATUS),
eq(arrayOf("test-key")),
eq("test-key-prefix"),
)
Expand All @@ -299,18 +307,18 @@ class LettuceLocksBackendTest {
every {
sync.eval<String>(
any<String>(),
eq(ScriptOutputType.VALUE),
eq(ScriptOutputType.STATUS),
eq(arrayOf("test-key")),
eq("test-key-prefix"),
)
} throws IOException()
} throws IOException("test exception")
val permit = backend.cleanUpExpiredSemaphoreLocks("test-key", "test-key-prefix")

assertNull(permit)
verify(exactly = 1) {
sync.eval<String>(
any<String>(),
eq(ScriptOutputType.VALUE),
eq(ScriptOutputType.STATUS),
eq(arrayOf("test-key")),
eq("test-key-prefix"),
)
Expand Down

0 comments on commit 096dd66

Please sign in to comment.