Skip to content

Commit

Permalink
[WIP] Try to fix tests when running in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkup committed Feb 26, 2025
1 parent 96a102e commit 8cbd026
Showing 1 changed file with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class UserDecisionRegistryTest {

@Before
fun setUp() {
mockkObject(NotificationUtils)
mockkObject(registry)
}

Expand Down Expand Up @@ -58,25 +57,32 @@ class UserDecisionRegistryTest {
val canSendFeedback = Semaphore(0)
val getUserDecision: suspend (X509Certificate) -> Boolean = mockk {
coEvery { this@mockk(testCert) } coAnswers {
canSendFeedback.acquire()
canSendFeedback.acquire() // block call until released
false
}
}
val results = Collections.synchronizedList(mutableListOf<Boolean>())
runBlocking {
repeat(5) {
launch(Dispatchers.Default) {
results += registry.check(testCert, this, getUserDecision)
runBlocking(Dispatchers.Default) {
try {
// "enqueue" 5 getUserDecision calls (each blocked by the semaphore)
repeat(5) {
launch {
results += registry.check(testCert, this, getUserDecision)
}
}
} finally {
// release all calls
canSendFeedback.release(5)
}
canSendFeedback.release()
}

// pendingDecisions should be empty
synchronized(registry.pendingDecisions) {
assertFalse(registry.pendingDecisions.containsKey(testCert))
}
assertEquals(5, results.size)
assertTrue(results.all { !it })
coVerify(exactly = 1) { getUserDecision(testCert) }
assertEquals(5, results.size) // should be 5 results
assertTrue(results.all { result -> !result }) // all results should be false
coVerify(exactly = 1) { getUserDecision(testCert) } // getUserDecision should be called only once
}

@Test
Expand All @@ -89,13 +95,16 @@ class UserDecisionRegistryTest {
}
}
val results = Collections.synchronizedList(mutableListOf<Boolean>())
runBlocking {
repeat(5) {
launch(Dispatchers.Default) {
results += registry.check(testCert, this, getUserDecision)
runBlocking(Dispatchers.Default) {
try {
repeat(5) {
launch {
results += registry.check(testCert, this, getUserDecision)
}
}
} finally {
canSendFeedback.release(5)
}
canSendFeedback.release()
}
synchronized(registry.pendingDecisions) {
assertFalse(registry.pendingDecisions.containsKey(testCert))
Expand Down

0 comments on commit 8cbd026

Please sign in to comment.