@@ -252,12 +252,31 @@ struct AsyncSocketTests {
252252 try ? Socket . unlink ( addr)
253253 }
254254#endif
255+
256+ @Test
257+ func testMessageSequence( ) async throws {
258+ let ( socket, port) = try await AsyncSocket . makeLoopbackDatagram ( )
259+ var messages = socket. messages
260+
261+ async let received = [ messages. next ( ) , messages. next ( ) ]
262+
263+ let client = try await AsyncSocket . makeLoopbackDatagram ( ) . 0
264+ try await client. sendString ( " Fish 🐡 " , to: . loopback( port: port) )
265+ try await client. sendString ( " Chips 🍟 " , to: . loopback( port: port) )
266+
267+ #expect(
268+ try await received. compactMap { try $0? . payloadString } == [
269+ " Fish 🐡 " ,
270+ " Chips 🍟 "
271+ ]
272+ )
273+ }
255274}
256275
257276extension AsyncSocket {
258277
259- static func make( type: SocketType = . stream) async throws -> AsyncSocket {
260- try await make ( pool: . client, type: type)
278+ static func make( domain : Int32 = AF_UNIX , type: SocketType = . stream) async throws -> AsyncSocket {
279+ try await make ( pool: . client, domain : domain , type: type)
261280 }
262281
263282 static func makeListening( pool: some AsyncSocketPool ) throws -> AsyncSocket {
@@ -270,11 +289,22 @@ extension AsyncSocket {
270289 return try AsyncSocket ( socket: socket, pool: pool)
271290 }
272291
273- static func make( pool: some AsyncSocketPool , type: SocketType = . stream) throws -> AsyncSocket {
274- let socket = try Socket ( domain: AF_UNIX, type: type)
292+ static func make( pool: some AsyncSocketPool ,
293+ domain: Int32 = AF_UNIX,
294+ type: SocketType = . stream) throws -> AsyncSocket {
295+ let socket = try Socket ( domain: domain, type: type)
275296 return try AsyncSocket ( socket: socket, pool: pool)
276297 }
277298
299+ static func makeLoopbackDatagram( ) async throws -> ( AsyncSocket , port: UInt16 ) {
300+ let socket = try await AsyncSocket . make ( domain: AF_INET6, type: . datagram)
301+ try socket. socket. bind ( to: . loopback( port: 0 ) )
302+ guard case let . ip6( _, port: port) = try socket. socket. sockname ( ) else {
303+ fatalError ( )
304+ }
305+ return ( socket, port)
306+ }
307+
278308 static func makeDatagramPair( ) async throws -> ( AsyncSocket , AsyncSocket , sockaddr_un ) {
279309 let socketPair = try await makePair ( pool: . client, type: . datagram)
280310 guard let endpoint = FileManager . default. makeTemporaryFile ( ) else {
@@ -305,6 +335,22 @@ extension AsyncSocket {
305335 }
306336 return string
307337 }
338+
339+ func sendString( _ string: String , to address: some SocketAddress ) async throws {
340+ try await send ( string. data ( using: . utf8) !, to: address)
341+ }
342+ }
343+
344+ private extension AsyncSocket . Message {
345+
346+ var payloadString : String {
347+ get throws {
348+ guard let text = String ( bytes: bytes, encoding: . utf8) else {
349+ throw SocketError . disconnected
350+ }
351+ return text
352+ }
353+ }
308354}
309355
310356struct DisconnectedPool : AsyncSocketPool {
0 commit comments