Kryo tests fixed

This commit is contained in:
alari 2019-01-22 18:14:20 +03:00
parent 7dee94d09b
commit d23e60f516
2 changed files with 12 additions and 15 deletions

View File

@ -17,7 +17,6 @@
package fluence.codec.kryo package fluence.codec.kryo
import cats.MonadError
import com.twitter.chill.KryoPool import com.twitter.chill.KryoPool
import fluence.codec.{CodecError, PureCodec} import fluence.codec.{CodecError, PureCodec}
import shapeless._ import shapeless._
@ -30,11 +29,9 @@ import scala.util.control.NonFatal
* Wrapper for a KryoPool with a list of registered classes * Wrapper for a KryoPool with a list of registered classes
* *
* @param pool Pre-configured KryoPool * @param pool Pre-configured KryoPool
* @param F Applicative error
* @tparam L List of classes registered with kryo * @tparam L List of classes registered with kryo
* @tparam F Effect
*/ */
class KryoCodecs[F[_], L <: HList] private (pool: KryoPool)(implicit F: MonadError[F, Throwable]) { class KryoCodecs[L <: HList] private (pool: KryoPool) {
/** /**
* Returns a codec for any registered type * Returns a codec for any registered type
@ -108,10 +105,10 @@ object KryoCodecs {
* @tparam F Effect type * @tparam F Effect type
* @return Configured instance of KryoCodecs * @return Configured instance of KryoCodecs
*/ */
def build[F[_]]( def build(
poolSize: Int = Runtime.getRuntime.availableProcessors poolSize: Int = Runtime.getRuntime.availableProcessors
)(implicit F: MonadError[F, Throwable]): KryoCodecs[F, L] = ): KryoCodecs[L] =
new KryoCodecs[F, L]( new KryoCodecs[L](
KryoPool.withByteArrayOutputStream( KryoPool.withByteArrayOutputStream(
poolSize, poolSize,
KryoFactory(klasses, registrationRequired = true) // registrationRequired should never be needed, as codec derivation is typesafe KryoFactory(klasses, registrationRequired = true) // registrationRequired should never be needed, as codec derivation is typesafe

View File

@ -18,6 +18,7 @@
package fluence.codec.kryo package fluence.codec.kryo
import cats.instances.try_._ import cats.instances.try_._
import cats.syntax.profunctor._
import org.scalatest.{Matchers, WordSpec} import org.scalatest.{Matchers, WordSpec}
import scala.util.Try import scala.util.Try
@ -31,15 +32,15 @@ class KryoCodecsSpec extends WordSpec with Matchers {
KryoCodecs() KryoCodecs()
.add[Array[Array[Byte]]] .add[Array[Array[Byte]]]
.addCase(classOf[TestClass]) .addCase(classOf[TestClass])
.build[Try]() .build()
"encode and decode" should { "encode and decode" should {
"be inverse functions" when { "be inverse functions" when {
"object defined" in { "object defined" in {
val codec = testCodecs.codec[TestClass] val codec = testCodecs.pureCodec[TestClass]
val result = codec.encode(testClass).flatMap(codec.decode).get val result = codec.inverse.unsafe(codec.direct.unsafe(testClass))
result.str shouldBe "one" result.str shouldBe "one"
result.num shouldBe 2 result.num shouldBe 2
@ -47,8 +48,8 @@ class KryoCodecsSpec extends WordSpec with Matchers {
} }
"object is null" in { "object is null" in {
val codec = testCodecs.codec[TestClass] val codec = testCodecs.pureCodec[TestClass]
val result = codec.encode(null).flatMap(codec.decode) val result = codec.direct.runF[Try](null).flatMap(codec.inverse.runF[Try])
result.isFailure shouldBe true result.isFailure shouldBe true
} }
} }
@ -57,9 +58,8 @@ class KryoCodecsSpec extends WordSpec with Matchers {
"encode" should { "encode" should {
"not write full class name to binary representation" when { "not write full class name to binary representation" when {
"class registered" in { "class registered" in {
//val codec = KryoCodec[TestClass](Seq(classOf[TestClass], classOf[Array[Byte]], classOf[Array[Array[Byte]]]), registerRequired = true) val codec = testCodecs.pureCodec[TestClass]
val codec = testCodecs.codec[TestClass] val encoded = codec.direct.rmap(new String(_)).unsafe(testClass)
val encoded = codec.encode(testClass).map(new String(_)).get
val reasonableMaxSize = 20 // bytes val reasonableMaxSize = 20 // bytes
encoded should not contain "TestClass" encoded should not contain "TestClass"
encoded.length should be < reasonableMaxSize encoded.length should be < reasonableMaxSize