Skip to content

Commit

Permalink
Implement skipping invalid relays in RelayPool
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandarIlic committed Jul 31, 2023
1 parent ac44694 commit 31d28c6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,26 @@ class RelayPool @Inject constructor(
}
}

private fun Relay.toWssRequest() = Request.Builder()
.url(url)
.addHeader("User-Agent", UserAgentProvider.USER_AGENT)
.build()
private fun Relay.toWssRequestOrNull() = try {
Request.Builder()
.url(url)
.addHeader("User-Agent", UserAgentProvider.USER_AGENT)
.build()
} catch (error: IllegalArgumentException) {
null
}

private suspend fun createClientsPool(relays: List<Relay>) {
clearClientsPool()
poolMutex.withLock {
clientsPool = relays.map {
NostrSocketClient(
okHttpClient = okHttpClient,
wssRequest = it.toWssRequest()
)
}
clientsPool = relays
.mapNotNull { it.toWssRequestOrNull() }
.map {
NostrSocketClient(
okHttpClient = okHttpClient,
wssRequest = it
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
package net.primal.android.networking.relays

class RelayPoolTest
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import net.primal.android.test.advanceUntilIdleAndDelay
import net.primal.android.user.active.ActiveAccountStore
import net.primal.android.user.active.ActiveUserAccountState
import net.primal.android.user.domain.Relay
import net.primal.android.user.domain.UserAccount
import okhttp3.OkHttpClient
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class RelayPoolTest {

private fun buildActiveAccountStore(
relays: List<Relay> = emptyList()
) = mockk<ActiveAccountStore>(relaxed = true) {
every { activeAccountState } returns flowOf(
ActiveUserAccountState.ActiveUserAccount(
data = UserAccount
.buildLocal(pubkey = "")
.copy(relays = relays)
)
)
}

@Test
fun `invalid relays does not cause the crash`() = runTest {
RelayPool(
okHttpClient = OkHttpClient(),
activeAccountStore = buildActiveAccountStore(
relays = listOf(
Relay(url = "abcdefghijkl", true, true),
Relay(url = "wss://nostr-relay.untethr.me\t", true, true),
Relay(url = "⬤ wss://nostr-pub.wellorder.net", true, true),
Relay(url = "wss://filter.nostr.wine/npubxyz\n", true, true),
)
)
)
advanceUntilIdleAndDelay()
}

}

0 comments on commit 31d28c6

Please sign in to comment.