Skip to content

Commit

Permalink
Merge branch 'master' into 3649-ZStream.fromTcpSocketServer
Browse files Browse the repository at this point in the history
  • Loading branch information
regis-leray committed Jun 6, 2020
2 parents 849ebef + e9124af commit 9fcc4d8
Show file tree
Hide file tree
Showing 96 changed files with 1,711 additions and 946 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -52,6 +52,7 @@ Want to see your company here? [Submit a PR](https://github.com/zio/zio/edit/mas
* [Wehkamp](https://www.wehkamp.nl)
* [LeadIQ](https://leadiq.com)
* [Call Handling](https://www.callhandling.co.uk/)
* [Univalence](https://univalence.io)

# Sponsors

Expand Down
Expand Up @@ -9,7 +9,7 @@ import zio.Chunk
@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
class ChunkAddBenchmarks {
class ChunkAppendBenchmarks {

val chunk = Chunk(1)
val vector = Vector(1)
Expand All @@ -18,25 +18,25 @@ class ChunkAddBenchmarks {
var size: Int = _

@Benchmark
def chunkAdd(): Chunk[Int] = {
def chunkAppend(): Chunk[Int] = {
var i = 0
var current = chunk

while (i < size) {
current = current :+ 2
current = current :+ i
i += 1
}

current
}

@Benchmark
def vectorAdd(): Vector[Int] = {
def vectorAppend(): Vector[Int] = {
var i = 0
var current = vector

while (i < size) {
current = current :+ 2
current = current :+ i
i += 1
}

Expand Down
45 changes: 45 additions & 0 deletions benchmarks/src/main/scala/zio/chunks/ChunkPrependBenchmark.scala
@@ -0,0 +1,45 @@
package zio.chunks

import java.util.concurrent.TimeUnit

import org.openjdk.jmh.annotations._

import zio.Chunk

@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
class ChunkPrependBenchmarks {

val chunk = Chunk(1)
val vector = Vector(1)

@Param(Array("10000"))
var size: Int = _

@Benchmark
def chunkPrepend(): Chunk[Int] = {
var i = 0
var current = chunk

while (i < size) {
current = i +: current
i += 1
}

current
}

@Benchmark
def vectorPrepend(): Vector[Int] = {
var i = 0
var current = vector

while (i < size) {
current = i +: current
i += 1
}

current
}
}
Expand Up @@ -19,7 +19,7 @@ class TReentrantLockBenchmark {

val javaLock = new StampedLock()

val zioLock: ZIO[Any, Nothing, TReentrantLock] = TReentrantLock.make.commit
val zioLock: UIO[TReentrantLock] = TReentrantLock.make.commit

@Benchmark
@Group("ZioLockBasic")
Expand Down
8 changes: 4 additions & 4 deletions build.sbt
Expand Up @@ -327,9 +327,9 @@ lazy val benchmarks = project.module
Seq(
"co.fs2" %% "fs2-core" % "2.3.0",
"com.google.code.findbugs" % "jsr305" % "3.0.2",
"com.twitter" %% "util-core" % "20.4.1",
"com.twitter" %% "util-core" % "20.5.0",
"com.typesafe.akka" %% "akka-stream" % "2.6.5",
"io.monix" %% "monix" % "3.2.1",
"io.monix" %% "monix" % "3.2.2",
"io.projectreactor" % "reactor-core" % "3.3.5.RELEASE",
"io.reactivex.rxjava2" % "rxjava" % "2.2.19",
"org.ow2.asm" % "asm" % "8.0.1",
Expand Down Expand Up @@ -371,15 +371,15 @@ lazy val docs = project.module
scalacOptions ~= { _ filterNot (_ startsWith "-Xlint") },
libraryDependencies ++= Seq(
"com.github.ghik" % "silencer-lib" % "1.4.4" % Provided cross CrossVersion.full,
"commons-io" % "commons-io" % "2.6" % "provided",
"commons-io" % "commons-io" % "2.7" % "provided",
"org.jsoup" % "jsoup" % "1.13.1" % "provided",
"org.reactivestreams" % "reactive-streams-examples" % "1.0.3" % "provided",
"dev.zio" %% "zio-interop-cats" % "2.0.0.0-RC13",
"dev.zio" %% "zio-interop-future" % "2.12.8.0-RC6",
"dev.zio" %% "zio-interop-monix" % "3.0.0.0-RC7",
"dev.zio" %% "zio-interop-scalaz7x" % "7.2.27.0-RC9",
"dev.zio" %% "zio-interop-java" % "1.1.0.0-RC6",
"dev.zio" %% "zio-interop-reactivestreams" % "1.0.3.5-RC8",
"dev.zio" %% "zio-interop-reactivestreams" % "1.0.3.5-RC10",
"dev.zio" %% "zio-interop-twitter" % "19.7.0.0-RC2"
)
)
Expand Down
10 changes: 5 additions & 5 deletions core-tests/jvm/src/test/scala-2.12/zio/StacktracesSpec.scala
Expand Up @@ -165,7 +165,7 @@ object StackTracesSpec extends DefaultRunnableSpec {
}
},
testM("blocking trace") {
val io: ZIO[Blocking, Throwable, Unit] = for {
val io: RIO[Blocking, Unit] = for {
trace <- blockingTrace
} yield trace

Expand Down Expand Up @@ -310,13 +310,13 @@ object StackTracesSpec extends DefaultRunnableSpec {

def show(cause: Cause[Any]): Unit = if (debug) println(cause.prettyPrint)

def basicTest: ZIO[Any, Nothing, ZTrace] =
def basicTest: UIO[ZTrace] =
for {
_ <- ZIO.unit
trace <- ZIO.trace
} yield trace

def foreachTest: ZIO[Any, Nothing, ZTrace] = {
def foreachTest: UIO[ZTrace] = {
import foreachTraceFixture._
for {
_ <- effectTotal
Expand All @@ -342,7 +342,7 @@ object StackTracesSpec extends DefaultRunnableSpec {
t2 <- ZIO.trace
} yield (t1, t2)

def foreachParFail: ZIO[Any, Nothing, Unit] =
def foreachParFail: UIO[Unit] =
for {
_ <- ZIO.foreachPar(1 to 10)(i => (if (i >= 7) UIO(i / 0) else UIO(i / 10)))
} yield ()
Expand All @@ -352,7 +352,7 @@ object StackTracesSpec extends DefaultRunnableSpec {
_ <- ZIO.foreachParN(4)(1 to 10)(i => (if (i >= 7) UIO(i / 0) else UIO(i / 10)))
} yield ()

def leftAssociativeFold(n: Int): ZIO[Any, Nothing, ZTrace] =
def leftAssociativeFold(n: Int): UIO[ZTrace] =
(1 to n)
.foldLeft(ZIO.unit *> ZIO.unit) { (acc, _) =>
acc *> UIO(())
Expand Down
39 changes: 39 additions & 0 deletions core-tests/jvm/src/test/scala/zio/CancelableFutureSpecJVM.scala
@@ -0,0 +1,39 @@
package zio

import java.util.concurrent.Executors

import scala.concurrent.ExecutionContext

import zio.duration._
import zio.internal.Executor
import zio.test.Assertion._
import zio.test.TestAspect._
import zio.test._

object CancelableFutureSpecJVM extends ZIOBaseSpec {

import ZIOTag._

def spec =
suite("CancelableFutureSpecJVM")(
testM("fromFuture/unsafeRunToFuture doesn't deadlock") {

val tst =
for {
runtime <- ZIO.runtime[Any]
r <- ZIO.fromFuture(_ => runtime.unsafeRunToFuture(UIO.succeedNow(0)))
} yield assert(r)(equalTo(0))
ZIO
.runtime[Any]
.map(
_.mapPlatform(
_.withExecutor(
Executor.fromExecutionContext(1)(
ExecutionContext.fromExecutor(Executors.newSingleThreadScheduledExecutor())
)
)
).unsafeRun(tst)
)
} @@ timeout(1.second)
) @@ zioTag(future)
}
14 changes: 11 additions & 3 deletions core-tests/jvm/src/test/scala/zio/interop/JavaSpec.scala
Expand Up @@ -59,7 +59,7 @@ object JavaSpec extends ZIOBaseSpec {
testM("catch exceptions thrown by lazy block") {
val ex = new Exception("no future for you!")
lazy val noFuture: CompletionStage[Unit] = throw ex
assertM(ZIO.fromCompletionStage(noFuture).run)(dies(equalTo(ex)))
assertM(ZIO.fromCompletionStage(noFuture).run)(fails(equalTo(ex)))
} @@ zioTag(errors),
testM("return an `IO` that fails if `Future` fails (failedFuture)") {
val ex = new Exception("no value for you!")
Expand All @@ -78,7 +78,15 @@ object JavaSpec extends ZIOBaseSpec {
testM("handle null produced by the completed `Future`") {
lazy val someValue: CompletionStage[String] = CompletableFuture.completedFuture[String](null)
assertM(ZIO.fromCompletionStage(someValue).map(Option(_)))(isNone)
} @@ zioTag(errors)
} @@ zioTag(errors),
testM("be referentially transparent") {
var n = 0
val task = ZIO.fromCompletionStage(CompletableFuture.supplyAsync(() => n += 1))
for {
_ <- task
_ <- task
} yield assert(n)(equalTo(2))
}
) @@ zioTag(future),
suite("`Task.toCompletableFuture` must")(
testM("produce always a successful `IO` of `Future`") {
Expand Down Expand Up @@ -123,7 +131,7 @@ object JavaSpec extends ZIOBaseSpec {
testM("catch exceptions thrown by lazy block") {
val ex = new Exception("no future for you!")
def noFuture: CompletionStage[Unit] = throw ex
assertM(Fiber.fromCompletionStage(noFuture).join.run)(dies(equalTo(ex)))
assertM(Fiber.fromCompletionStage(noFuture).join.run)(fails(equalTo(ex)))
} @@ zioTag(errors),
testM("return an `IO` that fails if `Future` fails (failedFuture)") {
val ex = new Exception("no value for you!")
Expand Down
Expand Up @@ -10,16 +10,16 @@ object BracketTypeInferenceSpec {
class E1 extends E

def infersEType1: ZIO[R, E, B] = {
val acquire: ZIO[R, E, A] = ???
val release: A => ZIO[R, Nothing, Any] = ???
val use: A => ZIO[R, E1, B] = ???
val acquire: ZIO[R, E, A] = ???
val release: A => URIO[R, Any] = ???
val use: A => ZIO[R, E1, B] = ???
acquire.bracket(release)(use)
}

def infersEType2: ZIO[R, E, B] = {
val acquire: ZIO[R, E1, A] = ???
val release: A => ZIO[R, Nothing, Any] = ???
val use: A => ZIO[R, E, B] = ???
val acquire: ZIO[R, E1, A] = ???
val release: A => URIO[R, Any] = ???
val use: A => ZIO[R, E, B] = ???
acquire.bracket(release, use)
}

Expand Down
Expand Up @@ -44,6 +44,14 @@ object CancelableFutureSpec extends ZIOBaseSpec {

assertM(Live.live(result.timeout(1.seconds)))(isNone)
} @@ zioTag(supervision, regression),
testM("unsafeRunToFuture interruptibility") {
for {
runtime <- ZIO.runtime[Any]
f = runtime.unsafeRunToFuture(UIO.never)
_ <- UIO(f.cancel())
r <- ZIO.fromFuture(_ => f).run
} yield assert(r.succeeded)(isFalse) // not interrupted, as the Future fails when the effect in interrupted.
} @@ timeout(1.second) @@ jvmOnly @@ zioTag(interruption),
testM("roundtrip preserves interruptibility") {
for {
start <- Promise.make[Nothing, Unit]
Expand Down
49 changes: 48 additions & 1 deletion core-tests/shared/src/test/scala/zio/ChunkSpec.scala
Expand Up @@ -57,7 +57,7 @@ object ChunkSpec extends ZIOBaseSpec {
assert(chunk.size)(equalTo(chunk.length))
}
),
suite("add")(
suite("append")(
testM("apply") {
val chunksWithIndex: Gen[Random with Sized, (Chunk[Int], Chunk[Int], Int)] =
for {
Expand Down Expand Up @@ -104,6 +104,53 @@ object ChunkSpec extends ZIOBaseSpec {
}
}
),
suite("prepend")(
testM("apply") {
val chunksWithIndex: Gen[Random with Sized, (Chunk[Int], Chunk[Int], Int)] =
for {
p <- Gen.boolean
as <- Gen.chunkOf(Gen.anyInt)
bs <- Gen.chunkOf1(Gen.anyInt)
n <- Gen.int(0, as.length + bs.length - 1)
} yield if (p) (as, bs, n) else (bs, as, n)
check(chunksWithIndex) {
case (as, bs, n) =>
val actual = as.foldRight(bs)(_ +: _).apply(n)
val expected = (as ++ bs).apply(n)
assert(actual)(equalTo(expected))
}
},
testM("buffer full") {
check(Gen.chunkOf(Gen.anyInt), Gen.chunkOf(Gen.anyInt)) { (as, bs) =>
def addAll[A](l: Chunk[A], r: Chunk[A]): Chunk[A] = l.foldRight(r)(_ +: _)
val actual = List.fill(100)(as).foldRight(bs)(addAll)
val expected = List.fill(100)(as).foldRight(bs)(_ ++ _)
assert(actual)(equalTo(expected))
}
},
testM("buffer used") {
checkM(Gen.chunkOf(Gen.anyInt), Gen.chunkOf(Gen.anyInt)) { (as, bs) =>
val effect = ZIO.succeed(as.foldRight(bs)(_ +: _))
val actual = ZIO.collectAllPar(ZIO.replicate(100)(effect))
val expected = (as ++ bs)
assertM(actual)(forall(equalTo(expected)))
}
},
testM("equals") {
check(Gen.chunkOf(Gen.anyInt), Gen.chunkOf(Gen.anyInt)) { (as, bs) =>
val actual = as.foldRight(bs)(_ +: _)
val expected = (as ++ bs)
assert(actual)(equalTo(expected))
}
},
testM("length") {
check(Gen.chunkOf(Gen.anyInt), smallChunks(Gen.anyInt)) { (as, bs) =>
val actual = as.foldRight(bs)(_ +: _).length
val expected = (as ++ bs).length
assert(actual)(equalTo(expected))
}
}
),
testM("apply") {
check(chunkWithIndex(Gen.unit)) {
case (chunk, i) =>
Expand Down

0 comments on commit 9fcc4d8

Please sign in to comment.