mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-10-08 00:40:25 +00:00
potentially first version of SmartKeyRWLocker
This commit is contained in:
@@ -63,13 +63,54 @@ class SmartKeyRWLockerTests {
|
||||
assertFails {
|
||||
realWithTimeout(13.milliseconds) { locker.lockWrite() }
|
||||
}
|
||||
val readPermitsBeforeLock = locker.readSemaphore().freePermits
|
||||
realWithTimeout(1.seconds) { locker.acquireRead() }
|
||||
locker.releaseRead()
|
||||
assertTrue { locker.readSemaphore().freePermits == Int.MAX_VALUE }
|
||||
assertEquals(readPermitsBeforeLock, locker.readSemaphore().freePermits)
|
||||
|
||||
locker.releaseRead(it)
|
||||
}
|
||||
|
||||
assertTrue { locker.readSemaphore().freePermits == Int.MAX_VALUE }
|
||||
realWithTimeout(1.seconds) { locker.lockWrite() }
|
||||
assertFails {
|
||||
realWithTimeout(13.milliseconds) { locker.acquireRead() }
|
||||
}
|
||||
assertTrue { locker.unlockWrite() }
|
||||
assertTrue { locker.readSemaphore().freePermits == Int.MAX_VALUE }
|
||||
}
|
||||
@Test
|
||||
fun writesBlockingGlobalWrite() = runTest {
|
||||
val locker = SmartKeyRWLocker<String>()
|
||||
|
||||
val testKeys = (0 until 100).map { "test$it" }
|
||||
|
||||
for (i in testKeys.indices) {
|
||||
val it = testKeys[i]
|
||||
locker.lockWrite(it)
|
||||
val previous = testKeys.take(i)
|
||||
val next = testKeys.drop(i + 1)
|
||||
|
||||
previous.forEach {
|
||||
assertTrue { locker.writeMutexOrNull(it) ?.isLocked == true }
|
||||
}
|
||||
next.forEach {
|
||||
assertTrue { locker.writeMutexOrNull(it) ?.isLocked != true }
|
||||
}
|
||||
}
|
||||
|
||||
for (i in testKeys.indices) {
|
||||
val it = testKeys[i]
|
||||
assertFails { realWithTimeout(13.milliseconds) { locker.lockWrite() } }
|
||||
|
||||
val readPermitsBeforeLock = locker.readSemaphore().freePermits
|
||||
assertFails { realWithTimeout(13.milliseconds) { locker.acquireRead() } }
|
||||
assertEquals(readPermitsBeforeLock, locker.readSemaphore().freePermits)
|
||||
|
||||
locker.unlockWrite(it)
|
||||
}
|
||||
|
||||
assertTrue { locker.readSemaphore().freePermits == Int.MAX_VALUE }
|
||||
realWithTimeout(1.seconds) { locker.lockWrite() }
|
||||
assertFails {
|
||||
realWithTimeout(13.milliseconds) { locker.acquireRead() }
|
||||
|
@@ -6,7 +6,10 @@ import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFails
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
class SmartRWLockerTests {
|
||||
@Test
|
||||
@@ -148,4 +151,17 @@ class SmartRWLockerTests {
|
||||
assertEquals(false, locker.writeMutex.isLocked)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun exceptionOnLockingWillNotLockLocker() = runTest {
|
||||
val locker = SmartRWLocker()
|
||||
|
||||
locker.acquireRead()
|
||||
assertFails {
|
||||
realWithTimeout(1.seconds) {
|
||||
locker.lockWrite()
|
||||
}
|
||||
}
|
||||
assertFalse { locker.writeMutex.isLocked }
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user