Skip to content

Commit 283bb11

Browse files
committed
Fix tests running forever
1 parent 96a102e commit 283bb11

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,32 @@ 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 {
67-
repeat(5) {
68-
launch(Dispatchers.Default) {
69-
results += registry.check(testCert, this, getUserDecision)
66+
runBlocking(Dispatchers.Default) {
67+
try {
68+
// enqueue 5 "getUserDecision" calls (each blocked by the semaphore)
69+
repeat(5) {
70+
launch {
71+
results += registry.check(testCert, this, getUserDecision)
72+
}
7073
}
74+
} finally {
75+
// release all calls
76+
canSendFeedback.release(5)
7177
}
72-
canSendFeedback.release()
7378
}
79+
80+
// pendingDecisions should be empty
7481
synchronized(registry.pendingDecisions) {
7582
assertFalse(registry.pendingDecisions.containsKey(testCert))
7683
}
77-
assertEquals(5, results.size)
78-
assertTrue(results.all { !it })
79-
coVerify(exactly = 1) { getUserDecision(testCert) }
84+
assertEquals(5, results.size) // should be 5 results
85+
assertTrue(results.all { result -> !result }) // all results should be false
86+
coVerify(exactly = 1) { getUserDecision(testCert) } // getUserDecision should be called only once
8087
}
8188

8289
@Test
@@ -89,13 +96,16 @@ class UserDecisionRegistryTest {
8996
}
9097
}
9198
val results = Collections.synchronizedList(mutableListOf<Boolean>())
92-
runBlocking {
93-
repeat(5) {
94-
launch(Dispatchers.Default) {
95-
results += registry.check(testCert, this, getUserDecision)
99+
runBlocking(Dispatchers.Default) {
100+
try {
101+
repeat(5) {
102+
launch {
103+
results += registry.check(testCert, this, getUserDecision)
104+
}
96105
}
106+
} finally {
107+
canSendFeedback.release(5)
97108
}
98-
canSendFeedback.release()
99109
}
100110
synchronized(registry.pendingDecisions) {
101111
assertFalse(registry.pendingDecisions.containsKey(testCert))

0 commit comments

Comments
 (0)