Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cancel current Job on RejectedExecutionException
When the Executor that was used with Executor.asCoroutineDispatcher() extension rejects submitted task, it means that it had reached its capacity and so the executing current Job should be cancelled to terminate it as soon as possible. This way RejectedExecutionException works as a rate-limiter just like it serves this purpose in executor-based Java code. Fixes #2003
- Loading branch information
Showing
28 changed files
with
175 additions
and
42 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
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
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
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
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
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
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
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
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
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
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
119 changes: 119 additions & 0 deletions
119
kotlinx-coroutines-core/jvm/test/RejectedExecutionTest.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,119 @@ | ||
/* | ||
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines | ||
|
||
import org.junit.* | ||
import org.junit.Test | ||
import java.util.concurrent.* | ||
import kotlin.test.* | ||
|
||
class RejectedExecutionTest : TestBase() { | ||
private val threadName = "RejectedExecutionTest" | ||
private val executor = RejectingExecutor() | ||
|
||
@After | ||
fun tearDown() { | ||
executor.shutdown() | ||
executor.awaitTermination(10, TimeUnit.SECONDS) | ||
} | ||
|
||
@Test | ||
fun testRejectOnLaunch() = runTest { | ||
expect(1) | ||
val job = launch(executor.asCoroutineDispatcher()) { | ||
expectUnreached() | ||
} | ||
assertEquals(1, executor.submittedTasks) | ||
assertTrue(job.isCancelled) | ||
finish(2) | ||
} | ||
|
||
@Test | ||
fun testRejectOnLaunchAtomic() = runTest { | ||
expect(1) | ||
val job = launch(executor.asCoroutineDispatcher(), start = CoroutineStart.ATOMIC) { | ||
expect(2) | ||
assertEquals(true, coroutineContext[Job]?.isCancelled) | ||
assertNotSame(threadName, Thread.currentThread().name) // should have got dispatched on the DefaultExecutor | ||
} | ||
assertEquals(1, executor.submittedTasks) | ||
job.join() | ||
finish(3) | ||
} | ||
|
||
@Test | ||
fun testRejectOnWithContext() = runTest { | ||
expect(1) | ||
assertFailsWith<CancellationException> { | ||
withContext(executor.asCoroutineDispatcher()) { | ||
expectUnreached() | ||
} | ||
} | ||
assertEquals(1, executor.submittedTasks) | ||
finish(2) | ||
} | ||
|
||
@Test | ||
fun testRejectOnResumeInContext() = runTest { | ||
expect(1) | ||
executor.acceptTasks = 1 // accept one task | ||
assertFailsWith<CancellationException> { | ||
withContext(executor.asCoroutineDispatcher()) { | ||
expect(2) | ||
withContext(Dispatchers.Default) { | ||
expect(3) | ||
} | ||
// cancelled on resume back | ||
expectUnreached() | ||
} | ||
} | ||
assertEquals(2, executor.submittedTasks) | ||
finish(4) | ||
} | ||
|
||
@Test | ||
fun testRejectOnDelay() = runTest { | ||
expect(1) | ||
executor.acceptTasks = 1 // accept one task | ||
assertFailsWith<CancellationException> { | ||
withContext(executor.asCoroutineDispatcher()) { | ||
expect(2) | ||
delay(10) // cancelled | ||
expectUnreached() | ||
} | ||
} | ||
assertEquals(2, executor.submittedTasks) | ||
finish(3) | ||
} | ||
|
||
@Test | ||
fun testRejectWithTimeout() = runTest { | ||
expect(1) | ||
executor.acceptTasks = 1 // accept one task | ||
assertFailsWith<CancellationException> { | ||
withContext(executor.asCoroutineDispatcher()) { | ||
expect(2) | ||
withTimeout(1000) { | ||
expect(3) // atomic entry into the block (legacy behavior, it seem to be Ok with way) | ||
assertEquals(true, coroutineContext[Job]?.isCancelled) // but the job is already cancelled | ||
} | ||
expectUnreached() | ||
} | ||
} | ||
assertEquals(2, executor.submittedTasks) | ||
finish(4) | ||
} | ||
|
||
private inner class RejectingExecutor : ScheduledThreadPoolExecutor(1, { r -> Thread(r, threadName) }) { | ||
var acceptTasks = 0 | ||
var submittedTasks = 0 | ||
|
||
override fun schedule(command: Runnable, delay: Long, unit: TimeUnit): ScheduledFuture<*> { | ||
submittedTasks++ | ||
if (submittedTasks > acceptTasks) throw RejectedExecutionException() | ||
return super.schedule(command, delay, unit) | ||
} | ||
} | ||
} |
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
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
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
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
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
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
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
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
Oops, something went wrong.