@@ -7,6 +7,7 @@ import io.mockk.mockk
7
7
import io.mockk.mockkObject
8
8
import io.mockk.unmockkAll
9
9
import kotlinx.coroutines.Dispatchers
10
+ import kotlinx.coroutines.delay
10
11
import kotlinx.coroutines.launch
11
12
import kotlinx.coroutines.runBlocking
12
13
import org.junit.After
@@ -28,7 +29,6 @@ class UserDecisionRegistryTest {
28
29
29
30
@Before
30
31
fun setUp () {
31
- mockkObject(NotificationUtils )
32
32
mockkObject(registry)
33
33
}
34
34
@@ -58,25 +58,29 @@ class UserDecisionRegistryTest {
58
58
val canSendFeedback = Semaphore (0 )
59
59
val getUserDecision: suspend (X509Certificate ) -> Boolean = mockk {
60
60
coEvery { this @mockk(testCert) } coAnswers {
61
- canSendFeedback.acquire()
61
+ canSendFeedback.acquire() // block call until released
62
62
false
63
63
}
64
64
}
65
65
val results = Collections .synchronizedList(mutableListOf<Boolean >())
66
- runBlocking {
66
+ runBlocking(Dispatchers .Default ) {
67
+ // launch 5 getUserDecision calls (each will be blocked by the semaphore)
67
68
repeat(5 ) {
68
- launch( Dispatchers . Default ) {
69
+ launch {
69
70
results + = registry.check(testCert, this , getUserDecision)
70
71
}
71
72
}
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
73
75
}
76
+
77
+ // pendingDecisions should be empty
74
78
synchronized(registry.pendingDecisions) {
75
79
assertFalse(registry.pendingDecisions.containsKey(testCert))
76
80
}
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
80
84
}
81
85
82
86
@Test
@@ -89,12 +93,13 @@ class UserDecisionRegistryTest {
89
93
}
90
94
}
91
95
val results = Collections .synchronizedList(mutableListOf<Boolean >())
92
- runBlocking {
96
+ runBlocking( Dispatchers . Default ) {
93
97
repeat(5 ) {
94
- launch( Dispatchers . Default ) {
98
+ launch {
95
99
results + = registry.check(testCert, this , getUserDecision)
96
100
}
97
101
}
102
+ delay(1000 )
98
103
canSendFeedback.release()
99
104
}
100
105
synchronized(registry.pendingDecisions) {
0 commit comments