Skip to content

Commit

Permalink
Add a test checking that our Tox can bootstrap and connect
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Jan 9, 2025
1 parent 1584068 commit 8cc7d44
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions domain/src/androidTest/kotlin/tox/ToxTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020-2022 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2020-2025 Robin Lindén <[email protected]>
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -8,17 +8,21 @@ import androidx.room.Room
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlin.test.Test
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import ltd.evilcorp.core.db.Database
import ltd.evilcorp.core.repository.ContactRepository
import ltd.evilcorp.core.repository.UserRepository
import ltd.evilcorp.core.vo.ConnectionStatus
import org.junit.runner.RunWith

class FakeBootstrapNodeRegistry : BootstrapNodeRegistry {
override fun get(n: Int): List<BootstrapNode> = listOf()
class FakeBootstrapNodeRegistry(val nodes: List<BootstrapNode> = listOf()) : BootstrapNodeRegistry {
override fun get(n: Int): List<BootstrapNode> = nodes.take(n)
override fun reset() {}
}

Expand Down Expand Up @@ -51,4 +55,44 @@ class ToxTest {
tox.stop()
}
}

@ExperimentalCoroutinesApi
@Test
fun bootstrapping_against_a_live_node_works() = runTest {
val instrumentation = InstrumentationRegistry.getInstrumentation()
val db = Room.inMemoryDatabaseBuilder(instrumentation.context, Database::class.java).build()
val userRepository = UserRepository(db.userDao())
val contactRepository = ContactRepository(db.contactDao())

var connected = false
val eventListener = ToxEventListener().apply {
selfConnectionStatusHandler = { status ->
connected = status != ConnectionStatus.None
}
}

val tox = Tox(
this,
contactRepository,
userRepository,
FakeSaveManager(),
FakeBootstrapNodeRegistry(
listOf(
BootstrapNode(
"tox.abilinski.com",
33445,
PublicKey("10C00EB250C3233E343E2AEBA07115A5C28920E9C8D29492F6D00B29049EDC7E"),
),
),
),
)
tox.start(SaveOptions(null, false, ProxyType.None, "", 0), null, eventListener, ToxAvEventListener())

while (!connected) {
advanceTimeBy(100.milliseconds)
}

tox.stop()
advanceUntilIdle()
}
}

0 comments on commit 8cc7d44

Please sign in to comment.