-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add tests for Channel closed with an exception #4517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,196 @@ | ||||||
package kotlinx.coroutines.channels | ||||||
|
||||||
import kotlinx.coroutines.testing.* | ||||||
import kotlin.test.* | ||||||
|
||||||
class ClosedChannelTest : TestBase() { | ||||||
/** | ||||||
* Iterator. | ||||||
*/ | ||||||
|
||||||
@Test | ||||||
fun testIteratorClosedHasNextFalse() = runTest { | ||||||
TestChannelKind.entries.forEach { kind -> | ||||||
val channel = kind.create<Int>() | ||||||
val iterator = channel.iterator() | ||||||
channel.close() | ||||||
assertFalse(iterator.hasNext()) | ||||||
} | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun testIteratorClosedWithExceptionHasNextThrows() = runTest { | ||||||
TestChannelKind.entries.forEach { kind -> | ||||||
val channel = kind.create<Int>() | ||||||
val iterator = channel.iterator() | ||||||
channel.close(TestException()) | ||||||
assertFailsWith<TestException> { (iterator.hasNext()) } | ||||||
} | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun testClosedToListOk() = runTest { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, this test is not part of the |
||||||
TestChannelKind.entries.forEach { kind -> | ||||||
val channel = kind.create<Int>() | ||||||
channel.close() | ||||||
channel.toList() | ||||||
} | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun testClosedWithExceptionToListThrows() = runTest { | ||||||
TestChannelKind.entries.forEach { kind -> | ||||||
val channel = kind.create<Int>() | ||||||
channel.close(TestException()) | ||||||
assertFailsWith<TestException> { channel.toList() } | ||||||
} | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicates |
||||||
|
||||||
@Test | ||||||
fun testClosedConsumeEachOk() = runTest { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicates |
||||||
TestChannelKind.entries.forEach { kind -> | ||||||
val channel = kind.create<Int>() | ||||||
channel.close() | ||||||
channel.consumeEach { } | ||||||
} | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun testClosedWithExceptionConsumeEachThrows() = runTest { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicates |
||||||
TestChannelKind.entries.forEach { kind -> | ||||||
val channel = kind.create<Int>() | ||||||
channel.close(TestException()) | ||||||
assertFailsWith<TestException> { channel.consumeEach { } } | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* toList() alternative when a channel might close with an exception. | ||||||
*/ | ||||||
|
||||||
@Test | ||||||
fun testToListCatching() = runTest { | ||||||
TestChannelKind.entries.forEach { kind -> | ||||||
val channel = kind.create<Unit>() | ||||||
channel.close(TestException()) | ||||||
val lst = mutableListOf<Unit>() | ||||||
while (true) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this loop necessary? Only one iteration of it will run. channel.close(TestException())
val result = channel.receiveCatching()
result.onClosed { // suspends
assertTrue(it is TestException)
}
assertTrue(result.isClosed) This is covered at least by |
||||||
channel.receiveCatching().onClosed { // suspends | ||||||
assertTrue(it is TestException) | ||||||
break | ||||||
}.getOrThrow().apply(lst::add) | ||||||
} | ||||||
assertTrue(lst.isEmpty()) | ||||||
} | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun testTryToListCatching() = runTest { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||||||
TestChannelKind.entries.forEach { kind -> | ||||||
val channel = kind.create<Unit>() | ||||||
channel.close(TestException()) | ||||||
val lst = mutableListOf<Unit>() | ||||||
while (true) { | ||||||
channel.tryReceive().onClosed { // does not suspend | ||||||
assertTrue(it is TestException) | ||||||
break | ||||||
}.onFailure { | ||||||
// empty, but not yet closed | ||||||
break | ||||||
}.getOrThrow().apply(lst::add) | ||||||
} | ||||||
assertTrue(lst.isEmpty()) | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This syntax means the comment is a KDoc and refers to the entity directly below it. To make this a normal multiline comment for separating groups of tests, use That said, I think the separation is easy to break accidentally. Why not introduce separate classes to make this more robust? class ClosedChannelTest: TestBase() {
/**
* Properties.
*
* Properties stay the same regardless of whether the channel was closed with or without exception.
*/
class PropertiesTests: TestBase() {
}
} |
||||||
* Properties. | ||||||
* | ||||||
* Properties stay the same regardless of whether the channel was closed with or without exception. | ||||||
*/ | ||||||
|
||||||
@Test | ||||||
fun testClosedIsClosedForReceive() = runTest { | ||||||
TestChannelKind.entries.forEach { kind -> | ||||||
val channel = kind.create<Int>() | ||||||
assertFalse(channel.isClosedForReceive) | ||||||
channel.close() | ||||||
assertTrue(channel.isClosedForReceive) | ||||||
} | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun testClosedWithExceptionIsClosedForReceive() = runTest { | ||||||
TestChannelKind.entries.forEach { kind -> | ||||||
val channel = kind.create<Int>() | ||||||
assertFalse(channel.isClosedForReceive) | ||||||
channel.close(TestException()) | ||||||
assertTrue(channel.isClosedForReceive) | ||||||
} | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun testIsEmpty() = runTest { | ||||||
val channel = Channel<Int>() | ||||||
assertTrue(channel.isEmpty) | ||||||
assertFalse(channel.isClosedForReceive) | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unrelated to closing channels. |
||||||
|
||||||
@Test | ||||||
fun testClosedIsEmptyFalse() = runTest { | ||||||
TestChannelKind.entries.forEach { kind -> | ||||||
val channel = kind.create<Int>() | ||||||
assertTrue(channel.isEmpty) | ||||||
assertFalse(channel.isClosedForReceive) | ||||||
channel.close() | ||||||
// NB! Not obvious. | ||||||
assertFalse(channel.isEmpty) | ||||||
assertTrue(channel.isClosedForReceive) | ||||||
} | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun testClosedWithExceptionIsEmptyFalse() = runTest { | ||||||
TestChannelKind.entries.forEach { kind -> | ||||||
val channel = kind.create<Int>() | ||||||
assertTrue(channel.isEmpty) | ||||||
assertFalse(channel.isClosedForReceive) | ||||||
channel.close(TestException()) | ||||||
assertFalse(channel.isEmpty) | ||||||
assertTrue(channel.isClosedForReceive) | ||||||
} | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun testSendClosedReceiveIsEmptyFalse() = runTest { | ||||||
val channel = Channel<Int>(1) | ||||||
assertTrue(channel.isEmpty) | ||||||
assertFalse(channel.isClosedForReceive) | ||||||
channel.send(1) | ||||||
assertFalse(channel.isEmpty) | ||||||
assertFalse(channel.isClosedForReceive) | ||||||
channel.close() | ||||||
assertFalse(channel.isEmpty) | ||||||
assertFalse(channel.isClosedForReceive) | ||||||
channel.receive() | ||||||
assertFalse(channel.isEmpty) | ||||||
assertTrue(channel.isClosedForReceive) | ||||||
} | ||||||
|
||||||
@Test | ||||||
fun testSendClosedWithExceptionReceiveIsEmptyFalse() = runTest { | ||||||
val channel = Channel<Int>(1) | ||||||
assertTrue(channel.isEmpty) | ||||||
assertFalse(channel.isClosedForReceive) | ||||||
channel.send(1) | ||||||
assertFalse(channel.isEmpty) | ||||||
assertFalse(channel.isClosedForReceive) | ||||||
channel.close(TestException()) | ||||||
assertFalse(channel.isEmpty) | ||||||
assertFalse(channel.isClosedForReceive) | ||||||
channel.receive() | ||||||
assertFalse(channel.isEmpty) | ||||||
assertTrue(channel.isClosedForReceive) | ||||||
} | ||||||
|
||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Proposal:
May be more convenient, with how often this pattern occurs.