Skip to content

Commit

Permalink
Make the Redis tests actually run
Browse files Browse the repository at this point in the history
  • Loading branch information
cb372 committed Dec 8, 2015
1 parent fc828e6 commit c1fb108
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 74 deletions.
4 changes: 3 additions & 1 deletion redis/src/test/scala/scalacache/redis/RedisCacheSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class RedisCacheSpec
type JClient = Jedis
type JPool = JedisPool

def withJedis(tests: (JPool, JClient) => Unit): Unit = assumingRedisIsRunning _
val withJedis = assumingRedisIsRunning _

def constructCache(pool: JPool): Cache = RedisCache(pool)

def flushRedis(client: JClient): Unit = client.flushDB()

runTestsIfPossible()

}
148 changes: 76 additions & 72 deletions redis/src/test/scala/scalacache/redis/RedisCacheSpecBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,115 +23,119 @@ trait RedisCacheSpecBase
type JPool
type JClient <: JedisCommands with BinaryJedisCommands

def withJedis(tests: (JPool, JClient) => Unit): Unit
def withJedis: ((JPool, JClient) => Unit) => Unit
def constructCache(pool: JPool): Cache
def flushRedis(client: JClient): Unit

withJedis { (pool, client) =>
def runTestsIfPossible() = {

val cache = constructCache(pool)
withJedis { (pool, client) =>

before {
flushRedis(client)
}
val cache = constructCache(pool)

behavior of "get"
before {
flushRedis(client)
}

it should "return the value stored in Redis" in {
client.set(bytes("key1"), serialize(123))
whenReady(cache.get("key1")) { _ should be(Some(123)) }
}
behavior of "get"

it should "return None if the given key does not exist in the underlying cache" in {
whenReady(cache.get("non-existent-key")) { _ should be(None) }
}
it should "return the value stored in Redis" in {
client.set(bytes("key1"), serialize(123))
whenReady(cache.get("key1")) { _ should be(Some(123)) }
}

it should "return None if the given key does not exist in the underlying cache" in {
whenReady(cache.get("non-existent-key")) { _ should be(None) }
}

behavior of "put"
behavior of "put"

it should "store the given key-value pair in the underlying cache" in {
whenReady(cache.put("key2", 123, None)) { _ =>
deserialize[Int](client.get(bytes("key2"))) should be(123)
it should "store the given key-value pair in the underlying cache" in {
whenReady(cache.put("key2", 123, None)) { _ =>
deserialize[Int](client.get(bytes("key2"))) should be(123)
}
}
}

behavior of "put with TTL"
behavior of "put with TTL"

it should "store the given key-value pair in the underlying cache" in {
whenReady(cache.put("key3", 123, Some(1 second))) { _ =>
deserialize[Int](client.get(bytes("key3"))) should be(123)
it should "store the given key-value pair in the underlying cache" in {
whenReady(cache.put("key3", 123, Some(1 second))) { _ =>
deserialize[Int](client.get(bytes("key3"))) should be(123)

// Should expire after 1 second
eventually(timeout(Span(2, Seconds))) {
client.get(bytes("key3")) should be(null)
// Should expire after 1 second
eventually(timeout(Span(2, Seconds))) {
client.get(bytes("key3")) should be(null)
}
}
}
}

behavior of "put with TTL of zero"
behavior of "put with TTL of zero"

it should "store the given key-value pair in the underlying cache with no expiry" in {
whenReady(cache.put("key4", 123, Some(Duration.Zero))) { _ =>
deserialize[Int](client.get(bytes("key4"))) should be(123)
client.ttl(bytes("key4")) should be(-1L)
it should "store the given key-value pair in the underlying cache with no expiry" in {
whenReady(cache.put("key4", 123, Some(Duration.Zero))) { _ =>
deserialize[Int](client.get(bytes("key4"))) should be(123)
client.ttl(bytes("key4")) should be(-1L)
}
}
}

behavior of "put with TTL of less than 1 second"
behavior of "put with TTL of less than 1 second"

it should "store the given key-value pair in the underlying cache" in {
whenReady(cache.put("key5", 123, Some(100 milliseconds))) { _ =>
deserialize[Int](client.get(bytes("key5"))) should be(123)
client.pttl("key5").toLong should be > 0L
it should "store the given key-value pair in the underlying cache" in {
whenReady(cache.put("key5", 123, Some(100 milliseconds))) { _ =>
deserialize[Int](client.get(bytes("key5"))) should be(123)
client.pttl("key5").toLong should be > 0L

// Should expire after 1 second
eventually(timeout(Span(2, Seconds))) {
client.get(bytes("key5")) should be(null)
// Should expire after 1 second
eventually(timeout(Span(2, Seconds))) {
client.get(bytes("key5")) should be(null)
}
}
}
}

behavior of "caching with serialization"
behavior of "caching with serialization"

def roundTrip[V](key: String, value: V): Future[Option[V]] = {
cache.put(key, value, None).flatMap(_ => cache.get(key))
}
def roundTrip[V](key: String, value: V): Future[Option[V]] = {
cache.put(key, value, None).flatMap(_ => cache.get(key))
}

it should "round-trip a String" in {
whenReady(roundTrip("string", "hello")) { _ should be(Some("hello")) }
}
it should "round-trip a String" in {
whenReady(roundTrip("string", "hello")) { _ should be(Some("hello")) }
}

it should "round-trip a byte array" in {
whenReady(roundTrip("bytearray", bytes("world"))) { result =>
new String(result.get, "UTF-8") should be("world")
it should "round-trip a byte array" in {
whenReady(roundTrip("bytearray", bytes("world"))) { result =>
new String(result.get, "UTF-8") should be("world")
}
}
}

it should "round-trip an Int" in {
whenReady(roundTrip("int", 345)) { _ should be(Some(345)) }
}
it should "round-trip an Int" in {
whenReady(roundTrip("int", 345)) { _ should be(Some(345)) }
}

it should "round-trip a Double" in {
whenReady(roundTrip("double", 1.23)) { _ should be(Some(1.23)) }
}
it should "round-trip a Double" in {
whenReady(roundTrip("double", 1.23)) { _ should be(Some(1.23)) }
}

it should "round-trip a Long" in {
whenReady(roundTrip("long", 3456L)) { _ should be(Some(3456L)) }
}
it should "round-trip a Long" in {
whenReady(roundTrip("long", 3456L)) { _ should be(Some(3456L)) }
}

it should "round-trip a Serializable case class" in {
val cc = CaseClass(123, "wow")
whenReady(roundTrip("caseclass", cc)) { _ should be(Some(cc)) }
}
it should "round-trip a Serializable case class" in {
val cc = CaseClass(123, "wow")
whenReady(roundTrip("caseclass", cc)) { _ should be(Some(cc)) }
}

behavior of "remove"
behavior of "remove"

it should "delete the given key and its value from the underlying cache" in {
client.set(bytes("key1"), serialize(123))
deserialize[Int](client.get(bytes("key1"))) should be(123)
it should "delete the given key and its value from the underlying cache" in {
client.set(bytes("key1"), serialize(123))
deserialize[Int](client.get(bytes("key1"))) should be(123)

whenReady(cache.remove("key1")) { _ =>
client.get("key1") should be(null)
whenReady(cache.remove("key1")) { _ =>
client.get("key1") should be(null)
}
}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ShardedRedisCacheSpec extends RedisCacheSpecBase {
type JClient = ShardedJedis
type JPool = ShardedJedisPool

def withJedis(tests: (JPool, JClient) => Unit): Unit = assumingMultipleRedisAreRunning _
val withJedis = assumingMultipleRedisAreRunning _

def constructCache(pool: JPool): Cache = ShardedRedisCache(pool)

Expand All @@ -35,4 +35,6 @@ class ShardedRedisCacheSpec extends RedisCacheSpecBase {
}
}

runTestsIfPossible()

}

0 comments on commit c1fb108

Please sign in to comment.