Use new Duration methods

This commit is contained in:
Him188 2021-11-18 02:58:42 +00:00
parent 0f1227d401
commit 46865721b0
4 changed files with 17 additions and 12 deletions

View File

@ -27,6 +27,7 @@ import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.coroutineContext
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.ExperimentalTime
/**
@ -69,7 +70,10 @@ public open class BotConfiguration { // open for Java
ignoreUnknownKeys = true
prettyPrint = true
}
}.getOrElse { Json {} }
}.getOrElse {
@Suppress("JSON_FORMAT_REDUNDANT_DEFAULT") // compatible for older versions
Json {}
}
///////////////////////////////////////////////////////////////////////////
// Coroutines
@ -253,14 +257,14 @@ public open class BotConfiguration { // open for Java
/**
* iPad - 来自MiraiGo
*
*
* @since 2.8
*/
IPAD,
/**
* MacOS - 来自MiraiGo
*
*
* @since 2.8
*/
MACOS,
@ -503,7 +507,7 @@ public open class BotConfiguration { // open for Java
*/
@ExperimentalTime
public inline var saveInterval: Duration
@JvmSynthetic inline get() = Duration.milliseconds(saveIntervalMillis)
@JvmSynthetic inline get() = saveIntervalMillis.milliseconds
@JvmSynthetic inline set(v) {
saveIntervalMillis = v.inWholeMilliseconds
}

View File

@ -19,7 +19,7 @@ import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.TestOnly
import java.util.concurrent.atomic.AtomicInteger
import kotlin.test.*
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
internal val selectorLogger = MiraiLogger.Factory.create(TestSelector::class, "selector")
@ -52,7 +52,7 @@ internal class KeepAliveNetworkHandlerSelectorTest : AbstractMockNetworkHandlerT
setState(State.OK)
}
}
runBlockingUnit(timeout = Duration.seconds(1)) { selector.awaitResumeInstance() }
runBlockingUnit(timeout = 1.seconds) { selector.awaitResumeInstance() }
assertNotNull(selector.getCurrentInstanceOrNull())
}
@ -75,7 +75,7 @@ internal class KeepAliveNetworkHandlerSelectorTest : AbstractMockNetworkHandlerT
selector.setCurrent(handler)
assertSame(handler, selector.getCurrentInstanceOrNull())
handler.setState(State.CLOSED)
runBlockingUnit(timeout = Duration.seconds(3)) { selector.awaitResumeInstance() }
runBlockingUnit(timeout = 3.seconds) { selector.awaitResumeInstance() }
assertEquals(1, selector.createInstanceCount.get())
}

View File

@ -19,7 +19,7 @@ import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
/**
* @see awaitKt
@ -36,7 +36,7 @@ internal class NettyUtilsTest : AbstractTest() {
}
@Test
fun canAwait() = runBlockingUnit(timeout = Duration.seconds(5)) {
fun canAwait() = runBlockingUnit(timeout = 5.seconds) {
val future = DefaultChannelPromise(channel)
launch(start = CoroutineStart.UNDISPATCHED) { future.awaitKt() }
launch {
@ -45,7 +45,7 @@ internal class NettyUtilsTest : AbstractTest() {
}
@Test
fun returnsImmediatelyIfCompleted() = runBlockingUnit(timeout = Duration.seconds(5)) {
fun returnsImmediatelyIfCompleted() = runBlockingUnit(timeout = 5.seconds) {
val future = DefaultChannelPromise(channel)
future.setSuccess()
future.awaitKt()
@ -55,7 +55,7 @@ internal class NettyUtilsTest : AbstractTest() {
fun testAwait() {
class MyError : AssertionError("My") // coroutine debugger will modify the exception if inside coroutine
runBlockingUnit(timeout = Duration.seconds(5)) {
runBlockingUnit(timeout = 5.seconds) {
val future = DefaultChannelPromise(channel)
launch(start = CoroutineStart.UNDISPATCHED) {
assertFailsWith<AssertionError> {

View File

@ -16,13 +16,14 @@ import kotlinx.coroutines.withTimeout
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
fun runBlockingUnit(
context: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> Unit
) {
return runBlocking(context) {
withTimeout(Duration.seconds(60)) { // always checks for infinite runs.
withTimeout(60.seconds) { // always checks for infinite runs.
block()
}
}