-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test checking that our Tox can bootstrap and connect
- Loading branch information
1 parent
1584068
commit 8cc7d44
Showing
1 changed file
with
47 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
@@ -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() {} | ||
} | ||
|
||
|
@@ -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() | ||
} | ||
} |