Skip to content

Commit

Permalink
Merge pull request #3611 from armanbilge/issue/3585
Browse files Browse the repository at this point in the history
Treat non-positive sleep durations as `cede`s
  • Loading branch information
armanbilge authored May 11, 2023
2 parents 56fb90f + b0f7f6a commit 9236fb4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
27 changes: 15 additions & 12 deletions core/shared/src/main/scala/cats/effect/IOFiber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -909,21 +909,24 @@ private final class IOFiber[A](

case 19 =>
val cur = cur0.asInstanceOf[Sleep]
val delay = cur.delay

val next = IO.async[Unit] { cb =>
IO {
val scheduler = runtime.scheduler
val delay = cur.delay
val next =
if (delay.length > 0)
IO.async[Unit] { cb =>
IO {
val scheduler = runtime.scheduler

val cancel =
if (scheduler.isInstanceOf[WorkStealingThreadPool])
scheduler.asInstanceOf[WorkStealingThreadPool].sleepInternal(delay, cb)
else
scheduler.sleep(delay, () => cb(RightUnit))
val cancel =
if (scheduler.isInstanceOf[WorkStealingThreadPool])
scheduler.asInstanceOf[WorkStealingThreadPool].sleepInternal(delay, cb)
else
scheduler.sleep(delay, () => cb(RightUnit))

Some(IO(cancel.run()))
}
}
Some(IO(cancel.run()))
}
}
else IO.cede

runLoop(next, nextCancelation, nextAutoCede)

Expand Down
4 changes: 4 additions & 0 deletions tests/shared/src/test/scala/cats/effect/IOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,10 @@ class IOSpec extends BaseSpec with Discipline with IOPlatformSpecification {
affected must beTrue
}

"round up negative sleeps" in real {
IO.sleep(-1.seconds).as(ok)
}

"timeout" should {
"succeed" in real {
val op = IO.pure(true).timeout(100.millis)
Expand Down

0 comments on commit 9236fb4

Please sign in to comment.