Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222import Darwinimport Foundationimport Testing
@testable import ParcelIPC
private func withRawServer( reply: @escaping @Sendable (IPCRequest, SocketChannel) async throws -> Void, clientAction: @escaping @Sendable (IPCClient) async throws -> Void) async throws { let fixture = try SocketFixture() defer { fixture.remove() } let listener = try makeSocket() defer { Darwin.close(listener) } var address = try SocketAddress(fixture.path) guard address.withPointer({ Darwin.bind(listener, $0, $1) }) == 0, Darwin.listen(listener, 1) == 0 else { throw IPCTransportError.system(operation: "bind test listener", code: errno) } let client = try IPCClient(socketPath: fixture.path) try await withThrowingTaskGroup(of: Void.self) { group in group.addTask { var descriptor: Int32 = -1 while descriptor < 0 { descriptor = Darwin.accept(listener, nil, nil) if descriptor < 0 { guard errno == EAGAIN || errno == EWOULDBLOCK else { throw IPCTransportError.system(operation: "accept test client", code: errno) } try await Task.sleep(for: .milliseconds(2)) } } try configureSocket(descriptor) let channel = SocketChannel(descriptor: descriptor) do { let line = try #require(await channel.readLine()) let request = try IPCCodec.decode(IPCRequest.self, from: line) try await reply(request, channel) await channel.close() } catch { await channel.close() throw error } } group.addTask { try await clientAction(client) } defer { group.cancelAll() } try await group.waitForAll() } await client.close()}
@Suite("Client validation", .timeLimit(.minutes(1)))struct ClientTests { @Test(arguments: [0, 2, 99]) func responseVersion(_ version: Int) async throws { try await withRawServer { request, channel in try await channel.write(IPCCodec.encode(IPCResponse(id: request.id, result: .null, v: version))) } clientAction: { client in _ = await #expect(throws: IPCTransportError.self) { try await client.request(IPCRequest(command: "ping")) } } }
@Test func mismatchedID() async throws { try await withRawServer { _, channel in try await channel.write(IPCCodec.encode(IPCResponse(id: .string("wrong"), result: .null))) } clientAction: { client in _ = await #expect(throws: IPCTransportError.self) { try await client.request(IPCRequest(command: "ping")) } } }
@Test func malformedResponse() async throws { try await withRawServer { _, channel in try await channel.write(Data("{\"v\":1,\"ok\":true}\n".utf8)) } clientAction: { client in _ = await #expect(throws: DecodingError.self) { try await client.request(IPCRequest(command: "ping")) } } }
@Test func noResponse() async throws { try await withRawServer { _, _ in } clientAction: { client in _ = await #expect(throws: IPCTransportError.closed) { try await client.request(IPCRequest(command: "ping")) } } }
@Test func subscriptionHandshakeError() async throws { let expected = IPCError(code: "unavailable", message: "Try again later.") try await withRawServer { request, channel in try await channel.write(IPCCodec.encode(IPCResponse(id: request.id, error: expected))) } clientAction: { client in _ = await #expect(throws: expected) { try await client.subscribe() } } }
@Test func subscriptionVersion() async throws { try await withRawServer { request, channel in try await channel.write(IPCCodec.encode(IPCResponse(id: request.id, result: .null, v: 2))) } clientAction: { client in _ = await #expect(throws: IPCTransportError.self) { try await client.subscribe() } } }
@Test(arguments: [0, 2, 99]) func eventVersion(_ version: Int) async throws { try await withRawServer { request, channel in try await channel.write( IPCCodec.encode(IPCResponse(id: request.id, result: .object(["topics": .array([])])))) try await channel.write(IPCCodec.encode(IPCEvent(topic: .focus, data: .null, v: version))) } clientAction: { client in let subscription = try await client.subscribe() _ = await #expect(throws: IPCTransportError.self) { try await deadline { var iterator = subscription.events.makeAsyncIterator() return try await iterator.next() } } await subscription.cancel() } }
@Test func streamErrorResponse() async throws { let expected = IPCError(code: "slow_subscriber", message: "Reconnect.") try await withRawServer { request, channel in try await channel.write(IPCCodec.encode(IPCResponse(id: request.id, result: .null))) try await channel.write(IPCCodec.encode(IPCResponse(id: request.id, error: expected))) } clientAction: { client in let subscription = try await client.subscribe() _ = await #expect(throws: expected) { try await deadline { var iterator = subscription.events.makeAsyncIterator() return try await iterator.next() } } await subscription.cancel() } }
@Test func extraResponseIsNotAnEvent() async throws { try await withRawServer { request, channel in let response = IPCResponse(id: request.id, result: .null) try await channel.write(IPCCodec.encode(response)) try await channel.write(IPCCodec.encode(response)) } clientAction: { client in let subscription = try await client.subscribe() _ = await #expect(throws: IPCTransportError.self) { try await deadline { var iterator = subscription.events.makeAsyncIterator() return try await iterator.next() } } await subscription.cancel() } }
@Test func slowSubscriberGetsAnError() async throws { try await withServer { server, client, _, _ in let subscription = try await client.subscribe() for number in 0..<512 { try await server.publish(IPCEvent(topic: .focus, data: .integer(Int64(number)))) } // Publishing finishes before the socket reader necessarily fills the client buffer. try await eventually { await server.clientCount == 0 } let error = await #expect(throws: IPCError.self) { try await deadline { for try await _ in subscription.events {} } } #expect(error?.code == "slow_subscriber") await subscription.cancel() } }
@Test func subscriberThatKeepsUpDoesNotOverflow() async throws { try await withServer { server, client, _, _ in let subscription = try await client.subscribe() var iterator = subscription.events.makeAsyncIterator() for number in 0..<512 { let event = IPCEvent(topic: .focus, data: .integer(Int64(number))) try await server.publish(event) #expect(try await iterator.next() == event) } #expect(await server.subscriberCount == 1) await subscription.cancel() } }
@Test func clientCanBeReusedAfterClose() async throws { try await withServer { _, client, _, _ in await client.close() let response = try await client.request(IPCRequest(command: "ping")) #expect(response.ok) } }}
@Suite("Catalog resources")struct IPCResourceTests { @Test func compiledStringsMatchCatalog() throws { let catalogURL = try #require(Bundle.module.url(forResource: "Localizable", withExtension: "xcstrings")) let catalog = try JSONDecoder().decode(Catalog.self, from: Data(contentsOf: catalogURL)) let stringsURL = try #require( Bundle.module.url(forResource: "Localizable", withExtension: "strings", subdirectory: "en.lproj")) let strings = try PropertyListDecoder().decode([String: String].self, from: Data(contentsOf: stringsURL)) #expect(strings.count == catalog.strings.count) for (key, entry) in catalog.strings { #expect(strings[key] == entry.localizations["en"]?.stringUnit.value) } }
private struct Catalog: Decodable { let strings: [String: Entry] struct Entry: Decodable { let localizations: [String: Localization] } struct Localization: Decodable { let stringUnit: Unit } struct Unit: Decodable { let value: String } }}