Skip to content

Commit

Permalink
Merge pull request #4171 from armanbilge/issue/4170
Browse files Browse the repository at this point in the history
Fix `size` updates in `Dequeue` state machine
  • Loading branch information
armanbilge authored Nov 18, 2024
2 parents dd1c0f5 + 2856a62 commit 921c69c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions std/shared/src/main/scala/cats/effect/std/Dequeue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ object Dequeue {
state.modify {
case State(queue, size, takers, offerers) if takers.nonEmpty =>
val (taker, rest) = takers.dequeue
State(update(queue), size, rest, offerers) -> taker.complete(()).void
State(update(queue), size + 1, rest, offerers) -> taker.complete(()).void

case State(queue, size, takers, offerers) if size < capacity =>
State(update(queue), size + 1, takers, offerers) -> F.unit
Expand Down Expand Up @@ -187,7 +187,7 @@ object Dequeue {
state.flatModify {
case State(queue, size, takers, offerers) if takers.nonEmpty =>
val (taker, rest) = takers.dequeue
State(update(queue), size, rest, offerers) -> taker.complete(()).as(true)
State(update(queue), size + 1, rest, offerers) -> taker.complete(()).as(true)

case State(queue, size, takers, offerers) if size < capacity =>
State(update(queue), size + 1, takers, offerers) -> F.pure(true)
Expand Down
14 changes: 14 additions & 0 deletions tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -965,5 +965,19 @@ trait QueueTests[Q[_[_], _]] { self: BaseSpec =>
r <- IO(v2 must beEqualTo(2))
} yield r
}

"should return the queue size when take precedes offer" in ticked { implicit ticker =>
constructor(10).flatMap { q =>
take(q).background.use { took => IO.sleep(1.second) *> offer(q, 1) *> took *> size(q) }
} must completeAs(0)
}

"should return the queue size when take precedes tryOffer" in ticked { implicit ticker =>
constructor(10).flatMap { q =>
take(q).background.use { took =>
IO.sleep(1.second) *> tryOffer(q, 1) *> took *> size(q)
}
} must completeAs(0)
}
}
}

0 comments on commit 921c69c

Please sign in to comment.