forked from upstream-mirrors/quik
Fix duplicate Matrix forwarding, contact names and bridge reset
Build and Release APK / Build APK (push) Successful in 12m11s
Build and Release APK / Build APK (push) Successful in 12m11s
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package ch.skew.remotrix.bridge
|
||||
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.NonCancellable
|
||||
import kotlinx.coroutines.awaitCancellation
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
class BridgeResetTest {
|
||||
@Test
|
||||
fun waitsForInFlightForwardingBeforeDeletingMappings() = runBlocking {
|
||||
val work = CoroutineScope(Job() + Dispatchers.Default)
|
||||
val started = CompletableDeferred<Unit>()
|
||||
val mappings = mutableListOf<String>()
|
||||
var clientsStopped = false
|
||||
work.launch {
|
||||
try {
|
||||
started.complete(Unit)
|
||||
awaitCancellation()
|
||||
} finally {
|
||||
// A forwarding job may still persist its result during cleanup.
|
||||
withContext(NonCancellable) {
|
||||
delay(20)
|
||||
mappings.add("old room")
|
||||
}
|
||||
}
|
||||
}
|
||||
started.await()
|
||||
resetBridge(work,
|
||||
stopClients = { clientsStopped = true },
|
||||
clearData = {
|
||||
assertTrue(clientsStopped)
|
||||
assertEquals(listOf("old room"), mappings)
|
||||
mappings.clear()
|
||||
}
|
||||
)
|
||||
assertTrue(mappings.isEmpty())
|
||||
assertFalse(work.coroutineContext[Job]!!.isActive)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun doesNotDeleteStoresWhenStoppingClientsFails() = runBlocking {
|
||||
val work = CoroutineScope(Job())
|
||||
var deleted = false
|
||||
val result = runCatching {
|
||||
resetBridge(work,
|
||||
stopClients = { error("store is still open") },
|
||||
clearData = { deleted = true }
|
||||
)
|
||||
}
|
||||
assertTrue(result.isFailure)
|
||||
assertFalse(deleted)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package ch.skew.remotrix.bridge
|
||||
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
class RoomResolverTest {
|
||||
@Test
|
||||
fun concurrentMessagesAndCommandsCreateOneRoom() = runBlocking {
|
||||
val mappings = mutableMapOf<Pair<String, Int>, String>()
|
||||
var creations = 0
|
||||
val resolver = RoomResolver(
|
||||
lookup = { number, account -> mappings[number to account] },
|
||||
save = { number, account, room -> mappings[number to account] = room }
|
||||
)
|
||||
val results = (1..20).map {
|
||||
async {
|
||||
resolver.resolve("+48123456789", 1) {
|
||||
creations++
|
||||
delay(10) // Room creation suspends while other requests arrive.
|
||||
Result.success("!room:example.org")
|
||||
}.getOrThrow()
|
||||
}
|
||||
}.awaitAll()
|
||||
assertEquals(1, creations)
|
||||
assertEquals(1, results.count { it.created })
|
||||
assertTrue(results.all { it.roomId == "!room:example.org" })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun failedCreationCanBeRetriedAndAccountsStaySeparate() = runBlocking {
|
||||
val mappings = mutableMapOf<Pair<String, Int>, String>()
|
||||
val resolver = RoomResolver(
|
||||
lookup = { number, account -> mappings[number to account] },
|
||||
save = { number, account, room -> mappings[number to account] = room }
|
||||
)
|
||||
assertTrue(resolver.resolve("123", 1) {
|
||||
Result.failure(IllegalStateException("offline"))
|
||||
}.isFailure)
|
||||
assertTrue(mappings.isEmpty())
|
||||
assertEquals("!one:example.org", resolver.resolve("123", 1) {
|
||||
Result.success("!one:example.org")
|
||||
}.getOrThrow().roomId)
|
||||
assertEquals("!two:example.org", resolver.resolve("123", 2) {
|
||||
Result.success("!two:example.org")
|
||||
}.getOrThrow().roomId)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user