| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- /*
- * Copyright 2023, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import Atomics
- import GRPCCore
- import GRPCInProcessTransport
- import XCTest
- @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
- final class GRPCClientTests: XCTestCase {
- func withInProcessConnectedClient(
- services: [any RegistrableRPCService],
- interceptors: [any ClientInterceptor] = [],
- _ body: (GRPCClient, GRPCServer) async throws -> Void
- ) async throws {
- let inProcess = InProcessTransport.makePair()
- let client = GRPCClient(transport: inProcess.client, interceptors: interceptors)
- let server = GRPCServer(transport: inProcess.server, services: services)
- try await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- try await server.run()
- }
- group.addTask {
- try await client.run()
- }
- // Make sure both server and client are running
- try await Task.sleep(for: .milliseconds(100))
- try await body(client, server)
- client.close()
- server.stopListening()
- }
- }
- struct IdentitySerializer: MessageSerializer {
- typealias Message = [UInt8]
- func serialize(_ message: [UInt8]) throws -> [UInt8] {
- return message
- }
- }
- struct IdentityDeserializer: MessageDeserializer {
- typealias Message = [UInt8]
- func deserialize(_ serializedMessageBytes: [UInt8]) throws -> [UInt8] {
- return serializedMessageBytes
- }
- }
- func testUnary() async throws {
- try await self.withInProcessConnectedClient(services: [BinaryEcho()]) { client, _ in
- try await client.unary(
- request: .init(message: [3, 1, 4, 1, 5]),
- descriptor: BinaryEcho.Methods.collect,
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- let message = try response.message
- XCTAssertEqual(message, [3, 1, 4, 1, 5])
- }
- }
- }
- func testClientStreaming() async throws {
- try await self.withInProcessConnectedClient(services: [BinaryEcho()]) { client, _ in
- try await client.clientStreaming(
- request: .init(producer: { writer in
- for byte in [3, 1, 4, 1, 5] as [UInt8] {
- try await writer.write([byte])
- }
- }),
- descriptor: BinaryEcho.Methods.collect,
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- let message = try response.message
- XCTAssertEqual(message, [3, 1, 4, 1, 5])
- }
- }
- }
- func testServerStreaming() async throws {
- try await self.withInProcessConnectedClient(services: [BinaryEcho()]) { client, _ in
- try await client.serverStreaming(
- request: .init(message: [3, 1, 4, 1, 5]),
- descriptor: BinaryEcho.Methods.expand,
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- var responseParts = response.messages.makeAsyncIterator()
- for byte in [3, 1, 4, 1, 5] as [UInt8] {
- let message = try await responseParts.next()
- XCTAssertEqual(message, [byte])
- }
- }
- }
- }
- func testBidirectionalStreaming() async throws {
- try await self.withInProcessConnectedClient(services: [BinaryEcho()]) { client, _ in
- try await client.bidirectionalStreaming(
- request: .init(producer: { writer in
- for byte in [3, 1, 4, 1, 5] as [UInt8] {
- try await writer.write([byte])
- }
- }),
- descriptor: BinaryEcho.Methods.update,
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- var responseParts = response.messages.makeAsyncIterator()
- for byte in [3, 1, 4, 1, 5] as [UInt8] {
- let message = try await responseParts.next()
- XCTAssertEqual(message, [byte])
- }
- }
- }
- }
- func testUnimplementedMethod_Unary() async throws {
- try await self.withInProcessConnectedClient(services: [BinaryEcho()]) { client, _ in
- try await client.unary(
- request: .init(message: [3, 1, 4, 1, 5]),
- descriptor: MethodDescriptor(service: "not", method: "implemented"),
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- XCTAssertThrowsRPCError(try response.accepted.get()) { error in
- XCTAssertEqual(error.code, .unimplemented)
- }
- }
- }
- }
- func testUnimplementedMethod_ClientStreaming() async throws {
- try await self.withInProcessConnectedClient(services: [BinaryEcho()]) { client, _ in
- try await client.clientStreaming(
- request: .init(producer: { writer in
- for byte in [3, 1, 4, 1, 5] as [UInt8] {
- try await writer.write([byte])
- }
- }),
- descriptor: MethodDescriptor(service: "not", method: "implemented"),
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- XCTAssertThrowsRPCError(try response.accepted.get()) { error in
- XCTAssertEqual(error.code, .unimplemented)
- }
- }
- }
- }
- func testUnimplementedMethod_ServerStreaming() async throws {
- try await self.withInProcessConnectedClient(services: [BinaryEcho()]) { client, _ in
- try await client.serverStreaming(
- request: .init(message: [3, 1, 4, 1, 5]),
- descriptor: MethodDescriptor(service: "not", method: "implemented"),
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- XCTAssertThrowsRPCError(try response.accepted.get()) { error in
- XCTAssertEqual(error.code, .unimplemented)
- }
- }
- }
- }
- func testUnimplementedMethod_BidirectionalStreaming() async throws {
- try await self.withInProcessConnectedClient(services: [BinaryEcho()]) { client, _ in
- try await client.bidirectionalStreaming(
- request: .init(producer: { writer in
- for byte in [3, 1, 4, 1, 5] as [UInt8] {
- try await writer.write([byte])
- }
- }),
- descriptor: MethodDescriptor(service: "not", method: "implemented"),
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- XCTAssertThrowsRPCError(try response.accepted.get()) { error in
- XCTAssertEqual(error.code, .unimplemented)
- }
- }
- }
- }
- func testMultipleConcurrentRequests() async throws {
- try await self.withInProcessConnectedClient(services: [BinaryEcho()]) { client, _ in
- await withThrowingTaskGroup(of: Void.self) { group in
- for i in UInt8.min ..< UInt8.max {
- group.addTask {
- try await client.unary(
- request: .init(message: [i]),
- descriptor: BinaryEcho.Methods.collect,
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- let message = try response.message
- XCTAssertEqual(message, [i])
- }
- }
- }
- }
- }
- }
- func testInterceptorsAreAppliedInOrder() async throws {
- let counter1 = ManagedAtomic(0)
- let counter2 = ManagedAtomic(0)
- try await self.withInProcessConnectedClient(
- services: [BinaryEcho()],
- interceptors: [
- .requestCounter(counter1),
- .rejectAll(with: RPCError(code: .unavailable, message: "")),
- .requestCounter(counter2),
- ]
- ) { client, _ in
- try await client.unary(
- request: .init(message: [3, 1, 4, 1, 5]),
- descriptor: BinaryEcho.Methods.collect,
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- XCTAssertRejected(response) { error in
- XCTAssertEqual(error.code, .unavailable)
- }
- }
- }
- XCTAssertEqual(counter1.load(ordering: .sequentiallyConsistent), 1)
- XCTAssertEqual(counter2.load(ordering: .sequentiallyConsistent), 0)
- }
- func testNoNewRPCsAfterClientClose() async throws {
- try await withInProcessConnectedClient(services: [BinaryEcho()]) { client, _ in
- // Run an RPC so we know the client is running properly.
- try await client.unary(
- request: .init(message: [3, 1, 4, 1, 5]),
- descriptor: BinaryEcho.Methods.collect,
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- let message = try response.message
- XCTAssertEqual(message, [3, 1, 4, 1, 5])
- }
- // New RPCs should fail immediately after this.
- client.close()
- // RPC should fail now.
- await XCTAssertThrowsErrorAsync(ofType: RuntimeError.self) {
- try await client.unary(
- request: .init(message: [3, 1, 4, 1, 5]),
- descriptor: BinaryEcho.Methods.collect,
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { _ in }
- } errorHandler: { error in
- XCTAssertEqual(error.code, .clientIsStopped)
- }
- }
- }
- func testInFlightRPCsCanContinueAfterClientIsClosed() async throws {
- try await withInProcessConnectedClient(services: [BinaryEcho()]) { client, server in
- try await client.clientStreaming(
- request: .init(producer: { writer in
- // Close the client once this RCP has been started.
- client.close()
- // Attempts to start a new RPC should fail.
- await XCTAssertThrowsErrorAsync(ofType: RuntimeError.self) {
- try await client.unary(
- request: .init(message: [3, 1, 4, 1, 5]),
- descriptor: BinaryEcho.Methods.collect,
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { _ in }
- } errorHandler: { error in
- XCTAssertEqual(error.code, .clientIsStopped)
- }
- // Now write to the already opened stream to confirm that opened streams
- // can successfully run to completion.
- for byte in [3, 1, 4, 1, 5] as [UInt8] {
- try await writer.write([byte])
- }
- }),
- descriptor: BinaryEcho.Methods.collect,
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- let message = try response.message
- XCTAssertEqual(message, [3, 1, 4, 1, 5])
- }
- }
- }
- func testCancelRunningClient() async throws {
- let inProcess = InProcessTransport.makePair()
- let client = GRPCClient(transport: inProcess.client)
- try await withThrowingTaskGroup(of: Void.self) { group in
- group.addTask {
- let server = GRPCServer(transport: inProcess.server, services: [BinaryEcho()])
- try await server.run()
- }
- group.addTask {
- try await client.run()
- }
- // Wait for client and server to be running.
- try await Task.sleep(for: .milliseconds(10))
- let task = Task {
- try await client.clientStreaming(
- request: .init(producer: { writer in
- try await Task.sleep(for: .seconds(5))
- }),
- descriptor: BinaryEcho.Methods.collect,
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- XCTAssertRejected(response) { error in
- XCTAssertEqual(error.code, .unknown)
- }
- }
- }
- // Check requests are getting through.
- try await client.unary(
- request: .init(message: [3, 1, 4, 1, 5]),
- descriptor: BinaryEcho.Methods.collect,
- serializer: IdentitySerializer(),
- deserializer: IdentityDeserializer()
- ) { response in
- let message = try response.message
- XCTAssertEqual(message, [3, 1, 4, 1, 5])
- }
- task.cancel()
- try await task.value
- group.cancelAll()
- }
- }
- func testRunStoppedClient() async throws {
- let (_, clientTransport) = InProcessTransport.makePair()
- let client = GRPCClient(transport: clientTransport)
- // Run the client.
- let task = Task { try await client.run() }
- task.cancel()
- try await task.value
- // Client is stopped, should throw an error.
- await XCTAssertThrowsErrorAsync(ofType: RuntimeError.self) {
- try await client.run()
- } errorHandler: { error in
- XCTAssertEqual(error.code, .clientIsStopped)
- }
- }
- func testRunAlreadyRunningClient() async throws {
- let (_, clientTransport) = InProcessTransport.makePair()
- let client = GRPCClient(transport: clientTransport)
- // Run the client.
- let task = Task { try await client.run() }
- // Make sure the client is run for the first time here.
- try await Task.sleep(for: .milliseconds(10))
- // Client is already running, should throw an error.
- await XCTAssertThrowsErrorAsync(ofType: RuntimeError.self) {
- try await client.run()
- } errorHandler: { error in
- XCTAssertEqual(error.code, .clientIsAlreadyRunning)
- }
- task.cancel()
- }
- }
|