mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2026-10-20 11:50:20 +00:00
fixes
This commit is contained in:
@@ -50,7 +50,7 @@ sealed interface SmartMutex {
|
|||||||
override val lockStateFlow: StateFlow<Boolean> = _lockStateFlow.asStateFlow()
|
override val lockStateFlow: StateFlow<Boolean> = _lockStateFlow.asStateFlow()
|
||||||
|
|
||||||
private val internalChangesMutex = Mutex()
|
private val internalChangesMutex = Mutex()
|
||||||
private val backingMutex = Mutex()
|
private val backingMutex = Mutex(locked = locked)
|
||||||
|
|
||||||
fun immutable() = Immutable(lockStateFlow)
|
fun immutable() = Immutable(lockStateFlow)
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,17 @@ import kotlin.test.assertTrue
|
|||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
class SmartMutexTests {
|
class SmartMutexTests {
|
||||||
|
@Test
|
||||||
|
fun initiallyLockedMutexCanBeReleasedAndReacquired() = runTest(timeout = 5.seconds) {
|
||||||
|
val mutex = SmartMutex.Mutable(locked = true)
|
||||||
|
assertTrue(mutex.isLocked)
|
||||||
|
assertFalse(mutex.tryLock())
|
||||||
|
assertTrue(mutex.unlock())
|
||||||
|
assertFalse(mutex.isLocked)
|
||||||
|
assertTrue(mutex.tryLock())
|
||||||
|
assertTrue(mutex.unlock())
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun cancelledUnlockCompletesUnderContention() = runTest(timeout = 5.seconds) {
|
fun cancelledUnlockCompletesUnderContention() = runTest(timeout = 5.seconds) {
|
||||||
val mutex = SmartMutex.Mutable()
|
val mutex = SmartMutex.Mutable()
|
||||||
|
|||||||
@@ -13,6 +13,25 @@ import kotlin.time.Duration.Companion.days
|
|||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
class SmartRWLockerTests {
|
class SmartRWLockerTests {
|
||||||
|
@Test
|
||||||
|
fun initiallyWriteLockedLockerReleasesReadPermits() = runTest(timeout = 5.seconds) {
|
||||||
|
val locker = SmartRWLocker(readPermits = 2, writeIsLocked = true)
|
||||||
|
assertTrue(locker.writeMutex.isLocked)
|
||||||
|
assertEquals(0, locker.readSemaphore.freePermits)
|
||||||
|
|
||||||
|
assertTrue(locker.unlockWrite())
|
||||||
|
|
||||||
|
assertFalse(locker.writeMutex.isLocked)
|
||||||
|
assertEquals(2, locker.readSemaphore.freePermits)
|
||||||
|
locker.withReadAcquire {
|
||||||
|
assertEquals(1, locker.readSemaphore.freePermits)
|
||||||
|
}
|
||||||
|
locker.withWriteLock {
|
||||||
|
assertEquals(0, locker.readSemaphore.freePermits)
|
||||||
|
}
|
||||||
|
assertEquals(2, locker.readSemaphore.freePermits)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun compositeTest() {
|
fun compositeTest() {
|
||||||
val locker = SmartRWLocker()
|
val locker = SmartRWLocker()
|
||||||
|
|||||||
Reference in New Issue
Block a user