From c7f95ef14d1ea27415be91b7225b7f6dce944722 Mon Sep 17 00:00:00 2001 From: Tomasz Wujec Date: Thu, 11 Apr 2024 09:45:55 +0200 Subject: [PATCH] Update sbt version to 1.9.2, add REDIS_HOST environment variable, and use host from configuration file in Redis and Sentinel classes --- .github/workflows/push_main.yml | 48 +++ .gitignore | 52 ++- build.sbt | 9 +- github.sbt | 6 + project/build.properties | 2 +- src/main/resources/reference.conf | 2 + src/main/scala/brando/Redis.scala | 4 +- src/main/scala/brando/Sentinel.scala | 32 +- src/main/scala/brando/ShardManager.scala | 2 +- .../brando/RedisClientSentinelTest.scala | 33 +- src/test/scala/brando/RedisClientTest.scala | 299 +++++++++--------- src/test/scala/brando/SentinelTest.scala | 51 +-- src/test/scala/brando/ShardManagerTest.scala | 133 ++++---- .../brando/StashingRedisClientTest.scala | 15 +- version.sbt | 1 + 15 files changed, 390 insertions(+), 299 deletions(-) create mode 100644 .github/workflows/push_main.yml create mode 100644 github.sbt create mode 100644 version.sbt diff --git a/.github/workflows/push_main.yml b/.github/workflows/push_main.yml new file mode 100644 index 0000000..6c35c91 --- /dev/null +++ b/.github/workflows/push_main.yml @@ -0,0 +1,48 @@ +name: Build @horn/brando +on: + push: + branches: + - main + workflow_dispatch: + +jobs: + build: + name: Build $ Push + runs-on: ubuntu-22.04 + # Service containers to run with `container-job` + services: + # Label used to access the service container + redis: + # Docker Hub image + image: redis + # Set health checks to wait until redis has started + options: >- + --health-cmd "redis-cli ping" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Set up JDK 11 + uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: '11' + cache: 'sbt' + - name: Compile & Test + run: sbt test + env: + GITHUB_TOKEN: ${{ secrets.DEPLOY_GITHUB_TOKEN }} + REDIS_HOST: "redis" + - name: Fetch version from build.sbt + id: current_version + run: echo "version=$(grep "version.*:=" version.sbt | sed 's/.*"\(.*\)"/\1/')" >> $GITHUB_OUTPUT + - name: Publish API to github package registry + run: sbt +publish + env: + GITHUB_TOKEN: ${{ secrets.DEPLOY_GITHUB_TOKEN }} + - uses: rickstaa/action-create-tag@v1 + with: + tag: ${{ steps.current_version.outputs.version }} + tag_exists_error: false \ No newline at end of file diff --git a/.gitignore b/.gitignore index faf268a..078f776 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,45 @@ -.DS_Store +# bloop and metals +.bloop +.bsp +.metals +project/metals.sbt +project/project/metals.sbt +project/project/project/metals.sbt + +# vs code +.vscode + +# scala 3 +.tasty + +# sbt +project/project/ +project/target/ target/ -project/boot/ -project/build/target/ -project/plugins/target/ -project/plugins/lib_managed/ -project/plugins/src_managed/ -*.log -.cache + +# eclipse +build/ .classpath .project .settings -*.rdb -.idea/ -.idea_modules/ +.worksheet +bin/ +.cache + +# intellij idea +*.log +*.iml +*.ipr +*.iws +.idea + +# mac +.DS_Store + +# other? +.history +.scala_dependencies +.cache-main + +# general +*.class diff --git a/build.sbt b/build.sbt index b3d82ed..4504932 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,5 @@ name := "brando" organization := "com.digital-achiever" -version := "3.2.0" scalaVersion := "2.13.8" scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature") @@ -8,13 +7,7 @@ scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature") // for this bug: https://github.com/sbt/sbt/issues/3570 updateOptions := updateOptions.value.withGigahorse(false) -publishTo := Some("Horn SBT" at "https://sbt.horn.co/repository/internal") -credentials += Credentials( - "Repository Archiva Managed internal Repository", - "sbt.horn.co", - sys.env("HORN_SBT_USERNAME"), - sys.env("HORN_SBT_PASSWORD") -) +publishTo := Some("Horn SBT" at "https://maven.pkg.github.com/8eo/brando") val akkaV = "2.6.19" diff --git a/github.sbt b/github.sbt new file mode 100644 index 0000000..3317bec --- /dev/null +++ b/github.sbt @@ -0,0 +1,6 @@ +credentials += Credentials( + "GitHub Package Registry", + "maven.pkg.github.com", + "8eo", + sys.env("GITHUB_TOKEN") +) diff --git a/project/build.properties b/project/build.properties index f0be67b..875b706 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.5.1 +sbt.version=1.9.2 diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf index 37c77a0..f9367d4 100644 --- a/src/main/resources/reference.conf +++ b/src/main/resources/reference.conf @@ -1,6 +1,8 @@ brando{ connection{ timeout = 2s + host = "localhost" + host = ${?REDIS_HOST} #Delay before trying to reconnect retry.delay = 1 s diff --git a/src/main/scala/brando/Redis.scala b/src/main/scala/brando/Redis.scala index fcb4da4..cdbdec4 100644 --- a/src/main/scala/brando/Redis.scala +++ b/src/main/scala/brando/Redis.scala @@ -9,7 +9,7 @@ import scala.concurrent.duration._ object Redis { def apply( - host: String = "localhost", + host: Option[String]= None, port: Int = 6379, database: Int = 0, auth: Option[String] = None, @@ -21,7 +21,7 @@ object Redis { val config = ConfigFactory.load() Props(classOf[Redis], - host, + host.getOrElse(config.getString("brando.connection.host")), port, database, auth, diff --git a/src/main/scala/brando/Sentinel.scala b/src/main/scala/brando/Sentinel.scala index 4ffd554..a07817b 100644 --- a/src/main/scala/brando/Sentinel.scala +++ b/src/main/scala/brando/Sentinel.scala @@ -9,17 +9,24 @@ import com.typesafe.config.ConfigFactory import scala.concurrent.duration._ object Sentinel { + + val host = ConfigFactory.load().getString("brando.connection.host") + def apply( - sentinels: Seq[Server] = Seq(Server("localhost", 26379)), - listeners: Set[ActorRef] = Set(), - connectionTimeout: Option[FiniteDuration] = None, - connectionHeartbeatDelay: Option[FiniteDuration] = None): Props = { + sentinels: Seq[Server] = Seq(Server(host, 26379)), + listeners: Set[ActorRef] = Set(), + connectionTimeout: Option[FiniteDuration] = None, + connectionHeartbeatDelay: Option[FiniteDuration] = None + ): Props = { val config = ConfigFactory.load() - Props(classOf[Sentinel], sentinels, listeners, - connectionTimeout.getOrElse( - config.getDuration("brando.connection.timeout", TimeUnit.MILLISECONDS).millis), - connectionHeartbeatDelay) + Props( + classOf[Sentinel], + sentinels, + listeners, + connectionTimeout.getOrElse(config.getDuration("brando.connection.timeout", TimeUnit.MILLISECONDS).millis), + connectionHeartbeatDelay + ) } case class Server(host: String, port: Int) @@ -31,14 +38,15 @@ class Sentinel( var sentinels: Seq[Sentinel.Server], var listeners: Set[ActorRef], connectionTimeout: FiniteDuration, - connectionHeartbeatDelay: Option[FiniteDuration]) extends Actor { + connectionHeartbeatDelay: Option[FiniteDuration] +) extends Actor { import Sentinel._ implicit val timeout = Timeout(connectionTimeout) var connection = context.system.deadLetters - var retries = 0 + var retries = 0 override def preStart: Unit = { listeners.map(context.watch) @@ -64,8 +72,8 @@ class Sentinel( case Connect(Server(host, port) :: tail) ⇒ notifyStateChange(Connection.Connecting(host, port)) retries += 1 - connection = context.actorOf(Props(classOf[Connection], - self, host, port, connectionTimeout, connectionHeartbeatDelay)) + connection = + context.actorOf(Props(classOf[Connection], self, host, port, connectionTimeout, connectionHeartbeatDelay)) case x: Connection.Connected ⇒ context.become(connected) diff --git a/src/main/scala/brando/ShardManager.scala b/src/main/scala/brando/ShardManager.scala index 01e6b51..3394773 100644 --- a/src/main/scala/brando/ShardManager.scala +++ b/src/main/scala/brando/ShardManager.scala @@ -94,7 +94,7 @@ class ShardManager( (shard, sentinelClient) match { case (RedisShard(id, host, port, database, auth), _) ⇒ val brando = - context.actorOf(Redis(host, port, database, auth, listeners, + context.actorOf(Redis(Some(host), port, database, auth, listeners, Some(connectionTimeout), Some(connectionRetryDelay), None, connectionHeartbeatDelay)) add(shard, brando) diff --git a/src/test/scala/brando/RedisClientSentinelTest.scala b/src/test/scala/brando/RedisClientSentinelTest.scala index e05a64d..72e8a5f 100644 --- a/src/test/scala/brando/RedisClientSentinelTest.scala +++ b/src/test/scala/brando/RedisClientSentinelTest.scala @@ -3,6 +3,7 @@ package brando import akka.actor._ import akka.testkit._ import org.scalatest._ +import com.typesafe.config.ConfigFactory class RedisClientSentinelTest extends TestKit(ActorSystem("RedisClientSentinelTest")) with FunSpecLike with ImplicitSender { @@ -10,6 +11,8 @@ class RedisClientSentinelTest extends TestKit(ActorSystem("RedisClientSentinelTe import Connection._ import Sentinel._ + val host = ConfigFactory.load().getString("brando.connection.host") + describe("RedisClientSentinel") { describe("when connecting") { it("should use sentinel to resolve the ip and port") { @@ -25,7 +28,7 @@ class RedisClientSentinelTest extends TestKit(ActorSystem("RedisClientSentinelTe val sentinelProbe = TestProbe() val sentinel = system.actorOf(Sentinel( - sentinels = Seq(Server("localhost", 26379)), + sentinels = Seq(Server(host, 26379)), listeners = Set(sentinelProbe.ref))) val brando = system.actorOf(RedisSentinel( master = "mymaster", @@ -33,13 +36,13 @@ class RedisClientSentinelTest extends TestKit(ActorSystem("RedisClientSentinelTe listeners = Set(redisProbe.ref))) sentinelProbe.expectMsg( - Connecting("localhost", 26379)) + Connecting(host, 26379)) sentinelProbe.expectMsg( - Connected("localhost", 26379)) + Connected(host, 26379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) } } @@ -49,7 +52,7 @@ class RedisClientSentinelTest extends TestKit(ActorSystem("RedisClientSentinelTe val sentinelProbe = TestProbe() val sentinel = system.actorOf(Sentinel( - sentinels = Seq(Server("localhost", 26379)), + sentinels = Seq(Server(host, 26379)), listeners = Set(sentinelProbe.ref))) val brando = system.actorOf(RedisSentinel( master = "mymaster", @@ -57,27 +60,27 @@ class RedisClientSentinelTest extends TestKit(ActorSystem("RedisClientSentinelTe listeners = Set(redisProbe.ref))) sentinelProbe.expectMsg( - Connecting("localhost", 26379)) + Connecting(host, 26379)) sentinelProbe.expectMsg( - Connected("localhost", 26379)) + Connected(host, 26379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) - brando ! Disconnected("127.0.0.1", 6379) + brando ! Disconnected(host, 6379) redisProbe.expectMsg( - Disconnected("127.0.0.1", 6379)) + Disconnected(host, 6379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) } it("should return a failure when disconnected") { val sentinel = system.actorOf(Sentinel( - sentinels = Seq(Server("localhost", 26379)))) + sentinels = Seq(Server(host, 26379)))) val brando = system.actorOf(RedisSentinel( master = "mymaster", sentinelClient = sentinel)) diff --git a/src/test/scala/brando/RedisClientTest.scala b/src/test/scala/brando/RedisClientTest.scala index 97c405e..60ce4e3 100644 --- a/src/test/scala/brando/RedisClientTest.scala +++ b/src/test/scala/brando/RedisClientTest.scala @@ -11,17 +11,19 @@ import org.scalatest.FunSpecLike import scala.concurrent.Await import scala.concurrent.duration._ +import com.typesafe.config.ConfigFactory -class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSpecLike - with ImplicitSender { +class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSpecLike with ImplicitSender { import Connection._ + val host = ConfigFactory.load().getString("brando.connection.host") + describe("ping") { it("should respond with Pong") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Request("PING") @@ -32,8 +34,8 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("flushdb") { it("should respond with OK") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Request("FLUSHDB") @@ -44,8 +46,8 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("set") { it("should respond with OK") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Request("SET", "mykey", "somevalue") @@ -59,8 +61,8 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("get") { it("should respond with value option for existing key") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Request("SET", "mykey", "somevalue") @@ -76,8 +78,8 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp it("should respond with None for non-existent key") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Request("GET", "mykey") @@ -88,8 +90,8 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("incr") { it("should increment and return value for existing key") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Request("SET", "incr-test", "10") @@ -105,8 +107,8 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp it("should return 1 for non-existent key") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Request("INCR", "incr-test") @@ -120,8 +122,8 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("sadd") { it("should return number of members added to set") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Request("SADD", "sadd-test", "one") @@ -143,8 +145,8 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("smembers") { it("should return all members in a set") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Request("SADD", "smembers-test", "one", "two", "three", "four") @@ -153,9 +155,10 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp brando ! Request("SMEMBERS", "smembers-test") val resp = receiveOne(500.millis).asInstanceOf[Option[List[Any]]] - assert(resp.getOrElse(List()).toSet === - Set(Some(ByteString("one")), Some(ByteString("two")), - Some(ByteString("three")), Some(ByteString("four")))) + assert( + resp.getOrElse(List()).toSet === + Set(Some(ByteString("one")), Some(ByteString("two")), Some(ByteString("three")), Some(ByteString("four"))) + ) brando ! Request("FLUSHDB") expectMsg(Some(Ok)) @@ -166,8 +169,8 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("pipelining") { it("should respond to a Seq of multiple requests all at once") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) val ping = Request("PING") @@ -183,8 +186,8 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp it("should support pipelines of setex commands") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) val setex = Request("SETEX", "pipeline-setex-path", "10", "Some data") @@ -199,10 +202,10 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp it("should receive responses in the right order") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) - val ping = Request("PING") + val ping = Request("PING") val setex = Request("SETEX", "pipeline-setex-path", "10", "Some data") brando ! setex @@ -221,10 +224,10 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("large data sets") { it("should read and write large files") { - import java.io.{ File, FileInputStream } + import java.io.{File, FileInputStream} - val file = new File("src/test/resources/crime_and_punishment.txt") - val in = new FileInputStream(file) + val file = new File("src/test/resources/crime_and_punishment.txt") + val in = new FileInputStream(file) val bytes = new Array[Byte](file.length.toInt) in.read(bytes) in.close() @@ -232,8 +235,8 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp val largeText = new String(bytes, "UTF-8") val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Request("SET", "crime+and+punishment", largeText) @@ -251,32 +254,30 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("error reply") { it("should receive a failure with the redis error message") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Request("SET", "key") - expectMsgPF(5.seconds) { - case Status.Failure(e) ⇒ - assert(e.isInstanceOf[RedisException]) - assert(e.getMessage === "ERR wrong number of arguments for 'set' command") + expectMsgPF(5.seconds) { case Status.Failure(e) ⇒ + assert(e.isInstanceOf[RedisException]) + assert(e.getMessage === "ERR wrong number of arguments for 'set' command") } brando ! Request("EXPIRE", "1", "key") - expectMsgPF(5.seconds) { - case Status.Failure(e) ⇒ - assert(e.isInstanceOf[RedisException]) - assert(e.getMessage === "ERR value is not an integer or out of range") + expectMsgPF(5.seconds) { case Status.Failure(e) ⇒ + assert(e.isInstanceOf[RedisException]) + assert(e.getMessage === "ERR value is not an integer or out of range") } } } describe("select") { it("should execute commands on the selected database") { - val brando = system.actorOf(Redis("localhost", 6379, 5, listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + val brando = system.actorOf(Redis(Some(host), 6379, 5, listeners = Set(self))) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Request("SET", "mykey", "somevalue") @@ -304,28 +305,35 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("multi/exec requests") { it("should support multi requests as an atomic transaction") { - val brando = system.actorOf(Redis("localhost", 6379, 5, listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + val brando = system.actorOf(Redis(Some(host), 6379, 5, listeners = Set(self))) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) brando ! Batch(Request("MULTI"), Request("SET", "mykey", "somevalue"), Request("GET", "mykey"), Request("EXEC")) - expectMsg(List(Some(Ok), - Some(Queued), - Some(Queued), - Some(List(Some(Ok), Some(ByteString("somevalue")))))) + expectMsg(List(Some(Ok), Some(Queued), Some(Queued), Some(List(Some(Ok), Some(ByteString("somevalue")))))) } it("should support multi requests with multiple results") { - val brando = system.actorOf(Redis("localhost", 6379, 5, listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) - - brando ! Batch(Request("MULTI"), Request("SET", "mykey", "somevalue"), Request("GET", "mykey"), Request("GET", "mykey"), Request("EXEC")) - expectMsg(List(Some(Ok), - Some(Queued), - Some(Queued), - Some(Queued), - Some(List(Some(Ok), Some(ByteString("somevalue")), Some(ByteString("somevalue")))))) + val brando = system.actorOf(Redis(Some(host), 6379, 5, listeners = Set(self))) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) + + brando ! Batch( + Request("MULTI"), + Request("SET", "mykey", "somevalue"), + Request("GET", "mykey"), + Request("GET", "mykey"), + Request("EXEC") + ) + expectMsg( + List( + Some(Ok), + Some(Queued), + Some(Queued), + Some(Queued), + Some(List(Some(Ok), Some(ByteString("somevalue")), Some(ByteString("somevalue")))) + ) + ) } } @@ -333,35 +341,29 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("subscribe") { it("should be able to subscribe to a pubsub channel") { - val channel = UUID.randomUUID().toString + val channel = UUID.randomUUID().toString val subscriber = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) subscriber ! Request("SUBSCRIBE", channel) - expectMsg(Some(List(Some( - ByteString("subscribe")), - Some(ByteString(channel)), - Some(1)))) + expectMsg(Some(List(Some(ByteString("subscribe")), Some(ByteString(channel)), Some(1)))) } it("should receive published messages from a pubsub channel") { - val channel = UUID.randomUUID().toString + val channel = UUID.randomUUID().toString val subscriber = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) val publisher = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) subscriber ! Request("SUBSCRIBE", channel) - expectMsg(Some(List(Some( - ByteString("subscribe")), - Some(ByteString(channel)), - Some(1)))) + expectMsg(Some(List(Some(ByteString("subscribe")), Some(ByteString(channel)), Some(1)))) publisher ! Request("PUBLISH", channel, "test") expectMsg(Some(1)) //publisher gets back number of subscribers when publishing @@ -370,28 +372,22 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp } it("should be able to unsubscribe from a pubsub channel") { - val channel = UUID.randomUUID().toString + val channel = UUID.randomUUID().toString val subscriber = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) val publisher = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) subscriber ! Request("SUBSCRIBE", channel) - expectMsg(Some(List(Some( - ByteString("subscribe")), - Some(ByteString(channel)), - Some(1)))) + expectMsg(Some(List(Some(ByteString("subscribe")), Some(ByteString(channel)), Some(1)))) subscriber ! Request("UNSUBSCRIBE", channel) - expectMsg(Some(List(Some( - ByteString("unsubscribe")), - Some(ByteString(channel)), - Some(0)))) + expectMsg(Some(List(Some(ByteString("unsubscribe")), Some(ByteString(channel)), Some(0)))) publisher ! Request("PUBLISH", channel, "test") expectMsg(Some(0)) @@ -404,15 +400,15 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("blop") { it("should block then reply") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) try { val popRedis = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) - val probeRedis = TestProbe() + val probeRedis = TestProbe() val probePopRedis = TestProbe() popRedis.tell(Request("BLPOP", "blpop:list", "0"), probePopRedis.ref) @@ -421,10 +417,7 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp brando.tell(Request("LPUSH", "blpop:list", "blpop-value"), probeRedis.ref) - probePopRedis.expectMsg( - Some(List(Some( - ByteString("blpop:list")), - Some(ByteString("blpop-value"))))) + probePopRedis.expectMsg(Some(List(Some(ByteString("blpop:list")), Some(ByteString("blpop-value"))))) probeRedis.expectMsg(Some(1)) @@ -437,8 +430,8 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp it("should reply with Nil when timeout") { val popRedis = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) val probePopRedis = TestProbe() popRedis.tell(Request("BLPOP", "blpop:inexistant-list", "1"), probePopRedis.ref) @@ -450,61 +443,61 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("notifications") { it("should send a Connected event if connecting succeeds") { val probe = TestProbe() - system.actorOf(Redis("localhost", 6379, listeners = Set(probe.ref))) + system.actorOf(Redis(Some(host), 6379, listeners = Set(probe.ref))) - probe.expectMsg(Connecting("localhost", 6379)) - probe.expectMsg(Connected("localhost", 6379)) + probe.expectMsg(Connecting(host, 6379)) + probe.expectMsg(Connected(host, 6379)) } it("should send an ConnectionFailed event if connecting fails") { val probe = TestProbe() - system.actorOf(Redis("localhost", 13579, listeners = Set(probe.ref))) + system.actorOf(Redis(Some(host), 13579, listeners = Set(probe.ref))) - probe.expectMsg(Connecting("localhost", 13579)) - probe.expectMsg(ConnectionFailed("localhost", 13579)) + probe.expectMsg(Connecting(host, 13579)) + probe.expectMsg(ConnectionFailed(host, 13579)) } it("should send an AuthenticationFailed event if connecting succeeds but authentication fails") { val probe = TestProbe() - system.actorOf(Redis("localhost", 6379, auth = Some("not-the-auth"), listeners = Set(probe.ref))) + system.actorOf(Redis(Some(host), 6379, auth = Some("not-the-auth"), listeners = Set(probe.ref))) - probe.expectMsg(Connecting("localhost", 6379)) - probe.expectMsg(Redis.AuthenticationFailed("localhost", 6379)) + probe.expectMsg(Connecting(host, 6379)) + probe.expectMsg(Redis.AuthenticationFailed(host, 6379)) } it("should send a ConnectionFailed if redis is not responsive during connection") { val serverSocket = new ServerSocket(0) - val port = serverSocket.getLocalPort + val port = serverSocket.getLocalPort val probe = TestProbe() - system.actorOf(Redis("localhost", port, listeners = Set(probe.ref))) + system.actorOf(Redis(Some(host), port, listeners = Set(probe.ref))) - probe.expectMsg(Connecting("localhost", port)) - probe.expectMsg(ConnectionFailed("localhost", port)) + probe.expectMsg(Connecting(host, port)) + probe.expectMsg(ConnectionFailed(host, port)) } it("should send a notification to later added listener") { - val probe = TestProbe() + val probe = TestProbe() val probe2 = TestProbe() - val brando = system.actorOf(Redis("localhost", 13579, listeners = Set(probe2.ref))) + val brando = system.actorOf(Redis(Some(host), 13579, listeners = Set(probe2.ref))) brando ! probe.ref probe.expectMsg(Disconnected("unknown", 0)) - probe2.expectMsg(Connecting("localhost", 13579)) - probe.expectMsg(Connecting("localhost", 13579)) - probe2.expectMsg(ConnectionFailed("localhost", 13579)) - probe.expectMsg(ConnectionFailed("localhost", 13579)) + probe2.expectMsg(Connecting(host, 13579)) + probe.expectMsg(Connecting(host, 13579)) + probe2.expectMsg(ConnectionFailed(host, 13579)) + probe.expectMsg(ConnectionFailed(host, 13579)) } it("should send a notification with the current status to later added listener") { - val probe = TestProbe() + val probe = TestProbe() val probe2 = TestProbe() - val brando = system.actorOf(Redis("localhost", 6379, listeners = Set(probe2.ref))) + val brando = system.actorOf(Redis(Some(host), 6379, listeners = Set(probe2.ref))) - probe2.expectMsg(Connecting("localhost", 6379)) - probe2.expectMsg(Connected("localhost", 6379)) + probe2.expectMsg(Connecting(host, 6379)) + probe2.expectMsg(Connected(host, 6379)) brando ! probe.ref - probe.expectMsg(Connected("localhost", 6379)) + probe.expectMsg(Connected(host, 6379)) } } @@ -512,46 +505,47 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp import Connection._ it("should try to reconnect if connectionRetryDelay and connectionRetryAttempts are defined") { val listener = TestProbe() - val brando = TestActorRef(new Redis( - "localhost", 6379, 0, None, Set(listener.ref), 2.seconds, Some(1.seconds), Some(1), None)) + val brando = + TestActorRef(new Redis(host, 6379, 0, None, Set(listener.ref), 2.seconds, Some(1.seconds), Some(1), None)) - listener.expectMsg(Connecting("localhost", 6379)) + listener.expectMsg(Connecting(host, 6379)) assert(brando.underlyingActor.retries === 0) - listener.expectMsg(Connected("localhost", 6379)) + listener.expectMsg(Connected(host, 6379)) assert(brando.underlyingActor.retries === 0) - brando ! Disconnected("localhost", 6379) + brando ! Disconnected(host, 6379) - listener.expectMsg(Disconnected("localhost", 6379)) - listener.expectMsg(Connecting("localhost", 6379)) + listener.expectMsg(Disconnected(host, 6379)) + listener.expectMsg(Connecting(host, 6379)) } it("should not try to reconnect if connectionRetryDelay and connectionRetryAttempts are not defined") { val listener = TestProbe() - val brando = TestActorRef(new Redis( - "localhost", 6379, 0, None, Set(listener.ref), 2.seconds, None, None, None)) + val brando = TestActorRef(new Redis(host, 6379, 0, None, Set(listener.ref), 2.seconds, None, None, None)) - listener.expectMsg(Connecting("localhost", 6379)) - listener.expectMsg(Connected("localhost", 6379)) + listener.expectMsg(Connecting(host, 6379)) + listener.expectMsg(Connected(host, 6379)) - brando ! Disconnected("localhost", 6379) + brando ! Disconnected(host, 6379) - listener.expectMsg(Disconnected("localhost", 6379)) + listener.expectMsg(Disconnected(host, 6379)) listener.expectNoMsg } it("should not try to reconnect once the max retry attempts is reached") { val listener = TestProbe() - val brando = TestActorRef(new Redis( - "localhost", 16379, 0, None, Set(listener.ref), 2.seconds, Some(1.seconds), Some(1), None)) + val brando = + TestActorRef( + new Redis(host, 16379, 0, None, Set(listener.ref), 2.seconds, Some(1.seconds), Some(1), None) + ) - listener.expectMsg(Connecting("localhost", 16379)) + listener.expectMsg(Connecting(host, 16379)) assert(brando.underlyingActor.retries === 0) - listener.expectMsg(ConnectionFailed("localhost", 16379)) + listener.expectMsg(ConnectionFailed(host, 16379)) - listener.expectMsg(Connecting("localhost", 16379)) + listener.expectMsg(Connecting(host, 16379)) assert(brando.underlyingActor.retries === 1) - listener.expectMsg(ConnectionFailed("localhost", 16379)) + listener.expectMsg(ConnectionFailed(host, 16379)) listener.expectNoMsg } @@ -560,13 +554,10 @@ class RedisClientTest extends TestKit(ActorSystem("RedisClientTest")) with FunSp describe("eval") { it("should respond with result of operation") { val brando = system.actorOf(Redis(listeners = Set(self))) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) - brando ! Request("eval", - "if ARGV[1] == \"hello\" then return 1 end", - "0", - "hello") + brando ! Request("eval", "if ARGV[1] == \"hello\" then return 1 end", "0", "hello") expectMsg(Some(1)) } diff --git a/src/test/scala/brando/SentinelTest.scala b/src/test/scala/brando/SentinelTest.scala index 0bd2194..20a5b0f 100644 --- a/src/test/scala/brando/SentinelTest.scala +++ b/src/test/scala/brando/SentinelTest.scala @@ -4,6 +4,7 @@ import akka.actor._ import akka.testkit._ import akka.util._ import org.scalatest._ +import com.typesafe.config.ConfigFactory class SentinelTest extends TestKit(ActorSystem("SentinelTest")) with FunSpecLike with ImplicitSender { @@ -11,48 +12,50 @@ class SentinelTest extends TestKit(ActorSystem("SentinelTest")) with FunSpecLike import Connection._ import Sentinel._ + val host = ConfigFactory.load().getString("brando.connection.host") + describe("Sentinel") { describe("connection to sentinel instances") { it("should connect to the first working sentinel instance") { val probe = TestProbe() val sentinel = system.actorOf(Sentinel(Seq( Server("wrong-host", 26379), - Server("localhost", 26379)), Set(probe.ref))) + Server(host, 26379)), Set(probe.ref))) probe.expectMsg(Connecting("wrong-host", 26379)) - probe.expectMsg(Connecting("localhost", 26379)) - probe.expectMsg(Connected("localhost", 26379)) + probe.expectMsg(Connecting(host, 26379)) + probe.expectMsg(Connected(host, 26379)) } it("should send a notification to the listeners when connecting") { val probe = TestProbe() val sentinel = system.actorOf(Sentinel(Seq( - Server("localhost", 26379)), + Server(host, 26379)), Set(probe.ref))) - probe.expectMsg(Connecting("localhost", 26379)) + probe.expectMsg(Connecting(host, 26379)) } it("should send a notification to the listeners when connected") { val probe = TestProbe() val sentinel = system.actorOf(Sentinel(Seq( - Server("localhost", 26379)), Set(probe.ref))) + Server(host, 26379)), Set(probe.ref))) probe.receiveN(1) - probe.expectMsg(Connected("localhost", 26379)) + probe.expectMsg(Connected(host, 26379)) } it("should send a notification to the listeners when disconnected") { val probe = TestProbe() val sentinel = system.actorOf(Sentinel(Seq( - Server("localhost", 26379)), Set(probe.ref))) + Server(host, 26379)), Set(probe.ref))) probe.receiveN(1) - probe.expectMsg(Connected("localhost", 26379)) + probe.expectMsg(Connected(host, 26379)) - sentinel ! Disconnected("localhost", 26379) + sentinel ! Disconnected(host, 26379) - probe.expectMsg(Disconnected("localhost", 26379)) + probe.expectMsg(Disconnected(host, 26379)) } it("should send a notification to the listeners for connection failure") { @@ -68,17 +71,17 @@ class SentinelTest extends TestKit(ActorSystem("SentinelTest")) with FunSpecLike val probe = TestProbe() val sentinel = system.actorOf(Sentinel(Seq( Server("wrong-host", 26379), - Server("localhost", 26379)), Set(probe.ref))) + Server(host, 26379)), Set(probe.ref))) probe.expectMsg(Connecting("wrong-host", 26379)) - probe.expectMsg(Connecting("localhost", 26379)) - probe.expectMsg(Connected("localhost", 26379)) + probe.expectMsg(Connecting(host, 26379)) + probe.expectMsg(Connected(host, 26379)) - sentinel ! Disconnected("localhost", 26379) + sentinel ! Disconnected(host, 26379) - probe.expectMsg(Disconnected("localhost", 26379)) + probe.expectMsg(Disconnected(host, 26379)) - probe.expectMsg(Connecting("localhost", 26379)) + probe.expectMsg(Connecting(host, 26379)) } it("should send a notification to the listeners if it can't connect to any instance") { @@ -96,7 +99,7 @@ class SentinelTest extends TestKit(ActorSystem("SentinelTest")) with FunSpecLike describe("Request") { it("should return a failure when disconnected") { val sentinel = system.actorOf(Sentinel(Seq( - Server("localhost", 26379)))) + Server(host, 26379)))) sentinel ! Request("PING") @@ -108,14 +111,14 @@ class SentinelTest extends TestKit(ActorSystem("SentinelTest")) with FunSpecLike describe("Subscriptions") { it("should receive pub/sub notifications") { val sentinel = system.actorOf(Sentinel(Seq( - Server("localhost", 26379)), Set(self))) + Server(host, 26379)), Set(self))) val sentinel2 = system.actorOf(Sentinel(Seq( - Server("localhost", 26379)), Set(self))) + Server(host, 26379)), Set(self))) - expectMsg(Connecting("localhost", 26379)) - expectMsg(Connecting("localhost", 26379)) - expectMsg(Connected("localhost", 26379)) - expectMsg(Connected("localhost", 26379)) + expectMsg(Connecting(host, 26379)) + expectMsg(Connecting(host, 26379)) + expectMsg(Connected(host, 26379)) + expectMsg(Connected(host, 26379)) sentinel ! Request("subscribe", "+failover-end") diff --git a/src/test/scala/brando/ShardManagerTest.scala b/src/test/scala/brando/ShardManagerTest.scala index a7a4b9b..c09366d 100644 --- a/src/test/scala/brando/ShardManagerTest.scala +++ b/src/test/scala/brando/ShardManagerTest.scala @@ -4,6 +4,7 @@ import akka.actor._ import akka.testkit._ import akka.util.ByteString import org.scalatest.FunSpecLike +import com.typesafe.config.ConfigFactory import scala.util.Failure @@ -13,12 +14,14 @@ class ShardManagerTest extends TestKit(ActorSystem("ShardManagerTest")) import Connection._ import ShardManager._ + val host = ConfigFactory.load().getString("brando.connection.host") + describe("creating shards") { it("should create a pool of clients mapped to ids") { val shards = Seq( - RedisShard("server1", "localhost", 6379, 0), - RedisShard("server2", "localhost", 6379, 1), - RedisShard("server3", "localhost", 6379, 2)) + RedisShard("server1", host, 6379, 0), + RedisShard("server2", host, 6379, 1), + RedisShard("server3", host, 6379, 2)) val shardManager = TestActorRef[ShardManager](ShardManager( shards)) @@ -28,20 +31,20 @@ class ShardManagerTest extends TestKit(ActorSystem("ShardManagerTest")) it("should support updating existing shards but not creating new ones") { val shards = Seq( - RedisShard("server1", "localhost", 6379, 0), - RedisShard("server2", "localhost", 6379, 1), - RedisShard("server3", "localhost", 6379, 2)) + RedisShard("server1", host, 6379, 0), + RedisShard("server2", host, 6379, 1), + RedisShard("server3", host, 6379, 2)) val shardManager = TestActorRef[ShardManager](ShardManager( shards)) assert(shardManager.underlyingActor.pool.keys === Set("server1", "server2", "server3")) - shardManager ! RedisShard("server1", "localhost", 6379, 6) + shardManager ! RedisShard("server1", host, 6379, 6) assert(shardManager.underlyingActor.pool.keys === Set("server1", "server2", "server3")) - shardManager ! RedisShard("new_server", "localhost", 6378, 3) + shardManager ! RedisShard("new_server", host, 6378, 3) assert(shardManager.underlyingActor.pool.keys === Set("server1", "server2", "server3")) } @@ -59,14 +62,14 @@ class ShardManagerTest extends TestKit(ActorSystem("ShardManagerTest")) listeners = Set(redisProbe.ref))) sentinelProbe.expectMsg( - Connecting("localhost", 26379)) + Connecting(host, 26379)) sentinelProbe.expectMsg( - Connected("localhost", 26379)) + Connected(host, 26379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) shardManager ! ("key", Request("SET", "shard_manager_test", "some value")) @@ -80,9 +83,9 @@ class ShardManagerTest extends TestKit(ActorSystem("ShardManagerTest")) it("should forward each request to the appropriate client transparently") { val shards = Seq( - RedisShard("server1", "127.0.0.1", 6379, 0), - RedisShard("server2", "127.0.0.1", 6379, 1), - RedisShard("server3", "127.0.0.1", 6379, 2)) + RedisShard("server1", host, 6379, 0), + RedisShard("server2", host, 6379, 1), + RedisShard("server3", host, 6379, 2)) val sentinelProbe = TestProbe() val redisProbe = TestProbe() @@ -93,23 +96,23 @@ class ShardManagerTest extends TestKit(ActorSystem("ShardManagerTest")) listeners = Set(redisProbe.ref))) sentinelProbe.expectMsg( - Connecting("localhost", 26379)) + Connecting(host, 26379)) sentinelProbe.expectMsg( - Connected("localhost", 26379)) + Connected(host, 26379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) shardManager ! ("key", Request("SET", "shard_manager_test", "some value")) @@ -122,27 +125,27 @@ class ShardManagerTest extends TestKit(ActorSystem("ShardManagerTest")) it("should infer the key from the params list") { val shards = Seq( - RedisShard("server1", "127.0.0.1", 6379, 0), - RedisShard("server2", "127.0.0.1", 6379, 1), - RedisShard("server3", "127.0.0.1", 6379, 2)) + RedisShard("server1", host, 6379, 0), + RedisShard("server2", host, 6379, 1), + RedisShard("server3", host, 6379, 2)) val redisProbe = TestProbe() val shardManager = TestActorRef[ShardManager](ShardManager( shards, listeners = Set(redisProbe.ref))) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) shardManager ! Request("SET", "shard_manager_test", "some value") @@ -155,27 +158,27 @@ class ShardManagerTest extends TestKit(ActorSystem("ShardManagerTest")) it("should fail with IllegalArgumentException when params is empty") { val shards = Seq( - RedisShard("server1", "127.0.0.1", 6379, 0), - RedisShard("server2", "127.0.0.1", 6379, 1), - RedisShard("server3", "127.0.0.1", 6379, 2)) + RedisShard("server1", host, 6379, 0), + RedisShard("server2", host, 6379, 1), + RedisShard("server3", host, 6379, 2)) val redisProbe = TestProbe() val shardManager = TestActorRef[ShardManager](ShardManager( shards, listeners = Set(redisProbe.ref))) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) shardManager ! Request("SET") @@ -184,27 +187,27 @@ class ShardManagerTest extends TestKit(ActorSystem("ShardManagerTest")) it("should broadcast a Request to all shards") { val shards = Seq( - RedisShard("server1", "127.0.0.1", 6379, 0), - RedisShard("server2", "127.0.0.1", 6379, 1), - RedisShard("server3", "127.0.0.1", 6379, 2)) + RedisShard("server1", host, 6379, 0), + RedisShard("server2", host, 6379, 1), + RedisShard("server3", host, 6379, 2)) val redisProbe = TestProbe() val shardManager = TestActorRef[ShardManager](ShardManager( shards, listeners = Set(redisProbe.ref))) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connecting("127.0.0.1", 6379)) + Connecting(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) redisProbe.expectMsg( - Connected("127.0.0.1", 6379)) + Connected(host, 6379)) val listName = scala.util.Random.nextString(5) @@ -220,34 +223,34 @@ class ShardManagerTest extends TestKit(ActorSystem("ShardManagerTest")) describe("Listening to Shard state changes") { it("should notify listeners when a shard connect successfully") { - val shards = Seq(RedisShard("server1", "localhost", 6379, 0)) + val shards = Seq(RedisShard("server1", host, 6379, 0)) val probe = TestProbe() val shardManager = TestActorRef[ShardManager](ShardManager( shards, Set(probe.ref))) - probe.expectMsg(Connecting("localhost", 6379)) - probe.expectMsg(Connected("localhost", 6379)) + probe.expectMsg(Connecting(host, 6379)) + probe.expectMsg(Connected(host, 6379)) } it("should notify listeners when a shard fails to connect") { val shards = Seq( - RedisShard("server2", "localhost", 13579, 1)) + RedisShard("server2", host, 13579, 1)) val probe = TestProbe() val shardManager = TestActorRef[ShardManager](ShardManager( shards, Set(probe.ref))) - probe.expectMsg(Connecting("localhost", 13579)) - probe.expectMsg(ConnectionFailed("localhost", 13579)) + probe.expectMsg(Connecting(host, 13579)) + probe.expectMsg(ConnectionFailed(host, 13579)) } it("should cleaned up any dead listeners") { val shards = Seq( - RedisShard("server1", "localhost", 6379, 0)) + RedisShard("server1", host, 6379, 0)) val probe1 = TestProbe() val probe2 = TestProbe() @@ -258,8 +261,8 @@ class ShardManagerTest extends TestKit(ActorSystem("ShardManagerTest")) probe1.ref ! PoisonPill - probe2.expectMsg(Connecting("localhost", 6379)) - probe2.expectMsg(Connected("localhost", 6379)) + probe2.expectMsg(Connecting(host, 6379)) + probe2.expectMsg(Connected(host, 6379)) assertResult(1)(shardManager.listeners.size) @@ -267,18 +270,18 @@ class ShardManagerTest extends TestKit(ActorSystem("ShardManagerTest")) it("should notify listeners when a shard fails to authenticate") { val shards = Seq( - RedisShard("server1", "localhost", 6379, 0), - RedisShard("server2", "localhost", 6379, 1, auth = Some("not-valid-auth"))) + RedisShard("server1", host, 6379, 0), + RedisShard("server2", host, 6379, 1, auth = Some("not-valid-auth"))) val probe = TestProbe() val shardManager = TestActorRef[ShardManager](ShardManager( shards, Set(probe.ref))) - probe.expectMsg(Connecting("localhost", 6379)) - probe.expectMsg(Connecting("localhost", 6379)) - probe.expectMsg(Connected("localhost", 6379)) - probe.expectMsg(Redis.AuthenticationFailed("localhost", 6379)) + probe.expectMsg(Connecting(host, 6379)) + probe.expectMsg(Connecting(host, 6379)) + probe.expectMsg(Connected(host, 6379)) + probe.expectMsg(Redis.AuthenticationFailed(host, 6379)) } } } diff --git a/src/test/scala/brando/StashingRedisClientTest.scala b/src/test/scala/brando/StashingRedisClientTest.scala index 3bce549..60176af 100644 --- a/src/test/scala/brando/StashingRedisClientTest.scala +++ b/src/test/scala/brando/StashingRedisClientTest.scala @@ -3,17 +3,20 @@ package brando import akka.actor._ import akka.testkit._ import org.scalatest.FunSpecLike +import com.typesafe.config.ConfigFactory class StashingRedisClientTest extends TestKit(ActorSystem("StashingRedisClientTest")) with FunSpecLike with ImplicitSender { import Connection._ + val host = ConfigFactory.load().getString("brando.connection.host") + describe("stashing client should") { it("respond with Pong after connected") { val brando = system.actorOf(Redis(listeners = Set(self))) val stashing = system.actorOf(StashingRedis(brando)) - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) stashing ! Request("PING") expectMsg(Some(Pong)) } @@ -22,8 +25,8 @@ class StashingRedisClientTest extends TestKit(ActorSystem("StashingRedisClientTe val brando = system.actorOf(Redis(listeners = Set(self))) val stashing = system.actorOf(StashingRedis(brando)) stashing ! Request("PING") - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) expectMsg(Some(Pong)) } @@ -35,8 +38,8 @@ class StashingRedisClientTest extends TestKit(ActorSystem("StashingRedisClientTe stashing ! Request("PING") stashing ! Request("GET", "non-existing-key") stashing ! Request("PING") - expectMsg(Connecting("localhost", 6379)) - expectMsg(Connected("localhost", 6379)) + expectMsg(Connecting(host, 6379)) + expectMsg(Connected(host, 6379)) expectMsg(Some(Pong)) expectMsg(Some(Pong)) expectNoMsg() diff --git a/version.sbt b/version.sbt new file mode 100644 index 0000000..1ad9bd1 --- /dev/null +++ b/version.sbt @@ -0,0 +1 @@ +version in ThisBuild := "3.3.0" \ No newline at end of file