Skip to content

Commit

Permalink
Fix ZLayer Scope (#171)
Browse files Browse the repository at this point in the history
* Fix ZLayer Scope

* fix 2.11
  • Loading branch information
sideeffffect authored Aug 14, 2022
1 parent 65f1756 commit 3a31d4e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ lazy val commonSettings: List[Def.Setting[_]] = DecentScala.decentScalaSettings
)
),
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),
scalacOptions -= "-Xsource:3", // we cross-build for 2.11 too!
missinglinkExcludedDependencies ++= List(
moduleFilter(organization = "org.slf4j", name = "slf4j-api"),
moduleFilter(organization = "org.testcontainers", name = "testcontainers")
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/zio/testcontainers/ZIOTestcontainers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package zio.testcontainers
import com.dimafeng.testcontainers.{DockerComposeContainer, SingleContainer}
import org.testcontainers.containers.{GenericContainer => JavaGenericContainer}
import org.testcontainers.lifecycle.Startable
import zio.{RIO, RLayer, Scope, Tag, UIO, ZIO, ZLayer}
import zio.{Scope, Tag, UIO, ULayer, URIO, ZIO, ZLayer}

object ZIOTestcontainers {

Expand All @@ -21,13 +21,13 @@ object ZIOTestcontainers {
port <- ZIO.succeed(container.mappedPort(port))
} yield (host, port)

def toZIO[T <: Startable](startable: T): RIO[Scope, T] = {
def toZIO[T <: Startable](startable: T): URIO[Scope, T] = {
val acquire = ZIO.succeedBlocking(startable.start()).as(startable)
val release = (container: T) => ZIO.succeedBlocking(container.stop())
ZIO.acquireRelease(acquire)(release)
}

def toLayer[T <: Startable: Tag](container: T): RLayer[Scope, T] =
ZLayer.fromZIO(toZIO(container))
def toLayer[T <: Startable: Tag](container: T): ULayer[T] =
ZLayer.scoped(toZIO(container))

}
6 changes: 3 additions & 3 deletions src/main/scala/zio/testcontainers/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ package object testcontainers {

implicit final class StartableOps[T <: Startable](private val self: T) extends AnyVal {

def toZIO: RIO[Scope, T] =
def toZIO: URIO[Scope, T] =
ZIOTestcontainers.toZIO(self)

def toLayer(implicit ev: Tag[T]): RLayer[Scope, T] =
def toLayer(implicit ev: Tag[T]): ULayer[T] =
ZIOTestcontainers.toLayer(self)

}

implicit final class ZLayerTestContainerOps[T <: Startable](private val self: ZLayer.type) extends AnyVal {

def fromTestContainer(startable: T)(implicit ev: Tag[T]): RLayer[Scope, T] =
def fromTestContainer(startable: T)(implicit ev: Tag[T]): ULayer[T] =
ZIOTestcontainers.toLayer(startable)

}
Expand Down

0 comments on commit 3a31d4e

Please sign in to comment.