Skip to content

Commit 2d8e446

Browse files
committed
fix: add Linux compatibility to HTTP test server
- Add conditional imports for Glibc/Musl to support Linux builds - Replace macOS-specific CFSwapInt16BigToHost with portable ntohs for byte order conversion - Fix shutdown() calls to use platform-specific namespaces (Darwin/Glibc/Musl) - Fix minor code warnings (unused variable, boolean test) The HTTP test server now compiles and runs correctly on both macOS and Linux platforms, maintaining full backward compatibility while enabling cross-platform support. This change is required for the test suite to pass in Linux CI environments.
1 parent def504d commit 2d8e446

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Tests/Shared/TestUtils/HttpTestServer.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import XCTest
99
import FoundationNetworking
1010
#endif
1111

12+
#if canImport(Darwin)
13+
import Darwin
14+
#elseif canImport(Glibc)
15+
import Glibc
16+
#elseif canImport(Musl)
17+
import Musl
18+
#endif
19+
1220
/// A unified HTTP test server using POSIX sockets
1321
/// Combines functionality from both OTLP exporter tests and URLSession instrumentation tests
1422
public class HttpTestServer {
@@ -91,7 +99,12 @@ public class HttpTestServer {
9199
throw TestServerError.portRetrievalFailed
92100
}
93101

102+
// Convert from network byte order (big endian) to host byte order
103+
#if canImport(Darwin)
94104
serverPort = Int(CFSwapInt16BigToHost(actualAddr.sin_port))
105+
#else
106+
serverPort = Int(ntohs(actualAddr.sin_port))
107+
#endif
95108

96109
// Listen
97110
guard listen(serverSocket, 10) >= 0 else {
@@ -117,7 +130,14 @@ public class HttpTestServer {
117130

118131
// Close server socket
119132
if serverSocket >= 0 {
133+
// Use platform-specific shutdown
134+
#if canImport(Darwin)
120135
Darwin.shutdown(serverSocket, SHUT_RDWR)
136+
#elseif canImport(Glibc)
137+
Glibc.shutdown(serverSocket, SHUT_RDWR)
138+
#elseif canImport(Musl)
139+
Musl.shutdown(serverSocket, SHUT_RDWR)
140+
#endif
121141
close(serverSocket)
122142
serverSocket = -1
123143
}
@@ -262,7 +282,7 @@ public class HttpTestServer {
262282
return
263283
}
264284

265-
let method = parts[0]
285+
let _ = parts[0] // method - unused but needed for parsing
266286
let path = parts[1]
267287

268288
// Parse headers
@@ -303,7 +323,7 @@ public class HttpTestServer {
303323
}
304324

305325
// Handle URLSession test paths
306-
if let config = config {
326+
if config != nil {
307327
handleURLSessionRequest(socket: clientSocket, path: path)
308328
return
309329
}

0 commit comments

Comments
 (0)