Skip to content

Commit

Permalink
Fix Scoping Of Test Resources (#2407)
Browse files Browse the repository at this point in the history
* investigate ci flakiness

* provide per test scope

* provide per test scope

* format

* provide per test scope

* provide per test scope

* provide per test scope

* provide per test scope

* provide per test scope

* update example

* cleanup

* rename

* cleanup

* enable web socket spec

* revert

* cleanup

* cleanup

* endpoint spec

* revert

* fix

* client https spec

* client streaming spec

* endpoint spec cleanup

* cleanup

* cleanup

* fix

* client streaming spec

* ssl spec
  • Loading branch information
adamgfraser authored Aug 29, 2023
1 parent ca6513f commit 25bc720
Show file tree
Hide file tree
Showing 30 changed files with 83 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private[cli] final case class CliRequest(
}
for {

forms <- ZIO.foreach(body)(_.retrieve()).provideSome(clientLayer)
forms <- ZIO.foreach(body)(_.retrieve()).provide(clientLayer)
finalBody <- Body.fromMultipartFormUUID(Form(forms))
} yield Request(method = method, url = url.host(host).port(port), body = finalBody, headers = headers)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ object HttpCliApp {
for {
request <- req.toRequest(host, port, client)
response <- client match {
case CliZIOClient(client) => client.request(request).provideSome(Scope.default)
case CliZLayerClient(client) => Client.request(request).provideSome(Scope.default, client)
case DefaultClient() => Client.request(request).provideSome(Scope.default, Client.default)
case CliZIOClient(client) => client.request(request).provide(Scope.default)
case CliZLayerClient(client) => Client.request(request).provide(Scope.default, client)
case DefaultClient() => Client.request(request).provide(Scope.default, Client.default)
}

_ <- Console.printLine(s"Got response")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private[cli] object Retriever {
lazy val request = Request.get(http.URL(http.Path.decode(url)))
override def retrieve(): ZIO[Client, Throwable, FormField] = for {
client <- ZIO.service[Client]
response <- client.request(request).provideSome(Scope.default)
response <- client.request(request).provide(Scope.default)
chunk <- response.body.asChunk
} yield FormField.binaryField(name, chunk, media)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ object CliSpec extends ZIOSpecDefault {
}
}
},
) @@ timeout(60.second),
),
suite("Correct behaviour of CliApp")(
test("Simple endpoint") {
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ object WebSocketSimpleClient extends ZIOAppDefault {
val app: ZIO[Client with Scope, Throwable, Response] =
socketApp.connect(url) *> ZIO.never

val run: ZIO[ZIOAppArgs with Scope, Throwable, Any] =
ZIO.scoped {
app.provideSome[Scope](Client.default)
}
val run: ZIO[Any, Throwable, Any] =
app.provide(Client.default, Scope.default)

}
7 changes: 4 additions & 3 deletions zio-http-testkit/src/test/scala/zio/http/TestServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ object TestServerSpec extends ZIOHttpSpec {
)
} yield assertTrue(status(response1) == Status.Ok) &&
assertTrue(status(response2) == Status.InternalServerError)
}.provideSome[Client with Driver with Scope](
}.provideSome[Client with Driver](
TestServer.layer,
Scope.default,
),
suite("Exact Request=>Response version")(
test("matches") {
Expand Down Expand Up @@ -85,14 +86,14 @@ object TestServerSpec extends ZIOHttpSpec {
} yield assertTrue(status(finalResponse) == Status.NotFound)
},
)
.provideSome[Client with Driver with Scope](
.provideSome[Client with Driver](
TestServer.layer,
Scope.default,
),
).provide(
ZLayer.succeed(Server.Config.default.onAnyOpenPort),
Client.default,
NettyDriver.live,
Scope.default,
)

private def requestToCorrectPort =
Expand Down
2 changes: 1 addition & 1 deletion zio-http/src/main/scala/zio/http/DnsResolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ object DnsResolver {
}
}

lazy val live: ZLayer[DnsResolver.Config, Nothing, DnsResolver] = {
val live: ZLayer[DnsResolver.Config, Nothing, DnsResolver] = {
implicit val trace: Trace = Trace.empty

ZLayer.scoped {
Expand Down
2 changes: 1 addition & 1 deletion zio-http/src/main/scala/zio/http/Driver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ trait Driver {
object Driver {
final case class StartResult(port: Int, inFlightRequests: LongAdder)

def default: ZLayer[Server.Config, Throwable, Driver] =
val default: ZLayer[Server.Config, Throwable, Driver] =
NettyDriver.live
}
2 changes: 1 addition & 1 deletion zio-http/src/main/scala/zio/http/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ object Server {
ZLayer.succeed(Config.default) >>> live
}

lazy val live: ZLayer[Config, Throwable, Server] = {
val live: ZLayer[Config, Throwable, Server] = {
implicit val trace: Trace = Trace.empty
NettyDriver.live >+> base
}
Expand Down
2 changes: 1 addition & 1 deletion zio-http/src/main/scala/zio/http/ZClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ object ZClient {
driver,
)

lazy val live: ZLayer[ZClient.Config with NettyConfig with DnsResolver, Throwable, Client] = {
val live: ZLayer[ZClient.Config with NettyConfig with DnsResolver, Throwable, Client] = {
implicit val trace: Trace = Trace.empty
(NettyClientDriver.live ++ ZLayer.service[DnsResolver]) >>> customized
}.fresh
Expand Down
4 changes: 2 additions & 2 deletions zio-http/src/main/scala/zio/http/netty/server/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ package object server {
private[server] type AppRef = AtomicReference[(HttpApp[Any], ZEnvironment[Any])]
private[server] type EnvRef = AtomicReference[ZEnvironment[Any]]

def live: ZLayer[Server.Config, Throwable, Driver] =
val live: ZLayer[Server.Config, Throwable, Driver] =
NettyDriver.live

def manual: ZLayer[EventLoopGroup & ChannelFactory[ServerChannel] & Server.Config & NettyConfig, Nothing, Driver] =
val manual: ZLayer[EventLoopGroup & ChannelFactory[ServerChannel] & Server.Config & NettyConfig, Nothing, Driver] =
NettyDriver.manual
}
2 changes: 1 addition & 1 deletion zio-http/src/test/scala/zio/http/BodySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ object BodySpec extends ZIOHttpSpec {
),
),
),
) @@ timeout(10 seconds)
)
}
6 changes: 2 additions & 4 deletions zio-http/src/test/scala/zio/http/ClientHttpsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object ClientHttpsSpec extends ZIOHttpSpec {
)
.map(_.status)
assertZIO(actual)(equalTo(Status.BadRequest))
},
} @@ ignore,
test("should throw DecoderException for handshake failure") {
val actual = Client
.request(
Expand All @@ -77,7 +77,5 @@ object ClientHttpsSpec extends ZIOHttpSpec {
DnsResolver.default,
ZLayer.succeed(NettyConfig.default),
Scope.default,
) @@ timeout(
30 seconds,
) @@ ignore
)
}
5 changes: 2 additions & 3 deletions zio-http/src/test/scala/zio/http/ClientProxySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import zio.test.TestAspect.{sequential, timeout, withLiveClock}
import zio.test._
import zio.{Scope, ZIO, ZLayer, durationInt}

import zio.http.internal.{DynamicServer, HttpRunnableSpec, severTestLayer}
import zio.http.internal.{DynamicServer, HttpRunnableSpec, serverTestLayer}
import zio.http.netty.NettyConfig
import zio.http.netty.client.NettyClientDriver

Expand Down Expand Up @@ -109,6 +109,5 @@ object ClientProxySpec extends HttpRunnableSpec {

override def spec: Spec[TestEnvironment with Scope, Any] = suite("ClientProxy") {
serve.as(List(clientProxySpec))
}.provideShared(DynamicServer.live, severTestLayer) @@
timeout(5 seconds) @@ sequential @@ withLiveClock
}.provideShared(DynamicServer.live, serverTestLayer) @@ sequential @@ withLiveClock
}
6 changes: 3 additions & 3 deletions zio-http/src/test/scala/zio/http/ClientSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import zio.test._

import zio.stream.ZStream

import zio.http.internal.{DynamicServer, HttpRunnableSpec, severTestLayer}
import zio.http.internal.{DynamicServer, HttpRunnableSpec, serverTestLayer}

object ClientSpec extends HttpRunnableSpec {

Expand Down Expand Up @@ -91,7 +91,7 @@ object ClientSpec extends HttpRunnableSpec {
override def spec = {
suite("Client") {
serve.as(List(clientSpec))
}.provideSomeShared[Scope](DynamicServer.live, severTestLayer, Client.default) @@
timeout(5 seconds) @@ sequential @@ withLiveClock
}.provideSome[DynamicServer & Server & Client](Scope.default)
.provideShared(DynamicServer.live, serverTestLayer, Client.default) @@ sequential @@ withLiveClock
}
}
6 changes: 3 additions & 3 deletions zio-http/src/test/scala/zio/http/ClientStreamingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ object ClientStreamingSpec extends HttpRunnableSpec {
)
}
} yield result
} @@ timeout(5.minutes) @@ samples(50) @@ TestAspect.ifEnvNotSet("CI"), // NOTE: random hangs on CI
} @@ samples(50) @@ TestAspect.ifEnvNotSet("CI"),
test("decoding random pre-encoded form") {
for {
port <- server(streamingServer)
Expand Down Expand Up @@ -199,7 +199,7 @@ object ClientStreamingSpec extends HttpRunnableSpec {
)
}
} yield result
} @@ timeout(5.minutes) @@ samples(50) @@ TestAspect.ifEnvNotSet("CI"), // NOTE: random hangs on CI
} @@ samples(50) @@ TestAspect.ifEnvNotSet("CI"),
test("decoding large form with random chunk and buffer sizes") {
val N = 1024 * 1024
for {
Expand Down Expand Up @@ -234,7 +234,7 @@ object ClientStreamingSpec extends HttpRunnableSpec {
)).tapErrorCause(cause => ZIO.debug(cause.prettyPrint))
}
} yield result
} @@ samples(20) @@ timeout(5.minutes) @@ TestAspect.ifEnvNotSet("CI"), // NOTE: random hangs on CI
} @@ samples(20) @@ TestAspect.ifEnvNotSet("CI"),
test("failed stream") {
for {
port <- server(streamingServer)
Expand Down
6 changes: 2 additions & 4 deletions zio-http/src/test/scala/zio/http/ContentTypeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import zio.test.Assertion.{equalTo, isNone, isSome}
import zio.test.TestAspect.{timeout, withLiveClock}
import zio.test._

import zio.http.internal.{DynamicServer, HttpRunnableSpec, severTestLayer}
import zio.http.internal.{DynamicServer, HttpRunnableSpec, serverTestLayer}

object ContentTypeSpec extends HttpRunnableSpec {

Expand Down Expand Up @@ -74,8 +74,6 @@ object ContentTypeSpec extends HttpRunnableSpec {
override def spec = {
suite("Content-type") {
serve.as(List(contentSpec))
}.provideShared(DynamicServer.live, severTestLayer, Client.default, Scope.default) @@ timeout(
5 seconds,
) @@ withLiveClock
}.provideShared(DynamicServer.live, serverTestLayer, Client.default, Scope.default) @@ withLiveClock
}
}
2 changes: 1 addition & 1 deletion zio-http/src/test/scala/zio/http/HandlerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -422,5 +422,5 @@ object HandlerSpec extends ZIOHttpSpec with ExitAssertion {
} yield assertTrue(status == Status.Ok)
},
),
) @@ timeout(10 seconds)
)
}
8 changes: 3 additions & 5 deletions zio-http/src/test/scala/zio/http/KeepAliveSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import zio.test.TestAspect.{sequential, timeout, withLiveClock}
import zio.test.{Spec, assert}
import zio.{Scope, ZIO, durationInt}

import zio.http.internal.{DynamicServer, HttpRunnableSpec, severTestLayer}
import zio.http.internal.{DynamicServer, HttpRunnableSpec, serverTestLayer}

object KeepAliveSpec extends HttpRunnableSpec {

Expand Down Expand Up @@ -63,12 +63,10 @@ object KeepAliveSpec extends HttpRunnableSpec {
),
)

override def spec: Spec[Scope, Throwable] = {
override def spec: Spec[Any, Throwable] = {
suite("KeepAliveSpec") {
keepAliveSpec
}.provideSome[Scope](DynamicServer.live, severTestLayer, Client.default) @@ timeout(
30.seconds,
) @@ withLiveClock @@ sequential
}.provide(DynamicServer.live, serverTestLayer, Client.default, Scope.default) @@ withLiveClock @@ sequential
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,19 @@ object RequestStreamingServerSpec extends HttpRunnableSpec {
}
}
}: _*),
) @@ timeout(10 seconds)
)

override def spec =
suite("RequestStreamingServerSpec") {
suite("app with request streaming") {
ZIO.scoped(appWithReqStreaming.as(List(requestBodySpec, streamingServerSpec)))
appWithReqStreaming.as(List(requestBodySpec, streamingServerSpec))
}
}.provideSomeShared[Scope](
DynamicServer.live,
ZLayer.succeed(configAppWithRequestStreaming),
Server.live,
Client.default,
) @@ timeout(30 seconds) @@ diagnose(15.seconds) @@ sequential @@ shrinks(0) @@ withLiveClock
}.provideSome[DynamicServer & Server.Config & Server & Client](Scope.default)
.provideShared(
DynamicServer.live,
ZLayer.succeed(configAppWithRequestStreaming),
Server.live,
Client.default,
) @@ diagnose(15.seconds) @@ sequential @@ shrinks(0) @@ withLiveClock

}
3 changes: 1 addition & 2 deletions zio-http/src/test/scala/zio/http/SSLSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ object SSLSpec extends ZIOHttpSpec {
),
).provideShared(
Server.default,
) @@
timeout(5 second) @@ ignore
) @@ ignore

}
16 changes: 8 additions & 8 deletions zio-http/src/test/scala/zio/http/ServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package zio.http
import java.nio.charset.StandardCharsets
import java.nio.file.Paths

import zio._
import zio.test.Assertion._
import zio.test.TestAspect._
import zio.test._
import zio.{Chunk, Scope, ZIO, ZLayer, durationInt}

import zio.stream.{ZPipeline, ZStream}

Expand Down Expand Up @@ -457,12 +457,12 @@ object ServerSpec extends HttpRunnableSpec {
suite("ServerSpec") {
val spec = dynamicAppSpec + responseSpec + requestSpec + requestBodySpec + serverErrorSpec
suite("app without request streaming") { ZIO.scoped(app.as(List(spec))) }
}.provideSomeShared[TestEnvironment](
DynamicServer.live,
ZLayer.succeed(configApp),
Server.live,
Client.default,
Scope.default,
) @@ timeout(30 seconds) @@ sequential @@ withLiveClock
}.provideSome[DynamicServer & Server.Config & Server & Client](Scope.default)
.provideShared(
DynamicServer.live,
ZLayer.succeed(configApp),
Server.live,
Client.default,
) @@ sequential @@ withLiveClock

}
7 changes: 3 additions & 4 deletions zio-http/src/test/scala/zio/http/StaticFileServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import zio.test.Assertion._
import zio.test.TestAspect.{timeout, unix, withLiveClock}
import zio.test.assertZIO

import zio.http.internal.{DynamicServer, HttpRunnableSpec, severTestLayer}
import zio.http.internal.{DynamicServer, HttpRunnableSpec, serverTestLayer}

object StaticFileServerSpec extends HttpRunnableSpec {

Expand All @@ -41,9 +41,8 @@ object StaticFileServerSpec extends HttpRunnableSpec {
.deploy

override def spec = suite("StaticFileServerSpec") {
ZIO.scoped(serve.as(List(staticSpec)))
}.provideShared(DynamicServer.live, severTestLayer, Client.default, Scope.default) @@
timeout(5 seconds) @@ withLiveClock
serve.as(List(staticSpec))
}.provideShared(DynamicServer.live, serverTestLayer, Client.default, Scope.default) @@ withLiveClock

private def staticSpec = suite("Static RandomAccessFile Server")(
suite("fromResource")(
Expand Down
16 changes: 8 additions & 8 deletions zio-http/src/test/scala/zio/http/StaticServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package zio.http

import zio._
import zio.test.Assertion.equalTo
import zio.test.TestAspect.{timeout, withLiveClock}
import zio.test.{Gen, TestEnvironment, assertTrue, assertZIO, checkAll}
import zio.{Exit, Scope, ZIO, durationInt}

import zio.http.Header.AccessControlAllowMethods
import zio.http.Middleware.{CorsConfig, cors}
import zio.http.internal.{DynamicServer, HttpGen, HttpRunnableSpec, severTestLayer, testClientLayer}
import zio.http.internal.{DynamicServer, HttpGen, HttpRunnableSpec, serverTestLayer, testClientLayer}

object StaticServerSpec extends HttpRunnableSpec {

Expand Down Expand Up @@ -101,12 +101,12 @@ object StaticServerSpec extends HttpRunnableSpec {
.as(
List(staticAppSpec, nonZIOSpec, throwableAppSpec, multiHeadersSpec),
)
}.provideSomeShared[TestEnvironment](
DynamicServer.live,
severTestLayer,
testClientLayer,
Scope.default,
) @@ timeout(30 seconds) @@ withLiveClock
}.provideSome[DynamicServer & Server.Config & Server & Client](Scope.default)
.provideShared(
DynamicServer.live,
serverTestLayer,
testClientLayer,
) @@ withLiveClock

def staticAppSpec =
suite("StaticAppSpec")(
Expand Down
Loading

0 comments on commit 25bc720

Please sign in to comment.