Skip to content

Commit

Permalink
Fix BlockHound false-positive in ConflatedChannel
Browse files Browse the repository at this point in the history
Fixes #2866
  • Loading branch information
qwwdfsad committed Aug 13, 2021
1 parent e123c8a commit ddb48c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Expand Up @@ -133,7 +133,7 @@ public class CoroutinesBlockHoundIntegration : BlockHoundIntegration {
*/
private fun BlockHound.Builder.allowBlockingCallsInConflatedChannel() {
for (method in listOf("offerInternal", "offerSelectInternal", "pollInternal", "pollSelectInternal",
"onCancelIdempotent"))
"onCancelIdempotent", "isEmpty", "enqueueReceiveInternal"))
{
allowBlockingCallsInside("kotlinx.coroutines.channels.ConflatedChannel", method)
}
Expand Down
27 changes: 23 additions & 4 deletions kotlinx-coroutines-debug/test/BlockHoundTest.kt
Expand Up @@ -4,6 +4,7 @@ import kotlinx.coroutines.channels.*
import org.junit.*
import reactor.blockhound.*

@Suppress("UnusedEquals", "DeferredResultUnused", "BlockingMethodInNonBlockingContext")
class BlockHoundTest : TestBase() {

@Before
Expand All @@ -12,21 +13,21 @@ class BlockHoundTest : TestBase() {
}

@Test(expected = BlockingOperationError::class)
fun shouldDetectBlockingInDefault() = runTest {
fun testShouldDetectBlockingInDefault() = runTest {
withContext(Dispatchers.Default) {
Thread.sleep(1)
}
}

@Test
fun shouldNotDetectBlockingInIO() = runTest {
fun testShouldNotDetectBlockingInIO() = runTest {
withContext(Dispatchers.IO) {
Thread.sleep(1)
}
}

@Test
fun shouldNotDetectNonblocking() = runTest {
fun testShouldNotDetectNonblocking() = runTest {
withContext(Dispatchers.Default) {
val a = 1
val b = 2
Expand Down Expand Up @@ -54,7 +55,7 @@ class BlockHoundTest : TestBase() {
}

@Test
fun testChannelsNotBeingConsideredBlocking() = runTest {
fun testChannelNotBeingConsideredBlocking() = runTest {
withContext(Dispatchers.Default) {
// Copy of kotlinx.coroutines.channels.ArrayChannelTest.testSimple
val q = Channel<Int>(1)
Expand All @@ -74,6 +75,24 @@ class BlockHoundTest : TestBase() {
}
}

@Test
fun testConflatedChannelsNotBeingConsideredBlocking() = runTest {
withContext(Dispatchers.Default) {
val q = Channel<Int>(Channel.CONFLATED)
check(q.isEmpty)
check(!q.isClosedForReceive)
check(!q.isClosedForSend)
val sender = launch {
q.send(1)
}
val receiver = launch {
q.receive() == 1
}
sender.join()
receiver.join()
}
}

@Test(expected = BlockingOperationError::class)
fun testReusingThreadsFailure() = runTest {
val n = 100
Expand Down

0 comments on commit ddb48c7

Please sign in to comment.