Skip to content

Commit

Permalink
Fix Scope usage in ServerSentEventEndpointSpec (zio#3182)
Browse files Browse the repository at this point in the history
Co-authored-by: Nabil Abdel-Hafeez <[email protected]>
  • Loading branch information
kyri-petrou and 987Nabil authored Sep 29, 2024
1 parent 13ecf37 commit f29c699
Showing 1 changed file with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import zio.schema.{DeriveSchema, Schema}

import zio.http._
import zio.http.codec.HttpCodec
import zio.http.netty.NettyConfig

object ServerSentEventEndpointSpec extends ZIOHttpSpec {

Expand All @@ -40,13 +41,14 @@ object ServerSentEventEndpointSpec extends ZIOHttpSpec {
: Invocation[Unit, Unit, ZNothing, ZStream[Any, Nothing, ServerSentEvent[String]], AuthType.None] =
sseEndpoint(())

def client(port: Int): ZIO[Scope, Throwable, Chunk[ServerSentEvent[String]]] =
(for {
def client(port: Int): ZIO[Client, Throwable, Chunk[ServerSentEvent[String]]] = ZIO.scoped {
for {
client <- ZIO.service[Client]
executor = EndpointExecutor(client, locator(port))
stream <- executor(invocation)
events <- stream.take(5).runCollect
} yield events).provideSome[Scope](ZClient.default)
} yield events
}
}

object JsonPayload {
Expand Down Expand Up @@ -77,13 +79,14 @@ object ServerSentEventEndpointSpec extends ZIOHttpSpec {
: Invocation[Unit, Unit, ZNothing, ZStream[Any, Nothing, ServerSentEvent[Payload]], AuthType.None] =
sseEndpoint(())

def client(port: Int): ZIO[Scope, Throwable, Chunk[ServerSentEvent[Payload]]] =
(for {
def client(port: Int): ZIO[Client, Throwable, Chunk[ServerSentEvent[Payload]]] = ZIO.scoped {
for {
client <- ZIO.service[Client]
executor = EndpointExecutor(client, locator(port))
stream <- executor(invocation)
events <- stream.take(5).runCollect
} yield events).provideSome[Scope](ZClient.default)
} yield events
}
}

override def spec: Spec[TestEnvironment with Scope, Any] =
Expand All @@ -95,14 +98,22 @@ object ServerSentEventEndpointSpec extends ZIOHttpSpec {
port <- ZIO.serviceWithZIO[Server](_.port)
events <- client(port)
} yield assertTrue(events.size == 5 && events.forall(event => Try(ISO_LOCAL_TIME.parse(event.data)).isSuccess))
}.provideSome[Scope](Server.defaultWithPort(0)),
},
test("Send and receive ServerSentEvent with json payload") {
import JsonPayload._
for {
_ <- server.fork
port <- ZIO.serviceWithZIO[Server](_.port)
events <- client(port)
} yield assertTrue(events.size == 5 && events.forall(event => Try(event.data.timeStamp).isSuccess))
}.provideSome[Scope](Server.defaultWithPort(0)),
) @@ TestAspect.withLiveClock
},
)
.provideSomeLayer[Client & Server.Config & NettyConfig](Server.customized)
.provideShared(
Client.live,
ZLayer.succeed(Server.Config.default.port(0)),
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
ZLayer.succeed(ZClient.Config.default),
DnsResolver.default,
) @@ TestAspect.withLiveClock
}

0 comments on commit f29c699

Please sign in to comment.