Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zio): support Native #432

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that why JS tests were not executed? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

),
mimaPreviousArtifacts := Set.empty,
versionScheme := Some("semver-spec")
Expand Down Expand Up @@ -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
)
)

Expand Down Expand Up @@ -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,
Expand All @@ -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"))
Expand Down
11 changes: 10 additions & 1 deletion fs2/src/test/scala/sttp/capabilities/fs2/Fs2StreamsTest.scala
Original file line number Diff line number Diff line change
@@ -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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this is different from the default - could you add a comment how it is different, and why the change is made?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the scalatest suites have an implicit executionContext which is used to schedule the tests. If tests schedule tasks on other executioncontext it is undetectable by scalatest.

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 {
Expand Down
80 changes: 35 additions & 45 deletions zio/src/test/scala/sttp/capabilities/zio/ZioStreamsTest.scala
Original file line number Diff line number Diff line change
@@ -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)
}
)
}
Loading