Skip to content

Commit

Permalink
Merge pull request #158 from senia-psm/rollback_to_zio_1.0.15
Browse files Browse the repository at this point in the history
rollback to zio 1.0.15
  • Loading branch information
senia-psm authored Jun 16, 2022
2 parents 43f3104 + cf5cb84 commit 3ccd6df
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/scala.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
branches: [ master, 'backport/**' ]

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ThisBuild / publishTo := sonatypePublishToBundle.value
addCommandAlias("fmt", "all scalafmtSbt scalafmt test:scalafmt")
addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck")

lazy val zioVersion = "2.0.0-RC6"
lazy val zioVersion = "1.0.15"
lazy val akkaVersion = "2.6.19"
lazy val akkaHttpVersion = "10.2.9"

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/akka/http/expose/ExposedRouteTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import akka.http.scaladsl.server._
import akka.http.scaladsl.settings.{ParserSettings, RoutingSettings}
import akka.stream.Materializer
import zio.test.akkahttp.{RouteTest, RouteTestResult}
import zio.{URIO, ZIO}
import zio._

import scala.concurrent.ExecutionContextExecutor

Expand All @@ -17,7 +17,7 @@ trait ExposedRouteTest {
protected def executeRequest(
request: HttpRequest,
route: Route,
): URIO[RouteTest.Environment with ActorSystem, RouteTestResult.Lazy] =
): URIO[RouteTest.Environment with Has[ActorSystem], RouteTestResult.Lazy] =
for {
system <- ZIO.service[ActorSystem]
config <- ZIO.service[RouteTest.Config]
Expand Down
44 changes: 38 additions & 6 deletions src/main/scala/zio/test/akkahttp/AkkaZIOSpecDefault.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
package zio.test.akkahttp

import zio._
import zio.test.{Spec, TestEnvironment, ZIOSpec}
import zio.duration._
import zio.clock.Clock
import zio.test._
import zio.test.environment.{testEnvironment, TestEnvironment}

trait AkkaZIOSpecDefault extends ZIOSpec[RouteTestEnvironment.TestEnvironment with TestEnvironment] with RouteTest {
trait AkkaZIOSpecDefault
extends RunnableSpec[RouteTestEnvironment.TestEnvironment with TestEnvironment, Any]
with RouteTest {

override val bootstrap
: ZLayer[ZIOAppArgs with Scope, Any, RouteTestEnvironment.TestEnvironment with TestEnvironment] =
zio.ZEnv.live >>> TestEnvironment.live >+> RouteTestEnvironment.environment
override def aspects: List[TestAspect[Nothing, TestEnvironment, Nothing, Any]] =
List(TestAspect.timeoutWarning(60.seconds))

override def runner: TestRunner[RouteTestEnvironment.TestEnvironment with TestEnvironment, Any] =
TestRunner(TestExecutor.default(testEnvironment >+> RouteTestEnvironment.environment))

/** Returns an effect that executes a given spec, producing the results of the execution.
*/
private[zio] override def runSpec(
spec: ZSpec[Environment, Failure],
): URIO[TestLogger with Clock, ExecutedSpec[Failure]] =
runner.run(aspects.foldLeft(spec)(_ @@ _) @@ TestAspect.fibers)

/** Builds a suite containing a number of other specs.
*/
def suite[R, E, T](label: String)(specs: Spec[R, E, T]*): Spec[R, E, T] = zio.test.suite(label)(specs: _*)

/** Builds an effectual suite containing a number of other specs.
*/
def suiteM[R, E, T](label: String)(specs: ZIO[R, E, Iterable[Spec[R, E, T]]]): Spec[R, E, T] =
zio.test.suiteM(label)(specs)

/** Builds a spec with a single pure test.
*/
def test(label: String)(assertion: => TestResult)(implicit loc: SourceLocation): ZSpec[Any, Nothing] =
zio.test.test(label)(assertion)

/** Builds a spec with a single effectful test.
*/
def testM[R, E](label: String)(assertion: => ZIO[R, E, TestResult])(implicit loc: SourceLocation): ZSpec[R, E] =
zio.test.testM(label)(assertion)

def spec: Spec[RouteTestEnvironment.TestEnvironment with TestEnvironment with Scope, Any]
}
8 changes: 5 additions & 3 deletions src/main/scala/zio/test/akkahttp/MarshallingTestUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse, MediaRan
import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshal}
import akka.stream.Materializer
import zio._
import zio.clock.Clock
import zio.duration._
import zio.test.akkahttp.RouteTest.Environment

import java.util.concurrent.TimeUnit
Expand All @@ -21,7 +23,7 @@ trait MarshallingTestUtils {
private def fromFutureWithMarshalingTimeout[A](
eff: ExecutionContext => Future[A],
factor: Double = 1,
): ZIO[RouteTest.Config, Throwable, A] =
): ZIO[Has[RouteTest.Config] with Clock, Throwable, A] =
for {
config <- ZIO.service[RouteTest.Config]
marshallingTimeout = config.marshallingTimeout
Expand Down Expand Up @@ -50,13 +52,13 @@ trait MarshallingTestUtils {
def marshalToResponseForRequestAccepting[T: ToResponseMarshaller](
value: T,
mediaRanges: MediaRange*,
): RIO[RouteTest.Config, HttpResponse] =
): RIO[Has[RouteTest.Config] with Clock, HttpResponse] =
marshalToResponse(value, HttpRequest(headers = Accept(mediaRanges.toList) :: Nil))

def marshalToResponse[T: ToResponseMarshaller](
value: T,
request: HttpRequest = HttpRequest(),
): RIO[RouteTest.Config, HttpResponse] =
): RIO[Has[RouteTest.Config] with Clock, HttpResponse] =
fromFutureWithMarshalingTimeout(implicit ec => Marshal(value).toResponseFor(request))

def unmarshal[T: FromEntityUnmarshaller](entity: HttpEntity): RIO[Environment, T] =
Expand Down
5 changes: 3 additions & 2 deletions src/main/scala/zio/test/akkahttp/RequestBuilding.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.HttpCredentials
import zio._
import zio.clock.Clock

import scala.collection.immutable
import scala.reflect.ClassTag

trait RequestBuilding {
type RequestTransformer = HttpRequest => HttpRequest

type EIO[+T] = RIO[RouteTest.Config, T]
type EIO[+T] = RIO[Has[RouteTest.Config] with Clock, T]

class RequestBuilder(val method: HttpMethod) {
def apply(): HttpRequest = apply("/")
Expand All @@ -29,7 +30,7 @@ trait RequestBuilding {
apply(Uri(uri), content)

def apply[T](uri: Uri, content: T)(implicit m: ToEntityMarshaller[T]): EIO[HttpRequest] =
ZIO.serviceWith[RouteTest.Config](_.marshallingTimeout).flatMap { timeout =>
ZIO.service[RouteTest.Config].map(_.marshallingTimeout).flatMap { timeout =>
ZIO
.fromFuture(implicit ec => Marshal(content).to[RequestEntity])
.timeoutFail(new RuntimeException(s"Failed to marshal request entity within $timeout"))(timeout)
Expand Down
28 changes: 19 additions & 9 deletions src/main/scala/zio/test/akkahttp/RouteTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import akka.http.scaladsl.model.headers.Host
import akka.http.scaladsl.server.{RequestContext, RouteResult}
import akka.stream.Materializer
import zio._
import zio.clock.Clock
import zio.duration._
import zio.test.akkahttp.RouteTest.Environment

import scala.concurrent.Future
Expand All @@ -24,27 +26,35 @@ trait RouteTest extends ExposedRouteTest with MarshallingTestUtils with RequestB
}

implicit class WithRoute(request: HttpRequest) {
def ?~>(route: RequestContext => Future[RouteResult]): URIO[Environment with ActorSystem, RouteTestResult.Lazy] =
executeRequest(request, route)
def ?~>(
route: RequestContext => Future[RouteResult],
): URIO[Environment with Has[ActorSystem], RouteTestResult.Lazy] = executeRequest(request, route)

def ~>(route: RequestContext => Future[RouteResult]): URIO[Environment with ActorSystem, RouteTestResult.Eager] =
executeRequest(request, route).flatMap(_.toEager.catchAll(_ => ZIO.succeed(RouteTestResult.Timeout)))
def ~>(
route: RequestContext => Future[RouteResult],
): URIO[Environment with Has[ActorSystem], RouteTestResult.Eager] =
executeRequest(request, route).flatMap(
_.toEager.catchAll[Any, Nothing, RouteTestResult.Eager](_ => ZIO.succeed(RouteTestResult.Timeout)),
)
}

implicit class WithRouteM[R, E](request: ZIO[R, E, HttpRequest]) {
def ?~>(
route: RequestContext => Future[RouteResult],
): ZIO[Environment with ActorSystem with R, E, RouteTestResult.Lazy] = request.flatMap(executeRequest(_, route))
): ZIO[Environment with Has[ActorSystem] with R, E, RouteTestResult.Lazy] =
request.flatMap(executeRequest(_, route))

def ~>(
route: RequestContext => Future[RouteResult],
): ZIO[Environment with ActorSystem with R, E, RouteTestResult.Eager] =
request.flatMap(executeRequest(_, route)).flatMap(_.toEager.catchAll(_ => ZIO.succeed(RouteTestResult.Timeout)))
): ZIO[Environment with Has[ActorSystem] with R, E, RouteTestResult.Eager] =
request
.flatMap(executeRequest(_, route))
.flatMap(_.toEager.catchAll[Any, Nothing, RouteTestResult.Eager](_ => ZIO.succeed(RouteTestResult.Timeout)))
}
}

object RouteTest {
type Environment = Materializer with RouteTest.Config
type Environment = Has[Materializer] with Has[RouteTest.Config] with Clock

case class DefaultHostInfo(host: Host, securedConnection: Boolean)

Expand All @@ -54,5 +64,5 @@ object RouteTest {
routeTestTimeout: Duration = 1.second,
defaultHost: DefaultHostInfo = DefaultHostInfo(Host("example.com"), securedConnection = false))

val testConfig: ULayer[RouteTest.Config] = ZLayer.succeed(Config())
val testConfig: ULayer[Has[RouteTest.Config]] = ZLayer.succeed(Config())
}
21 changes: 10 additions & 11 deletions src/main/scala/zio/test/akkahttp/RouteTestEnvironment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import com.typesafe.config.{Config, ConfigFactory}
import zio._

object RouteTestEnvironment {
lazy val testSystemFromConfig: URLayer[Config, ActorSystem] = ZLayer.scoped {
ZIO.acquireRelease(ZIO.serviceWith[Config](conf => ActorSystem(actorSystemNameFrom(getClass), conf))) { sys =>
ZIO.fromFuture(_ => sys.terminate()).orDie
}
}
lazy val testSystemFromConfig: URLayer[Has[Config], Has[ActorSystem]] =
ZLayer.fromAcquireRelease(ZIO.service[Config].map(conf => ActorSystem(actorSystemNameFrom(getClass), conf)))(sys =>
ZIO.fromFuture(_ => sys.terminate()).orDie,
)

lazy val testSystem: ULayer[ActorSystem] = testConfig >>> testSystemFromConfig
lazy val testSystem: ULayer[Has[ActorSystem]] = testConfig >>> testSystemFromConfig

private def actorSystemNameFrom(clazz: Class[_]) =
clazz.getName
Expand All @@ -22,18 +21,18 @@ object RouteTestEnvironment {

def testConfigSource: String = ""

lazy val testConfig: ULayer[Config] =
lazy val testConfig: ULayer[Has[Config]] =
ZLayer.succeed {
val source = testConfigSource
val config = if (source.isEmpty) ConfigFactory.empty() else ConfigFactory.parseString(source)
config.withFallback(ConfigFactory.load())
}

lazy val testMaterializer: URLayer[ActorSystem, Materializer] =
ZLayer.fromFunction(SystemMaterializer(_: ActorSystem).materializer)
lazy val testMaterializer: URLayer[Has[ActorSystem], Has[Materializer]] =
ZLayer.fromService(SystemMaterializer(_: ActorSystem).materializer)

type TestEnvironment = ActorSystem with Materializer with RouteTest.Config
type TestEnvironment = Has[ActorSystem] with Has[Materializer] with Has[RouteTest.Config]

lazy val environment: ULayer[TestEnvironment] =
testSystem >>> (ZLayer.environment[ActorSystem] ++ testMaterializer) ++ RouteTest.testConfig
testSystem >>> (ZLayer.requires[Has[ActorSystem]] ++ testMaterializer) ++ RouteTest.testConfig
}
6 changes: 3 additions & 3 deletions src/main/scala/zio/test/akkahttp/RouteTestResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ object RouteTestResult {

private def awaitAllElements[T](data: Source[T, _]) =
for {
timeout <- ZIO.serviceWith[RouteTest.Config](_.timeout)
timeout <- ZIO.service[RouteTest.Config].map(_.timeout)
mat <- ZIO.service[Materializer]
res <- ZIO
.fromFuture(_ => data.limit(100000).runWith(Sink.seq)(mat))
Expand Down Expand Up @@ -136,12 +136,12 @@ object RouteTestResult {
for {
environment <- ZIO.environment[Environment]
freshEntityR <- freshEntityEff(response)
freshEntity = freshEntityR.provideEnvironment(environment)
freshEntity = freshEntityR.provide(environment)
} yield new LazyCompleted(
response,
freshEntity,
freshEntity.map(response.withEntity),
freshEntity.flatMap(getChunks).provideEnvironment(environment),
freshEntity.flatMap(getChunks).provide(environment),
)(environment.get[Materializer])
}

Expand Down
19 changes: 10 additions & 9 deletions src/test/scala/zio/test/akkahttp/AkkaZIOSpecDefaultSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ import akka.pattern.ask
import akka.stream.scaladsl.Source
import akka.testkit.TestProbe
import akka.util.{ByteString, Timeout}
import zio.ZIO
import zio._
import zio.blocking.effectBlocking
import zio.test._

import scala.concurrent.duration.DurationInt

object AkkaZIOSpecDefaultSpec extends AkkaZIOSpecDefault {
def spec =
suite("ZioRouteTestSpec")(
test("the most simple and direct route test") {
suite("AkkaZIOSpecDefaultSpec")(
testM("the most simple and direct route test") {
(Get() ~> complete(HttpResponse())).map { res =>
assertTrue(res.handled.get.response == HttpResponse())
}
},
test("a test using a directive and some checks") {
testM("a test using a directive and some checks") {
val pinkHeader = RawHeader("Fancy", "pink")

val result = Get() ~> addHeader(pinkHeader) ~> {
Expand All @@ -41,7 +42,7 @@ object AkkaZIOSpecDefaultSpec extends AkkaZIOSpecDefault {
)
}
},
test("proper rejection collection") {
testM("proper rejection collection") {
val result = Post("/abc", "content") ~> {
(get | put) {
complete("naah")
Expand All @@ -52,7 +53,7 @@ object AkkaZIOSpecDefaultSpec extends AkkaZIOSpecDefault {
assertTrue(res.rejected.get == List(MethodRejection(GET), MethodRejection(PUT)))
}
},
test("separation of route execution from checking") {
testM("separation of route execution from checking") {
val pinkHeader = RawHeader("Fancy", "pink")

case object Command
Expand All @@ -72,7 +73,7 @@ object AkkaZIOSpecDefaultSpec extends AkkaZIOSpecDefault {
}
}
}.fork
_ <- ZIO.attemptBlocking {
_ <- effectBlocking {
handler.expectMsg(Command)
handler.reply("abc")
}
Expand All @@ -83,7 +84,7 @@ object AkkaZIOSpecDefaultSpec extends AkkaZIOSpecDefault {
res.handled.get.header("Fancy").get == pinkHeader,
)
},
test("internal server error") {
testM("internal server error") {
val route = get {
throw new RuntimeException("BOOM")
}
Expand All @@ -92,7 +93,7 @@ object AkkaZIOSpecDefaultSpec extends AkkaZIOSpecDefault {
assertTrue(res.handled.get.status == InternalServerError)
}
},
test("infinite response") {
testM("infinite response") {
val pinkHeader = RawHeader("Fancy", "pink")

val route = get {
Expand Down
Loading

0 comments on commit 3ccd6df

Please sign in to comment.