Commit e2a019f6 authored by Sebastian Kaspari's avatar Sebastian Kaspari Committed by Christian Sadilek
Browse files

CrashRecoveryUseCase: All restoring list of sessions and all crashed sessions.

parent c4ee379d
......@@ -178,17 +178,36 @@ class SessionUseCases(
/**
* Tries to recover from a crash by restoring the last know state.
*
* Executing this use case on a [Session] will clear the [Session.crashed] flag.
*/
class CrashRecoveryUseCase internal constructor(
private val sessionManager: SessionManager
) {
fun invoke(session: Session): Boolean {
val recovered = sessionManager.getOrCreateEngineSession(session)
.recoverFromCrash()
/**
* Tries to recover the state of the provided [Session].
*/
fun invoke(session: Session): Boolean = invoke(listOf(session))
/**
* Tries to recover the state of all crashed [Session]s (with [Session.crashed] flag set).
*/
fun invoke(): Boolean {
return invoke(sessionManager.sessions.filter { it.crashed })
}
session.crashed = false
/**
* Tries to recover the state of all [sessions].
*/
fun invoke(sessions: List<Session>): Boolean {
return sessions.fold(true) { recovered, session ->
val sessionRecovered = sessionManager.getOrCreateEngineSession(session)
.recoverFromCrash()
return recovered
session.crashed = false
sessionRecovered && recovered
}
}
}
......
......@@ -231,4 +231,52 @@ class SessionUseCasesTest {
verify(engineSession).recoverFromCrash()
verify(session).crashed = false
}
@Test
fun `CrashRecoveryUseCase will restore list of crashed sessions`() {
val engineSession1 = mock(EngineSession::class.java)
doReturn(true).`when`(engineSession1).recoverFromCrash()
val engineSession2 = mock(EngineSession::class.java)
doReturn(true).`when`(engineSession2).recoverFromCrash()
val session1 = mock(Session::class.java)
`when`(sessionManager.getOrCreateEngineSession(session1)).thenReturn(engineSession1)
val session2 = mock(Session::class.java)
`when`(sessionManager.getOrCreateEngineSession(session2)).thenReturn(engineSession2)
assertTrue(useCases.crashRecovery.invoke(listOf(session1, session2)))
verify(engineSession1).recoverFromCrash()
verify(engineSession2).recoverFromCrash()
verify(session1).crashed = false
verify(session2).crashed = false
}
@Test
fun `CrashRecoveryUseCase will restore crashed sessions`() {
val engineSession1 = mock(EngineSession::class.java)
doReturn(true).`when`(engineSession1).recoverFromCrash()
val engineSession2 = mock(EngineSession::class.java)
doReturn(true).`when`(engineSession2).recoverFromCrash()
val session1 = mock(Session::class.java)
`when`(sessionManager.getOrCreateEngineSession(session1)).thenReturn(engineSession1)
doReturn(true).`when`(session1).crashed
val session2 = mock(Session::class.java)
doReturn(false).`when`(session2).crashed
`when`(sessionManager.getOrCreateEngineSession(session2)).thenReturn(engineSession2)
doReturn(listOf(session1, session2)).`when`(sessionManager).sessions
assertTrue(useCases.crashRecovery.invoke())
verify(engineSession1).recoverFromCrash()
verify(engineSession2, never()).recoverFromCrash()
verify(session1).crashed = false
verify(session2, never()).crashed = false
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment