Skip to content

Commit 337777f

Browse files
authored
Merge pull request #169 from swhitty/temporary-directory
Use temp directory for unix socket tests
2 parents b92b5bd + e41cf82 commit 337777f

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

FlyingSocks/Tests/AsyncSocketTests.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ struct AsyncSocketTests {
169169
}
170170
}
171171

172-
@Test
172+
@Test(.disabled("problematic test because file descriptor by openened by another parallel test"))
173173
func socket_Throws_WhenAlreadyCLosed() async throws {
174174
let s1 = try await AsyncSocket.make()
175175

@@ -188,14 +188,8 @@ struct AsyncSocketTests {
188188
)
189189
}
190190

191-
#if canImport(WinSDK)
192-
@Test
193-
func datagramPairCreation_Throws() async throws {
194-
await #expect(throws: SocketError.self) {
195-
_ = try await AsyncSocket.makeDatagramPair()
196-
}
197-
}
198-
#else
191+
#if !canImport(WinSDK)
192+
199193
@Test
200194
func datagramSocketReceivesChunk_WhenAvailable() async throws {
201195
let (s1, s2, addr) = try await AsyncSocket.makeDatagramPair()
@@ -215,9 +209,7 @@ struct AsyncSocketTests {
215209
try s2.close()
216210
try? Socket.unlink(addr)
217211
}
218-
#endif
219212

220-
#if !canImport(WinSDK)
221213
#if canImport(Darwin)
222214
@Test
223215
func messageSequence_sendsMessage_receivesTuple() async throws {
@@ -314,7 +306,7 @@ extension AsyncSocket {
314306
}
315307

316308
static func makeListening(pool: some AsyncSocketPool) throws -> AsyncSocket {
317-
let tempFile = FileManager.default.temporaryDirectory.appendingPathComponent("\(UUID().uuidString.prefix(8)).sock")
309+
let tempFile = try FileManager.default.makeTemporaryDirectory().appending(path: "socket")
318310
let address = sockaddr_un.unix(path: tempFile.path)
319311
try? Socket.unlink(address)
320312
defer { try? Socket.unlink(address) }
@@ -345,11 +337,10 @@ extension AsyncSocket {
345337
return (socket, port)
346338
}
347339

340+
#if !canImport(WinSDK)
348341
static func makeDatagramPair() async throws -> (AsyncSocket, AsyncSocket, sockaddr_un) {
349342
let socketPair = try await makePair(pool: .client, type: .datagram)
350-
guard let endpoint = FileManager.default.makeTemporaryFile() else {
351-
throw SocketError.makeFailed("MakeTemporaryFile")
352-
}
343+
let endpoint = try FileManager.default.makeTemporaryDirectory().appending(path: "socket")
353344
let addr = sockaddr_un.unix(path: endpoint.path)
354345

355346
try socketPair.1.socket.bind(to: addr)
@@ -359,6 +350,7 @@ extension AsyncSocket {
359350

360351
return (socketPair.0, socketPair.1, addr)
361352
}
353+
#endif
362354

363355
static func makePair() async throws -> (AsyncSocket, AsyncSocket) {
364356
try await makePair(pool: .client, type: .stream)

FlyingSocks/Tests/FileManager+TemporaryFile.swift

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,33 @@
1515
@testable import FlyingSocks
1616
import Foundation
1717

18+
1819
extension FileManager {
19-
func makeTemporaryFile() -> URL? {
20-
let dirPath = temporaryDirectory.appendingPathComponent("FlyingSocks.XXXXXX")
21-
return dirPath.withUnsafeFileSystemRepresentation { maybePath in
22-
guard let path = maybePath else { return nil }
2320

24-
#if canImport(WinSDK)
25-
let pathMax = Int(MAX_PATH)
26-
#else
27-
let pathMax = Int(PATH_MAX)
28-
#endif
21+
#if canImport(WinSDK)
22+
func makeTemporaryDirectory(template: String = "FlyingSocks.XXXXXX") throws -> URL {
23+
let suffix = UUID().uuidString.replacingOccurrences(of: "-", with: "").prefix(6)
24+
let url = temporaryDirectory.appendingPathComponent("FlyingSocks.\(suffix)", isDirectory: true)
25+
try createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
26+
return url
27+
}
28+
#else
29+
func makeTemporaryDirectory(template: String = "FlyingSocks.XXXXXX") throws -> URL {
30+
let base = temporaryDirectory.path
31+
let needsSlash = base.hasSuffix("/") ? "" : "/"
32+
var tmpl = Array((base + needsSlash + template).utf8CString)
2933

30-
var mutablePath = Array(repeating: Int8(0), count: pathMax)
31-
mutablePath.withUnsafeMutableBytes { mutablePathBufferPtr in
32-
mutablePathBufferPtr.baseAddress!.copyMemory(
33-
from: path, byteCount: Int(strlen(path)) + 1)
34-
}
35-
guard mktemp(&mutablePath) != nil else { return nil }
36-
return URL(
37-
fileURLWithFileSystemRepresentation: mutablePath, isDirectory: false,
38-
relativeTo: nil)
34+
let url = tmpl.withUnsafeMutableBufferPointer { buf -> URL? in
35+
guard let p = buf.baseAddress, mkdtemp(p) != nil else { return nil }
36+
let path = String(cString: p)
37+
return URL(fileURLWithPath: path, isDirectory: true)
3938
}
40-
}
41-
}
4239

43-
func withTemporaryFile(f: (URL) -> ()) throws {
44-
guard let tmp = FileManager.default.makeTemporaryFile() else {
45-
throw SocketError.makeFailed("MakeTemporaryFile")
40+
guard let url = url else {
41+
throw SocketError.makeFailed("makeTemporaryDirectory()")
42+
}
43+
return url
4644
}
47-
defer { try? FileManager.default.removeItem(atPath: tmp.path) }
48-
f(tmp)
45+
#endif
4946
}
47+

FlyingSocks/XCTests/AsyncSocketTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ final class AsyncSocketTests: XCTestCase {
142142
await AsyncAssertThrowsError(try await s1.accept(), of: SocketError.self)
143143
}
144144

145-
func testSocket_Throws_WhenAlreadyCLosed() async throws {
145+
func disabled_testSocket_Throws_WhenAlreadyCLosed() async throws {
146146
let s1 = try await AsyncSocket.make()
147147

148148
try s1.close()

0 commit comments

Comments
 (0)