Skip to content

Commit 02e89e5

Browse files
committed
more datagram tests
1 parent e598f17 commit 02e89e5

File tree

1 file changed

+72
-36
lines changed

1 file changed

+72
-36
lines changed

FlyingSocks/Tests/AsyncSocketTests.swift

+72-36
Original file line numberDiff line numberDiff line change
@@ -209,52 +209,43 @@ struct AsyncSocketTests {
209209
}
210210

211211
#if !canImport(WinSDK)
212+
#if canImport(Darwin)
212213
@Test
213-
func datagramSocketReceivesMessageTupleAPI_WhenAvailable() async throws {
214-
let (s1, s2, addr) = try await AsyncSocket.makeDatagramPair()
214+
func messageSequence_sendsMessage_receivesTuple() async throws {
215+
let (socket, port) = try await AsyncSocket.makeLoopbackDatagram()
215216

216-
async let d2: AsyncSocket.Message = s2.receive(atMost: 100)
217-
#if canImport(Darwin)
218-
try await s1.write("Swift".data(using: .utf8)!)
219-
#else
220-
try await s1.send(message: "Swift".data(using: .utf8)!, to: addr, from: addr)
221-
#endif
222-
let v2 = try await d2
223-
#expect(String(data: Data(v2.bytes), encoding: .utf8) == "Swift")
217+
async let received: (any SocketAddress, [UInt8]) = socket.receive(atMost: 100)
224218

225-
try s1.close()
226-
try s2.close()
227-
try? Socket.unlink(addr)
228-
}
229-
#endif
219+
let client = try await AsyncSocket.makeLoopbackDatagram().0
220+
let message = AsyncSocket.Message(peerAddress: .loopback(port: port), payload: "Chips 🍟")
221+
try await client.send(message: message)
230222

231-
#if !canImport(WinSDK)
223+
#expect(
224+
try await received.1 == Array("Chips 🍟".data(using: .utf8)!)
225+
)
226+
}
227+
#else
232228
@Test
233-
func datagramSocketReceivesMessageStructAPI_WhenAvailable() async throws {
229+
func sendMessage_receivesTuple() async throws {
234230
let (s1, s2, addr) = try await AsyncSocket.makeDatagramPair()
235-
let messageToSend = AsyncSocket.Message(
236-
peerAddress: addr,
237-
bytes: Array("Swift".data(using: .utf8)!),
238-
localAddress: addr
239-
)
231+
defer {
232+
try? s1.close()
233+
try? s2.close()
234+
try? Socket.unlink(addr)
235+
}
236+
async let received: (any SocketAddress, [UInt8]) = s2.receive(atMost: 100)
240237

241-
async let d2: AsyncSocket.Message = s2.receive(atMost: 100)
242-
#if canImport(Darwin)
243-
try await s1.write("Swift".data(using: .utf8)!)
244-
#else
245-
try await s1.send(message: messageToSend)
246-
#endif
247-
let v2 = try await d2
248-
#expect(String(data: Data(v2.bytes), encoding: .utf8) == "Swift")
238+
let message = AsyncSocket.Message(peerAddress: addr, payload: "Shrimp 🦐")
239+
try await s1.send(message: message)
249240

250-
try s1.close()
251-
try s2.close()
252-
try? Socket.unlink(addr)
241+
#expect(
242+
try await received.1 == Array("Shrimp 🦐".data(using: .utf8)!)
243+
)
253244
}
254-
#endif
245+
#endif
255246

256247
@Test
257-
func messageSequence_receives_messages() async throws {
248+
func messageSequence_sendsData_receivesMessage() async throws {
258249
let (socket, port) = try await AsyncSocket.makeLoopbackDatagram()
259250
var messages = socket.messages
260251

@@ -267,6 +258,44 @@ struct AsyncSocketTests {
267258
try await received?.payloadString == "Fish 🐡"
268259
)
269260
}
261+
262+
#if canImport(Darwin)
263+
@Test
264+
func messageSequence_sendsMessage_receivesMessage() async throws {
265+
let (socket, port) = try await AsyncSocket.makeLoopbackDatagram()
266+
var messages = socket.messages
267+
268+
async let received = messages.next()
269+
270+
let client = try await AsyncSocket.makeLoopbackDatagram().0
271+
let message = AsyncSocket.Message(peerAddress: .loopback(port: port), payload: "Chips 🍟")
272+
try await client.send(message: message)
273+
274+
#expect(
275+
try await received?.payloadString == "Chips 🍟"
276+
)
277+
}
278+
#else
279+
@Test
280+
func sendMessage_receivesMessage() async throws {
281+
let (s1, s2, addr) = try await AsyncSocket.makeDatagramPair()
282+
defer {
283+
try? s1.close()
284+
try? s2.close()
285+
try? Socket.unlink(addr)
286+
}
287+
288+
async let received: AsyncSocket.Message = s2.receive(atMost: 100)
289+
290+
let message = AsyncSocket.Message(peerAddress: addr, payload: "Shrimp 🦐")
291+
try await s1.send(message: message)
292+
293+
#expect(
294+
try await received.payloadString == "Shrimp 🦐"
295+
)
296+
}
297+
#endif
298+
#endif
270299
}
271300

272301
extension AsyncSocket {
@@ -341,12 +370,19 @@ private extension AsyncSocket.Message {
341370

342371
var payloadString: String {
343372
get throws {
344-
guard let text = String(bytes: bytes, encoding: .utf8) else {
373+
guard let text = String(data: payload, encoding: .utf8) else {
345374
throw SocketError.disconnected
346375
}
347376
return text
348377
}
349378
}
379+
380+
init(peerAddress: some SocketAddress, payload: String) {
381+
self.init(
382+
peerAddress: peerAddress,
383+
payload: payload.data(using: .utf8)!
384+
)
385+
}
350386
}
351387

352388
struct DisconnectedPool: AsyncSocketPool {

0 commit comments

Comments
 (0)