Skip to content

Commit

Permalink
use testContainers for Redis tests (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
geomagilles authored Nov 4, 2023
1 parent f621813 commit a37f35b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 47 deletions.
5 changes: 3 additions & 2 deletions infinitic-storage/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ dependencies {
implementation(Libs.Compress.commons)

// Redis
implementation("redis.clients:jedis:4.3.2")
testImplementation("com.github.kstyrc:embedded-redis:0.6")
implementation("redis.clients:jedis:5.0.2")
// For integration tests
testImplementation("org.testcontainers:testcontainers:1.19.1")

// MySql
// For connection pooling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,67 +23,79 @@
package io.infinitic.storage.redis

import io.infinitic.storage.Bytes
import io.infinitic.storage.DockerOnly
import io.infinitic.storage.config.Redis
import io.infinitic.storage.config.redis.RedisKeySetStorage
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import redis.embedded.RedisServer
import org.testcontainers.containers.GenericContainer
import org.testcontainers.utility.DockerImageName

@EnabledIf(DockerOnly::class)
class RedisKeySetStorageTests :
StringSpec({
val redisServer = RedisServer(6380).also { it.start() }
val config = Redis("localhost", 6380)
val storage = RedisKeySetStorage.of(config)
StringSpec(
{
val redisServer =
GenericContainer(DockerImageName.parse("redis:7.2.3")).withExposedPorts(6379).also {
it.start()
}
val config = Redis(host = redisServer.host, port = redisServer.firstMappedPort)
val storage = RedisKeySetStorage.of(config)

afterSpec {
config.close()
redisServer.stop()
}
afterSpec {
config.close()
redisServer.stop()
}

beforeTest { storage.add("foo", "bar".toByteArray()) }
beforeTest { storage.add("foo", "bar".toByteArray()) }

afterTest { storage.flush() }
afterTest { storage.flush() }

fun equalsTo(set1: Set<ByteArray>, set2: Set<ByteArray>) =
set1.map { Bytes(it) }.toSet() == set2.map { Bytes(it) }.toSet()
fun equalsTo(set1: Set<ByteArray>, set2: Set<ByteArray>) =
set1.map { Bytes(it) }.toSet() == set2.map { Bytes(it) }.toSet()

"get should return an empty set on unknown key" {
storage.get("unknown") shouldBe setOf<ByteArray>()
}
"get should return an empty set on unknown key" {
storage.get("unknown") shouldBe setOf()
}

"get should return the set on known key" {
equalsTo(storage.get("foo"), setOf("bar".toByteArray())) shouldBe true
}
"get should return the set on known key" {
equalsTo(storage.get("foo"), setOf("bar".toByteArray())) shouldBe true
}

"add on unknown key should create a new set" {
storage.add("unknown", "42".toByteArray())
"add on unknown key should create a new set" {
storage.add("unknown", "42".toByteArray())

equalsTo(storage.get("unknown"), setOf("42".toByteArray())) shouldBe true
}
equalsTo(storage.get("unknown"), setOf("42".toByteArray())) shouldBe true
}

"add on known key should add to set" {
storage.add("foo", "42".toByteArray())
"add on known key should add to set" {
storage.add("foo", "42".toByteArray())

equalsTo(storage.get("foo"), setOf("42".toByteArray(), "bar".toByteArray())) shouldBe true
}
equalsTo(storage.get("foo"), setOf("42".toByteArray(), "bar".toByteArray())) shouldBe
true
}

"add known value on known key should do nothing" {
storage.add("foo", "bar".toByteArray())
"add known value on known key should do nothing" {
storage.add("foo", "bar".toByteArray())

equalsTo(storage.get("foo"), setOf("bar".toByteArray())) shouldBe true
}
equalsTo(storage.get("foo"), setOf("bar".toByteArray())) shouldBe true
}

"remove on unknown key should do nothing" { storage.remove("unknown", "42".toByteArray()) }
"remove on unknown key should do nothing" {
storage.remove("unknown", "42".toByteArray())
}

"remove unknown value on known key should do nothing" {
storage.remove("foo", "42".toByteArray())
"remove unknown value on known key should do nothing" {
storage.remove("foo", "42".toByteArray())

equalsTo(storage.get("foo"), setOf("bar".toByteArray())) shouldBe true
}
equalsTo(storage.get("foo"), setOf("bar".toByteArray())) shouldBe true
}

"remove known value on known key should remove from set" {
storage.remove("foo", "bar".toByteArray())
"remove known value on known key should remove from set" {
storage.remove("foo", "bar".toByteArray())

equalsTo(storage.get("foo"), setOf()) shouldBe true
}
})
equalsTo(storage.get("foo"), setOf()) shouldBe true
}
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,27 @@
*/
package io.infinitic.storage.redis

import io.infinitic.storage.DockerOnly
import io.infinitic.storage.config.Redis
import io.infinitic.storage.config.redis.RedisKeyValueStorage
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import redis.embedded.RedisServer
import org.testcontainers.containers.GenericContainer
import org.testcontainers.utility.DockerImageName

@EnabledIf(DockerOnly::class)
class RedisKeyValueStorageTests :
StringSpec({
val redisServer = RedisServer(6380).also { it.start() }
val storage = RedisKeyValueStorage.of(Redis("localhost", 6380))
val redisServer =
GenericContainer(DockerImageName.parse("redis:7.2.3")).withExposedPorts(6379).also {
it.start()
}
val config = Redis(host = redisServer.host, port = redisServer.firstMappedPort)
val storage = RedisKeyValueStorage.of(config)

afterSpec {
Redis.close()
config.close()
redisServer.stop()
}

Expand Down

0 comments on commit a37f35b

Please sign in to comment.