Skip to content

Commit 8b2c4e7

Browse files
authored
Properly test WearDnsRequestListener using gms task (#5880)
1 parent ec9a2a0 commit 8b2c4e7

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

app/src/full/kotlin/io/homeassistant/companion/android/data/wear/WearDnsRequestListener.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,21 @@ import kotlinx.coroutines.withContext
1717
import okhttp3.Dns
1818
import timber.log.Timber
1919

20-
class WearDnsRequestListener @VisibleForTesting constructor(private val dns: Dns = Dns.SYSTEM) :
21-
WearableListenerService() {
22-
23-
private val mainScope: CoroutineScope = CoroutineScope(Dispatchers.Main + Job())
20+
class WearDnsRequestListener @VisibleForTesting constructor(
21+
private val dns: Dns = Dns.SYSTEM,
22+
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Main + Job()),
23+
) : WearableListenerService() {
2424

2525
override fun onRequest(nodeId: String, path: String, request: ByteArray): Task<ByteArray>? {
2626
if (path == PATH_DNS_LOOKUP) {
27-
return mainScope.async { dnsRequest(request) }.asTask()
27+
return scope.async { dnsRequest(request) }.asTask()
2828
} else {
2929
Timber.w("Received a path ($path) that is not supported by this listener. Check the manifest intent-filter")
3030
return null
3131
}
3232
}
3333

34-
@VisibleForTesting
35-
internal suspend fun dnsRequest(request: ByteArray): ByteArray = withContext(Dispatchers.IO) {
34+
private suspend fun dnsRequest(request: ByteArray): ByteArray = withContext(Dispatchers.IO) {
3635
val hostname = request.decodeDNSRequest()
3736
try {
3837
dns.lookup(hostname).encodeDNSResult()
@@ -45,6 +44,6 @@ class WearDnsRequestListener @VisibleForTesting constructor(private val dns: Dns
4544
override fun onDestroy() {
4645
super.onDestroy()
4746

48-
mainScope.cancel("WearDataListener.onDestroy")
47+
scope.cancel("WearDataListener.onDestroy")
4948
}
5049
}

app/src/test/kotlin/io/homeassistant/companion/android/data/wear/WearDnsRequestListenerTest.kt

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,56 @@ package io.homeassistant.companion.android.data.wear
33
import io.homeassistant.companion.android.common.util.WearDataMessages.DnsLookup.PATH_DNS_LOOKUP
44
import io.homeassistant.companion.android.common.util.WearDataMessages.DnsLookup.decodeDNSResult
55
import io.homeassistant.companion.android.common.util.WearDataMessages.DnsLookup.encodeDNSRequest
6-
import io.homeassistant.companion.android.testing.unit.MainDispatcherJUnit5Extension
76
import java.net.InetAddress
7+
import kotlinx.coroutines.ExperimentalCoroutinesApi
8+
import kotlinx.coroutines.tasks.await
9+
import kotlinx.coroutines.test.StandardTestDispatcher
10+
import kotlinx.coroutines.test.TestScope
11+
import kotlinx.coroutines.test.UnconfinedTestDispatcher
812
import kotlinx.coroutines.test.runTest
913
import okhttp3.Dns
1014
import org.junit.jupiter.api.Assertions.assertEquals
1115
import org.junit.jupiter.api.Test
1216
import org.junit.jupiter.api.assertNotNull
1317
import org.junit.jupiter.api.assertNull
14-
import org.junit.jupiter.api.extension.ExtendWith
1518

16-
@ExtendWith(MainDispatcherJUnit5Extension::class)
1719
class WearDnsRequestListenerTest {
1820
private val testHostname = "homeassistant.local"
1921

2022
private val homeAssistantLocal: InetAddress = InetAddress.getByAddress(testHostname, byteArrayOf(192.toByte(), 168.toByte(), 0, 23))
2123

24+
@OptIn(ExperimentalCoroutinesApi::class)
2225
@Test
23-
fun `Given a hostname when a DNS request is made then the IP address is returned`() = runTest {
26+
fun `Given a request with a DNS lookup path when a request is made the IP address is returned`() = runTest {
2427
// Given
28+
val testScope = TestScope(UnconfinedTestDispatcher())
2529
val fakeDns = Dns {
2630
if (it == testHostname) {
2731
listOf(homeAssistantLocal)
2832
} else {
2933
throw IllegalArgumentException("hostname not found")
3034
}
3135
}
32-
val service = WearDnsRequestListener(fakeDns)
36+
val service = WearDnsRequestListener(fakeDns, scope = testScope)
3337

3438
// When
35-
val response = service.dnsRequest(testHostname.encodeDNSRequest())
36-
val addresses = response.decodeDNSResult(testHostname)
39+
val task = service.onRequest("", PATH_DNS_LOOKUP, testHostname.encodeDNSRequest())
3740

3841
// Then
42+
assertNotNull(task)
43+
val addresses = task.await().decodeDNSResult(testHostname)
3944
assertEquals(testHostname, addresses.single().hostName)
4045
assertEquals("192.168.0.23", addresses.single().hostAddress)
4146
}
4247

43-
@Test
44-
fun `Given a request with a DNS lookup path when a request is made then a task is returned`() {
45-
// Given
46-
val service = WearDnsRequestListener()
47-
48-
// When
49-
val task = service.onRequest("", PATH_DNS_LOOKUP, "homeassistant.local".encodeDNSRequest())
50-
51-
// Then
52-
assertNotNull(task)
53-
}
54-
5548
@Test
5649
fun `Given a request without a DNS lookup path when a request is made then no task is returned`() {
5750
// Given
58-
val service = WearDnsRequestListener()
51+
val testScope = TestScope(StandardTestDispatcher())
52+
val service = WearDnsRequestListener(scope = testScope)
5953

6054
// When
61-
val task = service.onRequest("", "", "homeassistant.local".encodeDNSRequest())
55+
val task = service.onRequest("", "", testHostname.encodeDNSRequest())
6256

6357
// Then
6458
assertNull(task)

0 commit comments

Comments
 (0)