@@ -3,62 +3,56 @@ package io.homeassistant.companion.android.data.wear
33import io.homeassistant.companion.android.common.util.WearDataMessages.DnsLookup.PATH_DNS_LOOKUP
44import io.homeassistant.companion.android.common.util.WearDataMessages.DnsLookup.decodeDNSResult
55import io.homeassistant.companion.android.common.util.WearDataMessages.DnsLookup.encodeDNSRequest
6- import io.homeassistant.companion.android.testing.unit.MainDispatcherJUnit5Extension
76import 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
812import kotlinx.coroutines.test.runTest
913import okhttp3.Dns
1014import org.junit.jupiter.api.Assertions.assertEquals
1115import org.junit.jupiter.api.Test
1216import org.junit.jupiter.api.assertNotNull
1317import org.junit.jupiter.api.assertNull
14- import org.junit.jupiter.api.extension.ExtendWith
1518
16- @ExtendWith(MainDispatcherJUnit5Extension ::class )
1719class 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