Debloat your Android TV, install apps, browse and transfer files on it, all from your phone. Just 3 MB, no tracking, no root, no PC.
Network names: start a lookup only once it is stored
jesse-agent-821421a4c7a00ec
diff --git a/app/src/main/java/app/tellyscope/data/NetworkNames.kt b/app/src/main/java/app/tellyscope/data/NetworkNames.ktindex fb19828..a226238 100644--- a/app/src/main/java/app/tellyscope/data/NetworkNames.kt+++ b/app/src/main/java/app/tellyscope/data/NetworkNames.kt@@ -3,6 +3,7 @@package app.tellyscope.dataimport kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.CoroutineStartimport kotlinx.coroutines.Deferredimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.SupervisorJob@@ -11,6 +12,7 @@ import kotlinx.coroutines.awaitAllimport kotlinx.coroutines.withTimeoutOrNullimport java.net.InetAddressimport java.util.concurrent.ConcurrentHashMap+import kotlin.coroutines.CoroutineContextimport kotlin.time.Durationimport kotlin.time.Duration.Companion.minutesimport kotlin.time.Duration.Companion.seconds@@ -27,8 +29,9 @@ class NetworkNames(private val keep: Duration = 10.minutes,private val clock: () -> Long = System::currentTimeMillis,private val resolve: (String) -> String? = { ip -> InetAddress.getByName(ip).canonicalHostName },+ context: CoroutineContext = Dispatchers.IO,) {- private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)+ private val scope = CoroutineScope(SupervisorJob() + context)private val known = ConcurrentHashMap<String, Pair<Long, String?>>()private val asking = ConcurrentHashMap<String, Deferred<String?>>()@@ -37,12 +40,13 @@ class NetworkNames(val now = clock()val asks = addresses.distinct().filter { a -> known[a]?.let { now - it.first > keep.inWholeMilliseconds } ?: true }- .map { a -> asking.computeIfAbsent(a) { ask(a) } }+ // Started only once it's in [asking], so a quick answer can't remove itself before it's there.+ .map { a -> asking.computeIfAbsent(a) { ask(a) }.also { it.start() } }if (asks.isNotEmpty()) withTimeoutOrNull(wait) { asks.awaitAll() }return addresses.mapNotNull { a -> known[a]?.second?.let { a to it } }.toMap()}- private fun ask(address: String) = scope.async {+ private fun ask(address: String) = scope.async(start = CoroutineStart.LAZY) {val name = runCatching { clean(resolve(address), address) }.getOrNull()known[address] = clock() to nameasking.remove(address)diff --git a/app/src/test/java/app/tellyscope/NetworkNamesTest.kt b/app/src/test/java/app/tellyscope/NetworkNamesTest.ktindex 56241b0..78065b2 100644--- a/app/src/test/java/app/tellyscope/NetworkNamesTest.kt+++ b/app/src/test/java/app/tellyscope/NetworkNamesTest.kt@@ -3,6 +3,7 @@package app.tellyscopeimport app.tellyscope.data.NetworkNames+import kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.delayimport kotlinx.coroutines.runBlockingimport org.junit.Assert.assertEquals@@ -51,6 +52,17 @@ class NetworkNamesTest {assertEquals(2, asked.get())}+ @Test fun aQuickAnswerStillRefreshesWhenOld() = runBlocking {+ // Unconfined answers before the lookup is even stored, the race a fast resolver can win on IO.+ val asked = AtomicInteger()+ var now = 0L+ val names = NetworkNames(keep = 10.minutes, clock = { now }, context = Dispatchers.Unconfined,+ resolve = { "box-${asked.incrementAndGet()}" })+ assertEquals(mapOf("192.0.2.47" to "box-1"), names.of(listOf("192.0.2.47")))+ now += 11 * 60_000+ assertEquals(mapOf("192.0.2.47" to "box-2"), names.of(listOf("192.0.2.47")))+ }+@Test fun aFailingLookupIsNoName() = runBlocking {val names = NetworkNames(resolve = { throw java.net.UnknownHostException(it) })assertEquals(emptyMap<String, String>(), names.of(listOf("192.0.2.47")))