Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package zio.test.environment

import zio._
import zio.test.Assertion._
import zio.test.TestAspect.nonFlaky
import zio.test.{TestClock, ZIOBaseSpec, assert}
import zio.test.TestAspect.{nonFlaky, timeout}
import zio.test.{TestClock, ZIOBaseSpec, assert, assertCompletes}

import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -126,6 +126,11 @@ object TestClockSpecJVM extends ZIOBaseSpec {
_ <- ZIO.logInfo(s"Values after interruption: $values")
} yield assert(values.reverse)(equalTo(List(5L)))
}
),
suite("adjust")(
test("is thread safe") {
(TestClock.adjust(1.second) &> TestClock.adjust(1.second)).as(assertCompletes)
} @@ timeout(10.seconds)
)
) @@ nonFlaky(20)
}
19 changes: 13 additions & 6 deletions test/shared/src/main/scala/zio/test/TestClock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ object TestClock extends Serializable {
) extends TestClock
with TestClockPlatformSpecific {

/**
* See https://github.com/zio/zio/issues/9385
*/
private[this] val freezeLock = Semaphore.unsafe.make(1)(Unsafe)

/**
* Increments the current clock time by the specified duration. Any effects
* that were scheduled to occur on or before the new time will be run in
Expand Down Expand Up @@ -294,12 +299,14 @@ object TestClock extends Serializable {
* snapshot may not be fully consistent.
*/
private def freeze(implicit trace: Trace): IO[Unit, Map[FiberId, Fiber.Status]] =
supervisedFibers.flatMap { fibers =>
ZIO.foldLeft(fibers)(Map.empty[FiberId, Fiber.Status]) { (map, fiber) =>
fiber.status.flatMap {
case done @ Fiber.Status.Done => ZIO.succeed(map.updated(fiber.id, done))
case suspended @ Fiber.Status.Suspended(_, _, _) => ZIO.succeed(map.updated(fiber.id, suspended))
case _ => ZIO.fail(())
freezeLock.withPermit {
supervisedFibers.flatMap { fibers =>
ZIO.foldLeft(fibers)(Map.empty[FiberId, Fiber.Status]) { (map, fiber) =>
fiber.status.flatMap {
case done @ Fiber.Status.Done => ZIO.succeed(map.updated(fiber.id, done))
case suspended @ Fiber.Status.Suspended(_, _, _) => ZIO.succeed(map.updated(fiber.id, suspended))
case _ => ZIO.fail(())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case _ => ZIO.fail(())
case _ => Exit.failUnit

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tackling a lot of these in #9404, just need to iron out a few more things w.r.t the TestClock before I mark it as ready for review. Just wanted to have this PR separate since there is an open issue for it.

If that's okay, I'll leave it as is for this PR

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fine

}
}
}
}
Expand Down
Loading