Skip to content

Commit

Permalink
testing with docker
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Siatkowski committed Aug 29, 2019
1 parent 8ac0ebc commit 657742f
Show file tree
Hide file tree
Showing 30 changed files with 227 additions and 193 deletions.
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,3 @@ script:
jdk:
- openjdk8
- openjdk11
services:
- redis-server
before_script:
- sudo redis-server /etc/redis/redis.conf --port 6380
- sudo redis-server /etc/redis/redis.conf --port 6381
- sudo redis-server /etc/redis/redis.conf --port 6382
15 changes: 13 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,27 @@ lazy val commonSettings: Seq[Setting[_]] = Seq(
)
)

def dockerTestKit(version: String): Seq[ModuleID] = {
Seq("docker-testkit-scalatest", "docker-testkit-impl-docker-java").map("com.whisk" %% _ % version % Test) :+
// https://github.com/eclipse-ee4j/jaxb-ri/issues/1222
"javax.xml.bind" % "jaxb-api" % "2.3.1" % Test
}

lazy val coreSettings = commonSettings ++ Seq(
name := "RedisClient",
libraryDependencies ++= Seq(
"org.apache.commons" % "commons-pool2" % "2.6.2",
"org.slf4j" % "slf4j-api" % "1.7.26",
"org.slf4j" % "slf4j-log4j12" % "1.7.26" % "provided",
"log4j" % "log4j" % "1.2.17" % "provided",
"org.scalatest" %% "scalatest" % "3.0.8" % "test"),
"org.scalatest" %% "scalatest" % "3.0.8" % "test"
) ++
(scalaBinaryVersion.value match {
case "2.10" => dockerTestKit("0.9.8")
case _ => dockerTestKit("0.9.9")
})
,

parallelExecution in Test := false,
publishTo := version { (v: String) =>
val nexus = "https://oss.sonatype.org/"
if (v.trim.endsWith("SNAPSHOT")) Some("snapshots" at nexus + "content/repositories/snapshots")
Expand Down
7 changes: 4 additions & 3 deletions src/test/scala/com/redis/PatternsSpec.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.redis

import com.redis.Patterns._
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, FunSpec, Matchers}
import com.redis.common.RedisDocker
import org.scalatest.{BeforeAndAfterEach, FunSpec, Matchers}

class PatternsSpec extends FunSpec
with Matchers
with BeforeAndAfterEach
with BeforeAndAfterAll {
with RedisDocker {

implicit val clients = new RedisClientPool("localhost", 6379)
implicit lazy val clients = new RedisClientPool(redisContainerHost, redisContainerPort)

override def afterEach = clients.withClient{
client => client.flushdb
Expand Down
3 changes: 2 additions & 1 deletion src/test/scala/com/redis/PipelineSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class PipelineSpec extends FunSpec
with IntSpec
with Inside {

val r = new RedisClient("localhost", 6379)
override protected lazy val r: RedisClient =
new RedisClient(redisContainerHost, redisContainerPort)

describe("pipeline1") {
it("should do pipelined commands") {
Expand Down
37 changes: 14 additions & 23 deletions src/test/scala/com/redis/PoolSpec.scala
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
package com.redis

import org.scalatest.FunSpec
import org.scalatest.BeforeAndAfterEach
import org.scalatest.BeforeAndAfterAll
import org.scalatest.Matchers
import com.redis.common.RedisDocker
import org.scalatest.{BeforeAndAfterEach, FunSpec, Matchers}

import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global

class PoolSpec extends FunSpec
with Matchers
with BeforeAndAfterEach
with BeforeAndAfterAll {
with RedisDocker {

implicit val clients = new RedisClientPool("localhost", 6379)
implicit lazy val clients: RedisClientPool = new RedisClientPool(redisContainerHost, redisContainerPort)

override def beforeEach = {
override protected def beforeEach(): Unit = {
super.beforeEach()
clients.port should be > 0 // initialize lazy val hack :(
}

override def afterEach = clients.withClient{
client => client.flushdb
override def afterEach(): Unit = {
clients.withClient{
client => client.flushdb
}
super.afterEach()
}

override def afterAll = {
override def afterAll(): Unit = {
clients.withClient{ client => client.disconnect }
clients.close
super.afterAll()
}

def lp(msgs: List[String]) = {
Expand Down Expand Up @@ -69,18 +72,6 @@ class PoolSpec extends FunSpec
}
}

def leftp(msgs: List[String]) = {
clients.withClient {
client => {
val ln = new scala.util.Random().nextString(10)
msgs.foreach(client.lpush(ln, _))
val len = client.llen(ln)
println(len)
len
}
}
}

import Bench._

private val amountMultiplier = 1 // unit test multiplier
Expand Down
12 changes: 5 additions & 7 deletions src/test/scala/com/redis/PubSubSpec.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.redis

import org.scalatest.FunSpec
import org.scalatest.BeforeAndAfterAll
import org.scalatest.Matchers
import com.redis.common.RedisDocker
import org.scalatest.{FunSpec, Matchers}

class PubSubSpec extends FunSpec
with Matchers
with BeforeAndAfterAll {
with RedisDocker {

val r = new RedisClient("localhost", 6379)
val t = new RedisClient("localhost", 6379)
lazy val r = new RedisClient(redisContainerHost, redisContainerPort)
lazy val t = new RedisClient(redisContainerHost, redisContainerPort)

override def afterAll(): Unit = {
r.flushall
r.close()
t.close()
}
Expand Down
22 changes: 15 additions & 7 deletions src/test/scala/com/redis/RedisClientSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,36 @@ import org.scalatest.Matchers
class RedisClientSpec extends FunSpec
with Matchers with ApiSpec {

override val r: RedisClient =
new RedisClient("localhost", 6379)
override protected lazy val r: RedisClient =
new RedisClient(redisContainerHost, redisContainerPort)

private lazy val redisUrl = s"$redisContainerHost:$redisContainerPort"

describe("constructor") {
it("should parse the db-number from the path of connection uri") {
val client = new RedisClient(new URI("redis://localhost:6379/4"))
val client = new RedisClient(new URI(s"redis://$redisUrl/4"))
client.database shouldBe 4
client.close()
}

it("should default to db 0 for connection uri without db-number") {
val client = new RedisClient(new URI("redis://localhost:6379"))
val client = new RedisClient(new URI(s"redis://$redisUrl"))
client.database shouldBe 0
client.close()
}
}

describe("toString") {
it("should include the db-number") {
new RedisClient("localhost", 6379, 1).toString shouldBe "localhost:6379/1"
val c = new RedisClient(redisContainerHost, redisContainerPort, 1)
c.toString shouldBe s"$redisUrl/1"
c.close()
}
}

describe("test subscribe") {
val r = new RedisClient("localhost", 6379)
it("should subscribe") {
val r = new RedisClient(redisContainerHost, redisContainerPort)

println(r.get("vvl:qm"))

Expand All @@ -58,5 +65,6 @@ class RedisClientSpec extends FunSpec
Thread.sleep(3000)

r.get("vvl:qm")
}
r.close()
}}
}
3 changes: 2 additions & 1 deletion src/test/scala/com/redis/SerializationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class SerializationSpec extends FunSpec
with Matchers
with IntSpec {

val r = new RedisClient("localhost", 6379)
override protected lazy val r: RedisClient =
new RedisClient(redisContainerHost, redisContainerPort)

it("should not conflict when using all built in parsers") {
import Parse.Implicits._
Expand Down
30 changes: 7 additions & 23 deletions src/test/scala/com/redis/WatchSpec.scala
Original file line number Diff line number Diff line change
@@ -1,40 +1,23 @@
package com.redis

import org.scalatest.FunSpec
import org.scalatest.BeforeAndAfterEach
import org.scalatest.BeforeAndAfterAll
import org.scalatest.Matchers
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.{Milliseconds, Seconds => SSeconds, Span}
import com.redis.common.RedisDocker
import org.scalatest.{FunSpec, Matchers}

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

class WatchSpec extends FunSpec with ScalaFutures
class WatchSpec extends FunSpec
with Matchers
with BeforeAndAfterEach
with BeforeAndAfterAll {

implicit val pc: PatienceConfig = PatienceConfig(Span(5, SSeconds), Span(100, Milliseconds))

val clients: RedisClientPool = new RedisClientPool("localhost", 6379)

override def beforeAll(): Unit = {
clients.withClient(_.flushall)
}

override def afterAll(): Unit = {
clients.close
}
with RedisDocker {

describe("watch") {
it("should fail a transaction if modified from another client") {
val clients = new RedisClientPool(redisContainerHost, redisContainerPort)
val p1: Future[Option[List[Any]]] = Future {
clients.withClient { client =>
client.watch("key")
client.pipeline { p =>
p.set("key", "debasish")
Thread.sleep(50)
Thread.sleep(100)
p.get("key")
p.get("key1")
}
Expand All @@ -50,6 +33,7 @@ class WatchSpec extends FunSpec with ScalaFutures

p2.futureValue should equal(true)
p1.futureValue should equal(None)
clients.close
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/redis/api/ApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait ApiSpec
with SortedSetApiSpec
with StringApiSpec {

override val r: AutoCloseable
override protected def r: AutoCloseable
with BaseApi
with EvalApi
with GeoApi
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/redis/api/BaseApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait BaseApiSpec extends FunSpec
with IntSpec {

// todo: remove SetApi, HashApi and ListApi
override val r: BaseApi with StringApi with AutoCloseable with SetApi with HashApi with ListApi
override protected def r: BaseApi with StringApi with AutoCloseable with SetApi with HashApi with ListApi

dbsize()
del()
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/redis/api/EvalApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait EvalApiSpec extends FunSpec
with Matchers
with IntSpec {

override val r: BaseApi with StringApi with EvalApi with ListApi with SortedSetApi with AutoCloseable
override protected def r: BaseApi with StringApi with EvalApi with ListApi with SortedSetApi with AutoCloseable

describe("eval") {
getStringReply()
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/redis/api/GeoApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait GeoApiSpec extends FunSpec
with Matchers
with IntSpec {

override val r: BaseApi with StringApi with GeoApi with AutoCloseable
override protected def r: BaseApi with StringApi with GeoApi with AutoCloseable

geoadd()
geopos()
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/redis/api/HashApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait HashApiSpec extends FunSpec
with Matchers
with IntSpec {

override val r: BaseApi with StringApi with HashApi with AutoCloseable
override protected def r: BaseApi with StringApi with HashApi with AutoCloseable

hset()
hset1()
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/redis/api/HyperLogLogApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait HyperLogLogApiSpec extends FunSpec
with Matchers
with IntSpec {

override val r: BaseApi with StringApi with HyperLogLogApi with AutoCloseable
override protected def r: BaseApi with StringApi with HyperLogLogApi with AutoCloseable

pfadd()
pfcount()
Expand Down
11 changes: 3 additions & 8 deletions src/test/scala/com/redis/api/ListApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package com.redis.api
import com.redis.RedisClient
import com.redis.common.IntSpec
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.{Milliseconds, Span, Seconds => SSeconds}
import org.scalatest.{BeforeAndAfterEach, FunSpec, Matchers}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future


Expand All @@ -15,9 +13,7 @@ trait ListApiSpec extends FunSpec with ScalaFutures
with BeforeAndAfterEach
with IntSpec {

implicit val pc: PatienceConfig = PatienceConfig(Span(3, SSeconds), Span(100, Milliseconds))

override val r: BaseApi with StringApi with ListApi with AutoCloseable
override protected def r: BaseApi with StringApi with ListApi with AutoCloseable

blpop()
brpoplpush()
Expand Down Expand Up @@ -397,7 +393,7 @@ trait ListApiSpec extends FunSpec with ScalaFutures
}

it("should pop blockingly") {
val r1 = new RedisClient("localhost", 6379)
val r1 = new RedisClient(redisContainerHost, redisContainerPort)

val testVal: Future[Option[String]] = Future {
r1.brpoplpush("l1", "l2", 3) should equal(Some("a"))
Expand All @@ -423,11 +419,10 @@ trait ListApiSpec extends FunSpec with ScalaFutures
}
}

// todo: this does not fail the ScalaTest even if the matcher fails
protected def blpop(): Unit = {
describe("blpop") {
it("should pop in a blocking mode") {
val r1 = new RedisClient("localhost", 6379)
val r1 = new RedisClient(redisContainerHost, redisContainerPort)

val blpopV: Future[Option[(String, String)]] = Future {
r1.blpop(3, "l1", "l2")
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/redis/api/NodeApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.scalatest.{FunSpec, Matchers}
trait NodeApiSpec extends FunSpec with Matchers
with IntSpec {

override val r: BaseApi with StringApi with NodeApi with AutoCloseable
override protected def r: BaseApi with StringApi with NodeApi with AutoCloseable

describe("NodeApiTest") {

Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/redis/api/SetApiSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait SetApiSpec extends FunSpec
with IntSpec {

// todo: remove HashApi, ListApi
override val r: BaseApi with StringApi with SetApi with AutoCloseable with HashApi with ListApi
override protected def r: BaseApi with StringApi with SetApi with AutoCloseable with HashApi with ListApi

sadd()
saddWithVariadicArguments()
Expand Down
Loading

0 comments on commit 657742f

Please sign in to comment.