srctree

Robin Linden parent 26d8ef09 c85c9af1
Clean up use of deprecated coroutine APIs in tests

inlinesplit
WORKSPACE added: 33, removed: 50, total 0
@@ -179,8 +179,8 @@ maven_install(
"com.typesafe.scala-logging:scala-logging_2.11:3.7.2",
"javax.inject:javax.inject:1",
"junit:junit:4.13.1",
"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2",
"org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.2",
"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0",
"org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0",
"org.jetbrains:annotations:13.0",
"org.slf4j:slf4j-api:1.7.25",
"org.robolectric:robolectric:4.7.3",
 
core/src/androidTest/kotlin/db/ContactDaoTest.kt added: 33, removed: 50, total 0
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020 aTox contributors
// SPDX-FileCopyrightText: 2020-2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only
 
@@ -8,11 +8,8 @@ import androidx.room.Room
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.asExecutor
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.runTest
import ltd.evilcorp.core.vo.ConnectionStatus
import ltd.evilcorp.core.vo.Contact
import ltd.evilcorp.core.vo.UserStatus
@@ -27,12 +24,8 @@ import org.junit.runner.RunWith
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class ContactDaoTest {
private val dispatcher = TestCoroutineDispatcher()
private val scope = TestCoroutineScope(dispatcher)
private val db =
Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getInstrumentation().targetContext, Database::class.java)
.setTransactionExecutor(dispatcher.asExecutor())
.setQueryExecutor(dispatcher.asExecutor())
.allowMainThreadQueries()
.build()
private val dao = db.contactDao()
@@ -58,14 +51,14 @@ class ContactDaoTest {
}
 
@Test
fun save_and_load() = scope.runBlockingTest {
fun save_and_load() = runTest {
assertEquals(0, dao.loadAll().first().size)
dao.save(first)
assertEquals(first, dao.load(first.publicKey).first())
}
 
@Test
fun delete() = scope.runBlockingTest {
fun delete() = runTest {
dao.save(first)
dao.save(second)
dao.delete(first)
@@ -74,7 +67,7 @@ class ContactDaoTest {
}
 
@Test
fun exists() = scope.runBlockingTest {
fun exists() = runTest {
assertFalse(dao.exists(first.publicKey))
dao.save(first)
assertTrue(dao.exists(first.publicKey))
@@ -84,7 +77,7 @@ class ContactDaoTest {
}
 
@Test
fun overwrite_with_save() = scope.runBlockingTest {
fun overwrite_with_save() = runTest {
dao.save(first)
assertEquals(1, dao.loadAll().first().size)
dao.save(first)
@@ -92,7 +85,7 @@ class ContactDaoTest {
}
 
@Test
fun save_multiple_contacts() = scope.runBlockingTest {
fun save_multiple_contacts() = runTest {
dao.save(first)
assertEquals(1, dao.loadAll().first().size)
dao.save(second)
@@ -102,7 +95,7 @@ class ContactDaoTest {
}
 
@Test
fun reset_transient_data() = scope.runBlockingTest {
fun reset_transient_data() = runTest {
dao.save(first)
dao.resetTransientData()
assertNotEquals(first, dao.load(first.publicKey).first())
@@ -113,7 +106,7 @@ class ContactDaoTest {
}
 
@Test
fun setters() = scope.runBlockingTest {
fun setters() = runTest {
dao.save(second)
dao.setName(second.publicKey, first.name)
dao.setStatusMessage(second.publicKey, first.statusMessage)
@@ -128,7 +121,7 @@ class ContactDaoTest {
}
 
@Test
fun update() = scope.runBlockingTest {
fun update() = runTest {
dao.save(first)
dao.update(first.copy(name = "new name"))
assertNotEquals(first, dao.load(first.publicKey).first())
 
core/src/androidTest/kotlin/db/UserDaoTest.kt added: 33, removed: 50, total 0
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021 aTox contributors
// SPDX-FileCopyrightText: 2021-2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only
 
@@ -8,11 +8,8 @@ import androidx.room.Room
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.asExecutor
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.runTest
import ltd.evilcorp.core.vo.ConnectionStatus
import ltd.evilcorp.core.vo.User
import ltd.evilcorp.core.vo.UserStatus
@@ -28,12 +25,8 @@ import org.junit.runner.RunWith
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class UserDaoTest {
private val dispatcher = TestCoroutineDispatcher()
private val scope = TestCoroutineScope(dispatcher)
private val db =
Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getInstrumentation().targetContext, Database::class.java)
.setTransactionExecutor(dispatcher.asExecutor())
.setQueryExecutor(dispatcher.asExecutor())
.allowMainThreadQueries()
.build()
private val dao = db.userDao()
@@ -51,13 +44,13 @@ class UserDaoTest {
fun clearDb() = db.clearAllTables()
 
@Test
fun save_and_load() = scope.runBlockingTest {
fun save_and_load() = runTest {
dao.save(first)
assertEquals(first, dao.load(first.publicKey).first())
}
 
@Test
fun update() = scope.runBlockingTest {
fun update() = runTest {
dao.save(first)
dao.update(first.copy(name = "new name"))
assertNotEquals(first, dao.load(first.publicKey).first())
@@ -65,14 +58,14 @@ class UserDaoTest {
}
 
@Test
fun exists() = scope.runBlockingTest {
fun exists() = runTest {
assertFalse(dao.exists(first.publicKey))
dao.save(first)
assertTrue(dao.exists(first.publicKey))
}
 
@Test
fun cant_replace_user_with_save() = scope.runBlockingTest {
fun cant_replace_user_with_save() = runTest {
dao.save(first)
try {
dao.save(first.copy(name = "new name"))
@@ -82,7 +75,7 @@ class UserDaoTest {
}
 
@Test
fun update_name() = scope.runBlockingTest {
fun update_name() = runTest {
dao.save(first)
dao.updateName(first.publicKey, "new name")
assertNotEquals(first, dao.load(first.publicKey).first())
@@ -90,7 +83,7 @@ class UserDaoTest {
}
 
@Test
fun update_status_message() = scope.runBlockingTest {
fun update_status_message() = runTest {
dao.save(first)
dao.updateStatusMessage(first.publicKey, "new status")
assertNotEquals(first, dao.load(first.publicKey).first())
@@ -98,7 +91,7 @@ class UserDaoTest {
}
 
@Test
fun update_connection() = scope.runBlockingTest {
fun update_connection() = runTest {
dao.save(first)
dao.updateConnection(first.publicKey, ConnectionStatus.TCP)
assertNotEquals(first, dao.load(first.publicKey).first())
@@ -106,7 +99,7 @@ class UserDaoTest {
}
 
@Test
fun update_status() = scope.runBlockingTest {
fun update_status() = runTest {
dao.save(first)
dao.updateStatus(first.publicKey, UserStatus.Busy)
assertNotEquals(first, dao.load(first.publicKey).first())
 
domain/src/androidTest/kotlin/tox/ToxTest.kt added: 33, removed: 50, total 0
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020-2021 aTox contributors
// SPDX-FileCopyrightText: 2020-2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only
 
@@ -6,10 +6,10 @@ package ltd.evilcorp.domain.tox
 
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.mockk.mockk
import java.lang.Thread.sleep
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
 
@@ -17,20 +17,17 @@ import org.junit.runner.RunWith
class ToxTest {
@ExperimentalCoroutinesApi
@Test
fun quitting_does_not_crash() {
val dispatcher = TestCoroutineDispatcher()
val scope = TestCoroutineScope(dispatcher)
 
fun quitting_does_not_crash() = runTest {
repeat(10) {
val tox = Tox(
scope,
TestScope(),
mockk(relaxUnitFun = true),
mockk(relaxUnitFun = true),
mockk(relaxUnitFun = true),
mockk(),
).apply { isBootstrapNeeded = false }
tox.start(SaveOptions(null, false, ProxyType.None, "", 0), null, ToxEventListener(), ToxAvEventListener())
sleep(25)
delay(25)
tox.stop()
}
}