mirror of
https://github.com/mamoe/mirai.git
synced 2025-04-25 13:03:35 +08:00
[core] AuthControl: require explicit .start()
instead of launching job in constructor
This commit is contained in:
parent
10e731f260
commit
0c24053198
mirai-core/src
commonMain/kotlin/network
commonTest/kotlin/network/component
@ -86,7 +86,7 @@ internal class AuthControl(
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
fun start() {
|
||||
userDecisions.expectMore(null)
|
||||
}
|
||||
|
||||
|
@ -103,6 +103,7 @@ internal sealed interface ProducerState<T, V> {
|
||||
class ProducerReady<T, V>(
|
||||
launchProducer: () -> OnDemandProducerScope<T, V>,
|
||||
) : HasProducer<T, V> {
|
||||
// Lazily start the producer job since it's on-demand
|
||||
override val producer: OnDemandProducerScope<T, V> by lazy(launchProducer) // `lazy` is synchronized
|
||||
|
||||
fun startProducerIfNotYet() {
|
||||
|
@ -11,7 +11,8 @@ package net.mamoe.mirai.internal.network.components
|
||||
|
||||
import kotlinx.atomicfu.AtomicRef
|
||||
import kotlinx.atomicfu.atomic
|
||||
import net.mamoe.mirai.auth.*
|
||||
import net.mamoe.mirai.auth.BotAuthInfo
|
||||
import net.mamoe.mirai.auth.BotAuthorization
|
||||
import net.mamoe.mirai.internal.network.Packet
|
||||
import net.mamoe.mirai.internal.network.QQAndroidClient
|
||||
import net.mamoe.mirai.internal.network.QRCodeLoginData
|
||||
@ -159,13 +160,13 @@ internal class SsoProcessorImpl(
|
||||
*/
|
||||
override suspend fun login(handler: NetworkHandler) {
|
||||
|
||||
fun initAuthControl() {
|
||||
fun initAndStartAuthControl() {
|
||||
authControl = AuthControl(
|
||||
botAuthInfo,
|
||||
ssoContext.bot.account.authorization,
|
||||
ssoContext.bot.network.logger,
|
||||
ssoContext.bot.coroutineContext, // do not use network context because network may restart whilst auth control should keep alive
|
||||
)
|
||||
).also { it.start() }
|
||||
}
|
||||
|
||||
suspend fun loginSuccess() {
|
||||
@ -195,7 +196,7 @@ internal class SsoProcessorImpl(
|
||||
kotlin.runCatching {
|
||||
FastLoginImpl(handler).doLogin()
|
||||
}.onFailure { e ->
|
||||
initAuthControl()
|
||||
initAndStartAuthControl()
|
||||
authControl!!.exceptionCollector.collect(e)
|
||||
|
||||
throw SelectorRequireReconnectException()
|
||||
@ -207,7 +208,7 @@ internal class SsoProcessorImpl(
|
||||
}
|
||||
}
|
||||
|
||||
if (authControl == null) initAuthControl()
|
||||
if (authControl == null) initAndStartAuthControl()
|
||||
val authControl0 = authControl!!
|
||||
|
||||
|
||||
|
@ -60,6 +60,7 @@ internal class BotAuthControlTest : AbstractCommonNHTest() {
|
||||
}
|
||||
}, bot.logger, backgroundScope.coroutineContext)
|
||||
|
||||
control.start()
|
||||
control.assertRequire(SsoProcessorImpl.AuthMethod.Pwd::class)
|
||||
control.actComplete()
|
||||
control.assertRequire(SsoProcessorImpl.AuthMethod.NotAvailable::class)
|
||||
@ -78,6 +79,7 @@ internal class BotAuthControlTest : AbstractCommonNHTest() {
|
||||
}
|
||||
}, bot.logger, backgroundScope.coroutineContext)
|
||||
|
||||
control.start()
|
||||
control.assertRequire(SsoProcessorImpl.AuthMethod.Pwd::class)
|
||||
control.actMethodFailed(MyLoginFailedException())
|
||||
|
||||
@ -100,6 +102,7 @@ internal class BotAuthControlTest : AbstractCommonNHTest() {
|
||||
}
|
||||
}, bot.logger, backgroundScope.coroutineContext)
|
||||
|
||||
control.start()
|
||||
control.assertRequire(SsoProcessorImpl.AuthMethod.Pwd::class)
|
||||
control.actComplete()
|
||||
control.assertRequire(SsoProcessorImpl.AuthMethod.NotAvailable::class)
|
||||
|
Loading…
Reference in New Issue
Block a user