Skip to content

Commit

Permalink
Socket Sequence Test
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Oct 24, 2023
1 parent a76530b commit a674b8d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions FlyingSocks/Tests/AsyncSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ final class AsyncSocketTests: XCTestCase {
try s1.close()
await AsyncAssertThrowsError(try s1.close(), of: SocketError.self)
}

func testSocketSequence_Ends_WhenDisconnected() async throws {
let s1 = try AsyncSocket.makeListening(pool: DisconnectedPool())
var sockets = s1.sockets
await AsyncAssertNil(
try await sockets.next()
)
}
}

extension AsyncSocket {
Expand All @@ -156,6 +164,16 @@ extension AsyncSocket {
try await make(pool: .client)
}

static func makeListening(pool: AsyncSocketPool) throws -> AsyncSocket {
let address = sockaddr_un.unix(path: "foxsocks")
try? Socket.unlink(address)
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
try socket.setValue(true, for: .localAddressReuse)
try socket.bind(to: address)
try socket.listen()
return try AsyncSocket(socket: socket, pool: pool)
}

static func make(pool: AsyncSocketPool) throws -> AsyncSocket {
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
return try AsyncSocket(socket: socket, pool: pool)
Expand Down Expand Up @@ -188,3 +206,14 @@ extension AsyncSocket {
return string
}
}

struct DisconnectedPool: AsyncSocketPool {

func prepare() async throws { }

func run() async throws { }

func suspendSocket(_ socket: FlyingSocks.Socket, untilReadyFor events: FlyingSocks.Socket.Events) async throws {
throw SocketError.disconnected
}
}

0 comments on commit a674b8d

Please sign in to comment.