-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #832 from simple-robot/pref-jvm-blocking-runner
优化 `BlockingRunner` 内部实现
- Loading branch information
Showing
2 changed files
with
144 additions
and
49 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
72 changes: 72 additions & 0 deletions
72
...-suspend-runner/src/jvmTest/kotlin/love/forte/simbot/suspendrunner/BlockingRunnerTests.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,72 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* Project https://github.com/simple-robot/simpler-robot | ||
* Email [email protected] | ||
* | ||
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.). | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Lesser GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the Lesser GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package love.forte.simbot.suspendrunner | ||
|
||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
import kotlin.test.Test | ||
import kotlin.test.assertFails | ||
import kotlin.test.assertIs | ||
|
||
|
||
/** | ||
* | ||
* @author ForteScarlet | ||
*/ | ||
class BlockingRunnerTests { | ||
@Test | ||
fun runBlockingExceptionallyTest() { | ||
runInNoScopeBlocking { runNormally() } | ||
|
||
assertIs<RunException>( | ||
assertFails { | ||
runInNoScopeBlocking { runExceptionally1() } | ||
} | ||
) | ||
|
||
assertIs<RunException>( | ||
assertFails { | ||
runInNoScopeBlocking { runExceptionally2() } | ||
} | ||
) | ||
} | ||
|
||
private suspend fun runNormally() = suspendCancellableCoroutine { continuation -> | ||
continuation.resume(0) | ||
} | ||
|
||
private suspend fun runExceptionally1() { | ||
delay(1) | ||
throw RunException() | ||
} | ||
|
||
private suspend fun runExceptionally2() = suspendCancellableCoroutine<Int> { continuation -> | ||
continuation.resumeWithException(RunException()) | ||
} | ||
|
||
|
||
private class RunException : RuntimeException() | ||
} |