Skip to content

Commit 8695f97

Browse files
committed
Undeprecate ReceiveChannel.isEmpty
1 parent 5d8e893 commit 8695f97

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

kotlinx-coroutines-core/common/src/channels/AbstractChannel.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,7 @@ internal abstract class AbstractChannel<E> : AbstractSendChannel<E>(), Channel<E
562562
// ------ ReceiveChannel ------
563563

564564
public final override val isClosedForReceive: Boolean get() = closedForReceive != null && isBufferEmpty
565-
public final override val isEmpty: Boolean get() = empty
566-
private val empty: Boolean get() = queue.nextNode !is Send && isBufferEmpty // TODO rename to `isEmpty`
565+
public final override val isEmpty: Boolean get() = queue.nextNode !is Send && isBufferEmpty
567566

568567
@Suppress("UNCHECKED_CAST")
569568
public final override suspend fun receive(): E {
@@ -750,7 +749,7 @@ internal abstract class AbstractChannel<E> : AbstractSendChannel<E>(), Channel<E
750749
private fun <R> registerSelectReceive(select: SelectInstance<R>, block: suspend (E) -> R) {
751750
while (true) {
752751
if (select.isSelected) return
753-
if (empty) {
752+
if (isEmpty) {
754753
val enqueueOp = TryEnqueueReceiveDesc(select, block as (suspend (E?) -> R), nullOnClose = false)
755754
val enqueueResult = select.performAtomicIfNotSelected(enqueueOp) ?: return
756755
when {
@@ -784,7 +783,7 @@ internal abstract class AbstractChannel<E> : AbstractSendChannel<E>(), Channel<E
784783
private fun <R> registerSelectReceiveOrNull(select: SelectInstance<R>, block: suspend (E?) -> R) {
785784
while (true) {
786785
if (select.isSelected) return
787-
if (empty) {
786+
if (isEmpty) {
788787
val enqueueOp = TryEnqueueReceiveDesc(select, block, nullOnClose = true)
789788
val enqueueResult = select.performAtomicIfNotSelected(enqueueOp) ?: return
790789
when {

kotlinx-coroutines-core/common/src/channels/Channel.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,8 @@ public interface ReceiveChannel<out E> {
151151
/**
152152
* Returns `true` if the channel is empty (contains no elements) and the [receive] attempt will suspend.
153153
* This function returns `false` for [isClosedForReceive] channel.
154-
*
155-
* @suppress **Will be removed in next releases, no replacement.**
156154
*/
157155
@ExperimentalCoroutinesApi
158-
@Deprecated(level = DeprecationLevel.ERROR, message = "Will be removed in next releases without replacement")
159156
public val isEmpty: Boolean
160157

161158
/**

kotlinx-coroutines-core/common/test/channels/ArrayChannelTest.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ class ArrayChannelTest : TestBase() {
1111
@Test
1212
fun testSimple() = runTest {
1313
val q = Channel<Int>(1)
14+
check(q.isEmpty)
1415
expect(1)
1516
val sender = launch {
1617
expect(4)
1718
q.send(1) // success -- buffered
19+
check(!q.isEmpty)
1820
expect(5)
1921
q.send(2) // suspends (buffer full)
2022
expect(9)
@@ -23,27 +25,29 @@ class ArrayChannelTest : TestBase() {
2325
val receiver = launch {
2426
expect(6)
2527
check(q.receive() == 1) // does not suspend -- took from buffer
28+
check(!q.isEmpty) // waiting sender's element moved to buffer
2629
expect(7)
2730
check(q.receive() == 2) // does not suspend (takes from sender)
2831
expect(8)
2932
}
3033
expect(3)
3134
sender.join()
3235
receiver.join()
36+
check(q.isEmpty)
3337
finish(10)
3438
}
3539

3640
@Test
3741
fun testClosedBufferedReceiveOrNull() = runTest {
3842
val q = Channel<Int>(1)
39-
check(!q.isClosedForSend && !q.isClosedForReceive)
43+
check(q.isEmpty && !q.isClosedForSend && !q.isClosedForReceive)
4044
expect(1)
4145
launch {
4246
expect(5)
43-
check(q.isClosedForSend && !q.isClosedForReceive)
47+
check(!q.isEmpty && q.isClosedForSend && !q.isClosedForReceive)
4448
assertEquals(42, q.receiveOrNull())
4549
expect(6)
46-
check(q.isClosedForSend && q.isClosedForReceive)
50+
check(!q.isEmpty && q.isClosedForSend && q.isClosedForReceive)
4751
assertEquals(null, q.receiveOrNull())
4852
expect(7)
4953
}
@@ -52,9 +56,9 @@ class ArrayChannelTest : TestBase() {
5256
expect(3)
5357
q.close() // goes on
5458
expect(4)
55-
check(q.isClosedForSend && !q.isClosedForReceive)
59+
check(!q.isEmpty && q.isClosedForSend && !q.isClosedForReceive)
5660
yield()
57-
check(q.isClosedForSend && q.isClosedForReceive)
61+
check(!q.isEmpty && q.isClosedForSend && q.isClosedForReceive)
5862
finish(8)
5963
}
6064

kotlinx-coroutines-core/common/test/channels/RendezvousChannelTest.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class RendezvousChannelTest : TestBase() {
1111
@Test
1212
fun testSimple() = runTest {
1313
val q = Channel<Int>(Channel.RENDEZVOUS)
14+
check(q.isEmpty)
1415
expect(1)
1516
val sender = launch {
1617
expect(4)
@@ -30,13 +31,14 @@ class RendezvousChannelTest : TestBase() {
3031
expect(3)
3132
sender.join()
3233
receiver.join()
34+
check(q.isEmpty)
3335
finish(10)
3436
}
3537

3638
@Test
3739
fun testClosedReceiveOrNull() = runTest {
3840
val q = Channel<Int>(Channel.RENDEZVOUS)
39-
check(!q.isClosedForSend && !q.isClosedForReceive)
41+
check(q.isEmpty && !q.isClosedForSend && !q.isClosedForReceive)
4042
expect(1)
4143
launch {
4244
expect(3)
@@ -49,9 +51,9 @@ class RendezvousChannelTest : TestBase() {
4951
q.send(42)
5052
expect(5)
5153
q.close()
52-
check(q.isClosedForSend && q.isClosedForReceive)
54+
check(!q.isEmpty && q.isClosedForSend && q.isClosedForReceive)
5355
yield()
54-
check(q.isClosedForSend && q.isClosedForReceive)
56+
check(!q.isEmpty && q.isClosedForSend && q.isClosedForReceive)
5557
finish(7)
5658
}
5759

@@ -252,7 +254,7 @@ class RendezvousChannelTest : TestBase() {
252254
expect(1)
253255
send(bad)
254256
}
255-
assertTrue(c.receive() === bad)
257+
assertSame(c.receive(), bad)
256258
finish(2)
257259
}
258260

kotlinx-coroutines-core/common/test/channels/TestChannelKind.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ private class ChannelViaBroadcast<E>(
5454
val sub = broadcast.openSubscription()
5555

5656
override val isClosedForReceive: Boolean get() = sub.isClosedForReceive
57-
@Suppress("DEPRECATION_ERROR")
5857
override val isEmpty: Boolean get() = sub.isEmpty
5958

6059
override suspend fun receive(): E = sub.receive()

0 commit comments

Comments
 (0)