Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
ca216bc4
Commit
ca216bc4
authored
Apr 21, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix `syncFromEvent`, add Tests
parent
422e8a9b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
15 deletions
+78
-15
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
...ore/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
+17
-15
mirai-core/src/commonTest/kotlin/net/mamoe/mirai/message/SubscribingGetTest.kt
...Test/kotlin/net/mamoe/mirai/message/SubscribingGetTest.kt
+61
-0
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/linear.kt
View file @
ca216bc4
...
@@ -15,7 +15,6 @@ import kotlinx.coroutines.*
...
@@ -15,7 +15,6 @@ import kotlinx.coroutines.*
import
net.mamoe.mirai.utils.SinceMirai
import
net.mamoe.mirai.utils.SinceMirai
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.EmptyCoroutineContext
import
kotlin.coroutines.EmptyCoroutineContext
import
kotlin.coroutines.suspendCoroutine
import
kotlin.jvm.JvmSynthetic
import
kotlin.jvm.JvmSynthetic
import
kotlin.reflect.KClass
import
kotlin.reflect.KClass
...
@@ -37,7 +36,16 @@ suspend inline fun <reified E : Event, R : Any> syncFromEvent(
...
@@ -37,7 +36,16 @@ suspend inline fun <reified E : Event, R : Any> syncFromEvent(
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
):
R
{
):
R
{
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or > 0"
}
return
syncFromEventOrNull
(
timeoutMillis
,
mapper
)
?:
error
(
"timeout $timeoutMillis ms asyncFromEvent"
)
return
if
(
timeoutMillis
==
-
1L
)
{
coroutineScope
{
syncFromEventImpl
<
E
,
R
>(
E
::
class
,
this
,
mapper
)
}
}
else
{
withTimeout
(
timeoutMillis
)
{
syncFromEventImpl
<
E
,
R
>(
E
::
class
,
this
,
mapper
)
}
}
}
}
/**
/**
...
@@ -54,19 +62,13 @@ suspend inline fun <reified E : Event, R : Any> syncFromEvent(
...
@@ -54,19 +62,13 @@ suspend inline fun <reified E : Event, R : Any> syncFromEvent(
@JvmSynthetic
@JvmSynthetic
@SinceMirai
(
"0.38.0"
)
@SinceMirai
(
"0.38.0"
)
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
syncFromEventOrNull
(
suspend
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
syncFromEventOrNull
(
timeoutMillis
:
Long
=
-
1
,
timeoutMillis
:
Long
,
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
):
R
?
{
):
R
?
{
require
(
timeoutMillis
==
-
1L
||
timeoutMillis
>
0
)
{
"timeoutMillis must be -1 or
> 0"
}
require
(
timeoutMillis
>
0
)
{
"timeoutMillis must be
> 0"
}
return
if
(
timeoutMillis
==
-
1L
)
{
return
withTimeoutOrNull
(
timeoutMillis
)
{
coroutineScope
{
syncFromEventImpl
<
E
,
R
>(
E
::
class
,
this
,
mapper
)
syncFromEventOrNullImpl
<
E
,
R
>(
E
::
class
,
this
,
mapper
)
}
}
else
{
withTimeoutOrNull
(
timeoutMillis
)
{
syncFromEventOrNullImpl
<
E
,
R
>(
E
::
class
,
this
,
mapper
)
}
}
}
}
}
...
@@ -83,7 +85,7 @@ suspend inline fun <reified E : Event, R : Any> syncFromEventOrNull(
...
@@ -83,7 +85,7 @@ suspend inline fun <reified E : Event, R : Any> syncFromEventOrNull(
@Suppress
(
"DeferredIsResult"
)
@Suppress
(
"DeferredIsResult"
)
@SinceMirai
(
"0.38.0"
)
@SinceMirai
(
"0.38.0"
)
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
CoroutineScope
.
asyncFromEventOrNull
(
inline
fun
<
reified
E
:
Event
,
R
:
Any
>
CoroutineScope
.
asyncFromEventOrNull
(
timeoutMillis
:
Long
=
-
1
,
timeoutMillis
:
Long
,
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
coroutineContext
:
CoroutineContext
=
EmptyCoroutineContext
,
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
noinline
mapper
:
suspend
E
.(
E
)
->
R
?
// 不要 crossinline: crossinline 后 stacktrace 会不正常
):
Deferred
<
R
?
>
{
):
Deferred
<
R
?
>
{
...
@@ -123,11 +125,11 @@ inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEvent(
...
@@ -123,11 +125,11 @@ inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEvent(
//////////////
//////////////
@PublishedApi
@PublishedApi
internal
suspend
fun
<
E
:
Event
,
R
>
syncFromEvent
OrNull
Impl
(
internal
suspend
fun
<
E
:
Event
,
R
>
syncFromEventImpl
(
eventClass
:
KClass
<
E
>,
eventClass
:
KClass
<
E
>,
coroutineScope
:
CoroutineScope
,
coroutineScope
:
CoroutineScope
,
mapper
:
suspend
E
.(
E
)
->
R
?
mapper
:
suspend
E
.(
E
)
->
R
?
):
R
?
=
suspend
Coroutine
{
cont
->
):
R
=
suspendCancellable
Coroutine
{
cont
->
coroutineScope
.
subscribe
(
eventClass
)
{
coroutineScope
.
subscribe
(
eventClass
)
{
cont
.
resumeWith
(
kotlin
.
runCatching
{
cont
.
resumeWith
(
kotlin
.
runCatching
{
mapper
.
invoke
(
this
,
it
)
?:
return
@
subscribe
ListeningStatus
.
LISTENING
mapper
.
invoke
(
this
,
it
)
?:
return
@
subscribe
ListeningStatus
.
LISTENING
...
...
mirai-core/src/commonTest/kotlin/net/mamoe/mirai/message/SubscribingGetTest.kt
0 → 100644
View file @
ca216bc4
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package
net.mamoe.mirai.message
import
kotlinx.coroutines.CoroutineScope
import
kotlinx.coroutines.TimeoutCancellationException
import
kotlinx.coroutines.launch
import
kotlinx.coroutines.withTimeout
import
net.mamoe.mirai.event.TestEvent
import
net.mamoe.mirai.event.broadcast
import
net.mamoe.mirai.event.syncFromEvent
import
net.mamoe.mirai.test.runBlocking
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.EmptyCoroutineContext
import
kotlin.coroutines.resume
import
kotlin.coroutines.suspendCoroutine
import
kotlin.test.Test
import
kotlin.test.assertFailsWith
internal
class
SubscribingGetTest
{
@Test
fun
testSyncFromEvent
():
Unit
=
runBlocking
{
withTimeout
(
500
)
{
suspendCoroutine
<
Unit
>
{
cont
->
launch
{
syncFromEvent
(
5000
)
{
_
:
TestEvent
->
cont
.
resume
(
Unit
)
}
}
launch
{
TestEvent
().
broadcast
()
}
}
}
}
@Test
fun
testSyncFromEventTimeout
()
{
runBlockingWithTimeout
(
500
)
{
assertFailsWith
<
TimeoutCancellationException
>
{
syncFromEvent
(
100
)
{
_
:
TestEvent
->
}
}
}
}
}
internal
fun
<
R
>
runBlockingWithTimeout
(
millis
:
Long
,
context
:
CoroutineContext
=
EmptyCoroutineContext
,
block
:
suspend
CoroutineScope
.()
->
R
):
R
=
runBlocking
(
context
)
{
withTimeout
(
millis
,
block
)
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment