mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-10 12:10:10 +08:00
Make NettyUtilsTest faster
This commit is contained in:
parent
ff6c63fafc
commit
e0c0a189dc
@ -11,7 +11,7 @@ package net.mamoe.mirai.internal.network.impl.netty
|
|||||||
|
|
||||||
import io.netty.channel.DefaultChannelPromise
|
import io.netty.channel.DefaultChannelPromise
|
||||||
import io.netty.channel.embedded.EmbeddedChannel
|
import io.netty.channel.embedded.EmbeddedChannel
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.CoroutineStart
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import net.mamoe.mirai.internal.test.AbstractTest
|
import net.mamoe.mirai.internal.test.AbstractTest
|
||||||
import net.mamoe.mirai.internal.test.runBlockingUnit
|
import net.mamoe.mirai.internal.test.runBlockingUnit
|
||||||
@ -19,7 +19,7 @@ import org.junit.jupiter.api.AfterAll
|
|||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import kotlin.test.assertFailsWith
|
import kotlin.test.assertFailsWith
|
||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
import kotlin.time.seconds
|
import kotlin.time.Duration
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see awaitKt
|
* @see awaitKt
|
||||||
@ -36,17 +36,16 @@ internal class NettyUtilsTest : AbstractTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun canAwait() = runBlockingUnit(timeout = 5.seconds) {
|
fun canAwait() = runBlockingUnit(timeout = Duration.seconds(5)) {
|
||||||
val future = DefaultChannelPromise(channel)
|
val future = DefaultChannelPromise(channel)
|
||||||
|
launch(start = CoroutineStart.UNDISPATCHED) { future.awaitKt() }
|
||||||
launch {
|
launch {
|
||||||
delay(2000)
|
|
||||||
future.setSuccess()
|
future.setSuccess()
|
||||||
}
|
}
|
||||||
future.awaitKt()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun returnsImmediatelyIfCompleted() = runBlockingUnit(timeout = 5.seconds) {
|
fun returnsImmediatelyIfCompleted() = runBlockingUnit(timeout = Duration.seconds(5)) {
|
||||||
val future = DefaultChannelPromise(channel)
|
val future = DefaultChannelPromise(channel)
|
||||||
future.setSuccess()
|
future.setSuccess()
|
||||||
future.awaitKt()
|
future.awaitKt()
|
||||||
@ -56,17 +55,18 @@ internal class NettyUtilsTest : AbstractTest() {
|
|||||||
fun testAwait() {
|
fun testAwait() {
|
||||||
class MyError : AssertionError("My") // coroutine debugger will modify the exception if inside coroutine
|
class MyError : AssertionError("My") // coroutine debugger will modify the exception if inside coroutine
|
||||||
|
|
||||||
runBlockingUnit(timeout = 5.seconds) {
|
runBlockingUnit(timeout = Duration.seconds(5)) {
|
||||||
val future = DefaultChannelPromise(channel)
|
val future = DefaultChannelPromise(channel)
|
||||||
launch {
|
launch(start = CoroutineStart.UNDISPATCHED) {
|
||||||
delay(2000)
|
|
||||||
future.setFailure(MyError())
|
|
||||||
}
|
|
||||||
assertFailsWith<AssertionError> {
|
assertFailsWith<AssertionError> {
|
||||||
future.awaitKt()
|
future.awaitKt()
|
||||||
}.let { actual ->
|
}.let { actual ->
|
||||||
assertTrue { actual is MyError }
|
assertTrue { actual is MyError }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
launch {
|
||||||
|
future.setFailure(MyError())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user