From b8a114ae37de6aa0bf8257199d4b0627b6940cf6 Mon Sep 17 00:00:00 2001 From: Thijs Broersen Date: Sun, 25 Aug 2024 23:08:52 +0000 Subject: [PATCH 1/3] feat(zio): support Native --- build.sbt | 18 ++++- .../capabilities/zio/ZioStreamsTest.scala | 80 ++++++++----------- 2 files changed, 49 insertions(+), 49 deletions(-) diff --git a/build.sbt b/build.sbt index 76931ec..11fbafb 100644 --- a/build.sbt +++ b/build.sbt @@ -27,7 +27,7 @@ def dependenciesFor(version: String)(deps: (Option[(Long, Long)] => ModuleID)*): val commonSettings = commonSmlBuildSettings ++ ossPublishSettings ++ Seq( organization := "com.softwaremill.sttp.shared", libraryDependencies ++= Seq( - "org.scalatest" %% "scalatest" % scalaTestVersion % Test + "org.scalatest" %%% "scalatest" % scalaTestVersion % Test ), mimaPreviousArtifacts := Set.empty, versionScheme := Some("semver-spec") @@ -59,14 +59,15 @@ val commonJsSettings = commonSettings ++ Seq( } }, libraryDependencies ++= Seq( - "org.scala-js" %%% "scalajs-dom" % "2.8.0" + "org.scala-js" %%% "scalajs-dom" % "2.8.0", + "io.github.cquiroz" %%% "scala-java-time" % "2.6.0" % Test ) ) val commonNativeSettings = commonSettings ++ Seq( ideSkipProject := true, libraryDependencies ++= Seq( - "org.scala-native" %%% "test-interface" % nativeVersion + "io.github.cquiroz" %%% "scala-java-time" % "2.6.0" % Test ) ) @@ -231,7 +232,12 @@ lazy val zio1 = (projectMatrix in file("zio1")) lazy val zio = (projectMatrix in file("zio")) .settings( name := "zio", - libraryDependencies ++= Seq("dev.zio" %%% "zio-streams" % zio2Version, "dev.zio" %%% "zio" % zio2Version) + libraryDependencies ++= Seq("dev.zio" %%% "zio-streams" % zio2Version, "dev.zio" %%% "zio" % zio2Version) ++ + Seq( + "dev.zio" %%% "zio-test" % zio2Version % Test, + "dev.zio" %%% "zio-test-sbt" % zio2Version % Test + ), + testFrameworks += TestFrameworks.ZIOTest ) .jvmPlatform( scalaVersions = scala2 ++ scala3, @@ -241,6 +247,10 @@ lazy val zio = (projectMatrix in file("zio")) scalaVersions = scala2alive ++ scala3, settings = commonJsSettings ++ browserChromeTestSettings ) + .nativePlatform( + scalaVersions = scala3, + settings = commonNativeSettings + ) .dependsOn(core) lazy val vertx = (projectMatrix in file("vertx")) diff --git a/zio/src/test/scala/sttp/capabilities/zio/ZioStreamsTest.scala b/zio/src/test/scala/sttp/capabilities/zio/ZioStreamsTest.scala index aa3790e..a7b764e 100644 --- a/zio/src/test/scala/sttp/capabilities/zio/ZioStreamsTest.scala +++ b/zio/src/test/scala/sttp/capabilities/zio/ZioStreamsTest.scala @@ -1,54 +1,44 @@ package sttp.capabilities.zio -import org.scalatest.flatspec.AsyncFlatSpec -import org.scalatest.matchers.should.Matchers import sttp.capabilities.StreamMaxLengthExceededException import zio._ import zio.stream.ZStream - -class ZioStreamsTest extends AsyncFlatSpec with Matchers { - behavior of "ZioStreams" - - implicit val r: Runtime[Any] = Runtime.default - - it should "Pass all bytes if limit is not exceeded" in { - // given - val inputByteCount = 8192 - val maxBytes = 8192L - val inputStream = ZStream.fromIterator(Iterator.fill[Byte](inputByteCount)('5'.toByte)) - - // when - val stream = ZioStreams.limitBytes(inputStream, maxBytes) - - // then - Unsafe.unsafe(implicit u => - r.unsafe.runToFuture(stream.runFold(0L)((acc, _) => acc + 1).map { count => - count shouldBe inputByteCount - }) - ) - } - - it should "Fail stream if limit is exceeded" in { - // given - val inputByteCount = 8192 - val maxBytes = 8191L - val inputStream = ZStream.fromIterator(Iterator.fill[Byte](inputByteCount)('5'.toByte)) - - // when - val stream = ZioStreams.limitBytes(inputStream, maxBytes) - - // then - Unsafe.unsafe(implicit u => - r.unsafe.runToFuture( - stream.runLast - .flatMap(_ => ZIO.succeed(fail("Unexpected end of stream"))) - .catchSome { +import zio.test._ + +object ZioStreamsTest extends ZIOSpecDefault { + def spec: Spec[TestEnvironment, Any] = suite("ZioStreams")( + test("should Pass all bytes if limit is not exceeded") { + // given + val inputByteCount = 8192 + val maxBytes = 8192L + val inputStream = ZStream.fromIterator(Iterator.fill[Byte](inputByteCount)('5'.toByte)) + + // when + val stream = ZioStreams.limitBytes(inputStream, maxBytes) + + // then + for { + count <- stream.runFold(0L)((acc, _) => acc + 1) + } yield assertTrue(count == inputByteCount) + }, + test("should Fail stream if limit is exceeded") { + val inputByteCount = 8192 + val maxBytes = 8191L + val inputStream = ZStream.fromIterator(Iterator.fill[Byte](inputByteCount)('5'.toByte)) + + // when + val stream = ZioStreams.limitBytes(inputStream, maxBytes) + + // then + for { + limit <- stream.runLast.flip + .flatMap { case StreamMaxLengthExceededException(limit) => - ZIO.succeed(limit shouldBe maxBytes) + ZIO.succeed(limit) case other => - ZIO.succeed(fail(s"Unexpected failure cause: $other")) + ZIO.fail(s"Unexpected failure cause: $other") } - ) - ) - } + } yield assertTrue(limit == maxBytes) + } + ) } From f6eb87f1cccc665486b50eedd6aff8756a6295a9 Mon Sep 17 00:00:00 2001 From: Thijs Broersen Date: Tue, 3 Sep 2024 17:05:33 +0200 Subject: [PATCH 2/3] fix fs2 tests --- .../scala/sttp/capabilities/fs2/Fs2StreamsTest.scala | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/fs2/src/test/scala/sttp/capabilities/fs2/Fs2StreamsTest.scala b/fs2/src/test/scala/sttp/capabilities/fs2/Fs2StreamsTest.scala index 657fcfd..e297b05 100644 --- a/fs2/src/test/scala/sttp/capabilities/fs2/Fs2StreamsTest.scala +++ b/fs2/src/test/scala/sttp/capabilities/fs2/Fs2StreamsTest.scala @@ -1,13 +1,22 @@ package sttp.capabilities.fs2 import cats.effect.IO -import cats.effect.unsafe.implicits.global +import cats.effect.unsafe import fs2._ import org.scalatest.flatspec.AsyncFlatSpec import org.scalatest.matchers.should.Matchers import sttp.capabilities.StreamMaxLengthExceededException class Fs2StreamsTest extends AsyncFlatSpec with Matchers { + + implicit val runtime: unsafe.IORuntime = unsafe.IORuntime( + executionContext, + executionContext, + unsafe.IORuntime.global.scheduler, + unsafe.IORuntime.global.shutdown, + unsafe.IORuntime.global.config + ) + behavior of "Fs2Streams" it should "Pass all bytes if limit is not exceeded" in { From 965eb35da2ea6e280a5ae50f68702a6b33e787a8 Mon Sep 17 00:00:00 2001 From: Thijs Broersen Date: Tue, 3 Sep 2024 18:39:45 +0200 Subject: [PATCH 3/3] drop zio-test, override executioncontext --- build.sbt | 7 +- .../capabilities/fs2/Fs2StreamsTest.scala | 10 +-- .../capabilities/zio/ZioStreamsTest.scala | 83 +++++++++++-------- 3 files changed, 52 insertions(+), 48 deletions(-) diff --git a/build.sbt b/build.sbt index 11fbafb..2f66760 100644 --- a/build.sbt +++ b/build.sbt @@ -232,12 +232,7 @@ lazy val zio1 = (projectMatrix in file("zio1")) lazy val zio = (projectMatrix in file("zio")) .settings( name := "zio", - libraryDependencies ++= Seq("dev.zio" %%% "zio-streams" % zio2Version, "dev.zio" %%% "zio" % zio2Version) ++ - Seq( - "dev.zio" %%% "zio-test" % zio2Version % Test, - "dev.zio" %%% "zio-test-sbt" % zio2Version % Test - ), - testFrameworks += TestFrameworks.ZIOTest + libraryDependencies ++= Seq("dev.zio" %%% "zio-streams" % zio2Version, "dev.zio" %%% "zio" % zio2Version) ) .jvmPlatform( scalaVersions = scala2 ++ scala3, diff --git a/fs2/src/test/scala/sttp/capabilities/fs2/Fs2StreamsTest.scala b/fs2/src/test/scala/sttp/capabilities/fs2/Fs2StreamsTest.scala index e297b05..24428ac 100644 --- a/fs2/src/test/scala/sttp/capabilities/fs2/Fs2StreamsTest.scala +++ b/fs2/src/test/scala/sttp/capabilities/fs2/Fs2StreamsTest.scala @@ -2,20 +2,16 @@ package sttp.capabilities.fs2 import cats.effect.IO import cats.effect.unsafe +import cats.effect.unsafe.implicits.global import fs2._ import org.scalatest.flatspec.AsyncFlatSpec import org.scalatest.matchers.should.Matchers import sttp.capabilities.StreamMaxLengthExceededException +import scala.concurrent.ExecutionContext class Fs2StreamsTest extends AsyncFlatSpec with Matchers { - implicit val runtime: unsafe.IORuntime = unsafe.IORuntime( - executionContext, - executionContext, - unsafe.IORuntime.global.scheduler, - unsafe.IORuntime.global.shutdown, - unsafe.IORuntime.global.config - ) + override implicit val executionContext: ExecutionContext = unsafe.IORuntime.global.compute behavior of "Fs2Streams" diff --git a/zio/src/test/scala/sttp/capabilities/zio/ZioStreamsTest.scala b/zio/src/test/scala/sttp/capabilities/zio/ZioStreamsTest.scala index a7b764e..482a273 100644 --- a/zio/src/test/scala/sttp/capabilities/zio/ZioStreamsTest.scala +++ b/zio/src/test/scala/sttp/capabilities/zio/ZioStreamsTest.scala @@ -1,44 +1,57 @@ package sttp.capabilities.zio +import org.scalatest.flatspec.AsyncFlatSpec +import org.scalatest.matchers.should.Matchers import sttp.capabilities.StreamMaxLengthExceededException import zio._ import zio.stream.ZStream -import zio.test._ - -object ZioStreamsTest extends ZIOSpecDefault { - def spec: Spec[TestEnvironment, Any] = suite("ZioStreams")( - test("should Pass all bytes if limit is not exceeded") { - // given - val inputByteCount = 8192 - val maxBytes = 8192L - val inputStream = ZStream.fromIterator(Iterator.fill[Byte](inputByteCount)('5'.toByte)) - - // when - val stream = ZioStreams.limitBytes(inputStream, maxBytes) - - // then - for { - count <- stream.runFold(0L)((acc, _) => acc + 1) - } yield assertTrue(count == inputByteCount) - }, - test("should Fail stream if limit is exceeded") { - val inputByteCount = 8192 - val maxBytes = 8191L - val inputStream = ZStream.fromIterator(Iterator.fill[Byte](inputByteCount)('5'.toByte)) - - // when - val stream = ZioStreams.limitBytes(inputStream, maxBytes) - - // then - for { - limit <- stream.runLast.flip - .flatMap { +import scala.concurrent.ExecutionContext + +class ZioStreamsTest extends AsyncFlatSpec with Matchers { + override implicit val executionContext: ExecutionContext = Runtime.defaultExecutor.asExecutionContext + + behavior of "ZioStreams" + + implicit val r: Runtime[Any] = Runtime.default + + it should "Pass all bytes if limit is not exceeded" in { + // given + val inputByteCount = 8192 + val maxBytes = 8192L + val inputStream = ZStream.fromIterator(Iterator.fill[Byte](inputByteCount)('5'.toByte)) + + // when + val stream = ZioStreams.limitBytes(inputStream, maxBytes) + + // then + Unsafe.unsafe(implicit u => + r.unsafe.runToFuture(stream.runFold(0L)((acc, _) => acc + 1).map { count => + count shouldBe inputByteCount + }) + ) + } + + it should "Fail stream if limit is exceeded" in { + // given + val inputByteCount = 8192 + val maxBytes = 8191L + val inputStream = ZStream.fromIterator(Iterator.fill[Byte](inputByteCount)('5'.toByte)) + + // when + val stream = ZioStreams.limitBytes(inputStream, maxBytes) + + // then + Unsafe.unsafe(implicit u => + r.unsafe.runToFuture( + stream.runLast + .flatMap(_ => ZIO.succeed(fail("Unexpected end of stream"))) + .catchSome { case StreamMaxLengthExceededException(limit) => - ZIO.succeed(limit) + ZIO.succeed(limit shouldBe maxBytes) case other => - ZIO.fail(s"Unexpected failure cause: $other") + ZIO.succeed(fail(s"Unexpected failure cause: $other")) } - } yield assertTrue(limit == maxBytes) - } - ) + ) + ) + } }