Skip to content

Remove last continuation in list when cancelled #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package at.bitfire.cert4android

import androidx.core.app.NotificationManagerCompat
import androidx.test.platform.app.InstrumentationRegistry
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.runs
import io.mockk.unmockkAll
import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.*
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import java.util.Collections
Expand Down Expand Up @@ -111,6 +118,37 @@ class UserDecisionRegistryTest {
verify(exactly = 1) { registry.requestDecision(any(), any(), any()) }
}

@Test
fun testCheck_MultipleDecisionsForSameCert_cancel() {
val canSendFeedback = Semaphore(0)
val nm = mockk<NotificationManagerCompat>()
every { nm.cancel(any(), any()) } just runs
every { NotificationUtils.createChannels(any()) } returns nm
every { registry.requestDecision(testCert, any(), any()) } answers {
thread {
canSendFeedback.acquire()
registry.onUserDecision(testCert, false)
}
}
val results = Collections.synchronizedList(mutableListOf<Boolean>())
runBlocking {
repeat(5) {
val job = launch(Dispatchers.Default) {
results += registry.check(testCert, true)
}
delay(1000)
job.cancel() // Cancel the job
delay(1000)
}
canSendFeedback.release()
}
synchronized(registry.pendingDecisions) {
assertFalse(registry.pendingDecisions.containsKey(testCert))
}
assertEquals(0, results.size)
verify(exactly = 5) { registry.requestDecision(any(), any(), any()) }
}

@Test
fun testCheck_UserDecisionImpossible() {
every { NotificationUtils.notificationsPermitted(any()) } returns false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ class UserDecisionRegistry private constructor(
// User decision possible → remember request in pendingDecisions so that a later decision will be applied to this request

cont.invokeOnCancellation {
// remove from pending decisions on cancellation
synchronized(pendingDecisions) {
pendingDecisions[cert]?.remove(cont)
val decisionsList = pendingDecisions[cert]

// remove from pending decisions on cancellation
decisionsList?.remove(cont)

// Remove decisions list if empty
if (decisionsList?.isEmpty() == true)
pendingDecisions -= cert
}

val nm = NotificationUtils.createChannels(context)
Expand Down