Skip to content

Commit 2363e26

Browse files
committed
Address PR feedback. Use shared configureIosSimulatorTasks
1 parent 94e5878 commit 2363e26

File tree

4 files changed

+37
-100
lines changed

4 files changed

+37
-100
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
kotlin-version = "2.1.0"
33
dokka-version = "1.9.10"
44

5-
aws-kotlin-repo-tools-version = "0.4.21-kn"
5+
aws-kotlin-repo-tools-version = "0.4.22-kn"
66

77
# libs
88
coroutines-version = "1.9.0"

runtime/build.gradle.kts

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import aws.sdk.kotlin.gradle.kmp.*
77
import aws.sdk.kotlin.gradle.util.typedProp
88
import org.gradle.kotlin.dsl.withType
99
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
10-
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeSimulatorTest
11-
import org.jetbrains.kotlin.konan.target.HostManager
1210

1311
plugins {
1412
alias(libs.plugins.dokka)
@@ -70,49 +68,6 @@ subprojects {
7068
}
7169
}
7270
}
73-
74-
// disable "standalone" mode in simulator tests since it causes TLS issues. this means we need to manage the simulator
75-
// ourselves (booting / shutting down). FIXME: https://youtrack.jetbrains.com/issue/KT-38317
76-
val simulatorDeviceName = project.findProperty("iosSimulatorDevice") as? String ?: "iPhone 15"
77-
78-
val xcrun = "/usr/bin/xcrun"
79-
80-
tasks.register<Exec>("bootIosSimulatorDevice") {
81-
isIgnoreExitValue = true
82-
commandLine(xcrun, "simctl", "boot", simulatorDeviceName)
83-
84-
doLast {
85-
val result = executionResult.get()
86-
val code = result.exitValue
87-
if (code != 148 && code != 149) { // ignore "simulator already running" errors
88-
result.assertNormalExitValue()
89-
}
90-
}
91-
}
92-
93-
tasks.register<Exec>("shutdownIosSimulatorDevice") {
94-
mustRunAfter(tasks.withType<KotlinNativeSimulatorTest>())
95-
commandLine(xcrun, "simctl", "shutdown", simulatorDeviceName)
96-
97-
doLast {
98-
val result = executionResult.get()
99-
if (result.exitValue != 405) {
100-
result.assertNormalExitValue()
101-
}
102-
}
103-
}
104-
105-
tasks.withType<KotlinNativeSimulatorTest>().configureEach {
106-
if (!HostManager.hostIsMac) {
107-
return@configureEach
108-
}
109-
110-
dependsOn("bootIosSimulatorDevice")
111-
finalizedBy("shutdownIosSimulatorDevice")
112-
113-
standalone = false
114-
device = simulatorDeviceName
115-
}
11671
}
11772

11873
kotlin.sourceSets.all {
@@ -157,4 +112,6 @@ subprojects {
157112
}
158113
}
159114
}
115+
116+
configureIosSimulatorTasks()
160117
}

runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/net/HostResolverTest.kt

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,12 @@ class HostResolverTest {
1515

1616
addresses.forEach { addr ->
1717
assertEquals("localhost", addr.hostname)
18-
when (val ip = addr.address) {
19-
is IpV4Addr -> {
20-
assertEquals(4, ip.octets.size)
21-
// localhost should resolve to 127.0.0.1
22-
assertContentEquals(byteArrayOf(127, 0, 0, 1), ip.octets)
23-
}
24-
is IpV6Addr -> {
25-
assertEquals(16, ip.octets.size)
26-
// ::1 in IPv6
27-
val expectedIpv6 = ByteArray(16) { 0 }
28-
expectedIpv6[15] = 1
29-
assertContentEquals(expectedIpv6, ip.octets)
30-
}
18+
19+
val localHostAddr = when (addr.address) {
20+
is IpV4Addr -> IpV4Addr.LOCALHOST
21+
is IpV6Addr -> IpV6Addr.LOCALHOST
3122
}
23+
assertEquals(addr.address, localHostAddr)
3224
}
3325
}
3426

runtime/runtime-core/native/src/aws/smithy/kotlin/runtime/net/HostResolverNative.kt

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,39 @@ internal actual object DefaultHostResolver : HostResolver {
1717

1818
val result = allocPointerTo<addrinfo>()
1919

20-
// Perform the DNS lookup
21-
val status = getaddrinfo(hostname, null, hints.ptr, result.ptr)
22-
if (status != 0) {
23-
throw RuntimeException("Failed to resolve host $hostname: ${gai_strerror(status)?.toKString()}")
20+
try {
21+
// Perform the DNS lookup
22+
val status = getaddrinfo(hostname, null, hints.ptr, result.ptr)
23+
check (status == 0) { "Failed to resolve host $hostname: ${gai_strerror(status)?.toKString()}" }
24+
25+
return generateSequence(result.value) { it.pointed.ai_next }
26+
.map { it.pointed.ai_addr!!.pointed.toIpAddr() }
27+
.map { HostAddress(hostname, it) }
28+
.toList()
29+
} finally {
30+
freeaddrinfo(result.value)
2431
}
32+
}
2533

26-
val addresses = mutableListOf<HostAddress>()
27-
var current = result.value
28-
29-
while (current != null) {
30-
val sockaddr = current.pointed.ai_addr!!.pointed
31-
32-
@OptIn(UnsafeNumber::class)
33-
when (sockaddr.sa_family.toInt()) {
34-
AF_INET -> {
35-
val addr = sockaddr.reinterpret<sockaddr_in>()
36-
val ipBytes = ByteArray(4)
37-
memcpy(ipBytes.refTo(0), addr.sin_addr.ptr, 4uL)
38-
39-
addresses.add(
40-
HostAddress(
41-
hostname = hostname,
42-
address = IpV4Addr(ipBytes),
43-
),
44-
)
45-
}
46-
AF_INET6 -> {
47-
val addr = sockaddr.reinterpret<sockaddr_in6>()
48-
val ipBytes = ByteArray(16)
49-
memcpy(ipBytes.refTo(0), addr.sin6_addr.ptr, 16.convert())
50-
addresses.add(
51-
HostAddress(
52-
hostname = hostname,
53-
address = IpV6Addr(ipBytes),
54-
),
55-
)
56-
}
57-
}
58-
current = current.pointed.ai_next
34+
@OptIn(UnsafeNumber::class)
35+
private fun sockaddr.toIpAddr(): IpAddr {
36+
val (size, addrPtr, constructor) = when (sa_family.toInt()) {
37+
AF_INET -> Triple(
38+
4,
39+
reinterpret<sockaddr_in>().sin_addr.ptr,
40+
{ bytes: ByteArray -> IpV4Addr(bytes) },
41+
)
42+
AF_INET6 -> Triple(
43+
16,
44+
reinterpret<sockaddr_in6>().sin6_addr.ptr,
45+
{ bytes: ByteArray -> IpV6Addr(bytes) },
46+
)
47+
else -> throw IllegalArgumentException("Unsupported sockaddr family $sa_family")
5948
}
6049

61-
// Free the getaddrinfo results
62-
freeaddrinfo(result.value)
63-
64-
addresses
50+
val ipBytes = ByteArray(size)
51+
memcpy(ipBytes.refTo(0), addrPtr, size.toULong())
52+
return constructor(ipBytes)
6553
}
6654

6755
actual override fun reportFailure(addr: HostAddress) {

0 commit comments

Comments
 (0)