Skip to content

Commit 2776ce7

Browse files
committed
Fix tests
1 parent 96a102e commit 2776ce7

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.mockk.mockk
77
import io.mockk.mockkObject
88
import io.mockk.unmockkAll
99
import kotlinx.coroutines.Dispatchers
10+
import kotlinx.coroutines.delay
1011
import kotlinx.coroutines.launch
1112
import kotlinx.coroutines.runBlocking
1213
import org.junit.After
@@ -28,7 +29,6 @@ class UserDecisionRegistryTest {
2829

2930
@Before
3031
fun setUp() {
31-
mockkObject(NotificationUtils)
3232
mockkObject(registry)
3333
}
3434

@@ -58,25 +58,29 @@ class UserDecisionRegistryTest {
5858
val canSendFeedback = Semaphore(0)
5959
val getUserDecision: suspend (X509Certificate) -> Boolean = mockk {
6060
coEvery { this@mockk(testCert) } coAnswers {
61-
canSendFeedback.acquire()
61+
canSendFeedback.acquire() // block call until released
6262
false
6363
}
6464
}
6565
val results = Collections.synchronizedList(mutableListOf<Boolean>())
66-
runBlocking {
66+
runBlocking(Dispatchers.Default) {
67+
// launch 5 getUserDecision calls (each will be blocked by the semaphore)
6768
repeat(5) {
68-
launch(Dispatchers.Default) {
69+
launch {
6970
results += registry.check(testCert, this, getUserDecision)
7071
}
7172
}
72-
canSendFeedback.release()
73+
delay(1000) // wait a bit for all getUserDecision calls to be launched and blocked
74+
canSendFeedback.release() // now unblock all calls at the same time
7375
}
76+
77+
// pendingDecisions should be empty
7478
synchronized(registry.pendingDecisions) {
7579
assertFalse(registry.pendingDecisions.containsKey(testCert))
7680
}
77-
assertEquals(5, results.size)
78-
assertTrue(results.all { !it })
79-
coVerify(exactly = 1) { getUserDecision(testCert) }
81+
assertEquals(5, results.size) // should be 5 results
82+
assertTrue(results.all { result -> !result }) // all results should be false
83+
coVerify(exactly = 1) { getUserDecision(testCert) } // getUserDecision should be called only once
8084
}
8185

8286
@Test
@@ -89,12 +93,13 @@ class UserDecisionRegistryTest {
8993
}
9094
}
9195
val results = Collections.synchronizedList(mutableListOf<Boolean>())
92-
runBlocking {
96+
runBlocking(Dispatchers.Default) {
9397
repeat(5) {
94-
launch(Dispatchers.Default) {
98+
launch {
9599
results += registry.check(testCert, this, getUserDecision)
96100
}
97101
}
102+
delay(1000)
98103
canSendFeedback.release()
99104
}
100105
synchronized(registry.pendingDecisions) {

0 commit comments

Comments
 (0)