From 9d2da989fb7ead9f773ace5c3b699155e003bc10 Mon Sep 17 00:00:00 2001 From: Amit Kumar Singh Date: Fri, 1 Apr 2022 16:14:51 +0530 Subject: [PATCH] Upgrade to ZIO 2.0.0-RC3 (#1174) * upgrade zio version * fix implementation of use forever * delete collect scoped * Revert "delete collect scoped" This reverts commit 7d74f6627b5ddb858035654d842110fade705b81. * switch order of intersection type * cleanup * rename Co-authored-by: Adam Fraser --- .../src/main/scala/example/Endpoints.scala | 2 +- .../scala/example/HelloWorldAdvanced.scala | 4 +- .../main/scala/example/HttpsHelloWorld.scala | 4 +- .../example/PlainTextBenchmarkServer.scala | 6 +-- .../scala/example/WebSocketAdvanced.scala | 2 +- .../main/scala/example/WebSocketEcho.scala | 2 +- project/Dependencies.scala | 2 +- .../zhttp.benchmarks/HttpRouteTextPerf.scala | 6 +-- zio-http/src/main/scala/zhttp/http/Http.scala | 43 +++++++++-------- .../src/main/scala/zhttp/http/HttpData.scala | 23 ++++----- .../scala/zhttp/http/HttpDataExtension.scala | 8 ++-- .../main/scala/zhttp/http/Middleware.scala | 10 ++-- .../src/main/scala/zhttp/http/Response.scala | 2 +- .../scala/zhttp/http/middleware/Auth.scala | 12 ++--- .../scala/zhttp/http/middleware/Csrf.scala | 4 +- .../scala/zhttp/http/middleware/Web.scala | 2 +- .../scala/zhttp/service/ChannelFactory.scala | 4 +- .../scala/zhttp/service/ChannelFuture.scala | 16 +++---- .../scala/zhttp/service/EventLoopGroup.scala | 48 ++++++++++--------- .../main/scala/zhttp/service/Handler.scala | 12 ++--- .../scala/zhttp/service/HttpRuntime.scala | 4 +- .../src/main/scala/zhttp/service/Server.scala | 46 ++++++++---------- .../handlers/ServerResponseHandler.scala | 6 +-- .../src/main/scala/zhttp/socket/Socket.scala | 4 +- .../main/scala/zhttp/socket/SocketApp.scala | 4 +- .../scala/zhttp/endpoint/EndpointSpec.scala | 6 +-- .../scala/zhttp/http/ContentTypeSpec.scala | 7 +-- .../src/test/scala/zhttp/http/HExitSpec.scala | 10 ++-- .../src/test/scala/zhttp/http/HttpSpec.scala | 12 ++--- .../scala/zhttp/http/MiddlewareSpec.scala | 12 ++--- .../zhttp/http/middleware/AuthSpec.scala | 6 +-- .../scala/zhttp/http/middleware/WebSpec.scala | 10 ++-- .../scala/zhttp/internal/DynamicServer.scala | 6 +-- .../zhttp/internal/HttpRunnableSpec.scala | 8 ++-- .../test/scala/zhttp/service/ClientSpec.scala | 6 +-- .../scala/zhttp/service/KeepAliveSpec.scala | 13 ++--- .../test/scala/zhttp/service/SSLSpec.scala | 5 +- .../test/scala/zhttp/service/ServerSpec.scala | 4 +- .../zhttp/service/StaticFileServerSpec.scala | 6 +-- .../zhttp/service/StaticServerSpec.scala | 17 ++++--- .../zhttp/service/WebSocketServerSpec.scala | 4 +- 41 files changed, 205 insertions(+), 203 deletions(-) diff --git a/example/src/main/scala/example/Endpoints.scala b/example/src/main/scala/example/Endpoints.scala index 511feddca..1f2a02e82 100644 --- a/example/src/main/scala/example/Endpoints.scala +++ b/example/src/main/scala/example/Endpoints.scala @@ -16,7 +16,7 @@ object Endpoints extends ZIOAppDefault { } def h3 = GET / "b" / *[Int] / "c" / *[Boolean] to { a => - UIO(Response.text(a.params.toString)) + ZIO.succeed(Response.text(a.params.toString)) } // Run it like any simple app diff --git a/example/src/main/scala/example/HelloWorldAdvanced.scala b/example/src/main/scala/example/HelloWorldAdvanced.scala index ad60e187b..065cdea00 100644 --- a/example/src/main/scala/example/HelloWorldAdvanced.scala +++ b/example/src/main/scala/example/HelloWorldAdvanced.scala @@ -32,13 +32,13 @@ object HelloWorldAdvanced extends ZIOAppDefault { // Create a new server server.make - .use(start => + .flatMap(start => // Waiting for the server to start Console.printLine(s"Server started on port ${start.port}") // Ensures the server doesn't die after printing *> ZIO.never, ) - .provideCustom(ServerChannelFactory.auto, EventLoopGroup.auto(nThreads)) + .provideCustom(ServerChannelFactory.auto, EventLoopGroup.auto(nThreads), Scope.default) } } diff --git a/example/src/main/scala/example/HttpsHelloWorld.scala b/example/src/main/scala/example/HttpsHelloWorld.scala index 9bb8a87b4..5a84fb60c 100644 --- a/example/src/main/scala/example/HttpsHelloWorld.scala +++ b/example/src/main/scala/example/HttpsHelloWorld.scala @@ -32,6 +32,6 @@ object HttpsHelloWorld extends ZIOAppDefault { ) override val run = - server.make.useForever - .provide(ServerChannelFactory.auto, EventLoopGroup.auto(0)) + (server.make *> ZIO.never) + .provide(ServerChannelFactory.auto, EventLoopGroup.auto(0), Scope.default) } diff --git a/example/src/main/scala/example/PlainTextBenchmarkServer.scala b/example/src/main/scala/example/PlainTextBenchmarkServer.scala index 4da1215ba..09d586633 100644 --- a/example/src/main/scala/example/PlainTextBenchmarkServer.scala +++ b/example/src/main/scala/example/PlainTextBenchmarkServer.scala @@ -4,7 +4,7 @@ import io.netty.util.AsciiString import zhttp.http.{Http, _} import zhttp.service.server.ServerChannelFactory import zhttp.service.{EventLoopGroup, Server} -import zio.{ExitCode, UIO, URIO, ZIOAppDefault} +import zio._ /** * This server is used to run plaintext benchmarks on CI. @@ -42,8 +42,8 @@ object Main extends ZIOAppDefault { val run: URIO[zio.ZEnv, ExitCode] = app - .flatMap(server(_).make.useForever) - .provideCustomLayer(ServerChannelFactory.auto ++ EventLoopGroup.auto(8)) + .flatMap(server(_).make *> ZIO.never) + .provideCustomLayer(ServerChannelFactory.auto ++ EventLoopGroup.auto(8) ++ Scope.default) .exitCode private def server(app: HttpApp[Any, Nothing]) = diff --git a/example/src/main/scala/example/WebSocketAdvanced.scala b/example/src/main/scala/example/WebSocketAdvanced.scala index b95a01c3e..b6e2b4a9a 100644 --- a/example/src/main/scala/example/WebSocketAdvanced.scala +++ b/example/src/main/scala/example/WebSocketAdvanced.scala @@ -48,7 +48,7 @@ object WebSocketAdvanced extends ZIOAppDefault { private val app = Http.collectZIO[Request] { - case Method.GET -> !! / "greet" / name => UIO(Response.text(s"Greetings ${name}!")) + case Method.GET -> !! / "greet" / name => ZIO.succeed(Response.text(s"Greetings ${name}!")) case Method.GET -> !! / "subscriptions" => socketApp.toResponse } diff --git a/example/src/main/scala/example/WebSocketEcho.scala b/example/src/main/scala/example/WebSocketEcho.scala index f698c69d0..64c227b7b 100644 --- a/example/src/main/scala/example/WebSocketEcho.scala +++ b/example/src/main/scala/example/WebSocketEcho.scala @@ -18,7 +18,7 @@ object WebSocketEcho extends ZIOAppDefault { private val app = Http.collectZIO[Request] { - case Method.GET -> !! / "greet" / name => UIO(Response.text(s"Greetings {$name}!")) + case Method.GET -> !! / "greet" / name => ZIO.succeed(Response.text(s"Greetings {$name}!")) case Method.GET -> !! / "subscriptions" => socket.toResponse } diff --git a/project/Dependencies.scala b/project/Dependencies.scala index afeb0b2e1..40e4923fd 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -5,7 +5,7 @@ object Dependencies { val NettyVersion = "4.1.75.Final" val NettyIncubatorVersion = "0.0.13.Final" val ScalaCompactCollectionVersion = "2.7.0" - val ZioVersion = "2.0.0-RC2" + val ZioVersion = "2.0.0-RC3" val SttpVersion = "3.3.18" val `jwt-core` = "com.github.jwt-scala" %% "jwt-core" % JwtCoreVersion diff --git a/zio-http-benchmarks/src/main/scala/zhttp.benchmarks/HttpRouteTextPerf.scala b/zio-http-benchmarks/src/main/scala/zhttp.benchmarks/HttpRouteTextPerf.scala index 7f4d21219..c51e2aad5 100644 --- a/zio-http-benchmarks/src/main/scala/zhttp.benchmarks/HttpRouteTextPerf.scala +++ b/zio-http-benchmarks/src/main/scala/zhttp.benchmarks/HttpRouteTextPerf.scala @@ -1,12 +1,12 @@ package zhttp.benchmarks -import org.openjdk.jmh.annotations._ +import org.openjdk.jmh.annotations.{Scope => JScope, _} import zhttp.http._ import zio._ import java.util.concurrent.TimeUnit -@State(Scope.Thread) +@State(JScope.Thread) @BenchmarkMode(Array(Mode.Throughput)) @OutputTimeUnit(TimeUnit.SECONDS) class HttpRouteTextPerf { @@ -17,7 +17,7 @@ class HttpRouteTextPerf { private val app = Http.succeed(res) private val req: Request = Request(Version.`HTTP/1.1`, Method.GET, URL(!!)) private val httpProgram = ZIO.foreachDiscard(0 to 1000) { _ => app.execute(req).toZIO } - private val UIOProgram = ZIO.foreachDiscard(0 to 1000) { _ => UIO(res) } + private val UIOProgram = ZIO.foreachDiscard(0 to 1000) { _ => ZIO.succeed(res) } @Benchmark def benchmarkHttpProgram(): Unit = { diff --git a/zio-http/src/main/scala/zhttp/http/Http.scala b/zio-http/src/main/scala/zhttp/http/Http.scala index f225086e4..ab433852e 100644 --- a/zio-http/src/main/scala/zhttp/http/Http.scala +++ b/zio-http/src/main/scala/zhttp/http/Http.scala @@ -182,13 +182,13 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self => * Extracts body */ final def body(implicit eb: B <:< Response, ee: E <:< Throwable): Http[R, Throwable, A, Chunk[Byte]] = - self.bodyAsByteBuf.mapZIO(buf => Task(Chunk.fromArray(ByteBufUtil.getBytes(buf)))) + self.bodyAsByteBuf.mapZIO(buf => ZIO.attempt(Chunk.fromArray(ByteBufUtil.getBytes(buf)))) /** * Extracts body as a string */ final def bodyAsString(implicit eb: B <:< Response, ee: E <:< Throwable): Http[R, Throwable, A, String] = - self.bodyAsByteBuf.mapZIO(bytes => Task(bytes.toString(HTTP_CHARSET))) + self.bodyAsByteBuf.mapZIO(bytes => ZIO.attempt(bytes.toString(HTTP_CHARSET))) /** * Catches all the exceptions that the http app can fail with @@ -256,10 +256,10 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self => final def collect[R1 <: R, E1 >: E, A1 <: A, B1 >: B, C](pf: PartialFunction[B1, C]): Http[R1, E1, A1, C] = self >>> Http.collect(pf) - final def collectManaged[R1 <: R, E1 >: E, A1 <: A, B1 >: B, C]( - pf: PartialFunction[B1, ZManaged[R1, E1, C]], + final def collectScoped[R1 <: R, E1 >: E, A1 <: A, B1 >: B, C]( + pf: PartialFunction[B1, ZIO[Scope with R1, E1, C]], ): Http[R1, E1, A1, C] = - self >>> Http.collectManaged(pf) + self >>> Http.collectScoped[B1][R1, E1, C](pf) /** * Collects some of the results of the http and effectfully converts it to @@ -318,13 +318,14 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self => /** * Delays production of output B for the specified duration of time */ - final def delayAfter(duration: Duration): Http[R with Clock, E, A, B] = self.mapZIO(b => UIO(b).delay(duration)) + final def delayAfter(duration: Duration): Http[R with Clock, E, A, B] = + self.mapZIO(b => ZIO.succeed(b).delay(duration)) /** * Delays consumption of input A for the specified duration of time */ final def delayBefore(duration: Duration): Http[R with Clock, E, A, B] = - self.contramapZIO(a => UIO(a).delay(duration)) + self.contramapZIO(a => ZIO.succeed(a).delay(duration)) /** * Returns an http app whose failure and success have been lifted into an @@ -457,7 +458,7 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self => /** * Provides the environment to Http. */ - final def provideEnvironment(r: ZEnvironment[R])(implicit ev: NeedsEnv[R]): Http[Any, E, A, B] = + final def provideEnvironment(r: ZEnvironment[R]): Http[Any, E, A, B] = Http.fromOptionFunction[A](a => self(a).provideEnvironment(r)) /** @@ -465,14 +466,14 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self => */ final def provideLayer[E1 >: E, R0]( layer: ZLayer[R0, E1, R], - )(implicit ev2: NeedsEnv[R]): Http[R0, E1, A, B] = + ): Http[R0, E1, A, B] = Http.fromOptionFunction[A](a => self(a).provideLayer(layer.mapError(Option(_)))) /** * Provides some of the environment to Http. */ - final def provideSomeEnvironment[R1](r: ZEnvironment[R1] => ZEnvironment[R])(implicit - ev: NeedsEnv[R], + final def provideSomeEnvironment[R1]( + r: ZEnvironment[R1] => ZEnvironment[R], ): Http[R1, E, A, B] = Http.fromOptionFunction[A](a => self(a).provideSomeEnvironment(r)) @@ -718,9 +719,9 @@ object Http { /** * Creates an Http app which accepts a request and produces response from a - * managed resource + * scoped resource */ - def collectManaged[A]: Http.PartialCollectManaged[A] = Http.PartialCollectManaged(()) + def collectScoped[A]: Http.PartialCollectScoped[A] = Http.PartialCollectScoped(()) /** * Creates an HTTP app which accepts a request and produces response @@ -803,7 +804,7 @@ object Http { /** * Creates an Http app from the contents of a file. */ - def fromFile(file: => java.io.File): HttpApp[Any, Throwable] = Http.fromFileZIO(UIO(file)) + def fromFile(file: => java.io.File): HttpApp[Any, Throwable] = Http.fromFileZIO(ZIO.succeed(file)) /** * Creates an Http app from the contents of a file which is produced from an @@ -813,7 +814,7 @@ object Http { def fromFileZIO[R](fileZIO: ZIO[R, Throwable, java.io.File]): HttpApp[R, Throwable] = { val response: ZIO[R, Throwable, HttpApp[R, Throwable]] = fileZIO.flatMap { file => - Task { + ZIO.attempt { if (file.isFile) { val length = Headers.contentLength(file.length()) val response = Response(headers = length, data = HttpData.fromFile(file)) @@ -999,9 +1000,9 @@ object Http { Http.collect[A] { case a if pf.isDefinedAt(a) => Http.fromZIO(pf(a)) }.flatten } - final case class PartialCollectManaged[A](unit: Unit) extends AnyVal { - def apply[R, E, B](pf: PartialFunction[A, ZManaged[R, E, B]]): Http[R, E, A, B] = - Http.collect[A] { case a if pf.isDefinedAt(a) => Http.fromZIO(pf(a).useNow) }.flatten + final case class PartialCollectScoped[A](unit: Unit) extends AnyVal { + def apply[R, E, B](pf: PartialFunction[A, ZIO[Scope with R, E, B]]): Http[R, E, A, B] = + Http.collect[A] { case a if pf.isDefinedAt(a) => Http.fromZIO(ZIO.scoped[R](pf(a))) }.flatten } final case class PartialCollect[A](unit: Unit) extends AnyVal { @@ -1039,10 +1040,10 @@ object Http { f(a) .map(Http.succeed) .catchAll { - case Some(error) => UIO(Http.fail(error)) - case None => UIO(Http.empty) + case Some(error) => ZIO.succeed(Http.fail(error)) + case None => ZIO.succeed(Http.empty) } - .catchAllDefect(defect => UIO(Http.die(defect))) + .catchAllDefect(defect => ZIO.succeed(Http.die(defect))) } .flatten } diff --git a/zio-http/src/main/scala/zhttp/http/HttpData.scala b/zio-http/src/main/scala/zhttp/http/HttpData.scala index bc3b97166..2b677805e 100644 --- a/zio-http/src/main/scala/zhttp/http/HttpData.scala +++ b/zio-http/src/main/scala/zhttp/http/HttpData.scala @@ -6,7 +6,7 @@ import io.netty.handler.codec.http.{HttpContent, LastHttpContent} import io.netty.util.AsciiString import zhttp.http.HttpData.ByteBufConfig import zio.stream.ZStream -import zio.{Chunk, Task, UIO, ZIO} +import zio.{Chunk, Task, ZIO} import java.io.FileInputStream import java.nio.charset.Charset @@ -148,7 +148,7 @@ object HttpData { val buffer = Unpooled.compositeBuffer() msg => { buffer.addComponent(true, msg.content) - if (msg.isLast) cb(UIO(buffer)) else ch.read() + if (msg.isLast) cb(ZIO.succeed(buffer)) else ch.read() } }), ) @@ -178,7 +178,8 @@ object HttpData { * Encodes the HttpData into a ByteBuf. Takes in ByteBufConfig to have a * more fine grained control over the encoding. */ - override def toByteBuf(config: ByteBufConfig): Task[ByteBuf] = Task(Unpooled.wrappedBuffer(asciiString.array())) + override def toByteBuf(config: ByteBufConfig): Task[ByteBuf] = + ZIO.attempt(Unpooled.wrappedBuffer(asciiString.array())) /** * Encodes the HttpData into a Stream of ByteBufs. Takes in ByteBufConfig to @@ -202,7 +203,7 @@ object HttpData { /** * Encodes the HttpData into a ByteBuf. */ - override def toByteBuf(config: ByteBufConfig): Task[ByteBuf] = UIO(encode) + override def toByteBuf(config: ByteBufConfig): Task[ByteBuf] = ZIO.succeed(encode) /** * Encodes the HttpData into a Stream of ByteBufs @@ -218,7 +219,7 @@ object HttpData { /** * Encodes the HttpData into a ByteBuf. */ - override def toByteBuf(config: ByteBufConfig): Task[ByteBuf] = Task(data) + override def toByteBuf(config: ByteBufConfig): Task[ByteBuf] = ZIO.attempt(data) /** * Encodes the HttpData into a Stream of ByteBufs @@ -261,18 +262,18 @@ object HttpData { override def toByteBufStream(config: ByteBufConfig): ZStream[Any, Throwable, ByteBuf] = ZStream.unwrap { for { - file <- Task(unsafeFile()) - fs <- Task(new FileInputStream(file)) + file <- ZIO.attempt(unsafeFile()) + fs <- ZIO.attempt(new FileInputStream(file)) size = config.chunkSize(file.length()) buffer = new Array[Byte](size) } yield ZStream .repeatZIOOption[Any, Throwable, ByteBuf] { for { - len <- Task(fs.read(buffer)).mapError(Some(_)) - bytes <- if (len > 0) UIO(Unpooled.copiedBuffer(buffer, 0, len)) else ZIO.fail(None) + len <- ZIO.attempt(fs.read(buffer)).mapError(Some(_)) + bytes <- if (len > 0) ZIO.succeed(Unpooled.copiedBuffer(buffer, 0, len)) else ZIO.fail(None) } yield bytes } - .ensuring(UIO(fs.close())) + .ensuring(ZIO.succeed(fs.close())) } override def toHttp(config: ByteBufConfig): Http[Any, Throwable, Any, ByteBuf] = @@ -288,7 +289,7 @@ object HttpData { /** * Encodes the HttpData into a ByteBuf. */ - override def toByteBuf(config: ByteBufConfig): Task[ByteBuf] = UIO(Unpooled.EMPTY_BUFFER) + override def toByteBuf(config: ByteBufConfig): Task[ByteBuf] = ZIO.succeed(Unpooled.EMPTY_BUFFER) /** * Encodes the HttpData into a Stream of ByteBufs diff --git a/zio-http/src/main/scala/zhttp/http/HttpDataExtension.scala b/zio-http/src/main/scala/zhttp/http/HttpDataExtension.scala index 32daf83fc..8deb770eb 100644 --- a/zio-http/src/main/scala/zhttp/http/HttpDataExtension.scala +++ b/zio-http/src/main/scala/zhttp/http/HttpDataExtension.scala @@ -4,7 +4,7 @@ import io.netty.buffer.{ByteBuf, ByteBufUtil} import io.netty.util.AsciiString import zhttp.http.headers.HeaderExtension import zio.stream.ZStream -import zio.{Chunk, Task, UIO, ZIO} +import zio.{Chunk, Task, ZIO} private[zhttp] trait HttpDataExtension[+A] extends HeaderExtension[A] { self: A => private[zhttp] final def bodyAsByteBuf: Task[ByteBuf] = data.toByteBuf @@ -18,7 +18,9 @@ private[zhttp] trait HttpDataExtension[+A] extends HeaderExtension[A] { self: A bodyAsByteArray.map(Chunk.fromArray) final def bodyAsByteArray: Task[Array[Byte]] = - bodyAsByteBuf.flatMap(buf => Task(ByteBufUtil.getBytes(buf)).ensuring(UIO(buf.release(buf.refCnt())))) + bodyAsByteBuf.flatMap(buf => + ZIO.attempt(ByteBufUtil.getBytes(buf)).ensuring(ZIO.succeed(buf.release(buf.refCnt()))), + ) /** * Decodes the content of request as CharSequence @@ -31,7 +33,7 @@ private[zhttp] trait HttpDataExtension[+A] extends HeaderExtension[A] { self: A */ final def bodyAsStream: ZStream[Any, Throwable, Byte] = data.toByteBufStream .mapZIO[Any, Throwable, Chunk[Byte]] { buf => - Task { + ZIO.attempt { val bytes = Chunk.fromArray(ByteBufUtil.getBytes(buf)) buf.release(buf.refCnt()) bytes diff --git a/zio-http/src/main/scala/zhttp/http/Middleware.scala b/zio-http/src/main/scala/zhttp/http/Middleware.scala index 5571be2d6..c2f246fcc 100644 --- a/zio-http/src/main/scala/zhttp/http/Middleware.scala +++ b/zio-http/src/main/scala/zhttp/http/Middleware.scala @@ -1,7 +1,7 @@ package zhttp.http import zhttp.http.middleware.Web -import zio.{Clock, Duration, UIO, ZIO} +import zio.{Clock, Duration, ZIO} /** * Middlewares are essentially transformations that one can apply on any Http to @@ -80,7 +80,7 @@ trait Middleware[-R, +E, +AIn, -BIn, -AOut, +BOut] { self => * Preprocesses the incoming value for the outgoing Http. */ final def contramap[AOut0](f: AOut0 => AOut): Middleware[R, E, AIn, BIn, AOut0, BOut] = - self.contramapZIO[AOut0](a => UIO(f(a))) + self.contramapZIO[AOut0](a => ZIO.succeed(f(a))) /** * Preprocesses the incoming value using a ZIO, for the outgoing Http. @@ -92,7 +92,7 @@ trait Middleware[-R, +E, +AIn, -BIn, -AOut, +BOut] { self => * Delays the production of Http output for the specified duration */ final def delay(duration: Duration): Middleware[R with Clock, E, AIn, BIn, AOut, BOut] = - self.mapZIO(b => UIO(b).delay(duration)) + self.mapZIO(b => ZIO.succeed(b).delay(duration)) /** * Creates a new Middleware from another @@ -158,7 +158,7 @@ trait Middleware[-R, +E, +AIn, -BIn, -AOut, +BOut] { self => * Applies Middleware based only if the condition function evaluates to true */ final def when[AOut0 <: AOut](cond: AOut0 => Boolean): Middleware[R, E, AIn, BIn, AOut0, BOut] = - whenZIO(a => UIO(cond(a))) + whenZIO(a => ZIO.succeed(cond(a))) /** * Applies Middleware based only if the condition effectful function evaluates @@ -269,7 +269,7 @@ object Middleware extends Web { final class PartialIntercept[A, B](val unit: Unit) extends AnyVal { def apply[S, BOut](incoming: A => S)(outgoing: (B, S) => BOut): Middleware[Any, Nothing, A, B, A, BOut] = - interceptZIO[A, B](a => UIO(incoming(a)))((b, s) => UIO(outgoing(b, s))) + interceptZIO[A, B](a => ZIO.succeed(incoming(a)))((b, s) => ZIO.succeed(outgoing(b, s))) } final class PartialInterceptZIO[A, B](val unit: Unit) extends AnyVal { diff --git a/zio-http/src/main/scala/zhttp/http/Response.scala b/zio-http/src/main/scala/zhttp/http/Response.scala index fa2b44fac..f82335e9f 100644 --- a/zio-http/src/main/scala/zhttp/http/Response.scala +++ b/zio-http/src/main/scala/zhttp/http/Response.scala @@ -75,7 +75,7 @@ final case class Response private ( * to be counter productive. */ def freeze: UIO[Response] = - UIO(self.copy(attribute = self.attribute.withEncodedResponse(unsafeEncode(), self))) + ZIO.succeed(self.copy(attribute = self.attribute.withEncodedResponse(unsafeEncode(), self))) /** * Sets the response attributes diff --git a/zio-http/src/main/scala/zhttp/http/middleware/Auth.scala b/zio-http/src/main/scala/zhttp/http/middleware/Auth.scala index e5e2e4a70..ff3d889da 100644 --- a/zio-http/src/main/scala/zhttp/http/middleware/Auth.scala +++ b/zio-http/src/main/scala/zhttp/http/middleware/Auth.scala @@ -4,7 +4,7 @@ import io.netty.handler.codec.http.HttpHeaderNames import zhttp.http.Headers.{BasicSchemeName, BearerSchemeName} import zhttp.http._ import zhttp.http.middleware.Auth.Credentials -import zio.{UIO, ZIO} +import zio.ZIO private[zhttp] trait Auth { @@ -12,7 +12,7 @@ private[zhttp] trait Auth { * Creates a middleware for basic authentication */ final def basicAuth(f: Credentials => Boolean): HttpMiddleware[Any, Nothing] = - basicAuthZIO(credentials => UIO(f(credentials))) + basicAuthZIO(credentials => ZIO.succeed(f(credentials))) /** * Creates a middleware for basic authentication that checks if the @@ -29,7 +29,7 @@ private[zhttp] trait Auth { customAuthZIO( _.basicAuthorizationCredentials match { case Some(credentials) => f(credentials) - case None => UIO(false) + case None => ZIO.succeed(false) }, Headers(HttpHeaderNames.WWW_AUTHENTICATE, BasicSchemeName), ) @@ -41,7 +41,7 @@ private[zhttp] trait Auth { * function that validates the token string inside the Bearer Header */ final def bearerAuth(f: String => Boolean): HttpMiddleware[Any, Nothing] = - bearerAuthZIO(token => UIO(f(token))) + bearerAuthZIO(token => ZIO.succeed(f(token))) /** * Creates a middleware for bearer authentication that checks the token using @@ -54,7 +54,7 @@ private[zhttp] trait Auth { customAuthZIO( _.bearerToken match { case Some(token) => f(token) - case None => UIO(false) + case None => ZIO.succeed(false) }, Headers(HttpHeaderNames.WWW_AUTHENTICATE, BearerSchemeName), ) @@ -68,7 +68,7 @@ private[zhttp] trait Auth { responseHeaders: Headers = Headers.empty, responseStatus: Status = Status.Unauthorized, ): HttpMiddleware[Any, Nothing] = - customAuthZIO(headers => UIO(verify(headers)), responseHeaders, responseStatus) + customAuthZIO(headers => ZIO.succeed(verify(headers)), responseHeaders, responseStatus) /** * Creates an authentication middleware that only allows authenticated diff --git a/zio-http/src/main/scala/zhttp/http/middleware/Csrf.scala b/zio-http/src/main/scala/zhttp/http/middleware/Csrf.scala index 08ea73e8e..b94be961d 100644 --- a/zio-http/src/main/scala/zhttp/http/middleware/Csrf.scala +++ b/zio-http/src/main/scala/zhttp/http/middleware/Csrf.scala @@ -1,7 +1,7 @@ package zhttp.http.middleware import zhttp.http._ -import zio.{UIO, ZIO} +import zio.ZIO import java.util.UUID @@ -19,7 +19,7 @@ private[zhttp] trait Csrf { */ final def csrfGenerate[R, E]( tokenName: String = "x-csrf-token", - tokenGen: ZIO[R, Nothing, String] = UIO(UUID.randomUUID.toString), + tokenGen: ZIO[R, Nothing, String] = ZIO.succeed(UUID.randomUUID.toString), ): HttpMiddleware[R, E] = Middleware.addCookieZIO(tokenGen.map(Cookie(tokenName, _))) diff --git a/zio-http/src/main/scala/zhttp/http/middleware/Web.scala b/zio-http/src/main/scala/zhttp/http/middleware/Web.scala index 61fe4e397..a0fc6ca2d 100644 --- a/zio-http/src/main/scala/zhttp/http/middleware/Web.scala +++ b/zio-http/src/main/scala/zhttp/http/middleware/Web.scala @@ -110,7 +110,7 @@ private[zhttp] trait Web extends Cors with Csrf with Auth with HeaderModifier[Ht * middleware is applied. */ final def runBefore[R, E](effect: ZIO[R, E, Any]): HttpMiddleware[R, E] = - Middleware.interceptZIOPatch(_ => effect.mapError(Option(_)).unit)((_, _) => UIO(Patch.empty)) + Middleware.interceptZIOPatch(_ => effect.mapError(Option(_)).unit)((_, _) => ZIO.succeed(Patch.empty)) /** * Creates a new middleware that always sets the response status to the diff --git a/zio-http/src/main/scala/zhttp/service/ChannelFactory.scala b/zio-http/src/main/scala/zhttp/service/ChannelFactory.scala index da3fb1e41..654e0f1f3 100644 --- a/zio-http/src/main/scala/zhttp/service/ChannelFactory.scala +++ b/zio-http/src/main/scala/zhttp/service/ChannelFactory.scala @@ -6,7 +6,7 @@ import io.netty.channel.kqueue.{KQueue, KQueueSocketChannel} import io.netty.channel.socket.nio.NioSocketChannel import io.netty.channel.{Channel, ChannelFactory => JChannelFactory} import io.netty.incubator.channel.uring.IOUringSocketChannel -import zio.{UIO, ZLayer} +import zio.{UIO, ZIO, ZLayer} object ChannelFactory { def nio: ZLayer[Any, Nothing, ChannelFactory] = Live.nio.toLayer @@ -15,7 +15,7 @@ object ChannelFactory { def embedded: ZLayer[Any, Nothing, ChannelFactory] = Live.embedded.toLayer def auto: ZLayer[Any, Nothing, ChannelFactory] = Live.auto.toLayer - def make[A <: Channel](fn: () => A): UIO[JChannelFactory[A]] = UIO(new JChannelFactory[A] { + def make[A <: Channel](fn: () => A): UIO[JChannelFactory[A]] = ZIO.succeed(new JChannelFactory[A] { override def newChannel(): A = fn() }) diff --git a/zio-http/src/main/scala/zhttp/service/ChannelFuture.scala b/zio-http/src/main/scala/zhttp/service/ChannelFuture.scala index cfdefdf6e..815783d45 100644 --- a/zio-http/src/main/scala/zhttp/service/ChannelFuture.scala +++ b/zio-http/src/main/scala/zhttp/service/ChannelFuture.scala @@ -19,28 +19,28 @@ final class ChannelFuture[A] private (jFuture: Future[A]) { .async[Any, Throwable, Option[A]](cb => { handler = _ => { jFuture.cause() match { - case null => cb(Task(Option(jFuture.get))) - case _: CancellationException => cb(UIO(Option.empty)) + case null => cb(ZIO.attempt(Option(jFuture.get))) + case _: CancellationException => cb(ZIO.succeed(Option.empty)) case cause => cb(ZIO.fail(cause)) } } jFuture.addListener(handler) }) - .onInterrupt(UIO(jFuture.removeListener(handler))) + .onInterrupt(ZIO.succeed(jFuture.removeListener(handler))) } - def toManaged: ZManaged[Any, Throwable, Option[A]] = { - execute.toManagedWith(_ => cancel(true)) + def toZIO: ZIO[Scope, Throwable, Option[A]] = { + execute.withFinalizer(cancel(true)) } // Cancels the future - def cancel(interruptIfRunning: Boolean = false): UIO[Boolean] = UIO(jFuture.cancel(interruptIfRunning)) + def cancel(interruptIfRunning: Boolean = false): UIO[Boolean] = ZIO.succeed(jFuture.cancel(interruptIfRunning)) } object ChannelFuture { - def make[A](jFuture: => Future[A]): Task[ChannelFuture[A]] = Task(new ChannelFuture(jFuture)) + def make[A](jFuture: => Future[A]): Task[ChannelFuture[A]] = ZIO.attempt(new ChannelFuture(jFuture)) def unit[A](jFuture: => Future[A]): Task[Unit] = make(jFuture).flatMap(_.execute.unit) - def asManaged[A](jFuture: => Future[A]): TaskManaged[Unit] = make(jFuture).toManaged.flatMap(_.toManaged.unit) + def asZIO[A](jFuture: => Future[A]): ZIO[Scope, Throwable, Unit] = make(jFuture).flatMap(_.toZIO.unit) } diff --git a/zio-http/src/main/scala/zhttp/service/EventLoopGroup.scala b/zio-http/src/main/scala/zhttp/service/EventLoopGroup.scala index 2bf70892b..9beb8daef 100644 --- a/zio-http/src/main/scala/zhttp/service/EventLoopGroup.scala +++ b/zio-http/src/main/scala/zhttp/service/EventLoopGroup.scala @@ -12,49 +12,51 @@ import java.util.concurrent.Executor * Simple wrapper over NioEventLoopGroup */ object EventLoopGroup { - def nio(nThreads: Int = 0): ZLayer[Any, Nothing, EventLoopGroup] = EventLoopGroup.Live.nio(nThreads).toLayer + def nio(nThreads: Int = 0): ZLayer[Any, Nothing, EventLoopGroup] = ZLayer.scoped(EventLoopGroup.Live.nio(nThreads)) - def epoll(nThreads: Int = 0): ZLayer[Any, Nothing, EventLoopGroup] = EventLoopGroup.Live.epoll(nThreads).toLayer + def epoll(nThreads: Int = 0): ZLayer[Any, Nothing, EventLoopGroup] = + ZLayer.scoped(EventLoopGroup.Live.epoll(nThreads)) - def uring(nThreads: Int = 0): ZLayer[Any, Nothing, EventLoopGroup] = EventLoopGroup.Live.uring(nThreads).toLayer + def uring(nThreads: Int = 0): ZLayer[Any, Nothing, EventLoopGroup] = + ZLayer.scoped(EventLoopGroup.Live.uring(nThreads)) - def auto(nThreads: Int = 0): ZLayer[Any, Nothing, EventLoopGroup] = EventLoopGroup.Live.auto(nThreads).toLayer + def auto(nThreads: Int = 0): ZLayer[Any, Nothing, EventLoopGroup] = ZLayer.scoped(EventLoopGroup.Live.auto(nThreads)) - def default: ZLayer[Any, Nothing, EventLoopGroup] = EventLoopGroup.Live.default.toLayer + def default: ZLayer[Any, Nothing, EventLoopGroup] = ZLayer.scoped(EventLoopGroup.Live.default) object Live { - def nio(nThreads: Int): ZManaged[Any, Nothing, channel.EventLoopGroup] = - make(UIO(new channel.nio.NioEventLoopGroup(nThreads))) + def nio(nThreads: Int): ZIO[Scope, Nothing, channel.EventLoopGroup] = + make(ZIO.succeed(new channel.nio.NioEventLoopGroup(nThreads))) - def nio(nThreads: Int, executor: Executor): ZManaged[Any, Nothing, channel.EventLoopGroup] = - make(UIO(new channel.nio.NioEventLoopGroup(nThreads, executor))) + def nio(nThreads: Int, executor: Executor): ZIO[Scope, Nothing, channel.EventLoopGroup] = + make(ZIO.succeed(new channel.nio.NioEventLoopGroup(nThreads, executor))) - def make(eventLoopGroup: UIO[channel.EventLoopGroup]): ZManaged[Any, Nothing, channel.EventLoopGroup] = - eventLoopGroup.toManagedWith(ev => ChannelFuture.unit(ev.shutdownGracefully).orDie) + def make(eventLoopGroup: UIO[channel.EventLoopGroup]): ZIO[Scope, Nothing, channel.EventLoopGroup] = + ZIO.acquireRelease(eventLoopGroup)(ev => ChannelFuture.unit(ev.shutdownGracefully).orDie) - def epoll(nThreads: Int): ZManaged[Any, Nothing, channel.EventLoopGroup] = - make(UIO(new channel.epoll.EpollEventLoopGroup(nThreads))) + def epoll(nThreads: Int): ZIO[Scope, Nothing, channel.EventLoopGroup] = + make(ZIO.succeed(new channel.epoll.EpollEventLoopGroup(nThreads))) - def kQueue(nThreads: Int): ZManaged[Any, Nothing, channel.EventLoopGroup] = - make(UIO(new channel.kqueue.KQueueEventLoopGroup(nThreads))) + def kQueue(nThreads: Int): ZIO[Scope, Nothing, channel.EventLoopGroup] = + make(ZIO.succeed(new channel.kqueue.KQueueEventLoopGroup(nThreads))) - def epoll(nThreads: Int, executor: Executor): ZManaged[Any, Nothing, channel.EventLoopGroup] = - make(UIO(new channel.epoll.EpollEventLoopGroup(nThreads, executor))) + def epoll(nThreads: Int, executor: Executor): ZIO[Scope, Nothing, channel.EventLoopGroup] = + make(ZIO.succeed(new channel.epoll.EpollEventLoopGroup(nThreads, executor))) - def uring(nThread: Int): ZManaged[Any, Nothing, channel.EventLoopGroup] = - make(UIO(new IOUringEventLoopGroup(nThread))) + def uring(nThread: Int): ZIO[Scope, Nothing, channel.EventLoopGroup] = + make(ZIO.succeed(new IOUringEventLoopGroup(nThread))) - def uring(nThread: Int, executor: Executor): ZManaged[Any, Nothing, channel.EventLoopGroup] = - make(UIO(new IOUringEventLoopGroup(nThread, executor))) + def uring(nThread: Int, executor: Executor): ZIO[Scope, Nothing, channel.EventLoopGroup] = + make(ZIO.succeed(new IOUringEventLoopGroup(nThread, executor))) - def auto(nThreads: Int): ZManaged[Any, Nothing, channel.EventLoopGroup] = + def auto(nThreads: Int): ZIO[Scope, Nothing, channel.EventLoopGroup] = if (Epoll.isAvailable) epoll(nThreads) else if (KQueue.isAvailable) kQueue(nThreads) else nio(nThreads) - def default: ZManaged[Any, Nothing, channel.EventLoopGroup] = make(UIO(new channel.DefaultEventLoopGroup())) + def default: ZIO[Scope, Nothing, channel.EventLoopGroup] = make(ZIO.succeed(new channel.DefaultEventLoopGroup())) } } diff --git a/zio-http/src/main/scala/zhttp/service/Handler.scala b/zio-http/src/main/scala/zhttp/service/Handler.scala index 2a09265b6..cebe09a8a 100644 --- a/zio-http/src/main/scala/zhttp/service/Handler.scala +++ b/zio-http/src/main/scala/zhttp/service/Handler.scala @@ -6,7 +6,7 @@ import io.netty.handler.codec.http._ import zhttp.http._ import zhttp.service.server.content.handlers.ServerResponseHandler import zhttp.service.server.{ServerTime, WebSocketUpgrade} -import zio.{UIO, ZIO} +import zio.ZIO import java.net.{InetAddress, InetSocketAddress} @@ -138,20 +138,20 @@ private[zhttp] final case class Handler[R]( cause => cause.failureOrCause match { case Left(Some(cause)) => - UIO { + ZIO.succeed { writeResponse( Response.fromHttpError(HttpError.InternalServerError(cause = Some(cause))), jReq, ) } case Left(None) => - UIO { + ZIO.succeed { writeResponse(Response.status(Status.NotFound), jReq) } case Right(other) => other.dieOption match { case Some(defect) => - UIO { + ZIO.succeed { writeResponse( Response.fromHttpError(HttpError.InternalServerError(cause = Some(defect))), jReq, @@ -162,10 +162,10 @@ private[zhttp] final case class Handler[R]( } }, res => - if (self.isWebSocket(res)) UIO(self.upgradeToWebSocket(jReq, res)) + if (self.isWebSocket(res)) ZIO.succeed(self.upgradeToWebSocket(jReq, res)) else { for { - _ <- ZIO { + _ <- ZIO.attempt { writeResponse(res, jReq) } } yield () diff --git a/zio-http/src/main/scala/zhttp/service/HttpRuntime.scala b/zio-http/src/main/scala/zhttp/service/HttpRuntime.scala index 9092a3652..11ec03c84 100644 --- a/zio-http/src/main/scala/zhttp/service/HttpRuntime.scala +++ b/zio-http/src/main/scala/zhttp/service/HttpRuntime.scala @@ -24,13 +24,13 @@ final class HttpRuntime[+R](strategy: HttpRuntime.Strategy[R]) { rtm .unsafeRunAsyncWith(for { fiber <- program.fork - close <- UIO { + close <- ZIO.succeed { val close = closeListener(rtm, fiber) ctx.channel().closeFuture.addListener(close) close } _ <- fiber.join - _ <- UIO(ctx.channel().closeFuture().removeListener(close)) + _ <- ZIO.succeed(ctx.channel().closeFuture().removeListener(close)) } yield ()) { case Exit.Success(_) => () case Exit.Failure(cause) => diff --git a/zio-http/src/main/scala/zhttp/service/Server.scala b/zio-http/src/main/scala/zhttp/service/Server.scala index 9dc128e1d..68a9633f0 100644 --- a/zio-http/src/main/scala/zhttp/service/Server.scala +++ b/zio-http/src/main/scala/zhttp/service/Server.scala @@ -7,7 +7,7 @@ import zhttp.http.Http._ import zhttp.http.{Http, HttpApp} import zhttp.service.server.ServerSSLHandler._ import zhttp.service.server._ -import zio.{ZManaged, _} +import zio._ import java.net.{InetAddress, InetSocketAddress} @@ -36,11 +36,11 @@ sealed trait Server[-R, +E] { self => def make(implicit ev: E <:< Throwable, - ): ZManaged[R with EventLoopGroup with ServerChannelFactory, Throwable, Start] = + ): ZIO[R with EventLoopGroup with ServerChannelFactory with Scope, Throwable, Start] = Server.make(self.asInstanceOf[Server[R, Throwable]]) def start(implicit ev: E <:< Throwable): ZIO[R with EventLoopGroup with ServerChannelFactory, Throwable, Nothing] = - make.useForever + ZIO.scoped[R with EventLoopGroup with ServerChannelFactory](make *> ZIO.never) /** * Launches the app with current settings: default EventLoopGroup (nThreads = @@ -211,50 +211,42 @@ object Server { def start[R]( port: Int, http: HttpApp[R, Throwable], - ): ZIO[R, Throwable, Nothing] = { - (Server(http) - .withPort(port)) + ): ZIO[R, Throwable, Nothing] = + Server(http) + .withPort(port) .make - .flatMap(start => ZManaged.succeed(println(s"Server started on port: ${start.port}"))) - .useForever - .provideSomeLayer[R](EventLoopGroup.auto(0) ++ ServerChannelFactory.auto) - } + .flatMap(start => ZIO.succeed(println(s"Server started on port: ${start.port}")) *> ZIO.never) + .provideSomeLayer[R](EventLoopGroup.auto(0) ++ ServerChannelFactory.auto ++ Scope.default) def start[R]( address: InetAddress, port: Int, http: HttpApp[R, Throwable], ): ZIO[R, Throwable, Nothing] = - (Server(http) - .withBinding(address, port)) - .make - .useForever - .provideSomeLayer[R](EventLoopGroup.auto(0) ++ ServerChannelFactory.auto) + (Server(http).withBinding(address, port).make *> ZIO.never) + .provideSomeLayer[R](EventLoopGroup.auto(0) ++ ServerChannelFactory.auto ++ Scope.default) def start[R]( socketAddress: InetSocketAddress, http: HttpApp[R, Throwable], ): ZIO[R, Throwable, Nothing] = - (Server(http) - .withBinding(socketAddress)) - .make - .useForever - .provideSomeLayer[R](EventLoopGroup.auto(0) ++ ServerChannelFactory.auto) + (Server(http).withBinding(socketAddress).make *> ZIO.never) + .provideSomeLayer[R](EventLoopGroup.auto(0) ++ ServerChannelFactory.auto ++ Scope.default) def make[R]( server: Server[R, Throwable], - ): ZManaged[R with EventLoopGroup with ServerChannelFactory, Throwable, Start] = { + ): ZIO[R with EventLoopGroup with ServerChannelFactory with Scope, Throwable, Start] = { val settings = server.settings() for { - channelFactory <- ZManaged.service[ServerChannelFactory] - eventLoopGroup <- ZManaged.service[EventLoopGroup] - zExec <- HttpRuntime.sticky[R](eventLoopGroup).toManaged + channelFactory <- ZIO.service[ServerChannelFactory] + eventLoopGroup <- ZIO.service[EventLoopGroup] + zExec <- HttpRuntime.sticky[R](eventLoopGroup) reqHandler = settings.app.compile(zExec, settings, ServerTime.make) init = ServerChannelInitializer(zExec, settings, reqHandler) serverBootstrap = new ServerBootstrap().channelFactory(channelFactory).group(eventLoopGroup) - chf <- ZManaged.attempt(serverBootstrap.childHandler(init).bind(settings.address)) - _ <- ChannelFuture.asManaged(chf) - port <- ZManaged.attempt(chf.channel().localAddress().asInstanceOf[InetSocketAddress].getPort) + chf <- ZIO.attempt(serverBootstrap.childHandler(init).bind(settings.address)) + _ <- ChannelFuture.asZIO(chf) + port <- ZIO.attempt(chf.channel().localAddress().asInstanceOf[InetSocketAddress].getPort) } yield { ResourceLeakDetector.setLevel(settings.leakDetectionLevel.jResourceLeakDetectionLevel) Start(port) diff --git a/zio-http/src/main/scala/zhttp/service/server/content/handlers/ServerResponseHandler.scala b/zio-http/src/main/scala/zhttp/service/server/content/handlers/ServerResponseHandler.scala index 3ea49e322..2566a4ee8 100644 --- a/zio-http/src/main/scala/zhttp/service/server/content/handlers/ServerResponseHandler.scala +++ b/zio-http/src/main/scala/zhttp/service/server/content/handlers/ServerResponseHandler.scala @@ -6,8 +6,8 @@ import io.netty.handler.codec.http._ import zhttp.http.{HttpData, Response} import zhttp.service.server.ServerTime import zhttp.service.{ChannelFuture, HttpRuntime, Server} +import zio.ZIO import zio.stream.ZStream -import zio.{UIO, ZIO} import java.io.File @@ -105,7 +105,7 @@ private[zhttp] trait ServerResponseHandler[R] { case HttpData.BinaryStream(stream) => rt.unsafeRun(ctx) { - writeStreamContent(stream).ensuring(UIO(releaseAndRead(jReq))) + writeStreamContent(stream).ensuring(ZIO.succeed(releaseAndRead(jReq))) } case HttpData.JavaFile(unsafeGet) => @@ -121,7 +121,7 @@ private[zhttp] trait ServerResponseHandler[R] { stream: ZStream[R, Throwable, ByteBuf], )(implicit ctx: Ctx): ZIO[R, Throwable, Unit] = { for { - _ <- stream.foreach(c => UIO(ctx.writeAndFlush(c))) + _ <- stream.foreach(c => ZIO.succeed(ctx.writeAndFlush(c))) _ <- ChannelFuture.unit(ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT)) } yield () } diff --git a/zio-http/src/main/scala/zhttp/socket/Socket.scala b/zio-http/src/main/scala/zhttp/socket/Socket.scala index 31d5f9991..8c7396a9c 100644 --- a/zio-http/src/main/scala/zhttp/socket/Socket.scala +++ b/zio-http/src/main/scala/zhttp/socket/Socket.scala @@ -3,7 +3,7 @@ package zhttp.socket import zhttp.http.{Http, Response} import zhttp.service.{ChannelFactory, EventLoopGroup} import zio.stream.ZStream -import zio.{Cause, NeedsEnv, ZEnvironment, ZIO} +import zio.{Cause, ZEnvironment, ZIO} sealed trait Socket[-R, +E, -A, +B] { self => import Socket._ @@ -50,7 +50,7 @@ sealed trait Socket[-R, +E, -A, +B] { self => * dependency on R. This operation assumes that your socket requires an * environment. */ - def provideEnvironment(r: ZEnvironment[R])(implicit env: NeedsEnv[R]): Socket[Any, E, A, B] = + def provideEnvironment(r: ZEnvironment[R]): Socket[Any, E, A, B] = ProvideEnvironment(self, r) /** diff --git a/zio-http/src/main/scala/zhttp/socket/SocketApp.scala b/zio-http/src/main/scala/zhttp/socket/SocketApp.scala index aade14133..898f2b01c 100644 --- a/zio-http/src/main/scala/zhttp/socket/SocketApp.scala +++ b/zio-http/src/main/scala/zhttp/socket/SocketApp.scala @@ -82,7 +82,7 @@ final case class SocketApp[-R]( * Provides the socket app with its required environment, which eliminates its * dependency on `R`. */ - def provideEnvironment(env: ZEnvironment[R])(implicit ev: NeedsEnv[R]): SocketApp[Any] = + def provideEnvironment(env: ZEnvironment[R]): SocketApp[Any] = self.copy( timeout = self.timeout.map(_.provideEnvironment(env)), open = self.open.map(_.provideEnvironment(env)), @@ -134,7 +134,7 @@ object SocketApp { } } - def provideEnvironment(r: ZEnvironment[R])(implicit ev: NeedsEnv[R]): Handle[Any] = + def provideEnvironment(r: ZEnvironment[R]): Handle[Any] = self match { case WithEffect(f) => WithEffect(c => f(c).provideEnvironment(r)) case WithSocket(s) => WithSocket(s.provideEnvironment(r)) diff --git a/zio-http/src/test/scala/zhttp/endpoint/EndpointSpec.scala b/zio-http/src/test/scala/zhttp/endpoint/EndpointSpec.scala index 2efbfe432..00795a2a2 100644 --- a/zio-http/src/test/scala/zhttp/endpoint/EndpointSpec.scala +++ b/zio-http/src/test/scala/zhttp/endpoint/EndpointSpec.scala @@ -1,7 +1,7 @@ package zhttp.endpoint import zhttp.http._ -import zio.UIO +import zio.ZIO import zio.test.Assertion._ import zio.test.{DefaultRunnableSpec, assert, assertM} @@ -70,7 +70,7 @@ object EndpointSpec extends DefaultRunnableSpec { assertM(app(Request(url = URL(!! / "b"))).flip)(isNone) } + test("endpoint with effect doesn't match") { - val app = Method.GET / "a" to { _ => UIO(Response.ok) } + val app = Method.GET / "a" to { _ => ZIO.succeed(Response.ok) } assertM(app(Request(url = URL(!! / "b"))).flip)(isNone) } + test("endpoint matches") { @@ -78,7 +78,7 @@ object EndpointSpec extends DefaultRunnableSpec { assertM(app(Request(url = URL(!! / "a"))).map(_.status))(equalTo(Status.Ok)) } + test("endpoint with effect matches") { - val app = Method.GET / "a" to { _ => UIO(Response.ok) } + val app = Method.GET / "a" to { _ => ZIO.succeed(Response.ok) } assertM(app(Request(url = URL(!! / "a"))).map(_.status))(equalTo(Status.Ok)) } } diff --git a/zio-http/src/test/scala/zhttp/http/ContentTypeSpec.scala b/zio-http/src/test/scala/zhttp/http/ContentTypeSpec.scala index 17c7d3edc..157576c4b 100644 --- a/zio-http/src/test/scala/zhttp/http/ContentTypeSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/ContentTypeSpec.scala @@ -3,10 +3,10 @@ package zhttp.http import zhttp.internal.{DynamicServer, HttpRunnableSpec} import zhttp.service.server.ServerChannelFactory import zhttp.service.{ChannelFactory, EventLoopGroup} -import zio.durationInt import zio.test.Assertion.{equalTo, isNone, isSome} import zio.test.TestAspect.timeout import zio.test.assertM +import zio.{Scope, durationInt} object ContentTypeSpec extends HttpRunnableSpec { @@ -42,11 +42,12 @@ object ContentTypeSpec extends HttpRunnableSpec { } } - private val env = EventLoopGroup.nio() ++ ChannelFactory.nio ++ ServerChannelFactory.nio ++ DynamicServer.live + private val env = + EventLoopGroup.nio() ++ ChannelFactory.nio ++ ServerChannelFactory.nio ++ DynamicServer.live ++ Scope.default override def spec = { suite("Content-type") { - serve(DynamicServer.app).as(List(contentSpec)).useNow + serve(DynamicServer.app).as(List(contentSpec)) }.provideCustomLayerShared(env) @@ timeout(5 seconds) } } diff --git a/zio-http/src/test/scala/zhttp/http/HExitSpec.scala b/zio-http/src/test/scala/zhttp/http/HExitSpec.scala index 80856b189..2b20de5d5 100644 --- a/zio-http/src/test/scala/zhttp/http/HExitSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/HExitSpec.scala @@ -3,7 +3,7 @@ package zhttp.http import zio.test.Assertion._ import zio.test.TestAspect._ import zio.test._ -import zio.{UIO, durationInt} +import zio.{ZIO, durationInt} object HExitSpec extends DefaultRunnableSpec with HExitAssertion { def spec: ZSpec[Environment, Failure] = { @@ -13,7 +13,7 @@ object HExitSpec extends DefaultRunnableSpec with HExitAssertion { empty === isEmpty && succeed(1) === isSuccess(equalTo(1)) && fail(1) === isFailure(equalTo(1)) && - fromZIO(UIO(1)) === isEffect + fromZIO(ZIO.succeed(1)) === isEffect } + test("flatMapError") { succeed(0) *> fail(1) <> fail(2) === isFailure(equalTo(2)) && @@ -45,9 +45,9 @@ object HExitSpec extends DefaultRunnableSpec with HExitAssertion { empty <+> empty === isEmpty } + test("effect") { - fromZIO(UIO(1)) <+> empty === isEffect && - empty <+> fromZIO(UIO(1)) === isEffect && - empty *> fromZIO(UIO(1)) *> fromZIO(UIO(1)) === isEmpty + fromZIO(ZIO.succeed(1)) <+> empty === isEffect && + empty <+> fromZIO(ZIO.succeed(1)) === isEffect && + empty *> fromZIO(ZIO.succeed(1)) *> fromZIO(ZIO.succeed(1)) === isEmpty } + test("nested succeed") { empty <+> succeed(1) <+> succeed(2) === isSuccess(equalTo(1)) && diff --git a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala index bf101cdaa..ab104807f 100644 --- a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala @@ -229,17 +229,17 @@ object HttpSpec extends DefaultRunnableSpec with HExitAssertion { ) + suite("collectM")( test("should be empty") { - val a = Http.collectZIO[Int] { case 1 => UIO("A") } + val a = Http.collectZIO[Int] { case 1 => ZIO.succeed("A") } val actual = a.execute(2) assert(actual)(isEmpty) } + test("should resolve") { - val a = Http.collectZIO[Int] { case 1 => UIO("A") } + val a = Http.collectZIO[Int] { case 1 => ZIO.succeed("A") } val actual = a.execute(1) assert(actual)(isEffect) } + - test("should resolve managed") { - val a = Http.collectManaged[Int] { case 1 => ZManaged.succeed("A") } + test("should resolve scoped") { + val a = Http.collectScoped[Int] { case 1 => ZIO.succeed("A") } val actual = a.execute(1) assert(actual)(isEffect) } + @@ -387,11 +387,11 @@ object HttpSpec extends DefaultRunnableSpec with HExitAssertion { assertM(http(()))(equalTo(1)) } + test("sync right wins") { - val http = Http.fromZIO(UIO(1)) race Http.succeed(2) + val http = Http.fromZIO(ZIO.succeed(1)) race Http.succeed(2) assertM(http(()))(equalTo(2)) } + test("sync left wins") { - val http = Http.succeed(1) race Http.fromZIO(UIO(2)) + val http = Http.succeed(1) race Http.fromZIO(ZIO.succeed(2)) assertM(http(()))(equalTo(1)) } + test("async fast wins") { diff --git a/zio-http/src/test/scala/zhttp/http/MiddlewareSpec.scala b/zio-http/src/test/scala/zhttp/http/MiddlewareSpec.scala index 75b67c7fd..46e1f0fe7 100644 --- a/zio-http/src/test/scala/zhttp/http/MiddlewareSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/MiddlewareSpec.scala @@ -25,7 +25,7 @@ object MiddlewareSpec extends DefaultRunnableSpec with HExitAssertion { test("interceptZIO") { for { ref <- Ref.make(0) - mid = Middleware.interceptZIO[Int, Int](i => UIO(i * 10))((i, j) => ref.set(i + j)) + mid = Middleware.interceptZIO[Int, Int](i => ZIO.succeed(i * 10))((i, j) => ref.set(i + j)) app = Http.identity[Int] @@ mid _ <- app(1) i <- ref.get @@ -49,7 +49,7 @@ object MiddlewareSpec extends DefaultRunnableSpec with HExitAssertion { assertM(app(0))(equalTo(3)) } + test("mapZIO") { - val mid = increment.mapZIO(i => UIO(i + 1)) + val mid = increment.mapZIO(i => ZIO.succeed(i + 1)) val app = Http.identity[Int] @@ mid assertM(app(0))(equalTo(3)) } + @@ -88,7 +88,7 @@ object MiddlewareSpec extends DefaultRunnableSpec with HExitAssertion { } } + suite("ifThenElseZIO") { - val mid = Middleware.ifThenElseZIO[Int](i => UIO(i > 5))( + val mid = Middleware.ifThenElseZIO[Int](i => ZIO.succeed(i > 5))( isTrue = i => Middleware.succeed(i + 1), isFalse = i => Middleware.succeed(i - 1), ) @@ -108,7 +108,7 @@ object MiddlewareSpec extends DefaultRunnableSpec with HExitAssertion { assertM(app(0))(equalTo("0Foo0FooBar")) } + test("contramapZIO") { - val app = Http.identity[String] @@ mid.contramapZIO[Int] { i => UIO(s"${i}Foo") } + val app = Http.identity[String] @@ mid.contramapZIO[Int] { i => ZIO.succeed(s"${i}Foo") } assertM(app(0))(equalTo("0Foo0FooBar")) } } + @@ -126,11 +126,11 @@ object MiddlewareSpec extends DefaultRunnableSpec with HExitAssertion { suite("whenZIO") { val mid = Middleware.succeed(0) test("condition is true") { - val app = Http.identity[Int] @@ mid.whenZIO[Any, Nothing, Int](_ => UIO(true)) + val app = Http.identity[Int] @@ mid.whenZIO[Any, Nothing, Int](_ => ZIO.succeed(true)) assertM(app(10))(equalTo(0)) } + test("condition is false") { - val app = Http.identity[Int] @@ mid.whenZIO[Any, Nothing, Int](_ => UIO(false)) + val app = Http.identity[Int] @@ mid.whenZIO[Any, Nothing, Int](_ => ZIO.succeed(false)) assertM(app(1))(equalTo(1)) } } + diff --git a/zio-http/src/test/scala/zhttp/http/middleware/AuthSpec.scala b/zio-http/src/test/scala/zhttp/http/middleware/AuthSpec.scala index abd8f4c7a..daddcc2b2 100644 --- a/zio-http/src/test/scala/zhttp/http/middleware/AuthSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/middleware/AuthSpec.scala @@ -2,7 +2,7 @@ package zhttp.http.middleware import zhttp.http._ import zhttp.internal.HttpAppTestExtensions -import zio.UIO +import zio.ZIO import zio.test.Assertion._ import zio.test._ @@ -17,13 +17,13 @@ object AuthSpec extends DefaultRunnableSpec with HttpAppTestExtensions { c.uname.reverse == c.upassword } private val basicAuthZIOM: HttpMiddleware[Any, Nothing] = Middleware.basicAuthZIO { c => - UIO(c.uname.reverse == c.upassword) + ZIO.succeed(c.uname.reverse == c.upassword) } private val bearerAuthM: HttpMiddleware[Any, Nothing] = Middleware.bearerAuth { c => c == bearerToken } private val bearerAuthZIOM: HttpMiddleware[Any, Nothing] = Middleware.bearerAuthZIO { c => - UIO(c == bearerToken) + ZIO.succeed(c == bearerToken) } def spec = suite("AuthSpec") { diff --git a/zio-http/src/test/scala/zhttp/http/middleware/WebSpec.scala b/zio-http/src/test/scala/zhttp/http/middleware/WebSpec.scala index b1a2fdeec..9c8ca8104 100644 --- a/zio-http/src/test/scala/zhttp/http/middleware/WebSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/middleware/WebSpec.scala @@ -9,7 +9,7 @@ import zio.test._ object WebSpec extends DefaultRunnableSpec with HttpAppTestExtensions { self => private val app = Http.collectZIO[Request] { case Method.GET -> !! / "health" => - UIO(Response.ok).delay(1 second) + ZIO.succeed(Response.ok).delay(1 second) } private val midA = Middleware.addHeader("X-Custom", "A") private val midB = Middleware.addHeader("X-Custom", "B") @@ -59,11 +59,11 @@ object WebSpec extends DefaultRunnableSpec with HttpAppTestExtensions { self => } + suite("whenZIO") { test("condition is true") { - val program = runApp(self.app @@ debug.whenZIO(_ => UIO(true))) *> TestConsole.output + val program = runApp(self.app @@ debug.whenZIO(_ => ZIO.succeed(true))) *> TestConsole.output assertM(program)(equalTo(Vector("200 GET /health 1000ms\n"))) } + test("condition is false") { - val log = runApp(self.app @@ debug.whenZIO(_ => UIO(false))) *> TestConsole.output + val log = runApp(self.app @@ debug.whenZIO(_ => ZIO.succeed(false))) *> TestConsole.output assertM(log)(equalTo(Vector())) } } + @@ -146,7 +146,7 @@ object WebSpec extends DefaultRunnableSpec with HttpAppTestExtensions { self => test("addCookieM") { val cookie = Cookie("test", "testValue") val app = - (Http.ok @@ addCookieZIO(UIO(cookie))).header("set-cookie") + (Http.ok @@ addCookieZIO(ZIO.succeed(cookie))).header("set-cookie") assertM(app(Request()))( equalTo(Some(cookie.encode)), ) @@ -167,7 +167,7 @@ object WebSpec extends DefaultRunnableSpec with HttpAppTestExtensions { self => private def cond(flg: Boolean) = (_: Any) => flg - private def condM(flg: Boolean) = (_: Any) => UIO(flg) + private def condM(flg: Boolean) = (_: Any) => ZIO.succeed(flg) private def runApp[R, E](app: HttpApp[R, E]): ZIO[TestClock with R, Option[E], Response] = { for { diff --git a/zio-http/src/test/scala/zhttp/internal/DynamicServer.scala b/zio-http/src/test/scala/zhttp/internal/DynamicServer.scala index 4c13302ad..888e5a253 100644 --- a/zio-http/src/test/scala/zhttp/internal/DynamicServer.scala +++ b/zio-http/src/test/scala/zhttp/internal/DynamicServer.scala @@ -16,7 +16,7 @@ object DynamicServer { .fromOptionFunction[Request] { req => for { id <- req.headerValue(APP_ID) match { - case Some(id) => UIO(id) + case Some(id) => ZIO.succeed(id) case None => ZIO.fail(None) } app <- get(id) @@ -71,7 +71,7 @@ object DynamicServer { final class Live(ref: Ref[Map[Id, HttpApp[Any, Throwable]]], pr: Promise[Nothing, Start]) extends Service { def add(app: HttpApp[Any, Throwable]): UIO[Id] = for { - id <- UIO(UUID.randomUUID().toString) + id <- ZIO.succeed(UUID.randomUUID().toString) _ <- ref.update(map => map + (id -> app)) } yield id @@ -79,7 +79,7 @@ object DynamicServer { def port: ZIO[Any, Nothing, Int] = start.map(_.port) - def setStart(s: Start): UIO[Boolean] = pr.complete(ZIO(s).orDie) + def setStart(s: Start): UIO[Boolean] = pr.complete(ZIO.attempt(s).orDie) def start: IO[Nothing, Start] = pr.await } diff --git a/zio-http/src/test/scala/zhttp/internal/HttpRunnableSpec.scala b/zio-http/src/test/scala/zhttp/internal/HttpRunnableSpec.scala index 0809a114c..ed9a0f42c 100644 --- a/zio-http/src/test/scala/zhttp/internal/HttpRunnableSpec.scala +++ b/zio-http/src/test/scala/zhttp/internal/HttpRunnableSpec.scala @@ -7,7 +7,7 @@ import zhttp.service._ import zhttp.service.client.ClientSSLHandler.ClientSSLOptions import zhttp.socket.SocketApp import zio.test.DefaultRunnableSpec -import zio.{ZIO, ZManaged} +import zio.{Scope, ZIO} /** * Should be used only when e2e tests needs to be written. Typically we would @@ -88,12 +88,12 @@ abstract class HttpRunnableSpec extends DefaultRunnableSpec { self => def serve[R]( app: HttpApp[R, Throwable], server: Option[Server[R, Throwable]] = None, - ): ZManaged[R with EventLoopGroup with ServerChannelFactory with DynamicServer, Nothing, Unit] = + ): ZIO[R with EventLoopGroup with ServerChannelFactory with DynamicServer with Scope, Nothing, Unit] = for { - settings <- ZManaged + settings <- ZIO .succeed(server.foldLeft(Server.app(app) ++ Server.port(0) ++ Server.paranoidLeakDetection)(_ ++ _)) start <- Server.make(settings).orDie - _ <- DynamicServer.setStart(start).toManaged + _ <- DynamicServer.setStart(start) } yield () def status( diff --git a/zio-http/src/test/scala/zhttp/service/ClientSpec.scala b/zio-http/src/test/scala/zhttp/service/ClientSpec.scala index 6b82b8b61..7422bcced 100644 --- a/zio-http/src/test/scala/zhttp/service/ClientSpec.scala +++ b/zio-http/src/test/scala/zhttp/service/ClientSpec.scala @@ -3,17 +3,17 @@ package zhttp.service import zhttp.http._ import zhttp.internal.{DynamicServer, HttpRunnableSpec} import zhttp.service.server._ -import zio.durationInt import zio.test.Assertion._ import zio.test.TestAspect.{sequential, timeout} import zio.test._ +import zio.{Scope, durationInt} import java.net.ConnectException object ClientSpec extends HttpRunnableSpec { private val env = - EventLoopGroup.nio() ++ ChannelFactory.nio ++ ServerChannelFactory.nio ++ DynamicServer.live + EventLoopGroup.nio() ++ ChannelFactory.nio ++ ServerChannelFactory.nio ++ DynamicServer.live ++ Scope.default def clientSpec = suite("ClientSpec") { test("respond Ok") { @@ -48,7 +48,7 @@ object ClientSpec extends HttpRunnableSpec { override def spec = { suite("Client") { - serve(DynamicServer.app).as(List(clientSpec)).useNow + serve(DynamicServer.app).as(List(clientSpec)) }.provideCustomLayerShared(env) @@ timeout(5 seconds) @@ sequential } } diff --git a/zio-http/src/test/scala/zhttp/service/KeepAliveSpec.scala b/zio-http/src/test/scala/zhttp/service/KeepAliveSpec.scala index 34a846631..d56037d3e 100644 --- a/zio-http/src/test/scala/zhttp/service/KeepAliveSpec.scala +++ b/zio-http/src/test/scala/zhttp/service/KeepAliveSpec.scala @@ -4,17 +4,18 @@ import io.netty.handler.codec.http.HttpHeaderValues import zhttp.http.{HeaderNames, Headers, Http, Version} import zhttp.internal.{DynamicServer, HttpRunnableSpec} import zhttp.service.server._ -import zio.durationInt import zio.test.Assertion.{equalTo, isNone, isSome} import zio.test.TestAspect.timeout import zio.test.assertM +import zio.{Scope, durationInt} object KeepAliveSpec extends HttpRunnableSpec { - val app = Http.ok - val connectionCloseHeader = Headers.connection(HttpHeaderValues.CLOSE) - val keepAliveHeader = Headers.connection(HttpHeaderValues.KEEP_ALIVE) - private val env = EventLoopGroup.nio() ++ ChannelFactory.nio ++ ServerChannelFactory.nio ++ DynamicServer.live + val app = Http.ok + val connectionCloseHeader = Headers.connection(HttpHeaderValues.CLOSE) + val keepAliveHeader = Headers.connection(HttpHeaderValues.KEEP_ALIVE) + private val env = + EventLoopGroup.nio() ++ ChannelFactory.nio ++ ServerChannelFactory.nio ++ DynamicServer.live ++ Scope.default private val appKeepAliveEnabled = serve(DynamicServer.app) def keepAliveSpec = suite("KeepAlive") { @@ -44,7 +45,7 @@ object KeepAliveSpec extends HttpRunnableSpec { override def spec = { suite("ServerConfigSpec") { - appKeepAliveEnabled.as(List(keepAliveSpec)).useNow + appKeepAliveEnabled.as(List(keepAliveSpec)) }.provideCustomLayerShared(env) @@ timeout(30.seconds) } diff --git a/zio-http/src/test/scala/zhttp/service/SSLSpec.scala b/zio-http/src/test/scala/zhttp/service/SSLSpec.scala index f5561bada..afdacb3ac 100644 --- a/zio-http/src/test/scala/zhttp/service/SSLSpec.scala +++ b/zio-http/src/test/scala/zhttp/service/SSLSpec.scala @@ -12,7 +12,7 @@ import zio.test.TestAspect.{ignore, timeout} import zio.test.{DefaultRunnableSpec, Gen, assertM, checkAll} object SSLSpec extends DefaultRunnableSpec { - val env = EventLoopGroup.auto() ++ ChannelFactory.auto ++ ServerChannelFactory.auto + val env = EventLoopGroup.auto() ++ ChannelFactory.auto ++ ServerChannelFactory.auto ++ Scope.default val serverSSL = ctxFromCert( getClass().getClassLoader().getResourceAsStream("server.crt"), @@ -80,7 +80,6 @@ object SSLSpec extends DefaultRunnableSpec { } }, ), - ) - .useNow, + ), ).provideCustomLayer(env) @@ timeout(5 second) @@ ignore } diff --git a/zio-http/src/test/scala/zhttp/service/ServerSpec.scala b/zio-http/src/test/scala/zhttp/service/ServerSpec.scala index 6e782e96d..d54006c00 100644 --- a/zio-http/src/test/scala/zhttp/service/ServerSpec.scala +++ b/zio-http/src/test/scala/zhttp/service/ServerSpec.scala @@ -288,8 +288,8 @@ object ServerSpec extends HttpRunnableSpec { override def spec = suite("Server") { val spec = dynamicAppSpec + responseSpec + requestSpec + requestBodySpec + serverErrorSpec - suite("app without request streaming") { app.as(List(spec)).useNow } + - suite("app with request streaming") { appWithReqStreaming.as(List(spec)).useNow } + suite("app without request streaming") { ZIO.scoped(app.as(List(spec))) } + + suite("app with request streaming") { ZIO.scoped(appWithReqStreaming.as(List(spec))) } }.provideCustomLayerShared(env) @@ timeout(30 seconds) @@ sequential } diff --git a/zio-http/src/test/scala/zhttp/service/StaticFileServerSpec.scala b/zio-http/src/test/scala/zhttp/service/StaticFileServerSpec.scala index 9ed00ff8e..184a8416f 100644 --- a/zio-http/src/test/scala/zhttp/service/StaticFileServerSpec.scala +++ b/zio-http/src/test/scala/zhttp/service/StaticFileServerSpec.scala @@ -3,20 +3,20 @@ package zhttp.service import zhttp.http._ import zhttp.internal.{DynamicServer, HttpRunnableSpec} import zhttp.service.server._ -import zio.durationInt import zio.test.Assertion.{equalTo, isSome} import zio.test.TestAspect.timeout import zio.test.assertM +import zio.{Scope, durationInt} import java.io.File object StaticFileServerSpec extends HttpRunnableSpec { private val env = - EventLoopGroup.nio() ++ ChannelFactory.nio ++ ServerChannelFactory.nio ++ DynamicServer.live + EventLoopGroup.nio() ++ ChannelFactory.nio ++ ServerChannelFactory.nio ++ DynamicServer.live ++ Scope.default override def spec = suite("StaticFileServer") { - serve(DynamicServer.app).as(List(staticSpec)).useNow + serve(DynamicServer.app).as(List(staticSpec)) }.provideCustomLayerShared(env) @@ timeout(5 seconds) private def staticSpec = suite("Static RandomAccessFile Server") { diff --git a/zio-http/src/test/scala/zhttp/service/StaticServerSpec.scala b/zio-http/src/test/scala/zhttp/service/StaticServerSpec.scala index 776efb2bf..0ceafc228 100644 --- a/zio-http/src/test/scala/zhttp/service/StaticServerSpec.scala +++ b/zio-http/src/test/scala/zhttp/service/StaticServerSpec.scala @@ -6,12 +6,12 @@ import zhttp.service.server._ import zio.test.Assertion._ import zio.test.TestAspect._ import zio.test._ -import zio.{ZIO, durationInt} +import zio.{Scope, ZIO, durationInt} object StaticServerSpec extends HttpRunnableSpec { private val env = - EventLoopGroup.nio() ++ ChannelFactory.nio ++ ServerChannelFactory.nio ++ DynamicServer.live + EventLoopGroup.nio() ++ ChannelFactory.nio ++ ServerChannelFactory.nio ++ DynamicServer.live ++ Scope.default private val staticApp = Http.collectZIO[Request] { case Method.GET -> !! / "success" => ZIO.succeed(Response.ok) @@ -66,13 +66,17 @@ object StaticServerSpec extends HttpRunnableSpec { def serverStartSpec = suite("ServerStartSpec") { test("desired port") { val port = 8088 - (Server.port(port) ++ Server.app(Http.empty)).make.use { start => - assertM(ZIO.attempt(start.port))(equalTo(port)) + ZIO.scoped { + (Server.port(port) ++ Server.app(Http.empty)).make.flatMap { start => + assertM(ZIO.attempt(start.port))(equalTo(port)) + } } } + test("available port") { - (Server.port(0) ++ Server.app(Http.empty)).make.use { start => - assertM(ZIO.attempt(start.port))(not(equalTo(0))) + ZIO.scoped { + (Server.port(0) ++ Server.app(Http.empty)).make.flatMap { start => + assertM(ZIO.attempt(start.port))(not(equalTo(0))) + } } } } @@ -83,7 +87,6 @@ object StaticServerSpec extends HttpRunnableSpec { .as( List(serverStartSpec, staticAppSpec, nonZIOSpec, throwableAppSpec), ) - .useNow }.provideCustomLayerShared(env) @@ timeout(30 seconds) def staticAppSpec = suite("StaticAppSpec") { diff --git a/zio-http/src/test/scala/zhttp/service/WebSocketServerSpec.scala b/zio-http/src/test/scala/zhttp/service/WebSocketServerSpec.scala index c3394f48f..3a1e17d36 100644 --- a/zio-http/src/test/scala/zhttp/service/WebSocketServerSpec.scala +++ b/zio-http/src/test/scala/zhttp/service/WebSocketServerSpec.scala @@ -13,11 +13,11 @@ import zio.{Chunk, ZIO, _} object WebSocketServerSpec extends HttpRunnableSpec { private val env = - EventLoopGroup.nio() ++ ServerChannelFactory.nio ++ DynamicServer.live ++ ChannelFactory.nio + EventLoopGroup.nio() ++ ServerChannelFactory.nio ++ DynamicServer.live ++ ChannelFactory.nio ++ Scope.default private val app = serve { DynamicServer.app } override def spec = suite("Server") { - app.as(List(websocketServerSpec, websocketFrameSpec)).useNow + app.as(List(websocketServerSpec, websocketFrameSpec)) }.provideCustomLayerShared(env) @@ timeout(10 seconds) def websocketServerSpec = suite("WebSocketServer") {