Skip to content

Commit 0c0bbd5

Browse files
committed
Add test to check whether pendingDecisions are empty after cancellation
1 parent 12f1b45 commit 0c0bbd5

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

lib/src/androidTest/java/at/bitfire/cert4android/UserDecisionRegistryTest.kt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ import kotlinx.coroutines.delay
1111
import kotlinx.coroutines.launch
1212
import kotlinx.coroutines.runBlocking
1313
import org.junit.After
14-
import org.junit.Assert.*
14+
import org.junit.Assert.assertEquals
15+
import org.junit.Assert.assertFalse
16+
import org.junit.Assert.assertTrue
1517
import org.junit.Before
1618
import org.junit.Test
1719
import java.security.cert.X509Certificate
1820
import java.util.Collections
1921
import java.util.concurrent.Semaphore
22+
import java.util.logging.Logger
2023

2124
class UserDecisionRegistryTest {
2225

@@ -26,6 +29,7 @@ class UserDecisionRegistryTest {
2629

2730
private val testCert = TestCertificates.testCert
2831

32+
private val logger = Logger.getGlobal()
2933

3034
@Before
3135
fun setUp() {
@@ -53,6 +57,33 @@ class UserDecisionRegistryTest {
5357
})
5458
}
5559

60+
@Test
61+
fun testCheck_MultipleDecisionsForSameCert_noPendingDecisionsAfterCancel() {
62+
val getUserDecision: suspend (X509Certificate) -> Boolean = { _ ->
63+
delay(3000) // block for a while
64+
true
65+
}
66+
67+
runBlocking(Dispatchers.Default) {
68+
val job = launch { registry.check(testCert, this, getUserDecision) }
69+
delay(1000)
70+
job.cancel() // Cancel the job
71+
delay(1000)
72+
}
73+
74+
// pendingDecisions should be empty
75+
synchronized(registry.pendingDecisions) {
76+
logger.info("Before assert: "+ registry.pendingDecisions.toString())
77+
assertFalse(registry.pendingDecisions.containsKey(testCert))
78+
}
79+
80+
// Do another check
81+
// runs forever if pendingDecisions is not empty; succeeds otherwise
82+
assertTrue(runBlocking(Dispatchers.Default) {
83+
registry.check(testCert, this, getUserDecision)
84+
})
85+
}
86+
5687
@Test
5788
fun testCheck_MultipleDecisionsForSameCert_Negative() {
5889
val canSendFeedback = Semaphore(0)

0 commit comments

Comments
 (0)