diff --git a/src/main/scala/shade/memcached/Codec.scala b/src/main/scala/shade/memcached/Codec.scala index 74994c2..802c1ff 100644 --- a/src/main/scala/shade/memcached/Codec.scala +++ b/src/main/scala/shade/memcached/Codec.scala @@ -24,15 +24,15 @@ import scala.util.control.NonFatal */ @implicitNotFound("Could not find any Codec implementation for type ${T}. Please provide one or import shade.memcached.MemcachedCodecs._") trait Codec[T] { - def serialize(value: T): Array[Byte] - def deserialize(data: Array[Byte]): T + def serialize(value: T, flags: Int): Array[Byte] + def deserialize(data: Array[Byte], flags: Int): T } object Codec extends BaseCodecs trait BaseCodecs { implicit object IntBinaryCodec extends Codec[Int] { - def serialize(value: Int): Array[Byte] = + def serialize(value: Int, flags: Int): Array[Byte] = Array( (value >>> 24).asInstanceOf[Byte], (value >>> 16).asInstanceOf[Byte], @@ -40,7 +40,7 @@ trait BaseCodecs { value.asInstanceOf[Byte] ) - def deserialize(data: Array[Byte]): Int = + def deserialize(data: Array[Byte], flags: Int): Int = (data(0).asInstanceOf[Int] & 255) << 24 | (data(1).asInstanceOf[Int] & 255) << 16 | (data(2).asInstanceOf[Int] & 255) << 8 | @@ -49,32 +49,32 @@ trait BaseCodecs { implicit object DoubleBinaryCodec extends Codec[Double] { import java.lang.{ Double => JvmDouble } - def serialize(value: Double): Array[Byte] = { + def serialize(value: Double, flags: Int): Array[Byte] = { val l = JvmDouble.doubleToLongBits(value) - LongBinaryCodec.serialize(l) + LongBinaryCodec.serialize(l, flags) } - def deserialize(data: Array[Byte]): Double = { - val l = LongBinaryCodec.deserialize(data) + def deserialize(data: Array[Byte], flags: Int): Double = { + val l = LongBinaryCodec.deserialize(data, flags) JvmDouble.longBitsToDouble(l) } } implicit object FloatBinaryCodec extends Codec[Float] { import java.lang.{ Float => JvmFloat } - def serialize(value: Float): Array[Byte] = { + def serialize(value: Float, flags: Int): Array[Byte] = { val i = JvmFloat.floatToIntBits(value) - IntBinaryCodec.serialize(i) + IntBinaryCodec.serialize(i, flags) } - def deserialize(data: Array[Byte]): Float = { - val i = IntBinaryCodec.deserialize(data) + def deserialize(data: Array[Byte], flags: Int): Float = { + val i = IntBinaryCodec.deserialize(data, flags) JvmFloat.intBitsToFloat(i) } } implicit object LongBinaryCodec extends Codec[Long] { - def serialize(value: Long): Array[Byte] = + def serialize(value: Long, flags: Int): Array[Byte] = Array( (value >>> 56).asInstanceOf[Byte], (value >>> 48).asInstanceOf[Byte], @@ -86,7 +86,7 @@ trait BaseCodecs { value.asInstanceOf[Byte] ) - def deserialize(data: Array[Byte]): Long = + def deserialize(data: Array[Byte], flags: Int): Long = (data(0).asInstanceOf[Long] & 255) << 56 | (data(1).asInstanceOf[Long] & 255) << 48 | (data(2).asInstanceOf[Long] & 255) << 40 | @@ -98,45 +98,45 @@ trait BaseCodecs { } implicit object BooleanBinaryCodec extends Codec[Boolean] { - def serialize(value: Boolean): Array[Byte] = + def serialize(value: Boolean, flags: Int): Array[Byte] = Array((if (value) 1 else 0).asInstanceOf[Byte]) - def deserialize(data: Array[Byte]): Boolean = + def deserialize(data: Array[Byte], flags: Int): Boolean = data.isDefinedAt(0) && data(0) == 1 } implicit object CharBinaryCodec extends Codec[Char] { - def serialize(value: Char): Array[Byte] = Array( + def serialize(value: Char, flags: Int): Array[Byte] = Array( (value >>> 8).asInstanceOf[Byte], value.asInstanceOf[Byte] ) - def deserialize(data: Array[Byte]): Char = + def deserialize(data: Array[Byte], flags: Int): Char = ((data(0).asInstanceOf[Int] & 255) << 8 | data(1).asInstanceOf[Int] & 255) .asInstanceOf[Char] } implicit object ShortBinaryCodec extends Codec[Short] { - def serialize(value: Short): Array[Byte] = Array( + def serialize(value: Short, flags: Int): Array[Byte] = Array( (value >>> 8).asInstanceOf[Byte], value.asInstanceOf[Byte] ) - def deserialize(data: Array[Byte]): Short = + def deserialize(data: Array[Byte], flags: Int): Short = ((data(0).asInstanceOf[Short] & 255) << 8 | data(1).asInstanceOf[Short] & 255) .asInstanceOf[Short] } implicit object StringBinaryCodec extends Codec[String] { - def serialize(value: String): Array[Byte] = value.getBytes("UTF-8") - def deserialize(data: Array[Byte]): String = new String(data, "UTF-8") + def serialize(value: String, flags: Int): Array[Byte] = value.getBytes("UTF-8") + def deserialize(data: Array[Byte], flags: Int): String = new String(data, "UTF-8") } implicit object ArrayByteBinaryCodec extends Codec[Array[Byte]] { - def serialize(value: Array[Byte]): Array[Byte] = value - def deserialize(data: Array[Byte]): Array[Byte] = data + def serialize(value: Array[Byte], flags: Int): Array[Byte] = value + def deserialize(data: Array[Byte], flags: Int): Array[Byte] = data } } @@ -152,7 +152,7 @@ trait GenericCodec { case NonFatal(_) => // does nothing } - def serialize(value: S): Array[Byte] = + def serialize(value: S, flags: Int): Array[Byte] = using (new ByteArrayOutputStream()) { buf => using (new ObjectOutputStream(buf)) { out => out.writeObject(value) @@ -161,7 +161,7 @@ trait GenericCodec { } } - def deserialize(data: Array[Byte]): S = + def deserialize(data: Array[Byte], flags: Int): S = using (new ByteArrayInputStream(data)) { buf => val in = new GenericCodecObjectInputStream(classTag, buf) using (in) { inp => diff --git a/src/main/scala/shade/memcached/FakeMemcached.scala b/src/main/scala/shade/memcached/FakeMemcached.scala index f15411a..1d57967 100644 --- a/src/main/scala/shade/memcached/FakeMemcached.scala +++ b/src/main/scala/shade/memcached/FakeMemcached.scala @@ -21,47 +21,47 @@ import scala.concurrent.{ ExecutionContext, Future } class FakeMemcached(context: ExecutionContext) extends Memcached { private[this] implicit val ec = context - def add[T](key: String, value: T, exp: Duration)(implicit codec: Codec[T]): CancelableFuture[Boolean] = + def add[T](key: String, value: T, flags: Int, exp: Duration)(implicit codec: Codec[T]): CancelableFuture[Boolean] = value match { case null => CancelableFuture.successful(false) case _ => - CancelableFuture.successful(cache.add(key, codec.serialize(value).toSeq, exp)) + CancelableFuture.successful(cache.add(key, codec.serialize(value, flags).toSeq, exp)) } - def set[T](key: String, value: T, exp: Duration)(implicit codec: Codec[T]): CancelableFuture[Unit] = + def set[T](key: String, value: T, flags: Int, exp: Duration)(implicit codec: Codec[T]): CancelableFuture[Unit] = value match { case null => CancelableFuture.successful(()) case _ => - CancelableFuture.successful(cache.set(key, codec.serialize(value).toSeq, exp)) + CancelableFuture.successful(cache.set(key, codec.serialize(value, flags).toSeq, exp)) } def delete(key: String): CancelableFuture[Boolean] = CancelableFuture.successful(cache.delete(key)) def get[T](key: String)(implicit codec: Codec[T]): Future[Option[T]] = - Future.successful(cache.get[Seq[Byte]](key)).map(_.map(x => codec.deserialize(x.toArray))) + Future.successful(cache.get[Seq[Byte]](key)).map(_.map(x => codec.deserialize(x.toArray, 0))) - def compareAndSet[T](key: String, expecting: Option[T], newValue: T, exp: Duration)(implicit codec: Codec[T]): Future[Boolean] = - Future.successful(cache.compareAndSet(key, expecting.map(x => codec.serialize(x).toSeq), codec.serialize(newValue).toSeq, exp)) + def compareAndSet[T](key: String, expecting: Option[T], newValue: T, flags: Int, exp: Duration)(implicit codec: Codec[T]): Future[Boolean] = + Future.successful(cache.compareAndSet(key, expecting.map(x => codec.serialize(x, flags).toSeq), codec.serialize(newValue, flags).toSeq, exp)) def transformAndGet[T](key: String, exp: Duration)(cb: (Option[T]) => T)(implicit codec: Codec[T]): Future[T] = Future.successful(cache.transformAndGet[Seq[Byte]](key: String, exp) { current => - val cValue = current.map(x => codec.deserialize(x.toArray)) + val cValue = current.map(x => codec.deserialize(x.toArray, 0)) val update = cb(cValue) - codec.serialize(update).toSeq + codec.serialize(update, 0).toSeq }) map { update => - codec.deserialize(update.toArray) + codec.deserialize(update.toArray, 0) } def getAndTransform[T](key: String, exp: Duration)(cb: (Option[T]) => T)(implicit codec: Codec[T]): Future[Option[T]] = Future.successful(cache.getAndTransform[Seq[Byte]](key: String, exp) { current => - val cValue = current.map(x => codec.deserialize(x.toArray)) + val cValue = current.map(x => codec.deserialize(x.toArray, 0)) val update = cb(cValue) - codec.serialize(update).toSeq + codec.serialize(update, 0).toSeq }) map { update => - update.map(x => codec.deserialize(x.toArray)) + update.map(x => codec.deserialize(x.toArray, 0)) } def increment(key: String, by: Long, default: Option[Long], exp: Duration): Future[Long] = { diff --git a/src/main/scala/shade/memcached/Memcached.scala b/src/main/scala/shade/memcached/Memcached.scala index 1ff4e21..20f460b 100644 --- a/src/main/scala/shade/memcached/Memcached.scala +++ b/src/main/scala/shade/memcached/Memcached.scala @@ -28,20 +28,20 @@ trait Memcached extends java.io.Closeable { * * @return either true, in case the value was set, or false otherwise */ - def add[T](key: String, value: T, exp: Duration)(implicit codec: Codec[T]): CancelableFuture[Boolean] + def add[T](key: String, value: T, flags: Int, exp: Duration)(implicit codec: Codec[T]): CancelableFuture[Boolean] - def awaitAdd[T](key: String, value: T, exp: Duration)(implicit codec: Codec[T]): Boolean = - Await.result(add(key, value, exp), Duration.Inf) + def awaitAdd[T](key: String, value: T, flags: Int, exp: Duration)(implicit codec: Codec[T]): Boolean = + Await.result(add(key, value, flags, exp), Duration.Inf) /** * Sets a (key, value) in the cache store. * * The expiry time can be Duration.Inf (infinite duration). */ - def set[T](key: String, value: T, exp: Duration)(implicit codec: Codec[T]): CancelableFuture[Unit] + def set[T](key: String, value: T, flags: Int, exp: Duration)(implicit codec: Codec[T]): CancelableFuture[Unit] - def awaitSet[T](key: String, value: T, exp: Duration)(implicit codec: Codec[T]) { - Await.result(set(key, value, exp), Duration.Inf) + def awaitSet[T](key: String, value: T, flags: Int, exp: Duration)(implicit codec: Codec[T]) { + Await.result(set(key, value, flags, exp), Duration.Inf) } /** @@ -71,7 +71,7 @@ trait Memcached extends java.io.Closeable { * @param exp can be Duration.Inf (infinite) for not setting an expiration * @return either true (in case the compare-and-set succeeded) or false otherwise */ - def compareAndSet[T](key: String, expecting: Option[T], newValue: T, exp: Duration)(implicit codec: Codec[T]): Future[Boolean] + def compareAndSet[T](key: String, expecting: Option[T], newValue: T, flags: Int, exp: Duration)(implicit codec: Codec[T]): Future[Boolean] /** * Transforms the given key and returns the new value. diff --git a/src/main/scala/shade/memcached/MemcachedImpl.scala b/src/main/scala/shade/memcached/MemcachedImpl.scala index 7e1862a..4adf232 100644 --- a/src/main/scala/shade/memcached/MemcachedImpl.scala +++ b/src/main/scala/shade/memcached/MemcachedImpl.scala @@ -43,16 +43,14 @@ class MemcachedImpl(config: Configuration, ec: ExecutionContext) extends Memcach * * @return either true, in case the value was set, or false otherwise */ - def add[T](key: String, value: T, exp: Duration)(implicit codec: Codec[T]): CancelableFuture[Boolean] = + def add[T](key: String, value: T, flags: Int, exp: Duration)(implicit codec: Codec[T]): CancelableFuture[Boolean] = value match { case null => CancelableFuture.successful(false) case _ => - instance.realAsyncAdd(withPrefix(key), codec.serialize(value), 0, exp, config.operationTimeout) map { - case SuccessfulResult(givenKey, Some(_)) => - true - case SuccessfulResult(givenKey, None) => - false + instance.realAsyncAdd(withPrefix(key), codec.serialize(value, flags), flags, exp, config.operationTimeout) map { + case SuccessfulResult(givenKey, option, _) => + option.isDefined case failure: FailedResult => throwExceptionOn(failure) } @@ -63,13 +61,13 @@ class MemcachedImpl(config: Configuration, ec: ExecutionContext) extends Memcach * * The expiry time can be Duration.Inf (infinite duration). */ - def set[T](key: String, value: T, exp: Duration)(implicit codec: Codec[T]): CancelableFuture[Unit] = + def set[T](key: String, value: T, flags: Int, exp: Duration)(implicit codec: Codec[T]): CancelableFuture[Unit] = value match { case null => CancelableFuture.successful(()) case _ => - instance.realAsyncSet(withPrefix(key), codec.serialize(value), 0, exp, config.operationTimeout) map { - case SuccessfulResult(givenKey, _) => + instance.realAsyncSet(withPrefix(key), codec.serialize(value, flags), flags, exp, config.operationTimeout) map { + case SuccessfulResult(givenKey, _, _) => () case failure: FailedResult => throwExceptionOn(failure) @@ -83,7 +81,7 @@ class MemcachedImpl(config: Configuration, ec: ExecutionContext) extends Memcach */ def delete(key: String): CancelableFuture[Boolean] = instance.realAsyncDelete(withPrefix(key), config.operationTimeout) map { - case SuccessfulResult(givenKey, result) => + case SuccessfulResult(givenKey, result, _) => result case failure: FailedResult => throwExceptionOn(failure) @@ -96,8 +94,8 @@ class MemcachedImpl(config: Configuration, ec: ExecutionContext) extends Memcach */ def get[T](key: String)(implicit codec: Codec[T]): Future[Option[T]] = instance.realAsyncGet(withPrefix(key), config.operationTimeout) map { - case SuccessfulResult(givenKey, option) => - option.map(codec.deserialize) + case SuccessfulResult(givenKey, option, flags) => + option.map(codec.deserialize(_, flags)) case failure: FailedResult => throwExceptionOn(failure) } @@ -115,20 +113,20 @@ class MemcachedImpl(config: Configuration, ec: ExecutionContext) extends Memcach * @param exp can be Duration.Inf (infinite) for not setting an expiration * @return either true (in case the compare-and-set succeeded) or false otherwise */ - def compareAndSet[T](key: String, expecting: Option[T], newValue: T, exp: Duration)(implicit codec: Codec[T]): Future[Boolean] = + def compareAndSet[T](key: String, expecting: Option[T], newValue: T, flags: Int, exp: Duration)(implicit codec: Codec[T]): Future[Boolean] = expecting match { case None => - add[T](key, newValue, exp) + add[T](key, newValue, flags, exp) case Some(expectingValue) => instance.realAsyncGets(withPrefix(key), config.operationTimeout) flatMap { - case SuccessfulResult(givenKey, None) => + case SuccessfulResult(givenKey, None, _) => Future.successful(false) - case SuccessfulResult(givenKey, Some((currentData, casID))) => - if (codec.deserialize(currentData) == expectingValue) - instance.realAsyncCAS(withPrefix(key), casID, 0, codec.serialize(newValue), exp, config.operationTimeout) map { - case SuccessfulResult(_, bool) => + case SuccessfulResult(givenKey, Some((currentData, casID)), f) => + if (codec.deserialize(currentData, f) == expectingValue) + instance.realAsyncCAS(withPrefix(key), casID, flags, codec.serialize(newValue, flags), exp, config.operationTimeout) map { + case SuccessfulResult(_, bool, _) => bool case failure: FailedResult => throwExceptionOn(failure) @@ -163,22 +161,22 @@ class MemcachedImpl(config: Configuration, ec: ExecutionContext) extends Memcach throw new TimeoutException(key) instance.realAsyncGets(keyWithPrefix, remainingTime.millis) flatMap { - case SuccessfulResult(_, None) => + case SuccessfulResult(_, None, flags) => val result = cb(None) - add(key, result, exp) flatMap { + add(key, result, flags, exp) flatMap { case true => Future.successful(f(None, result)) case false => loop(retry + 1) } - case SuccessfulResult(_, Some((current, casID))) => - val currentOpt = Some(codec.deserialize(current)) + case SuccessfulResult(_, Some((current, casID)), flags) => + val currentOpt = Some(codec.deserialize(current, flags)) val result = cb(currentOpt) - instance.realAsyncCAS(keyWithPrefix, casID, 0, codec.serialize(result), exp, remainingTime.millis) flatMap { - case SuccessfulResult(_, true) => + instance.realAsyncCAS(keyWithPrefix, casID, flags, codec.serialize(result, flags), exp, remainingTime.millis) flatMap { + case SuccessfulResult(_, true, _) => Future.successful(f(currentOpt, result)) - case SuccessfulResult(_, false) => + case SuccessfulResult(_, false, _) => loop(retry + 1) case failure: FailedResult => throwExceptionOn(failure) @@ -258,7 +256,7 @@ class MemcachedImpl(config: Configuration, ec: ExecutionContext) extends Memcach */ def increment(key: String, by: Long, default: Option[Long], exp: Duration): Future[Long] = instance.realAsyncMutate(withPrefix(key), by, Mutator.incr, default, exp, config.operationTimeout) map { - case SuccessfulResult(_, value) => + case SuccessfulResult(_, value, _) => value case failure: FailedResult => throwExceptionOn(failure) @@ -287,7 +285,7 @@ class MemcachedImpl(config: Configuration, ec: ExecutionContext) extends Memcach */ def decrement(key: String, by: Long, default: Option[Long], exp: Duration): Future[Long] = instance.realAsyncMutate(withPrefix(key), by, Mutator.decr, default, exp, config.operationTimeout) map { - case SuccessfulResult(_, value) => + case SuccessfulResult(_, value, _) => value case failure: FailedResult => throwExceptionOn(failure) diff --git a/src/main/scala/shade/memcached/internals/Result.scala b/src/main/scala/shade/memcached/internals/Result.scala index e7d06fc..95cf66c 100644 --- a/src/main/scala/shade/memcached/internals/Result.scala +++ b/src/main/scala/shade/memcached/internals/Result.scala @@ -12,5 +12,5 @@ package shade.memcached.internals sealed trait Result[+T] -case class SuccessfulResult[+T](key: String, result: T) extends Result[T] +case class SuccessfulResult[+T](key: String, result: T, flags: Int) extends Result[T] case class FailedResult(key: String, state: Status) extends Result[Nothing] diff --git a/src/main/scala/shade/memcached/internals/SpyMemcachedIntegration.scala b/src/main/scala/shade/memcached/internals/SpyMemcachedIntegration.scala index 41d49f9..50abf21 100644 --- a/src/main/scala/shade/memcached/internals/SpyMemcachedIntegration.scala +++ b/src/main/scala/shade/memcached/internals/SpyMemcachedIntegration.scala @@ -186,14 +186,14 @@ class SpyMemcachedIntegration(cf: ConnectionFactory, addrs: Seq[InetSocketAddres def receivedStatus(opStatus: OperationStatus) { handleStatus(opStatus, key, result) { case CASNotFoundStatus => - result.tryComplete(Success(SuccessfulResult(key, None))) + result.tryComplete(Success(SuccessfulResult(key, None, 0))) case CASSuccessStatus => } } def gotData(k: String, flags: Int, data: Array[Byte]) { assert(key == k, "Wrong key returned") - result.tryComplete(Success(SuccessfulResult(key, Option(data)))) + result.tryComplete(Success(SuccessfulResult(key, Option(data), flags))) } def complete() { @@ -217,7 +217,7 @@ class SpyMemcachedIntegration(cf: ConnectionFactory, addrs: Seq[InetSocketAddres } def gotData(key: String, cas: Long) { - result.tryComplete(Success(SuccessfulResult(key, cas))) + result.tryComplete(Success(SuccessfulResult(key, cas, flags))) } def complete() { @@ -237,13 +237,13 @@ class SpyMemcachedIntegration(cf: ConnectionFactory, addrs: Seq[InetSocketAddres def receivedStatus(opStatus: OperationStatus) { handleStatus(opStatus, key, result) { case CASExistsStatus => - result.tryComplete(Success(SuccessfulResult(key, None))) + result.tryComplete(Success(SuccessfulResult(key, None, flags))) case CASSuccessStatus => } } def gotData(key: String, cas: Long) { - result.tryComplete(Success(SuccessfulResult(key, Some(cas)))) + result.tryComplete(Success(SuccessfulResult(key, Some(cas), flags))) } def complete() { @@ -269,9 +269,9 @@ class SpyMemcachedIntegration(cf: ConnectionFactory, addrs: Seq[InetSocketAddres def receivedStatus(opStatus: OperationStatus) { handleStatus(opStatus, key, result) { case CASSuccessStatus => - result.tryComplete(Success(SuccessfulResult(key, true))) + result.tryComplete(Success(SuccessfulResult(key, true, 0))) case CASNotFoundStatus => - result.tryComplete(Success(SuccessfulResult(key, false))) + result.tryComplete(Success(SuccessfulResult(key, false, 0))) } } }) @@ -288,7 +288,7 @@ class SpyMemcachedIntegration(cf: ConnectionFactory, addrs: Seq[InetSocketAddres def receivedStatus(opStatus: OperationStatus) { handleStatus(opStatus, key, result) { case CASNotFoundStatus => - result.tryComplete(Success(SuccessfulResult(key, None))) + result.tryComplete(Success(SuccessfulResult(key, None, 0))) case CASSuccessStatus => } } @@ -298,7 +298,7 @@ class SpyMemcachedIntegration(cf: ConnectionFactory, addrs: Seq[InetSocketAddres assert(cas > 0, s"CAS was less than zero: $cas") result.tryComplete(Try { - SuccessfulResult(key, Option(data).map(d => (d, cas))) + SuccessfulResult(key, Option(data).map(d => (d, cas)), flags) }) } @@ -319,11 +319,11 @@ class SpyMemcachedIntegration(cf: ConnectionFactory, addrs: Seq[InetSocketAddres def receivedStatus(opStatus: OperationStatus) { handleStatus(opStatus, key, result) { case CASSuccessStatus => - result.tryComplete(Success(SuccessfulResult(key, true))) + result.tryComplete(Success(SuccessfulResult(key, true, flags))) case CASExistsStatus => - result.tryComplete(Success(SuccessfulResult(key, false))) + result.tryComplete(Success(SuccessfulResult(key, false, flags))) case CASNotFoundStatus => - result.tryComplete(Success(SuccessfulResult(key, false))) + result.tryComplete(Success(SuccessfulResult(key, false, flags))) } } @@ -353,7 +353,7 @@ class SpyMemcachedIntegration(cf: ConnectionFactory, addrs: Seq[InetSocketAddres def receivedStatus(opStatus: OperationStatus) { handleStatus(opStatus, key, result) { case CASSuccessStatus => - result.tryComplete(Success(SuccessfulResult(key, opStatus.getMessage.toLong))) + result.tryComplete(Success(SuccessfulResult(key, opStatus.getMessage.toLong, 0))) } } diff --git a/src/test/scala/shade/memcached/internals/MutablePartialResultSuite.scala b/src/test/scala/shade/memcached/internals/MutablePartialResultSuite.scala index dd24a2d..3661953 100644 --- a/src/test/scala/shade/memcached/internals/MutablePartialResultSuite.scala +++ b/src/test/scala/shade/memcached/internals/MutablePartialResultSuite.scala @@ -26,7 +26,7 @@ class MutablePartialResultSuite val promise = Promise[Result[Boolean]]() toCheck.completePromise("key1", promise) whenReady(promise.future) { - case SuccessfulResult(_, r) => assert(r == expected) + case SuccessfulResult(_, r, _) => assert(r == expected) case _ => fail("not successful") } } @@ -42,27 +42,27 @@ class MutablePartialResultSuite test("#tryComplete on a fresh MutablePartialResult") { val pResult = new MutablePartialResult[Boolean] - pResult.tryComplete(Success(SuccessfulResult("key1", false))) + pResult.tryComplete(Success(SuccessfulResult("key1", false, 0))) assertCompletePromise(toCheck = pResult, expected = false) } test("#tryComplete on a MutablePartialResult that has already been completed") { val pResult = new MutablePartialResult[Boolean] - assert(pResult.tryComplete(Success(SuccessfulResult("key1", false)))) - assert(!pResult.tryComplete(Success(SuccessfulResult("key1", true)))) + assert(pResult.tryComplete(Success(SuccessfulResult("key1", false, 0)))) + assert(!pResult.tryComplete(Success(SuccessfulResult("key1", true, 0)))) assertCompletePromise(toCheck = pResult, expected = false) } test("#tryCompleteWith on a fresh MutablePartialResult") { val pResult = new MutablePartialResult[Boolean] - pResult.tryCompleteWith(Future.successful(SuccessfulResult("key1", false))) + pResult.tryCompleteWith(Future.successful(SuccessfulResult("key1", false, 0))) assertCompletePromise(toCheck = pResult, expected = false) } test("#tryCompleteWith on a MutablePartialResult that has already been completed") { val pResult = new MutablePartialResult[Boolean] - assert(pResult.tryCompleteWith(Future.successful(SuccessfulResult("key1", false)))) - assert(!pResult.tryCompleteWith(Future.successful(SuccessfulResult("key1", true)))) + assert(pResult.tryCompleteWith(Future.successful(SuccessfulResult("key1", false, 0)))) + assert(!pResult.tryCompleteWith(Future.successful(SuccessfulResult("key1", true, 0)))) assertCompletePromise(toCheck = pResult, expected = false) } diff --git a/src/test/scala/shade/tests/CodecsSuite.scala b/src/test/scala/shade/tests/CodecsSuite.scala index 412aad8..e575e55 100644 --- a/src/test/scala/shade/tests/CodecsSuite.scala +++ b/src/test/scala/shade/tests/CodecsSuite.scala @@ -23,8 +23,8 @@ class CodecsSuite extends FunSuite with MemcachedCodecs with GeneratorDrivenProp */ private def serdesCheck[A: Arbitrary](codec: Codec[A]): Unit = { forAll { n: A => - val serialised = codec.serialize(n) - val deserialised = codec.deserialize(serialised) + val serialised = codec.serialize(n, 0) + val deserialised = codec.deserialize(serialised, 0) assert(deserialised == n) } } diff --git a/src/test/scala/shade/tests/FakeMemcachedSuite.scala b/src/test/scala/shade/tests/FakeMemcachedSuite.scala index 8a73244..7f273cf 100644 --- a/src/test/scala/shade/tests/FakeMemcachedSuite.scala +++ b/src/test/scala/shade/tests/FakeMemcachedSuite.scala @@ -25,13 +25,13 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { test("add") { withFakeMemcached { cache => - val op1 = cache.awaitAdd("hello", Value("world"), 5.seconds) + val op1 = cache.awaitAdd("hello", Value("world"), 0, 5.seconds) assert(op1 === true) val stored = cache.awaitGet[Value]("hello") assert(stored === Some(Value("world"))) - val op2 = cache.awaitAdd("hello", Value("changed"), 5.seconds) + val op2 = cache.awaitAdd("hello", Value("changed"), 0, 5.seconds) assert(op2 === false) val changed = cache.awaitGet[Value]("hello") @@ -41,7 +41,7 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { test("add-null") { withFakeMemcached { cache => - val op1 = cache.awaitAdd("hello", null, 5.seconds) + val op1 = cache.awaitAdd("hello", null, 0, 5.seconds) assert(op1 === false) val stored = cache.awaitGet[Value]("hello") @@ -60,10 +60,10 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { withFakeMemcached { cache => assert(cache.awaitGet[Value]("hello") === None) - cache.awaitSet("hello", Value("world"), 3.seconds) + cache.awaitSet("hello", Value("world"), 0, 3.seconds) assert(cache.awaitGet[Value]("hello") === Some(Value("world"))) - cache.awaitSet("hello", Value("changed"), 3.seconds) + cache.awaitSet("hello", Value("changed"), 0, 3.seconds) assert(cache.awaitGet[Value]("hello") === Some(Value("changed"))) Thread.sleep(3000) @@ -74,7 +74,7 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { test("set-null") { withFakeMemcached { cache => - val op1 = cache.awaitAdd("hello", null, 5.seconds) + val op1 = cache.awaitAdd("hello", null, 0, 5.seconds) assert(op1 === false) val stored = cache.awaitGet[Value]("hello") @@ -87,7 +87,7 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { cache.awaitDelete("hello") assert(cache.awaitGet[Value]("hello") === None) - cache.awaitSet("hello", Value("world"), 1.minute) + cache.awaitSet("hello", Value("world"), 0, 1.minute) assert(cache.awaitGet[Value]("hello") === Some(Value("world"))) assert(cache.awaitDelete("hello") === true) @@ -103,27 +103,27 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { assert(cache.awaitGet[Value]("some-key") === None) // no can do - assert(Await.result(cache.compareAndSet("some-key", Some(Value("invalid")), Value("value1"), 15.seconds), Duration.Inf) === false) + assert(Await.result(cache.compareAndSet("some-key", Some(Value("invalid")), Value("value1"), 0, 15.seconds), Duration.Inf) === false) assert(cache.awaitGet[Value]("some-key") === None) // set to value1 - assert(Await.result(cache.compareAndSet("some-key", None, Value("value1"), 5.seconds), Duration.Inf) === true) + assert(Await.result(cache.compareAndSet("some-key", None, Value("value1"), 0, 5.seconds), Duration.Inf) === true) assert(cache.awaitGet[Value]("some-key") === Some(Value("value1"))) // no can do - assert(Await.result(cache.compareAndSet("some-key", Some(Value("invalid")), Value("value1"), 15.seconds), Duration.Inf) === false) + assert(Await.result(cache.compareAndSet("some-key", Some(Value("invalid")), Value("value1"), 0, 15.seconds), Duration.Inf) === false) assert(cache.awaitGet[Value]("some-key") === Some(Value("value1"))) // set to value2, from value1 - assert(Await.result(cache.compareAndSet("some-key", Some(Value("value1")), Value("value2"), 15.seconds), Duration.Inf) === true) + assert(Await.result(cache.compareAndSet("some-key", Some(Value("value1")), Value("value2"), 0, 15.seconds), Duration.Inf) === true) assert(cache.awaitGet[Value]("some-key") === Some(Value("value2"))) // no can do - assert(Await.result(cache.compareAndSet("some-key", Some(Value("invalid")), Value("value1"), 15.seconds), Duration.Inf) === false) + assert(Await.result(cache.compareAndSet("some-key", Some(Value("invalid")), Value("value1"), 0, 15.seconds), Duration.Inf) === false) assert(cache.awaitGet[Value]("some-key") === Some(Value("value2"))) // set to value3, from value2 - assert(Await.result(cache.compareAndSet("some-key", Some(Value("value2")), Value("value3"), 15.seconds), Duration.Inf) === true) + assert(Await.result(cache.compareAndSet("some-key", Some(Value("value2")), Value("value3"), 0, 15.seconds), Duration.Inf) === true) assert(cache.awaitGet[Value]("some-key") === Some(Value("value3"))) } } @@ -211,7 +211,7 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { withFakeMemcached { cache => assert(cache.awaitGet[Int]("hello") === None) - cache.awaitSet("hello", "123", 1.second)(StringBinaryCodec) + cache.awaitSet("hello", "123", 0, 1.second)(StringBinaryCodec) assert(cache.awaitGet[String]("hello")(StringBinaryCodec) === Some("123")) cache.awaitIncrement("hello", 1, None, 1.second) @@ -230,7 +230,7 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { withFakeMemcached { cache => assert(cache.awaitGet[Int]("hello") === None) - cache.awaitSet("hello", "123", 1.second)(StringBinaryCodec) + cache.awaitSet("hello", "123", 0, 1.second)(StringBinaryCodec) assert(cache.awaitGet[String]("hello")(StringBinaryCodec) === Some("123")) cache.awaitIncrement("hello", 5, None, 1.second) @@ -286,7 +286,7 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { test("big-instance-1") { withFakeMemcached { cache => val impression = shade.testModels.bigInstance - cache.awaitSet(impression.uuid, impression, 60.seconds) + cache.awaitSet(impression.uuid, impression, 0, 60.seconds) assert(cache.awaitGet[Impression](impression.uuid) === Some(impression)) } } @@ -300,7 +300,7 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { objectOut.writeObject(impression) val byteArray = byteOut.toByteArray - cache.awaitSet(impression.uuid, byteArray, 60.seconds) + cache.awaitSet(impression.uuid, byteArray, 0, 60.seconds) val inBytes = cache.awaitGet[Array[Byte]](impression.uuid) assert(inBytes.isDefined) @@ -311,7 +311,7 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { test("big-instance-2") { withFakeMemcached { cache => val impression = shade.testModels.bigInstance2 - cache.awaitSet(impression.uuid, impression, 60.seconds) + cache.awaitSet(impression.uuid, impression, 0, 60.seconds) assert(cache.awaitGet[Impression](impression.uuid) === Some(impression)) } } @@ -319,7 +319,7 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { test("big-instance-3") { withFakeMemcached { cache => val impression = shade.testModels.bigInstance - val result = cache.set(impression.uuid, impression, 60.seconds) flatMap { _ => + val result = cache.set(impression.uuid, impression, 0, 60.seconds) flatMap { _ => cache.get[Impression](impression.uuid) } @@ -331,7 +331,7 @@ class FakeMemcachedSuite extends FunSuite with MemcachedTestHelpers { withFakeMemcached { cache => Thread.sleep(100) val impression = shade.testModels.bigInstance2 - cache.awaitSet(impression.uuid, impression, 60.seconds) + cache.awaitSet(impression.uuid, impression, 0, 60.seconds) assert(cache.awaitGet[Impression](impression.uuid) === Some(impression)) } } diff --git a/src/test/scala/shade/tests/MemcachedSuite.scala b/src/test/scala/shade/tests/MemcachedSuite.scala index 5f187ec..a64d9db 100644 --- a/src/test/scala/shade/tests/MemcachedSuite.scala +++ b/src/test/scala/shade/tests/MemcachedSuite.scala @@ -27,13 +27,13 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { test("add") { withCache("add") { cache => - val op1 = cache.awaitAdd("hello", Value("world"), 5.seconds) + val op1 = cache.awaitAdd("hello", Value("world"), 0, 5.seconds) assert(op1 === true) val stored = cache.awaitGet[Value]("hello") assert(stored === Some(Value("world"))) - val op2 = cache.awaitAdd("hello", Value("changed"), 5.seconds) + val op2 = cache.awaitAdd("hello", Value("changed"), 0, 5.seconds) assert(op2 === false) val changed = cache.awaitGet[Value]("hello") @@ -43,7 +43,7 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { test("add-null") { withCache("add-null") { cache => - val op1 = cache.awaitAdd("hello", null, 5.seconds) + val op1 = cache.awaitAdd("hello", null, 0, 5.seconds) assert(op1 === false) val stored = cache.awaitGet[Value]("hello") @@ -62,10 +62,10 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { withCache("set") { cache => assert(cache.awaitGet[Value]("hello") === None) - cache.awaitSet("hello", Value("world"), 1.seconds) + cache.awaitSet("hello", Value("world"), 0, 1.seconds) assert(cache.awaitGet[Value]("hello") === Some(Value("world"))) - cache.awaitSet("hello", Value("changed"), 1.second) + cache.awaitSet("hello", Value("changed"), 0, 1.second) assert(cache.awaitGet[Value]("hello") === Some(Value("changed"))) Thread.sleep(3000) @@ -76,7 +76,7 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { test("set-null") { withCache("set-null") { cache => - val op1 = cache.awaitAdd("hello", null, 5.seconds) + val op1 = cache.awaitAdd("hello", null, 0, 5.seconds) assert(op1 === false) val stored = cache.awaitGet[Value]("hello") @@ -89,7 +89,7 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { cache.awaitDelete("hello") assert(cache.awaitGet[Value]("hello") === None) - cache.awaitSet("hello", Value("world"), 1.minute) + cache.awaitSet("hello", Value("world"), 0, 1.minute) assert(cache.awaitGet[Value]("hello") === Some(Value("world"))) assert(cache.awaitDelete("hello") === true) @@ -105,27 +105,27 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { assert(cache.awaitGet[Value]("some-key") === None) // no can do - assert(Await.result(cache.compareAndSet("some-key", Some(Value("invalid")), Value("value1"), 15.seconds), Duration.Inf) === false) + assert(Await.result(cache.compareAndSet("some-key", Some(Value("invalid")), Value("value1"), 0, 15.seconds), Duration.Inf) === false) assert(cache.awaitGet[Value]("some-key") === None) // set to value1 - assert(Await.result(cache.compareAndSet("some-key", None, Value("value1"), 5.seconds), Duration.Inf) === true) + assert(Await.result(cache.compareAndSet("some-key", None, Value("value1"), 0, 5.seconds), Duration.Inf) === true) assert(cache.awaitGet[Value]("some-key") === Some(Value("value1"))) // no can do - assert(Await.result(cache.compareAndSet("some-key", Some(Value("invalid")), Value("value1"), 15.seconds), Duration.Inf) === false) + assert(Await.result(cache.compareAndSet("some-key", Some(Value("invalid")), Value("value1"), 0, 15.seconds), Duration.Inf) === false) assert(cache.awaitGet[Value]("some-key") === Some(Value("value1"))) // set to value2, from value1 - assert(Await.result(cache.compareAndSet("some-key", Some(Value("value1")), Value("value2"), 15.seconds), Duration.Inf) === true) + assert(Await.result(cache.compareAndSet("some-key", Some(Value("value1")), Value("value2"), 0, 15.seconds), Duration.Inf) === true) assert(cache.awaitGet[Value]("some-key") === Some(Value("value2"))) // no can do - assert(Await.result(cache.compareAndSet("some-key", Some(Value("invalid")), Value("value1"), 15.seconds), Duration.Inf) === false) + assert(Await.result(cache.compareAndSet("some-key", Some(Value("invalid")), Value("value1"), 0, 15.seconds), Duration.Inf) === false) assert(cache.awaitGet[Value]("some-key") === Some(Value("value2"))) // set to value3, from value2 - assert(Await.result(cache.compareAndSet("some-key", Some(Value("value2")), Value("value3"), 15.seconds), Duration.Inf) === true) + assert(Await.result(cache.compareAndSet("some-key", Some(Value("value2")), Value("value3"), 0, 15.seconds), Duration.Inf) === true) assert(cache.awaitGet[Value]("some-key") === Some(Value("value3"))) } } @@ -264,7 +264,7 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { withCache("increment-decrement") { cache => assert(cache.awaitGet[Int]("hello") === None) - cache.awaitSet("hello", "123", 1.second)(StringBinaryCodec) + cache.awaitSet("hello", "123", 0, 1.second)(StringBinaryCodec) assert(cache.awaitGet[String]("hello")(StringBinaryCodec) === Some("123")) cache.awaitIncrement("hello", 1, None, 1.second) @@ -283,7 +283,7 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { withCache("increment-decrement-delta") { cache => assert(cache.awaitGet[Int]("hello") === None) - cache.awaitSet("hello", "123", 1.second)(StringBinaryCodec) + cache.awaitSet("hello", "123", 0, 1.second)(StringBinaryCodec) assert(cache.awaitGet[String]("hello")(StringBinaryCodec) === Some("123")) cache.awaitIncrement("hello", 5, None, 1.second) @@ -339,7 +339,7 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { test("vector-inherited-case-classes") { withCache("vector-inherited-case-classes") { cache => val content = shade.testModels.contentSeq - cache.awaitSet("blog-posts", content, 60.seconds) + cache.awaitSet("blog-posts", content, 0, 60.seconds) assert(cache.awaitGet[Vector[ContentPiece]]("blog-posts") === Some(content)) } } @@ -347,7 +347,7 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { test("big-instance-1") { withCache("big-instance-1") { cache => val impression = shade.testModels.bigInstance - cache.awaitSet(impression.uuid, impression, 60.seconds) + cache.awaitSet(impression.uuid, impression, 0, 60.seconds) assert(cache.awaitGet[Impression](impression.uuid) === Some(impression)) } } @@ -361,7 +361,7 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { objectOut.writeObject(impression) val byteArray = byteOut.toByteArray - cache.awaitSet(impression.uuid, byteArray, 60.seconds) + cache.awaitSet(impression.uuid, byteArray, 0, 60.seconds) val inBytes = cache.awaitGet[Array[Byte]](impression.uuid) assert(inBytes.isDefined) @@ -372,7 +372,7 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { test("big-instance-2") { withCache("big-instance-2") { cache => val impression = shade.testModels.bigInstance2 - cache.awaitSet(impression.uuid, impression, 60.seconds) + cache.awaitSet(impression.uuid, impression, 0, 60.seconds) assert(cache.awaitGet[Impression](impression.uuid) === Some(impression)) } } @@ -380,7 +380,7 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { test("big-instance-3") { withCache("big-instance-3") { cache => val impression = shade.testModels.bigInstance - val result = cache.set(impression.uuid, impression, 60.seconds) flatMap { _ => + val result = cache.set(impression.uuid, impression, 0, 60.seconds) flatMap { _ => cache.get[Impression](impression.uuid) } @@ -392,7 +392,7 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { withCache("cancel-strategy", failureMode = Some(FailureMode.Cancel)) { cache => Thread.sleep(100) val impression = shade.testModels.bigInstance2 - cache.awaitSet(impression.uuid, impression, 60.seconds) + cache.awaitSet(impression.uuid, impression, 0, 60.seconds) assert(cache.awaitGet[Impression](impression.uuid) === Some(impression)) } } @@ -401,7 +401,7 @@ class MemcachedSuite extends FunSuite with MemcachedTestHelpers { withCache("infinite-duration") { cache => assert(cache.awaitGet[Value]("hello") === None) try { - cache.awaitSet("hello", Value("world"), Duration.Inf) + cache.awaitSet("hello", Value("world"), 0, Duration.Inf) assert(cache.awaitGet[Value]("hello") === Some(Value("world"))) Thread.sleep(5000)