-
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,123 @@ | ||
| 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 { | ||
murfel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| TestChannelKind.entries.forEach { kind -> | ||
| val channel = kind.create<Int>() | ||
| channel.close() | ||
| channel.toList() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
Collaborator
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 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.