/src/grpc-swift/FuzzTesting/Sources/EchoImplementation/EchoAsyncProvider.swift
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2021, gRPC Authors All rights reserved. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | import EchoModel |
17 | | import GRPC |
18 | | |
19 | | @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) |
20 | | public final class EchoAsyncProvider: Echo_EchoAsyncProvider { |
21 | | public let interceptors: Echo_EchoServerInterceptorFactoryProtocol? |
22 | | |
23 | 0 | public init(interceptors: Echo_EchoServerInterceptorFactoryProtocol? = nil) { |
24 | 0 | self.interceptors = interceptors |
25 | 0 | } |
26 | | |
27 | | public func get( |
28 | | request: Echo_EchoRequest, |
29 | | context: GRPCAsyncServerCallContext |
30 | 0 | ) async throws -> Echo_EchoResponse { |
31 | 0 | return .with { |
32 | 0 | $0.text = "Swift echo get: " + request.text |
33 | 0 | } |
34 | 0 | } |
35 | | |
36 | | public func expand( |
37 | | request: Echo_EchoRequest, |
38 | | responseStream: GRPCAsyncResponseStreamWriter<Echo_EchoResponse>, |
39 | | context: GRPCAsyncServerCallContext |
40 | 0 | ) async throws { |
41 | 0 | for (i, part) in request.text.components(separatedBy: " ").lazy.enumerated() { |
42 | 0 | try await responseStream.send(.with { $0.text = "Swift echo expand (\(i)): \(part)" }) |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | | public func collect( |
47 | | requestStream: GRPCAsyncRequestStream<Echo_EchoRequest>, |
48 | | context: GRPCAsyncServerCallContext |
49 | 0 | ) async throws -> Echo_EchoResponse { |
50 | 0 | let text = try await requestStream.reduce(into: "Swift echo collect:") { result, request in |
51 | 0 | result += " \(request.text)" |
52 | 0 | } |
53 | 0 |
|
54 | 0 | return .with { $0.text = text } |
55 | 0 | } |
56 | | |
57 | | public func update( |
58 | | requestStream: GRPCAsyncRequestStream<Echo_EchoRequest>, |
59 | | responseStream: GRPCAsyncResponseStreamWriter<Echo_EchoResponse>, |
60 | | context: GRPCAsyncServerCallContext |
61 | 0 | ) async throws { |
62 | 0 | var counter = 0 |
63 | 0 | for try await request in requestStream { |
64 | 0 | let text = "Swift echo update (\(counter)): \(request.text)" |
65 | 0 | try await responseStream.send(.with { $0.text = text }) |
66 | 0 | counter += 1 |
67 | 0 | } |
68 | 0 | } |
69 | | } |