Skip to content

Commit

Permalink
Rename ZSchedule to Schedule (zio#2242)
Browse files Browse the repository at this point in the history
* rename ZSchedule to Schedule

* add type alias
  • Loading branch information
adamgfraser authored and Twizty committed Nov 18, 2019
1 parent d594ad7 commit f06cb75
Show file tree
Hide file tree
Showing 17 changed files with 971 additions and 1,130 deletions.
2 changes: 1 addition & 1 deletion core-tests/jvm/src/test/scala/zio/RTSSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ object RTSSpec
for {
f <- test.fork
c <- (IO.effectTotal[Int](c.get) <* clock.sleep(1.millis))
.repeat(ZSchedule.doUntil[Int](_ >= 1)) <* f.interrupt
.repeat(Schedule.doUntil[Int](_ >= 1)) <* f.interrupt
} yield c

assertM(zio.provide(Clock.Live), isGreaterThanEqualTo(1))
Expand Down
2 changes: 1 addition & 1 deletion core-tests/jvm/src/test/scala/zio/SerializableSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ object SerializableSpec
deserializedValue <- deserialized.await
} yield assert(deserializedValue, equalTo(value))
},
testM("ZSchedule is serializable") {
testM("Schedule is serializable") {
val schedule = Schedule.recurs(5)
for {
out1 <- ZIO.unit.repeat(schedule)
Expand Down
2 changes: 1 addition & 1 deletion core-tests/shared/src/test/scala/zio/FiberRefSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,6 @@ object FiberRefSpec
object FiberRefSpecUtil {
val (initial, update, update1, update2) = ("initial", "update", "update1", "update2")
val looseTimeAndCpu: ZIO[Live[Clock], Nothing, (Int, Int)] = Live.live {
ZIO.yieldNow.repeat(ZSchedule.spaced(Duration.fromNanos(1)) && Schedule.recurs(100))
ZIO.yieldNow.repeat(Schedule.spaced(Duration.fromNanos(1)) && Schedule.recurs(100))
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package zio

import zio.ZScheduleSpecUtil._
import zio.ScheduleSpecUtil._
import zio.clock.Clock
import zio.duration._
import zio.test.Assertion._
Expand All @@ -9,9 +9,9 @@ import zio.test.{ assert, assertM, suite, testM, TestResult }
import scala.concurrent.Future
import zio.test.environment.{ TestClock, TestRandom }

object ZScheduleSpec
object ScheduleSpec
extends ZIOBaseSpec(
suite("ZScheduleSpec")(
suite("ScheduleSpec")(
/**
* Retry `once` means that we try to exec `io`, get and error,
* try again to exec `io`, and whatever the output is, we return that
Expand Down Expand Up @@ -156,13 +156,13 @@ object ZScheduleSpec
assertM(retried, equalTo("Error: 2"))
},
testM("for a given number of times with random jitter in (0, 1)") {
val schedule = ZSchedule.spaced(500.millis).jittered(0, 1)
val schedule = Schedule.spaced(500.millis).jittered(0, 1)
val scheduled = TestClock.setTime(Duration.Infinity) *> run(schedule >>> testElapsed)(List.fill(5)(()))
val expected = List(0.millis, 250.millis, 500.millis, 750.millis, 1000.millis)
assertM(TestRandom.feedDoubles(0.5, 0.5, 0.5, 0.5, 0.5) *> scheduled, equalTo(expected))
},
testM("for a given number of times with random jitter in custom interval") {
val schedule = ZSchedule.spaced(500.millis).jittered(2, 4)
val schedule = Schedule.spaced(500.millis).jittered(2, 4)
val scheduled = TestClock.setTime(Duration.Infinity) *> run(schedule >>> testElapsed)((List.fill(5)(())))
val expected = List(0, 1500, 3000, 5000, 7000).map(_.millis)
assertM(TestRandom.feedDoubles(0.5, 0.5, 1, 1, 0.5) *> scheduled, equalTo(expected))
Expand All @@ -172,28 +172,28 @@ object ZScheduleSpec
val io = IO.effectTotal(i += 1).flatMap[Any, String, Unit] { _ =>
if (i < 5) IO.fail("KeepTryingError") else IO.fail("GiveUpError")
}
val strategy = ZSchedule.spaced(200.millis).whileInput[String](_ == "KeepTryingError")
val strategy = Schedule.spaced(200.millis).whileInput[String](_ == "KeepTryingError")
val expected = (800.millis, "GiveUpError", 4)
val result = io.retryOrElseEither(strategy, (e: String, r: Int) => TestClock.fiberTime.map((_, e, r)))
assertM(TestClock.setTime(Duration.Infinity) *> result, isLeft(equalTo(expected)))
},
testM("fibonacci delay") {
assertM(
TestClock
.setTime(Duration.Infinity) *> run(ZSchedule.fibonacci(100.millis) >>> testElapsed)(List.fill(5)(())),
.setTime(Duration.Infinity) *> run(Schedule.fibonacci(100.millis) >>> testElapsed)(List.fill(5)(())),
equalTo(List(0, 1, 2, 4, 7).map(i => (i * 100).millis))
)
},
testM("linear delay") {
assertM(
TestClock
.setTime(Duration.Infinity) *> run(ZSchedule.linear(100.millis) >>> testElapsed)(List.fill(5)(())),
.setTime(Duration.Infinity) *> run(Schedule.linear(100.millis) >>> testElapsed)(List.fill(5)(())),
equalTo(List(0, 1, 3, 6, 10).map(i => (i * 100).millis))
)
},
testM("modified linear delay") {
assertM(
TestClock.setTime(Duration.Infinity) *> run(ZSchedule.linear(100.millis).modifyDelay {
TestClock.setTime(Duration.Infinity) *> run(Schedule.linear(100.millis).modifyDelay {
case (_, d) => ZIO.succeed(d * 2)
} >>> testElapsed)(List.fill(5)(())),
equalTo(List(0, 1, 3, 6, 10).map(i => (i * 200).millis))
Expand All @@ -202,13 +202,13 @@ object ZScheduleSpec
testM("exponential delay with default factor") {
assertM(
TestClock
.setTime(Duration.Infinity) *> run(ZSchedule.exponential(100.millis) >>> testElapsed)(List.fill(5)(())),
.setTime(Duration.Infinity) *> run(Schedule.exponential(100.millis) >>> testElapsed)(List.fill(5)(())),
equalTo(List(0, 2, 6, 14, 30).map(i => (i * 100).millis))
)
},
testM("exponential delay with other factor") {
assertM(
TestClock.setTime(Duration.Infinity) *> run(ZSchedule.exponential(100.millis, 3.0) >>> testElapsed)(
TestClock.setTime(Duration.Infinity) *> run(Schedule.exponential(100.millis, 3.0) >>> testElapsed)(
List.fill(5)(())
),
equalTo(List(0, 3, 12, 39, 120).map(i => (i * 100).millis))
Expand Down Expand Up @@ -304,32 +304,31 @@ object ZScheduleSpec
_ => ZIO.fail(Error("Some error")),
ok => ZIO.succeed(Right(Success(ok)))
)
.retry(ZSchedule.spaced(2.seconds) && Schedule.recurs(1))
.retry(Schedule.spaced(2.seconds) && Schedule.recurs(1))
.catchAll(
error => ZIO.succeed(Left(Failure(error.message)))
)

val expected: Either[zio.ZScheduleSpecUtil.Failure, zio.ZScheduleSpecUtil.Success[String]] =
Right(Success("Ok"))
val expected = Right(Success("Ok"))
assertM(foo("Ok"), equalTo(expected))
},
testM("either should not wait if neither schedule wants to continue") {
assertM(
TestClock
.setTime(Duration.Infinity) *> run(
(Schedule.stop || (ZSchedule.spaced(2.seconds) && Schedule.stop)) >>> testElapsed
(Schedule.stop || (Schedule.spaced(2.seconds) && Schedule.stop)) >>> testElapsed
)(List.fill(5)(())),
equalTo(List(Duration.Zero))
)
}
)
)

object ZScheduleSpecUtil {
object ScheduleSpecUtil {
val ioSucceed: (String, Unit) => UIO[String] = (_: String, _: Unit) => IO.succeed("OrElse")
val ioFail: (String, Unit) => IO[String, Nothing] = (_: String, _: Unit) => IO.fail("OrElseFailed")

def repeat[B](schedule: Schedule[Int, B]): ZIO[Any with Clock, Nothing, B] =
def repeat[B](schedule: Schedule[Any, Int, B]): ZIO[Any with Clock, Nothing, B] =
for {
ref <- Ref.make(0)
res <- ref.update(_ + 1).repeat(schedule)
Expand All @@ -339,7 +338,7 @@ object ZScheduleSpecUtil {
* Run a schedule using the provided input and collect all outputs
*/
def run[R, A, B](
sched: ZSchedule[R, A, B]
sched: Schedule[R, A, B]
)(xs: Iterable[A]): ZIO[R, Nothing, List[B]] = {
def loop(xs: List[A], state: sched.State, acc: List[B]): ZIO[R, Nothing, List[B]] = xs match {
case Nil => ZIO.succeed(acc)
Expand All @@ -354,7 +353,7 @@ object ZScheduleSpecUtil {
sched.initial.flatMap(loop(xs.toList, _, Nil)).map(_.reverse)
}

def checkRepeat[B](schedule: Schedule[Int, B], expected: B): ZIO[Any with Clock, Nothing, TestResult] =
def checkRepeat[B](schedule: Schedule[Any, Int, B], expected: B): ZIO[Any with Clock, Nothing, TestResult] =
assertM(repeat(schedule), equalTo(expected))

/**
Expand Down Expand Up @@ -382,7 +381,7 @@ object ZScheduleSpecUtil {
* A schedule that tracks how much time has elapsed using TestClock#fiberTime
*/
val testElapsed =
ZSchedule[TestClock, Duration, Any, Duration](
Schedule[TestClock, Duration, Any, Duration](
ZIO.succeed(Duration.Zero),
{ case _ => TestClock.fiberTime },
{ case (_, elapsed) => elapsed }
Expand Down
4 changes: 2 additions & 2 deletions core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,9 @@ object ZIOSpec
)
)(_ => log("start 2") *> clock.sleep(10.millis) *> log("release 2"))(_ => ZIO.unit)
.fork
_ <- (ref.get <* clock.sleep(1.millis)).repeat(ZSchedule.doUntil[List[String]](_.contains("start 1")))
_ <- (ref.get <* clock.sleep(1.millis)).repeat(Schedule.doUntil[List[String]](_.contains("start 1")))
_ <- f.interrupt
_ <- (ref.get <* clock.sleep(1.millis)).repeat(ZSchedule.doUntil[List[String]](_.contains("release 2")))
_ <- (ref.get <* clock.sleep(1.millis)).repeat(Schedule.doUntil[List[String]](_.contains("release 2")))
l <- ref.get
} yield l

Expand Down
8 changes: 4 additions & 4 deletions core-tests/shared/src/test/scala/zio/ZQueueSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ object ZQueueSpec
values = Range.inclusive(1, 10).toList
f <- IO.forkAll(values.map(queue.offer))
_ <- waitForSize(queue, 10)
l <- queue.take.repeat(ZSchedule.recurs(9) *> ZSchedule.identity[Int].collectAll)
l <- queue.take.repeat(Schedule.recurs(9) *> Schedule.identity[Int].collectAll)
_ <- f.join
} yield assert(l.toSet, equalTo(values.toSet))
},
testM("offers are suspended by back pressure") {
for {
queue <- Queue.bounded[Int](10)
_ <- queue.offer(1).repeat(ZSchedule.recurs(9))
_ <- queue.offer(1).repeat(Schedule.recurs(9))
refSuspended <- Ref.make[Boolean](true)
f <- (queue.offer(2) *> refSuspended.set(false)).fork
_ <- waitForSize(queue, 11)
Expand All @@ -67,7 +67,7 @@ object ZQueueSpec
values = Range.inclusive(1, 10).toList
_ <- IO.forkAll(values.map(queue.offer))
_ <- waitForSize(queue, 10)
l <- queue.take.repeat(ZSchedule.recurs(9) *> ZSchedule.identity[Int].collectAll)
l <- queue.take.repeat(Schedule.recurs(9) *> Schedule.identity[Int].collectAll)
} yield assert(l.toSet, equalTo(values.toSet))
},
testM("take interruption") {
Expand Down Expand Up @@ -739,7 +739,7 @@ object ZQueueSpec

object ZQueueSpecUtil {
def waitForValue[T](ref: UIO[T], value: T): UIO[T] =
(ref <* clock.sleep(10.millis)).repeat(ZSchedule.doWhile(_ != value)).provide(Clock.Live)
(ref <* clock.sleep(10.millis)).repeat(Schedule.doWhile(_ != value)).provide(Clock.Live)

def waitForSize[A](queue: Queue[A], size: Int): UIO[Int] =
waitForValue(queue.size, size)
Expand Down

0 comments on commit f06cb75

Please sign in to comment.