-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide API to create limited view of experimental dispatcher
Fixes #475
- Loading branch information
Showing
4 changed files
with
123 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
core/kotlinx-coroutines-core/test/scheduling/LimitingCoroutineDispatcherStressTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package kotlinx.coroutines.experimental.scheduling | ||
|
||
import kotlinx.atomicfu.* | ||
import kotlinx.coroutines.experimental.* | ||
import org.junit.Test | ||
import kotlin.coroutines.experimental.* | ||
import kotlin.test.* | ||
|
||
class LimitingCoroutineDispatcherStressTest : SchedulerTestBase() { | ||
|
||
init { | ||
corePoolSize = 3 | ||
} | ||
|
||
private val blocking = blockingDispatcher(2) | ||
private val cpuView = view(2) | ||
private val cpuView2 = view(2) | ||
private val concurrentWorkers = atomic(0) | ||
private val iterations = 25_000 * stressTestMultiplierSqrt | ||
|
||
@Test | ||
fun testCpuLimitNotExtended() = runBlocking<Unit> { | ||
val tasks = ArrayList<Deferred<*>>(iterations * 2) | ||
repeat(iterations) { | ||
tasks += task(cpuView, 3) | ||
tasks += task(cpuView2, 3) | ||
} | ||
|
||
tasks.awaitAll() | ||
} | ||
|
||
@Test | ||
fun testCpuLimitWithBlocking() = runBlocking<Unit> { | ||
val tasks = ArrayList<Deferred<*>>(iterations * 2) | ||
repeat(iterations) { | ||
tasks += task(cpuView, 4) | ||
tasks += task(blocking, 4) | ||
} | ||
|
||
tasks.awaitAll() | ||
} | ||
|
||
private fun task(ctx: CoroutineContext, maxLimit: Int): Deferred<Unit> { | ||
return async(ctx) { | ||
try { | ||
val currentlyExecuting = concurrentWorkers.incrementAndGet() | ||
assertTrue(currentlyExecuting <= maxLimit, "Executing: $currentlyExecuting, max limit: $maxLimit") | ||
} finally { | ||
concurrentWorkers.decrementAndGet() | ||
} | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
core/kotlinx-coroutines-core/test/scheduling/LimitingDispatcherTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package kotlinx.coroutines.experimental.scheduling | ||
|
||
import kotlinx.coroutines.experimental.* | ||
import org.junit.* | ||
import java.util.concurrent.* | ||
|
||
class LimitingDispatcherTest : SchedulerTestBase() { | ||
|
||
@Test(expected = IllegalArgumentException::class) | ||
fun testTooLargeView() { | ||
view(corePoolSize + 1) | ||
} | ||
|
||
@Test(expected = IllegalArgumentException::class) | ||
fun testNegativeView() { | ||
view(-1) | ||
} | ||
|
||
@Test(expected = IllegalArgumentException::class) | ||
fun testZeroView() { | ||
view(0) | ||
} | ||
|
||
@Test(timeout = 10_000) | ||
fun testBlockingInterleave() = runBlocking { | ||
corePoolSize = 3 | ||
val view = view(2) | ||
val blocking = blockingDispatcher(4) | ||
val barrier = CyclicBarrier(6) | ||
val tasks = ArrayList<Job>(6) | ||
repeat(2) { | ||
tasks += async(view) { | ||
barrier.await() | ||
} | ||
|
||
repeat(2) { | ||
tasks += async(blocking) { | ||
barrier.await() | ||
} | ||
} | ||
} | ||
|
||
tasks.joinAll() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters