Skip to content

Commit 6db503b

Browse files
committed
#17 Coverage
1 parent 2fbf892 commit 6db503b

File tree

9 files changed

+158
-11
lines changed

9 files changed

+158
-11
lines changed

src/main/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/shardedcluster/RedisShardedClusterContextCustomizer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ internal class RedisShardedClusterContextCustomizer(
102102
.toList()
103103

104104
private fun createClient(addresses: List<Pair<String, Int>>): JedisCluster {
105-
return JedisCluster(addresses.map { HostAndPort(it.first, it.second) }.first())
105+
return JedisCluster(addresses.map { HostAndPort(it.first, it.second) }.toSet())
106106
}
107107

108108
private fun setSpringProperties(context: ConfigurableApplicationContext, addresses: List<Pair<String, Int>>) {

src/test/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/RedisTests.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ import org.springframework.context.ApplicationContextAware
1111
import org.springframework.context.annotation.Scope
1212
import org.springframework.data.redis.core.RedisTemplate
1313
import org.springframework.stereotype.Component
14+
import redis.embedded.RedisShardedCluster
15+
import java.time.Duration
1416
import java.util.*
1517
import kotlin.random.Random
1618

19+
private val INITIALIZATION_TIMEOUT_PROP = RedisShardedCluster::class.java.declaredFields
20+
.filter { it.name == "initializationTimeout" }
21+
.map { field -> field.isAccessible = true; field }
22+
.first()
23+
1724
/**
1825
* Used for sharing common test logic across different test classes.
1926
*/
@@ -148,6 +155,14 @@ internal open class RedisTests(
148155
val conf = RedisConfParser.parse(RedisConfLocator.locate(server))
149156
return RedisConfAssertion(conf)
150157
}
158+
159+
fun shouldHaveInitializationTimeout(timeout: Duration): EmbeddedRedis {
160+
val server = context?.let { RedisStore.server(it) }!!
161+
assertThat(server).isInstanceOf(RedisShardedCluster::class.java)
162+
val configuredTimeout = INITIALIZATION_TIMEOUT_PROP[server as RedisShardedCluster]
163+
assertThat(configuredTimeout).isEqualTo(timeout)
164+
return this
165+
}
151166
}
152167

153168
inner class RedisConfAssertion(val conf: RedisConf) {

src/test/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/server/ExecutionDirTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test
77
import org.springframework.beans.factory.annotation.Autowired
88

99
@IntegrationTest
10-
@EmbeddedRedisServer(executeInDirectory = "build", port = 10002)
10+
@EmbeddedRedisServer(executeInDirectory = "build")
1111
@DisplayName("Using @EmbeddedRedisServer with custom execution dir")
1212
internal class ExecutionDirTest {
1313

src/test/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/server/NoBindTest.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import org.junit.jupiter.api.DisplayName
66
import org.junit.jupiter.api.Test
77
import org.springframework.beans.factory.annotation.Autowired
88

9-
private const val TEST_PORT = 10003
10-
119
@IntegrationTest
12-
@EmbeddedRedisServer(bind = "", port = TEST_PORT)
10+
@EmbeddedRedisServer(bind = "")
1311
@DisplayName("Using @EmbeddedRedisServer with empty bind")
1412
internal class NoBindTest {
1513

src/test/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/server/RedisConfFileTest.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import org.junit.jupiter.api.DisplayName
66
import org.junit.jupiter.api.Test
77
import org.springframework.beans.factory.annotation.Autowired
88

9-
private const val TEST_PORT = 10004
109

1110
@IntegrationTest
1211
@EmbeddedRedisServer(
13-
port = TEST_PORT,
1412
configFile = "src/test/resources/server/redis.conf"
1513
)
1614
@DisplayName("Using @EmbeddedRedisServer with config file")
@@ -26,8 +24,7 @@ internal class RedisConfFileTest {
2624
.whenDoingNothing()
2725
.then().redisProperties()
2826
.shouldBeStandalone().and()
29-
.shouldHaveHost("localhost").and()
30-
.shouldHavePort(TEST_PORT)
27+
.shouldHaveHost("localhost")
3128
}
3229

3330
@Test

src/test/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/server/RedisServerContextCustomizerTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import redis.embedded.Redis
1414
@DisplayName("Tests for RedisServerContextCustomizer")
1515
internal class RedisServerContextCustomizerTest {
1616

17-
val givenCustomizer = RedisServerContextCustomizer(mockk<EmbeddedRedisServer>(), mockk<PortProvider>())
17+
private val givenCustomizer = RedisServerContextCustomizer(mockk<EmbeddedRedisServer>(), mockk<PortProvider>())
1818

1919
@Test
2020
@DisplayName("RedisServerContextCustomizer.equals() should be true for itself")
@@ -44,7 +44,7 @@ internal class RedisServerContextCustomizerTest {
4444
var client: UnifiedJedis? = null
4545
AnnotationConfigApplicationContext().use {
4646
RedisServerContextCustomizerFactory()
47-
.createContextCustomizer(AnnotatedClass::class.java, mutableListOf())!!
47+
.createContextCustomizer(AnnotatedClass::class.java, mutableListOf())
4848
.customizeContext(it, mockk())
4949
it.refresh()
5050
it.start()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.github.tobi.laa.spring.boot.embedded.redis.shardedcluster
2+
3+
import io.github.tobi.laa.spring.boot.embedded.redis.IntegrationTest
4+
import io.github.tobi.laa.spring.boot.embedded.redis.RedisTests
5+
import io.github.tobi.laa.spring.boot.embedded.redis.shardedcluster.EmbeddedRedisShardedCluster.Shard
6+
import org.junit.jupiter.api.DisplayName
7+
import org.junit.jupiter.api.Test
8+
import org.springframework.beans.factory.annotation.Autowired
9+
import java.time.Duration.ofSeconds
10+
11+
@IntegrationTest
12+
@EmbeddedRedisShardedCluster(
13+
shards = [
14+
Shard(replicas = 1),
15+
Shard(replicas = 2),
16+
Shard(replicas = 3)],
17+
ports = [6380, 7000, 7001, 7002, 7003, 7004, 7005, 11111, 22222]
18+
)
19+
@DisplayName("Using @EmbeddedRedisShardedCluster with custom settings")
20+
internal class CustomSettingsTest {
21+
22+
@Autowired
23+
private lateinit var given: RedisTests
24+
25+
@Test
26+
@DisplayName("RedisProperties should have the customized values")
27+
fun redisPropertiesShouldHaveCustomizedValues() {
28+
given.nothing()
29+
.whenDoingNothing()
30+
.then().redisProperties()
31+
.shouldBeCluster()
32+
.shouldHaveNode("127.0.0.1", 6380)
33+
.shouldHaveNode("127.0.0.1", 7000)
34+
.shouldHaveNode("127.0.0.1", 7001)
35+
.shouldHaveNode("127.0.0.1", 7002)
36+
.shouldHaveNode("127.0.0.1", 7003)
37+
.shouldHaveNode("127.0.0.1", 7004)
38+
.shouldHaveNode("127.0.0.1", 7005)
39+
.shouldHaveNode("127.0.0.1", 11111)
40+
.shouldHaveNode("127.0.0.1", 22222)
41+
.andAlso().embeddedRedis()
42+
.shouldHaveInitializationTimeout(ofSeconds(20))
43+
}
44+
45+
@Test
46+
@DisplayName("It should be possible to write to Redis and the data should be available afterwards")
47+
fun givenRandomTestdata_writingToRedis_dataShouldBeAvailable() {
48+
given.randomTestdata()
49+
.whenRedis().isBeingWrittenTo()
50+
.then().redis().shouldContainTheTestdata()
51+
}
52+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.tobi.laa.spring.boot.embedded.redis.shardedcluster
2+
3+
import io.github.tobi.laa.spring.boot.embedded.redis.IntegrationTest
4+
import io.github.tobi.laa.spring.boot.embedded.redis.RedisTests
5+
import org.junit.jupiter.api.DisplayName
6+
import org.junit.jupiter.api.Test
7+
import org.springframework.beans.factory.annotation.Autowired
8+
9+
@IntegrationTest
10+
@EmbeddedRedisShardedCluster(executeInDirectory = "build")
11+
@DisplayName("Using @EmbeddedRedisShardedCluster with custom execution dir")
12+
internal class ExecutionDirTest {
13+
14+
@Autowired
15+
private lateinit var given: RedisTests
16+
17+
@Test
18+
@DisplayName("It should be possible to write to Redis and the data should be available afterwards")
19+
fun givenRandomTestdata_writingToRedis_dataShouldBeAvailable() {
20+
given.randomTestdata()
21+
.whenRedis().isBeingWrittenTo()
22+
.then().redis().shouldContainTheTestdata()
23+
}
24+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.github.tobi.laa.spring.boot.embedded.redis.shardedcluster
2+
3+
import io.github.tobi.laa.spring.boot.embedded.redis.RedisStore
4+
import io.github.tobi.laa.spring.boot.embedded.redis.ports.PortProvider
5+
import io.mockk.mockk
6+
import org.assertj.core.api.Assertions.assertThat
7+
import org.assertj.core.api.Assertions.assertThatThrownBy
8+
import org.junit.jupiter.api.DisplayName
9+
import org.junit.jupiter.api.Test
10+
import org.springframework.context.annotation.AnnotationConfigApplicationContext
11+
import redis.clients.jedis.UnifiedJedis
12+
import redis.embedded.Redis
13+
14+
@DisplayName("Tests for RedisShardedClusterContextCustomizer")
15+
internal class RedisShardedClusterContextCustomizerTest {
16+
17+
private val givenCustomizer =
18+
RedisShardedClusterContextCustomizer(mockk<EmbeddedRedisShardedCluster>(), mockk<PortProvider>())
19+
20+
@Test
21+
@DisplayName("RedisShardedClusterContextCustomizer.equals() should be true for itself")
22+
fun sameObject_equals_shouldBeTrue() {
23+
val comparedToSelf = givenCustomizer.equals(givenCustomizer)
24+
assertThat(comparedToSelf).isTrue()
25+
}
26+
27+
@Test
28+
@DisplayName("RedisShardedClusterContextCustomizer.equals() should be false for null objects")
29+
fun null_equals_shouldBeFalse() {
30+
val comparedToDifferentClass = givenCustomizer.equals(null)
31+
assertThat(comparedToDifferentClass).isFalse()
32+
}
33+
34+
@Test
35+
@DisplayName("RedisShardedClusterContextCustomizer.equals() should be false for object with different class")
36+
fun differentClass_equals_shouldBeFalse() {
37+
val comparedToDifferentClass = givenCustomizer.equals("I'm not a RedisServerContextCustomizer")
38+
assertThat(comparedToDifferentClass).isFalse()
39+
}
40+
41+
@Test
42+
@DisplayName("Closing ApplicationContext should stop Redis server and Redis client")
43+
fun closingApplicationContext_shouldStopRedisServerAndRedisClient() {
44+
var server: Redis? = null
45+
var client: UnifiedJedis? = null
46+
AnnotationConfigApplicationContext().use {
47+
RedisShardedClusterContextCustomizerFactory()
48+
.createContextCustomizer(AnnotatedClass::class.java, mutableListOf())
49+
.customizeContext(it, mockk())
50+
it.refresh()
51+
it.start()
52+
server = RedisStore.server(it)
53+
client = RedisStore.client(it)
54+
}
55+
assertThat(server!!.isActive).isFalse()
56+
assertThatThrownBy { client!!.get("FOO") }.isInstanceOf(Exception::class.java)
57+
}
58+
59+
@EmbeddedRedisShardedCluster
60+
private class AnnotatedClass
61+
}

0 commit comments

Comments
 (0)