Skip to content

Commit

Permalink
upgrade zio version (#1238)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgfraser authored May 4, 2022
1 parent a50397f commit c29b787
Show file tree
Hide file tree
Showing 30 changed files with 236 additions and 237 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ object Spec extends ZIOSpecDefault {
test("should be ok") {
val req = ???
val expectedRes = resp => resp.status.toJHttpStatus.code() == Status.OK
assertM(app(req))(expectedRes) // an apply method is added via `zhttp.test` package
assertZIO(app(req))(expectedRes) // an apply method is added via `zhttp.test` package
}
)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/website/docs/v1.x/dsl/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ The below snippet tests an app that takes `Int` as input and responds by adding
def spec = suite("http")(
testM("1 + 1 = 2") {
val app: Http[Any, Nothing, Int, Int] = Http.fromFunction[Int](_ + 1)
assertM(app(1))(equalTo(2))
assertZIO(app(1))(equalTo(2))
}
)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/website/docs/v1.x/dsl/http/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ The below snippet tests an app that takes `Int` as input and responds by adding
def spec = suite("http")(
test("1 + 1 = 2") {
val app: Http[Any, Nothing, Int, Int] = Http.fromFunction[Int](_ + 1)
assertM(app(1))(equalTo(2))
assertZIO(app(1))(equalTo(2))
}
)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/website/docs/v1.x/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ object Spec extends ZIOSpecDefault {
test("should be ok") {
val app = Http.ok
val req = Request()
assertM(app(req))(equalTo(Response.ok))
assertZIO(app(req))(equalTo(Response.ok))
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object Main extends ZIOAppDefault {
private def server(app: HttpApp[Any, Nothing]) =
Server.app(app) ++
Server.port(8080) ++
Server.error(_ => UIO.unit) ++
Server.error(_ => ZIO.unit) ++
Server.disableLeakDetection ++
Server.consolidateFlush ++
Server.disableFlowControl
Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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-RC5"
val ZioVersion = "2.0.0-RC6"
val SttpVersion = "3.3.18"

val `jwt-core` = "com.github.jwt-scala" %% "jwt-core" % JwtCoreVersion
Expand Down
53 changes: 26 additions & 27 deletions zio-http/src/main/scala/zhttp/service/HttpRuntime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.netty.channel.{ChannelHandlerContext, EventLoopGroup => JEventLoopGrou
import io.netty.util.concurrent.{EventExecutor, Future, GenericFutureListener}
import zio._

import scala.collection.mutable
import scala.concurrent.{ExecutionContext => JExecutionContext}
import scala.jdk.CollectionConverters._

Expand Down Expand Up @@ -76,43 +75,43 @@ object HttpRuntime {
object Strategy {

def dedicated[R](group: JEventLoopGroup): ZIO[R, Nothing, Strategy[R]] =
ZIO.runtime[R].map(runtime => Dedicated(runtime, group))
ZIO.executorWith { executor =>
val dedicatedExecutor = Executor.fromExecutionContext(executor.yieldOpCount) {
JExecutionContext.fromExecutor(group)
}
ZIO.onExecutor(dedicatedExecutor) {
ZIO.runtime[R].map(runtime => Dedicated(runtime))
}
}

def default[R](): ZIO[R, Nothing, Strategy[R]] =
ZIO.runtime[R].map(runtime => Default(runtime))

def sticky[R](group: JEventLoopGroup): ZIO[R, Nothing, Strategy[R]] =
ZIO.runtime[R].map(runtime => Group(runtime, group))

case class Default[R](runtime: Runtime[R]) extends Strategy[R] {
override def runtime(ctx: ChannelHandlerContext): Runtime[R] = runtime
}

case class Dedicated[R](runtime: Runtime[R], group: JEventLoopGroup) extends Strategy[R] {
private val localRuntime: Runtime[R] = runtime.withExecutor {
Executor.fromExecutionContext(runtime.runtimeConfig.executor.yieldOpCount) {
JExecutionContext.fromExecutor(group)
}
ZIO.runtime[R].flatMap { default =>
ZIO
.foreach(group.asScala) { eventLoop =>
val stickyExecutor = Executor.fromExecutionContext(default.executor.yieldOpCount) {
JExecutionContext.fromExecutor(eventLoop)
}
ZIO.onExecutor(stickyExecutor) {
ZIO.runtime[R].map(runtime => eventLoop -> runtime)
}
}
.map(group => Group(default, group.toMap))
}

override def runtime(ctx: ChannelHandlerContext): Runtime[R] = localRuntime
case class Default[R](default: Runtime[R]) extends Strategy[R] {
override def runtime(ctx: ChannelHandlerContext): Runtime[R] = default
}

case class Group[R](runtime: Runtime[R], group: JEventLoopGroup) extends Strategy[R] {
private val localRuntime: mutable.Map[EventExecutor, Runtime[R]] = {
val map = mutable.Map.empty[EventExecutor, Runtime[R]]
for (exe <- group.asScala)
map += exe -> runtime.withExecutor {
Executor.fromExecutionContext(runtime.runtimeConfig.executor.yieldOpCount) {
JExecutionContext.fromExecutor(exe)
}
}

map
}
case class Dedicated[R](dedicated: Runtime[R]) extends Strategy[R] {
override def runtime(ctx: ChannelHandlerContext): Runtime[R] = dedicated
}

case class Group[R](default: Runtime[R], group: Map[EventExecutor, Runtime[R]]) extends Strategy[R] {
override def runtime(ctx: ChannelHandlerContext): Runtime[R] =
localRuntime.getOrElse(ctx.executor(), runtime)
group.getOrElse(ctx.executor(), default)
}
}
}
10 changes: 5 additions & 5 deletions zio-http/src/test/scala/zhttp/endpoint/EndpointSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package zhttp.endpoint
import zhttp.http._
import zio.ZIO
import zio.test.Assertion._
import zio.test.{ZIOSpecDefault, assert, assertM}
import zio.test.{ZIOSpecDefault, assert, assertZIO}

object EndpointSpec extends ZIOSpecDefault {
def spec = suite("Route") {
Expand Down Expand Up @@ -67,19 +67,19 @@ object EndpointSpec extends ZIOSpecDefault {
suite("to") {
test("endpoint doesn't match") {
val app = Method.GET / "a" to { _ => Response.ok }
assertM(app(Request(url = URL(!! / "b"))).flip)(isNone)
assertZIO(app(Request(url = URL(!! / "b"))).flip)(isNone)
} +
test("endpoint with effect doesn't match") {
val app = Method.GET / "a" to { _ => ZIO.succeed(Response.ok) }
assertM(app(Request(url = URL(!! / "b"))).flip)(isNone)
assertZIO(app(Request(url = URL(!! / "b"))).flip)(isNone)
} +
test("endpoint matches") {
val app = Method.GET / "a" to { _ => Response.ok }
assertM(app(Request(url = URL(!! / "a"))).map(_.status))(equalTo(Status.Ok))
assertZIO(app(Request(url = URL(!! / "a"))).map(_.status))(equalTo(Status.Ok))
} +
test("endpoint with effect matches") {
val app = Method.GET / "a" to { _ => ZIO.succeed(Response.ok) }
assertM(app(Request(url = URL(!! / "a"))).map(_.status))(equalTo(Status.Ok))
assertZIO(app(Request(url = URL(!! / "a"))).map(_.status))(equalTo(Status.Ok))
}
}
}
16 changes: 8 additions & 8 deletions zio-http/src/test/scala/zhttp/http/ContentTypeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@ import zhttp.service.{ChannelFactory, EventLoopGroup}
import zio._
import zio.test.Assertion.{equalTo, isNone, isSome}
import zio.test.TestAspect.timeout
import zio.test.assertM
import zio.test.assertZIO

object ContentTypeSpec extends HttpRunnableSpec {

val contentSpec = suite("Content type header on file response") {
test("mp4") {
val res = Http.fromResource("TestFile2.mp4").deploy.contentType.run()
assertM(res)(isSome(equalTo("video/mp4")))
assertZIO(res)(isSome(equalTo("video/mp4")))
} +
test("js") {
val res = Http.fromResource("TestFile3.js").deploy.contentType.run()
assertM(res)(isSome(equalTo("application/javascript")))
assertZIO(res)(isSome(equalTo("application/javascript")))
} +
test("no extension") {
val res = Http.fromResource("TestFile4").deploy.contentType.run()
assertM(res)(isNone)
assertZIO(res)(isNone)
} +
test("css") {
val res = Http.fromResource("TestFile5.css").deploy.contentType.run()
assertM(res)(isSome(equalTo("text/css")))
assertZIO(res)(isSome(equalTo("text/css")))
} +
test("mp3") {
val res = Http.fromResource("TestFile6.mp3").deploy.contentType.run()
assertM(res)(isSome(equalTo("audio/mpeg")))
assertZIO(res)(isSome(equalTo("audio/mpeg")))
} +
test("unidentified extension") {
val res = Http.fromResource("truststore.jks").deploy.contentType.run()
assertM(res)(isNone)
assertZIO(res)(isNone)
} +
test("already set content-type") {
val expected = MediaType.application.`json`
val res = Http.fromResource("TestFile6.mp3").map(_.withMediaType(expected)).deploy.contentType.run()
assertM(res)(isSome(equalTo(expected.fullType)))
assertZIO(res)(isSome(equalTo(expected.fullType)))
}
}

Expand Down
18 changes: 9 additions & 9 deletions zio-http/src/test/scala/zhttp/http/EncodeRequestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ object EncodeRequestSpec extends ZIOSpecDefault with EncodeRequest {
test("method") {
check(anyClientParam) { params =>
val req = encode(params).map(_.method())
assertM(req)(equalTo(params.method.toJava))
assertZIO(req)(equalTo(params.method.toJava))
}
} +
test("method on HttpData.RandomAccessFile") {
check(HttpGen.clientParamsForFileHttpData()) { params =>
val req = encode(params).map(_.method())
assertM(req)(equalTo(params.method.toJava))
assertZIO(req)(equalTo(params.method.toJava))
}
} +
suite("uri") {
test("uri") {
check(anyClientParam) { params =>
val req = encode(params).map(_.uri())
assertM(req)(equalTo(params.url.relative.encode))
assertZIO(req)(equalTo(params.url.relative.encode))
}
} +
test("uri on HttpData.RandomAccessFile") {
check(HttpGen.clientParamsForFileHttpData()) { params =>
val req = encode(params).map(_.uri())
assertM(req)(equalTo(params.url.relative.encode))
assertZIO(req)(equalTo(params.url.relative.encode))
}
}
} +
Expand All @@ -60,34 +60,34 @@ object EncodeRequestSpec extends ZIOSpecDefault with EncodeRequest {
val req = encode(params).map(
_.headers().getInt(HttpHeaderNames.CONTENT_LENGTH).toLong,
)
assertM(req)(equalTo(5L))
assertZIO(req)(equalTo(5L))
}
} +
test("host header") {
check(anyClientParam) { params =>
val req =
encode(params).map(i => Option(i.headers().get(HttpHeaderNames.HOST)))
assertM(req)(equalTo(params.url.host))
assertZIO(req)(equalTo(params.url.host))
}
} +
test("host header when absolute url") {
check(clientParamWithAbsoluteUrl) { params =>
val req = encode(params)
.map(i => Option(i.headers().get(HttpHeaderNames.HOST)))
assertM(req)(equalTo(params.url.host))
assertZIO(req)(equalTo(params.url.host))
}
} +
test("only one host header exists") {
check(clientParamWithAbsoluteUrl) { params =>
val req = encode(params)
.map(_.headers().getAll(HttpHeaderNames.HOST).size)
assertM(req)(equalTo(1))
assertZIO(req)(equalTo(1))
}
} +
test("http version") {
check(anyClientParam) { params =>
val req = encode(params).map(i => i.protocolVersion())
assertM(req)(equalTo(params.version.toJava))
assertZIO(req)(equalTo(params.version.toJava))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions zio-http/src/test/scala/zhttp/http/GetBodyAsStringSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ object GetBodyAsStringSpec extends ZIOSpecDefault {

val encoded = request.bodyAsString
val expected = new String(Chunk.fromArray("abc".getBytes(charset)).toArray, charset)
assertM(encoded)(equalTo(expected))
assertZIO(encoded)(equalTo(expected))
}
} +
test("should map bytes to default utf-8 if no charset given") {
val request = Request(url = URL(!!), data = HttpData.BinaryChunk(Chunk.fromArray("abc".getBytes())))
val encoded = request.bodyAsString
val expected = new String(Chunk.fromArray("abc".getBytes()).toArray, HTTP_CHARSET)
assertM(encoded)(equalTo(expected))
assertZIO(encoded)(equalTo(expected))
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions zio-http/src/test/scala/zhttp/http/HExitAssertion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ import zio.test._

private[zhttp] trait HExitAssertion {
def isDie[R, E, A](ass: Assertion[Throwable]): Assertion[HExit[R, E, A]] =
Assertion.assertion("isDie")() {
Assertion.assertion("isDie") {
case HExit.Die(t) => ass.test(t)
case _ => false
}

def isEffect[R, E, A]: Assertion[HExit[R, E, A]] =
Assertion.assertion("isEffect")() {
Assertion.assertion("isEffect") {
case HExit.Effect(_) => true
case _ => false
}

def isEmpty[R, E, A]: Assertion[HExit[R, E, A]] =
Assertion.assertion("isEmpty")() {
Assertion.assertion("isEmpty") {
case HExit.Empty => true
case _ => false
}

def isSuccess[R, E, A](ass: Assertion[A]): Assertion[HExit[R, E, A]] =
Assertion.assertion("isSuccess")() {
Assertion.assertion("isSuccess") {
case HExit.Success(a) => ass.test(a)
case _ => false
}

def isFailure[R, E, A](ass: Assertion[E]): Assertion[HExit[R, E, A]] =
Assertion.assertion("isFailure")() {
Assertion.assertion("isFailure") {
case HExit.Failure(e) => ass.test(e)
case _ => false
}
Expand Down
2 changes: 1 addition & 1 deletion zio-http/src/test/scala/zhttp/http/HExitSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import zio.test._
import zio.{ZIO, durationInt}

object HExitSpec extends ZIOSpecDefault with HExitAssertion {
def spec: ZSpec[Environment, Any] = {
def spec: Spec[Environment, Any] = {
import HExit._
suite("HExit")(
test("out") {
Expand Down
10 changes: 5 additions & 5 deletions zio-http/src/test/scala/zhttp/http/HttpDataSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import zio.durationInt
import zio.stream.ZStream
import zio.test.Assertion.{anything, equalTo, isLeft, isSubtype}
import zio.test.TestAspect.timeout
import zio.test.{Gen, ZIOSpecDefault, assertM, checkAll}
import zio.test.{Gen, ZIOSpecDefault, assertZIO, checkAll}

import java.io.File

Expand All @@ -21,24 +21,24 @@ object HttpDataSpec extends ZIOSpecDefault {
val stringBuffer = payload.getBytes(HTTP_CHARSET)
val responseContent = ZStream.fromIterable(stringBuffer)
val res = HttpData.fromStream(responseContent).toByteBuf.map(_.toString(HTTP_CHARSET))
assertM(res)(equalTo(payload))
assertZIO(res)(equalTo(payload))
}
}
},
suite("fromFile")(
test("failure") {
val res = HttpData.fromFile(throw new Error("Failure")).toByteBuf.either
assertM(res)(isLeft(isSubtype[Error](anything)))
assertZIO(res)(isLeft(isSubtype[Error](anything)))
},
test("success") {
lazy val file = testFile
val res = HttpData.fromFile(file).toByteBuf.map(_.toString(HTTP_CHARSET))
assertM(res)(equalTo("abc\nfoo"))
assertZIO(res)(equalTo("abc\nfoo"))
},
test("success small chunk") {
lazy val file = testFile
val res = HttpData.fromFile(file).toByteBuf(ByteBufConfig(3)).map(_.toString(HTTP_CHARSET))
assertM(res)(equalTo("abc\nfoo"))
assertZIO(res)(equalTo("abc\nfoo"))
},
),
)
Expand Down
Loading

0 comments on commit c29b787

Please sign in to comment.