From f5482db26ecce9c40d2b85e2673cd3e1fdec694a Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Mon, 18 Nov 2024 01:09:10 +0000 Subject: [PATCH 1/4] Add failing test for https://github.com/typelevel/cats-effect/issues/4170 --- tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala b/tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala index 2dee60880b..669af7f974 100644 --- a/tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala +++ b/tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala @@ -965,5 +965,11 @@ 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) + } } } From 590df8559dc5a5d72f8dcb7bb57bb704932baae0 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Mon, 18 Nov 2024 01:10:39 +0000 Subject: [PATCH 2/4] Fix missing increment in `Dequeue#offer` Co-authored-by: Justin Reardon --- std/shared/src/main/scala/cats/effect/std/Dequeue.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/shared/src/main/scala/cats/effect/std/Dequeue.scala b/std/shared/src/main/scala/cats/effect/std/Dequeue.scala index aef920368d..abad429650 100644 --- a/std/shared/src/main/scala/cats/effect/std/Dequeue.scala +++ b/std/shared/src/main/scala/cats/effect/std/Dequeue.scala @@ -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 From c7447bf12c7183dc0c4e42fcfd993f6927252edf Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Mon, 18 Nov 2024 01:15:23 +0000 Subject: [PATCH 3/4] Analog fix for `tryOffer` --- std/shared/src/main/scala/cats/effect/std/Dequeue.scala | 2 +- tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/std/shared/src/main/scala/cats/effect/std/Dequeue.scala b/std/shared/src/main/scala/cats/effect/std/Dequeue.scala index abad429650..50dae78542 100644 --- a/std/shared/src/main/scala/cats/effect/std/Dequeue.scala +++ b/std/shared/src/main/scala/cats/effect/std/Dequeue.scala @@ -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) diff --git a/tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala b/tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala index 669af7f974..b082bdcb4c 100644 --- a/tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala +++ b/tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala @@ -971,5 +971,11 @@ trait QueueTests[Q[_[_], _]] { self: BaseSpec => 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) + } } } From 2856a62ebffa0ad2daab6bfa076e7fc060571f9d Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Mon, 18 Nov 2024 01:20:18 +0000 Subject: [PATCH 4/4] Formatting --- tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala b/tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala index b082bdcb4c..40237e9e78 100644 --- a/tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala +++ b/tests/shared/src/test/scala/cats/effect/std/QueueSpec.scala @@ -974,7 +974,9 @@ trait QueueTests[Q[_[_], _]] { self: BaseSpec => "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) } + take(q).background.use { took => + IO.sleep(1.second) *> tryOffer(q, 1) *> took *> size(q) + } } must completeAs(0) } }