fix(proxy): не ждать 5 минут при падении старта FGS

This commit is contained in:
samosvalishe committed 2026-07-14 17:45:52 +03:00
1 parent ece90902f7
commit bd486671ac
4 files changed
+124 -6

No files matched your search

@@ -127,18 +127,24 @@ class LocalProxyManager(private val launcher: ProxyServiceLauncher) {
ProxyServiceState.setStartupResult(null)
ProxyServiceState.setConnectionStats(ConnectionStats.IDLE)
ProxyServiceState.clearConnectedSince()
launcher.start()
// Ждём StartupResult или остановки сервиса.
// Верхняя граница в 5 минут - страховка от зависания.
try {
launcher.start()
} catch (e: Exception) {
setErrorWithAutoReset(e.message ?: "Не удалось запустить сервис")
return
}
val result = withTimeoutOrNull(5 * 60_000L) {
// Дождаться, что сервис фактически поднялся (onStartCommand).
ProxyServiceState.isRunning.first { it }
var everRunning = false
combine(
ProxyServiceState.startupResult,
ProxyServiceState.isRunning
) { sr, running -> sr to running }
.first { (sr, running) -> sr != null || !running }
.first { (sr, running) ->
if (running) everRunning = true
sr != null || (everRunning && !running)
}
.first
}
@@ -0,0 +1,110 @@
package com.freeturn.app.domain.proxy
import com.freeturn.app.data.config.ClientConfig
import com.freeturn.app.domain.ConnectionStats
import com.freeturn.app.domain.ProxyState
import com.freeturn.app.domain.StartupResult
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
@OptIn(ExperimentalCoroutinesApi::class)
class LocalProxyManagerTest {
private val dispatcher = UnconfinedTestDispatcher()
private val validCfg = ClientConfig(serverAddress = "1.2.3.4", vkLink = "https://vk.com/x")
private class FakeLauncher : ProxyServiceLauncher {
var started = false
var stopped = false
override fun start() { started = true }
override fun stop() { stopped = true }
}
private class ThrowingLauncher : ProxyServiceLauncher {
override fun start(): Unit = throw IllegalStateException("not allowed")
override fun stop() {}
}
@Before
fun setUp() {
Dispatchers.setMain(dispatcher)
resetProxyState()
}
@After
fun tearDown() {
resetProxyState()
Dispatchers.resetMain()
}
private fun resetProxyState() {
ProxyServiceState.setRunning(false)
ProxyServiceState.setStartupResult(null)
ProxyServiceState.setConnectionStats(ConnectionStats.IDLE)
ProxyServiceState.setCaptchaSession(null)
ProxyServiceState.clearConnectedSince()
ProxyServiceState.clearLogs()
}
// Регрессия P1 #2: FGS-фейл пишет Failed при isRunning == false - раньше менеджер ждал 5 минут.
@Test
fun fgsStartFailureReportsErrorWithoutWaiting() = runTest(dispatcher) {
val launcher = FakeLauncher()
val mgr = LocalProxyManager(launcher)
val job = launch { mgr.startProxy(validCfg) }
runCurrent()
assertTrue(launcher.started)
// startForeground упал: сервис зафиксировал ошибку, isRunning так и не стал true.
ProxyServiceState.setStartupResult(StartupResult.Failed("FGS start failed"))
runCurrent()
val state = mgr.proxyState.value
assertTrue("ожидался Error, было $state", state is ProxyState.Error)
assertEquals("FGS start failed", (state as ProxyState.Error).message)
assertTrue(launcher.stopped)
job.cancel()
}
@Test
fun launcherExceptionReportsErrorImmediately() = runTest(dispatcher) {
val mgr = LocalProxyManager(ThrowingLauncher())
mgr.startProxy(validCfg)
val state = mgr.proxyState.value
assertTrue("ожидался Error, было $state", state is ProxyState.Error)
assertEquals("not allowed", (state as ProxyState.Error).message)
}
@Test
fun successfulStartupTransitionsToRunning() = runTest(dispatcher) {
val launcher = FakeLauncher()
val mgr = LocalProxyManager(launcher)
val job = launch { mgr.startProxy(validCfg) }
runCurrent()
ProxyServiceState.setConnectionStats(ConnectionStats(active = 1, total = 1))
ProxyServiceState.setRunning(true)
ProxyServiceState.setStartupResult(StartupResult.Success)
runCurrent()
val state = mgr.proxyState.value
assertTrue("ожидался Running, было $state", state is ProxyState.Running)
assertEquals(1, (state as ProxyState.Running).active)
job.cancel()
}
}