Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vkostyukov committed Jan 10, 2019
1 parent d5036c6 commit b93d6e4
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 27 deletions.
13 changes: 0 additions & 13 deletions core/src/test/scala/io/finch/EndpointResultSpec.scala

This file was deleted.

12 changes: 6 additions & 6 deletions core/src/test/scala/io/finch/StreamingLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()))
}

Expand All @@ -55,15 +55,15 @@ 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]
): StreamingLaws[S, F] = new StreamingLaws[S, F] {
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
}

}
4 changes: 2 additions & 2 deletions examples/src/main/scala/io/finch/iteratee/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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))
}

Expand Down
6 changes: 2 additions & 4 deletions fs2/src/main/scala/io/finch/fs2/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}
Expand Down
3 changes: 2 additions & 1 deletion fs2/src/test/scala/io/finch/fs2/Fs2StreamingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ 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

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)

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

}

0 comments on commit b93d6e4

Please sign in to comment.