Fix reentrant deadlock

pull/3971/head
Alex Baker 4 weeks ago
parent 5980bd497d
commit 2b63e33de2

@ -0,0 +1,25 @@
package org.tasks.data
import dagger.hilt.android.testing.HiltAndroidTest
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.junit.Assert.assertTrue
import org.junit.Test
import org.tasks.data.dao.CaldavDao
import org.tasks.data.entity.CaldavAccount
import org.tasks.injection.InjectingTestCase
import javax.inject.Inject
@HiltAndroidTest
class CaldavDaoExtensionsTest : InjectingTestCase() {
@Inject lateinit var caldavDao: CaldavDao
@Test
fun getLocalListCreatesAccountIfNeeded() = runBlocking {
withTimeout(5000L) {
assertTrue(caldavDao.getAccounts().isEmpty())
caldavDao.getLocalList()
assertTrue(caldavDao.getAccounts(CaldavAccount.TYPE_LOCAL).isNotEmpty())
}
}
}

@ -12,6 +12,17 @@ import tasks.kmp.generated.resources.default_list
private val mutex = Mutex()
suspend fun CaldavDao.newLocalAccount(): CaldavAccount = mutex.withLock {
newLocalAccountUnsafe()
}
suspend fun CaldavDao.getLocalList() = mutex.withLock {
getLocalList(getLocalAccount())
}
suspend fun CaldavDao.getLocalAccount() =
getAccounts(CaldavAccount.TYPE_LOCAL).firstOrNull() ?: newLocalAccountUnsafe()
private suspend fun CaldavDao.newLocalAccountUnsafe(): CaldavAccount {
val account = CaldavAccount(
accountType = CaldavAccount.TYPE_LOCAL,
uuid = UUIDHelper.newUUID(),
@ -21,13 +32,6 @@ suspend fun CaldavDao.newLocalAccount(): CaldavAccount = mutex.withLock {
return account
}
suspend fun CaldavDao.getLocalList() = mutex.withLock {
getLocalList(getLocalAccount())
}
suspend fun CaldavDao.getLocalAccount() =
getAccounts(CaldavAccount.TYPE_LOCAL).firstOrNull() ?: newLocalAccount()
private suspend fun CaldavDao.getLocalList(account: CaldavAccount): CaldavCalendar =
getCalendarsByAccount(account.uuid!!).getOrNull(0)
?: CaldavCalendar(

Loading…
Cancel
Save