From b93d6e4b5548851bdc7e8b8b4eba3b6dd6dffab0 Mon Sep 17 00:00:00 2001 From: Vladimir Kostyukov Date: Thu, 10 Jan 2019 09:27:39 -0800 Subject: [PATCH] Fix tests --- .../test/scala/io/finch/EndpointResultSpec.scala | 13 ------------- core/src/test/scala/io/finch/StreamingLaws.scala | 12 ++++++------ .../src/main/scala/io/finch/iteratee/Main.scala | 4 ++-- fs2/src/main/scala/io/finch/fs2/package.scala | 6 ++---- .../test/scala/io/finch/fs2/Fs2StreamingSpec.scala | 3 ++- .../io/finch/iteratee/IterateeStreamingSpec.scala | 3 ++- 6 files changed, 14 insertions(+), 27 deletions(-) delete mode 100644 core/src/test/scala/io/finch/EndpointResultSpec.scala diff --git a/core/src/test/scala/io/finch/EndpointResultSpec.scala b/core/src/test/scala/io/finch/EndpointResultSpec.scala deleted file mode 100644 index 4afae365c..000000000 --- a/core/src/test/scala/io/finch/EndpointResultSpec.scala +++ /dev/null @@ -1,13 +0,0 @@ -package io.finch - -import cats.effect.IO -import cats.laws.discipline.FunctorTests - -class EndpointResultSpec extends FinchSpec { - - checkAll( - "Functor[EndpointResult]", - FunctorTests[EndpointResult[IO, ?]].functor[String, String, String] - ) - -} diff --git a/core/src/test/scala/io/finch/StreamingLaws.scala b/core/src/test/scala/io/finch/StreamingLaws.scala index 58e866111..5b537e784 100644 --- a/core/src/test/scala/io/finch/StreamingLaws.scala +++ b/core/src/test/scala/io/finch/StreamingLaws.scala @@ -17,7 +17,7 @@ abstract class StreamingLaws[S[_[_], _], F[_] : Effect] extends Laws with AllIns def toResponse: ToResponse[S[F, Buf]] def fromList: List[Buf] => S[F, Buf] - def toList: S[F, Buf] => List[Buf] + def toList: S[F, Array[Byte]] => List[Buf] def roundTrip(a: List[Buf], cs: Charset): IsEq[List[Buf]] = { val req = Request() @@ -26,16 +26,16 @@ abstract class StreamingLaws[S[_[_], _], F[_] : Effect] extends Laws with AllIns Reader.copy(toResponse(fromList(a), cs).reader, req.writer).ensure(req.writer.close()) Endpoint - .streamBinaryBody[F, S] + .binaryBodyStream[F, S] .apply(Input.fromRequest(req)) .awaitValueUnsafe() .map(toList) .get <-> a } - def onlyChunked: EndpointResult[F, S[F, Buf]] = { + def onlyChunked: EndpointResult[F, S[F, Array[Byte]]] = { Endpoint - .streamBinaryBody[F, S] + .binaryBodyStream[F, S] .apply(Input.fromRequest(Request())) } @@ -55,7 +55,7 @@ object StreamingLaws { def apply[S[_[_], _], F[_] : Effect]( streamFromList: List[Buf] => S[F, Buf], - listFromStream: S[F, Buf] => List[Buf] + listFromStream: S[F, Array[Byte]] => List[Buf] )(implicit tr: ToResponse.Aux[S[F, Buf], Text.Plain], reader: LiftReader[S, F] @@ -63,7 +63,7 @@ object StreamingLaws { implicit val streamReader: LiftReader[S, F] = reader val toResponse: ToResponse[S[F, Buf]] = tr val fromList: List[Buf] => S[F, Buf] = streamFromList - val toList: S[F, Buf] => List[Buf] = listFromStream + val toList: S[F, Array[Byte]] => List[Buf] = listFromStream } } diff --git a/examples/src/main/scala/io/finch/iteratee/Main.scala b/examples/src/main/scala/io/finch/iteratee/Main.scala index 8c93be66e..1900df98f 100644 --- a/examples/src/main/scala/io/finch/iteratee/Main.scala +++ b/examples/src/main/scala/io/finch/iteratee/Main.scala @@ -49,7 +49,7 @@ object Main extends EndpointModule[IO] { private def stream: Stream[Int] = Stream.continually(Random.nextInt()) - val sumJson: Endpoint[IO, Result] = post("sumJson" :: streamJsonBody[Enumerator, Number]) { + val sumJson: Endpoint[IO, Result] = post("sumJson" :: jsonBodyStream[Enumerator, Number]) { enum: Enumerator[IO, Number] => enum.into(Iteratee.fold[IO, Number, Result](Result(0))(_ add _)).map(Ok) } @@ -59,7 +59,7 @@ object Main extends EndpointModule[IO] { } val isPrime: Endpoint[IO, Enumerator[IO, IsPrime]] = - post("streamPrime" :: streamJsonBody[Enumerator, Number]) { enum: Enumerator[IO, Number] => + post("streamPrime" :: jsonBodyStream[Enumerator, Number]) { enum: Enumerator[IO, Number] => Ok(enum.map(_.isPrime)) } diff --git a/fs2/src/main/scala/io/finch/fs2/package.scala b/fs2/src/main/scala/io/finch/fs2/package.scala index 9e9508a39..08f6cdd2d 100644 --- a/fs2/src/main/scala/io/finch/fs2/package.scala +++ b/fs2/src/main/scala/io/finch/fs2/package.scala @@ -16,10 +16,8 @@ package object fs2 extends StreamInstances { final def apply[A](reader: Reader[Buf], process: Buf => A): Stream[F, A] = { Stream .repeatEval(F.suspend(TE(reader.read()))) - .flatMap { - case Some(buf) => Stream.emit[F, A](process(buf)) - case None => Stream.empty[F, A] - } + .unNoneTerminate + .map(process) .onFinalize(F.delay(reader.discard())) } } diff --git a/fs2/src/test/scala/io/finch/fs2/Fs2StreamingSpec.scala b/fs2/src/test/scala/io/finch/fs2/Fs2StreamingSpec.scala index e0b51c08d..9fc0dd8a4 100644 --- a/fs2/src/test/scala/io/finch/fs2/Fs2StreamingSpec.scala +++ b/fs2/src/test/scala/io/finch/fs2/Fs2StreamingSpec.scala @@ -2,6 +2,7 @@ package io.finch.fs2 import _root_.fs2.Stream import cats.effect.IO +import com.twitter.io.Buf import io.finch._ import org.scalatest.prop.GeneratorDrivenPropertyChecks @@ -9,7 +10,7 @@ class Fs2StreamingSpec extends FinchSpec with GeneratorDrivenPropertyChecks { checkAll("fs2.streamBody", StreamingLaws[Stream, IO]( list => Stream(list:_*), - _.compile.toList.unsafeRunSync() + _.map(array => Buf.ByteArray.Owned(array)).compile.toList.unsafeRunSync() ).all) } diff --git a/iteratee/src/test/scala/io/finch/iteratee/IterateeStreamingSpec.scala b/iteratee/src/test/scala/io/finch/iteratee/IterateeStreamingSpec.scala index d89e89d95..a274c6c18 100644 --- a/iteratee/src/test/scala/io/finch/iteratee/IterateeStreamingSpec.scala +++ b/iteratee/src/test/scala/io/finch/iteratee/IterateeStreamingSpec.scala @@ -1,6 +1,7 @@ package io.finch.iteratee import cats.effect.IO +import com.twitter.io.Buf import io.finch._ import io.iteratee.Enumerator import org.scalatest.prop.GeneratorDrivenPropertyChecks @@ -9,7 +10,7 @@ class IterateeStreamingSpec extends FinchSpec with GeneratorDrivenPropertyChecks checkAll("Iteratee.streamBody", StreamingLaws[Enumerator, IO]( Enumerator.enumList, - _.toVector.unsafeRunSync().toList + _.map(array => Buf.ByteArray.Owned(array)).toVector.unsafeRunSync().toList ).all) }