Skip to content

Commit 82f9a32

Browse files
committed
Fix unit test
1 parent bc1fb0d commit 82f9a32

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

library/runtime-ctrl/src/nonJsTest/kotlin/io/matthewnelson/kmp/tor/runtime/ctrl/internal/ReentrantLockUnitTest.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,29 @@ class ReentrantLockUnitTest {
3232
fun givenWithLockAsync_whenBlockThrows_thenUnlocks() = runTest {
3333
val lock = reentrantLock()
3434

35+
var started = 0
36+
val jobs = Array(5) {
37+
// Occupy first 5 threads available from Dispatchers.IO
38+
launch(Dispatchers.IO) {
39+
lock.withLock { started++ }
40+
while (true) {
41+
Blocking.threadSleep(5.milliseconds)
42+
if (!isActive) break
43+
}
44+
}
45+
}
46+
47+
while (true) {
48+
if (lock.withLock { started } == jobs.size) break
49+
withContext(Dispatchers.IO) { delay(5.milliseconds) }
50+
}
51+
3552
val dispatcher = newFixedThreadPoolContext(1, "Test.BG")
36-
currentCoroutineContext().job.invokeOnCompletion { dispatcher.close() }
53+
54+
currentCoroutineContext().job.apply {
55+
invokeOnCompletion { dispatcher.close() }
56+
invokeOnCompletion { jobs.forEach { it.cancel() } }
57+
}
3758

3859
val job = launch(dispatcher) {
3960

@@ -67,6 +88,7 @@ class ReentrantLockUnitTest {
6788
}
6889
} catch (_: IllegalStateException) {}
6990

91+
jobs.forEach { it.cancelAndJoin() }
7092
job.join()
7193
}
7294
}

0 commit comments

Comments
 (0)