-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57e54e8
commit f67dd0a
Showing
6 changed files
with
127 additions
and
39 deletions.
There are no files selected for viewing
8 changes: 5 additions & 3 deletions
8
src/test/resources/docker-compose.yml → docker/master-slave/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
version: '3.9' | ||
|
||
services: | ||
|
||
redis-master: | ||
image: "bitnami/redis:7.2" | ||
hostname: redis-master | ||
ports: | ||
- "${REDIS_MASTER_PORT}:6379" | ||
networks: | ||
- redis-sentinel | ||
environment: | ||
- REDIS_REPLICATION_MODE=master | ||
- ALLOW_EMPTY_PASSWORD=yes | ||
|
||
redis-slave: | ||
image: "bitnami/redis:7.2" | ||
hostname: redis-slave | ||
ports: | ||
- "${REDIS_SLAVE_PORT}:6379" | ||
networks: | ||
- redis-sentinel | ||
environment: | ||
- REDIS_REPLICATION_MODE=slave | ||
- REDIS_MASTER_HOST=redis-master | ||
- ALLOW_EMPTY_PASSWORD=yes | ||
depends_on: | ||
- redis-master | ||
|
||
redis-sentinel-1: | ||
image: "bitnami/redis-sentinel:7.2" | ||
hostname: redis-sentinel-1 | ||
ports: | ||
- "${REDIS_SENTINEL_1_PORT}:26379" | ||
networks: | ||
- redis-sentinel | ||
environment: | ||
- REDIS_MASTER_SET=mymaster | ||
- REDIS_MASTER_HOST=redis-master | ||
- REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS=10000 | ||
- ALLOW_EMPTY_PASSWORD=yes | ||
depends_on: | ||
- redis-master | ||
- redis-slave | ||
|
||
redis-sentinel-2: | ||
image: "bitnami/redis-sentinel:7.2" | ||
hostname: redis-sentinel-2 | ||
ports: | ||
- "${REDIS_SENTINEL_2_PORT}:26379" | ||
networks: | ||
- redis-sentinel | ||
environment: | ||
- REDIS_MASTER_SET=mymaster | ||
- REDIS_MASTER_HOST=redis-master | ||
- REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS=10000 | ||
- ALLOW_EMPTY_PASSWORD=yes | ||
depends_on: | ||
- redis-master | ||
- redis-slave | ||
|
||
redis-sentinel-3: | ||
image: "bitnami/redis-sentinel:7.2" | ||
hostname: redis-sentinel-3 | ||
ports: | ||
- "${REDIS_SENTINEL_3_PORT}:26379" | ||
networks: | ||
- redis-sentinel | ||
environment: | ||
- REDIS_MASTER_SET=mymaster | ||
- REDIS_MASTER_HOST=redis-master | ||
- REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS=10000 | ||
- ALLOW_EMPTY_PASSWORD=yes | ||
depends_on: | ||
- redis-master | ||
- redis-slave | ||
|
||
networks: | ||
redis-sentinel: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 36 additions & 26 deletions
62
src/test/scala/play/api/cache/redis/test/RedisSentinelContainer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,53 @@ | ||
package play.api.cache.redis.test | ||
|
||
import com.dimafeng.testcontainers.{DockerComposeContainer, ExposedService} | ||
import org.scalatest.Suite | ||
import play.api.Logger | ||
import org.testcontainers.containers.wait.strategy.Wait | ||
|
||
import scala.concurrent.duration.DurationInt | ||
import java.io.File | ||
|
||
trait RedisSentinelContainer extends RedisContainer { | ||
trait RedisSentinelContainer extends ForAllTestContainer { | ||
this: Suite => | ||
|
||
private val log = Logger("play.api.cache.redis.test") | ||
override protected type TestContainer = DockerComposeContainer | ||
|
||
protected def nodes: Int = 3 | ||
protected def master: String = "mymaster" | ||
|
||
final protected def initialPort: Int = 7000 | ||
final protected val host = "localhost" | ||
|
||
final protected def sentinelPort: Int = initialPort - 2000 | ||
final private val initialPort = 7000 | ||
|
||
final protected val masterPort = initialPort | ||
final protected val slavePort = masterPort + 1 | ||
|
||
protected def master: String = s"sentinel$initialPort" | ||
protected def sentinels: Int = 3 | ||
|
||
private val waitForStart = 7.seconds | ||
final protected def sentinelPort: Int = initialPort - 2000 | ||
|
||
override protected lazy val redisConfig: RedisContainerConfig = | ||
RedisContainerConfig( | ||
redisDockerImage = "grokzen/redis-cluster:7.0.10", | ||
redisMappedPorts = Seq.empty, | ||
redisFixedPorts = 0.until(nodes).flatMap(i => Seq(initialPort + i, sentinelPort + i)), | ||
redisEnvironment = Map( | ||
"IP" -> "0.0.0.0", | ||
"INITIAL_PORT" -> initialPort.toString, | ||
"SENTINEL" -> "true", | ||
// note: waiting for sentinels is not working | ||
// private def sentinelWaitStrategy = new WaitAllStrategy() | ||
// .withStrategy(Wait.forLogMessage(".*\\+monitor master.*\\n", 1)) | ||
// .withStrategy(Wait.forLogMessage(".*\\+slave slave.*\\n", 1)) | ||
|
||
protected def newContainer: TestContainerDef[TestContainer] = | ||
DockerComposeContainer.Def( | ||
new File("docker/sentinel/docker-compose.yml"), | ||
tailChildContainers = true, | ||
env = Map( | ||
"REDIS_MASTER_PORT" -> s"$masterPort", | ||
"REDIS_SLAVE_PORT" -> s"$slavePort", | ||
"REDIS_SENTINEL_1_PORT" -> s"${sentinelPort + 0}", | ||
"REDIS_SENTINEL_2_PORT" -> s"${sentinelPort + 1}", | ||
"REDIS_SENTINEL_3_PORT" -> s"${sentinelPort + 2}", | ||
), | ||
exposedServices = Seq( | ||
ExposedService("redis-master", masterPort, Wait.forLogMessage(".*Ready to accept connections tcp.*\\n", 1)), | ||
ExposedService("redis-slave", slavePort, Wait.forLogMessage(".*MASTER <-> REPLICA sync: Finished with success.*\\n", 1)), | ||
// note: waiting for sentinels doesn't work, it says "service is not running" | ||
// ExposedService("redis-sentinel-1", sentinelPort + 0, sentinelWaitStrategy), | ||
// ExposedService("redis-sentinel-2", sentinelPort + 1, sentinelWaitStrategy), | ||
// ExposedService("redis-sentinel-3", sentinelPort + 2, sentinelWaitStrategy), | ||
), | ||
) | ||
|
||
@SuppressWarnings(Array("org.wartremover.warts.ThreadSleep")) | ||
override def beforeAll(): Unit = { | ||
super.beforeAll() | ||
log.info(s"Waiting for Redis Sentinel to start on ${container.containerIpAddress}, will wait for $waitForStart") | ||
Thread.sleep(waitForStart.toMillis) | ||
log.info(s"Finished waiting for Redis Sentinel to start on ${container.containerIpAddress}") | ||
} | ||
|
||
} |