|
1 | 1 | package sttp.capabilities.zio
|
2 | 2 |
|
3 |
| -import org.scalatest.flatspec.AsyncFlatSpec |
4 |
| -import org.scalatest.matchers.should.Matchers |
5 | 3 | import sttp.capabilities.StreamMaxLengthExceededException
|
6 | 4 | import zio._
|
7 | 5 | import zio.stream.ZStream
|
8 |
| - |
9 |
| -class ZioStreamsTest extends AsyncFlatSpec with Matchers { |
10 |
| - behavior of "ZioStreams" |
11 |
| - |
12 |
| - implicit val r: Runtime[Any] = Runtime.default |
13 |
| - |
14 |
| - it should "Pass all bytes if limit is not exceeded" in { |
15 |
| - // given |
16 |
| - val inputByteCount = 8192 |
17 |
| - val maxBytes = 8192L |
18 |
| - val inputStream = ZStream.fromIterator(Iterator.fill[Byte](inputByteCount)('5'.toByte)) |
19 |
| - |
20 |
| - // when |
21 |
| - val stream = ZioStreams.limitBytes(inputStream, maxBytes) |
22 |
| - |
23 |
| - // then |
24 |
| - Unsafe.unsafe(implicit u => |
25 |
| - r.unsafe.runToFuture(stream.runFold(0L)((acc, _) => acc + 1).map { count => |
26 |
| - count shouldBe inputByteCount |
27 |
| - }) |
28 |
| - ) |
29 |
| - } |
30 |
| - |
31 |
| - it should "Fail stream if limit is exceeded" in { |
32 |
| - // given |
33 |
| - val inputByteCount = 8192 |
34 |
| - val maxBytes = 8191L |
35 |
| - val inputStream = ZStream.fromIterator(Iterator.fill[Byte](inputByteCount)('5'.toByte)) |
36 |
| - |
37 |
| - // when |
38 |
| - val stream = ZioStreams.limitBytes(inputStream, maxBytes) |
39 |
| - |
40 |
| - // then |
41 |
| - Unsafe.unsafe(implicit u => |
42 |
| - r.unsafe.runToFuture( |
43 |
| - stream.runLast |
44 |
| - .flatMap(_ => ZIO.succeed(fail("Unexpected end of stream"))) |
45 |
| - .catchSome { |
| 6 | +import zio.test._ |
| 7 | + |
| 8 | +object ZioStreamsTest extends ZIOSpecDefault { |
| 9 | + def spec: Spec[TestEnvironment, Any] = suite("ZioStreams")( |
| 10 | + test("should Pass all bytes if limit is not exceeded") { |
| 11 | + // given |
| 12 | + val inputByteCount = 8192 |
| 13 | + val maxBytes = 8192L |
| 14 | + val inputStream = ZStream.fromIterator(Iterator.fill[Byte](inputByteCount)('5'.toByte)) |
| 15 | + |
| 16 | + // when |
| 17 | + val stream = ZioStreams.limitBytes(inputStream, maxBytes) |
| 18 | + |
| 19 | + // then |
| 20 | + for { |
| 21 | + count <- stream.runFold(0L)((acc, _) => acc + 1) |
| 22 | + } yield assertTrue(count == inputByteCount) |
| 23 | + }, |
| 24 | + test("should Fail stream if limit is exceeded") { |
| 25 | + val inputByteCount = 8192 |
| 26 | + val maxBytes = 8191L |
| 27 | + val inputStream = ZStream.fromIterator(Iterator.fill[Byte](inputByteCount)('5'.toByte)) |
| 28 | + |
| 29 | + // when |
| 30 | + val stream = ZioStreams.limitBytes(inputStream, maxBytes) |
| 31 | + |
| 32 | + // then |
| 33 | + for { |
| 34 | + limit <- stream.runLast.flip |
| 35 | + .flatMap { |
46 | 36 | case StreamMaxLengthExceededException(limit) =>
|
47 |
| - ZIO.succeed(limit shouldBe maxBytes) |
| 37 | + ZIO.succeed(limit) |
48 | 38 | case other =>
|
49 |
| - ZIO.succeed(fail(s"Unexpected failure cause: $other")) |
| 39 | + ZIO.fail(s"Unexpected failure cause: $other") |
50 | 40 | }
|
51 |
| - ) |
52 |
| - ) |
53 |
| - } |
| 41 | + } yield assertTrue(limit == maxBytes) |
| 42 | + } |
| 43 | + ) |
54 | 44 | }
|
0 commit comments