1- // SPDX-FileCopyrightText: 2020-2022 Robin Lindén <dev@robinlinden.eu>
1+ // SPDX-FileCopyrightText: 2020-2025 Robin Lindén <dev@robinlinden.eu>
22//
33// SPDX-License-Identifier: GPL-3.0-only
44
@@ -8,17 +8,21 @@ import androidx.room.Room
88import androidx.test.ext.junit.runners.AndroidJUnit4
99import androidx.test.platform.app.InstrumentationRegistry
1010import kotlin.test.Test
11+ import kotlin.time.Duration.Companion.milliseconds
1112import kotlinx.coroutines.ExperimentalCoroutinesApi
1213import kotlinx.coroutines.delay
1314import kotlinx.coroutines.test.TestScope
15+ import kotlinx.coroutines.test.advanceTimeBy
16+ import kotlinx.coroutines.test.advanceUntilIdle
1417import kotlinx.coroutines.test.runTest
1518import ltd.evilcorp.core.db.Database
1619import ltd.evilcorp.core.repository.ContactRepository
1720import ltd.evilcorp.core.repository.UserRepository
21+ import ltd.evilcorp.core.vo.ConnectionStatus
1822import org.junit.runner.RunWith
1923
20- class FakeBootstrapNodeRegistry : BootstrapNodeRegistry {
21- override fun get (n : Int ): List <BootstrapNode > = listOf ( )
24+ class FakeBootstrapNodeRegistry ( val nodes : List < BootstrapNode > = listOf()) : BootstrapNodeRegistry {
25+ override fun get (n : Int ): List <BootstrapNode > = nodes.take(n )
2226 override fun reset () {}
2327}
2428
@@ -51,4 +55,44 @@ class ToxTest {
5155 tox.stop()
5256 }
5357 }
58+
59+ @ExperimentalCoroutinesApi
60+ @Test
61+ fun bootstrapping_against_a_live_node_works () = runTest {
62+ val instrumentation = InstrumentationRegistry .getInstrumentation()
63+ val db = Room .inMemoryDatabaseBuilder(instrumentation.context, Database ::class .java).build()
64+ val userRepository = UserRepository (db.userDao())
65+ val contactRepository = ContactRepository (db.contactDao())
66+
67+ var connected = false
68+ val eventListener = ToxEventListener ().apply {
69+ selfConnectionStatusHandler = { status ->
70+ connected = status != ConnectionStatus .None
71+ }
72+ }
73+
74+ val tox = Tox (
75+ this ,
76+ contactRepository,
77+ userRepository,
78+ FakeSaveManager (),
79+ FakeBootstrapNodeRegistry (
80+ listOf (
81+ BootstrapNode (
82+ " tox.abilinski.com" ,
83+ 33445 ,
84+ PublicKey (" 10C00EB250C3233E343E2AEBA07115A5C28920E9C8D29492F6D00B29049EDC7E" ),
85+ ),
86+ ),
87+ ),
88+ )
89+ tox.start(SaveOptions (null , false , ProxyType .None , " " , 0 ), null , eventListener, ToxAvEventListener ())
90+
91+ while (! connected) {
92+ advanceTimeBy(100 .milliseconds)
93+ }
94+
95+ tox.stop()
96+ advanceUntilIdle()
97+ }
5498}
0 commit comments