1# Copyright 2017 gRPC authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15# Test stubs for various scenarios 16require 'spec_helper' 17 18# A test message 19class EchoMsg 20 attr_reader :msg 21 22 def initialize(msg: '') 23 @msg = msg 24 end 25 26 def self.marshal(o) 27 o.msg 28 end 29 30 def self.unmarshal(msg) 31 EchoMsg.new(msg: msg) 32 end 33end 34 35# A test service with an echo implementation. 36class EchoService 37 include GRPC::GenericService 38 rpc :an_rpc, EchoMsg, EchoMsg 39 rpc :a_client_streaming_rpc, stream(EchoMsg), EchoMsg 40 rpc :a_server_streaming_rpc, EchoMsg, stream(EchoMsg) 41 rpc :a_bidi_rpc, stream(EchoMsg), stream(EchoMsg) 42 rpc :a_client_streaming_rpc_unimplemented, stream(EchoMsg), EchoMsg 43 attr_reader :received_md 44 45 def initialize(**kw) 46 @trailing_metadata = kw 47 @received_md = [] 48 end 49 50 def an_rpc(req, call) 51 GRPC.logger.info('echo service received a request') 52 call.output_metadata.update(@trailing_metadata) 53 @received_md << call.metadata unless call.metadata.nil? 54 req 55 end 56 57 def a_client_streaming_rpc(call) 58 # iterate through requests so call can complete 59 call.output_metadata.update(@trailing_metadata) 60 call.each_remote_read.each do |r| 61 GRPC.logger.info(r) 62 end 63 EchoMsg.new 64 end 65 66 def a_server_streaming_rpc(_req, call) 67 call.output_metadata.update(@trailing_metadata) 68 [EchoMsg.new, EchoMsg.new] 69 end 70 71 def a_bidi_rpc(requests, call) 72 call.output_metadata.update(@trailing_metadata) 73 requests.each do |r| 74 GRPC.logger.info(r) 75 end 76 [EchoMsg.new, EchoMsg.new] 77 end 78end 79 80EchoStub = EchoService.rpc_stub_class 81 82# For testing server interceptors 83class TestServerInterceptor < GRPC::ServerInterceptor 84 def request_response(request:, call:, method:) 85 GRPC.logger.info("Received request/response call at method #{method}" \ 86 " with request #{request} for call #{call}") 87 call.output_metadata[:interc] = 'from_request_response' 88 GRPC.logger.info("[GRPC::Ok] (#{method.owner.name}.#{method.name})") 89 yield 90 end 91 92 def client_streamer(call:, method:) 93 call.output_metadata[:interc] = 'from_client_streamer' 94 call.each_remote_read.each do |r| 95 GRPC.logger.info("In interceptor: #{r}") 96 end 97 GRPC.logger.info( 98 "Received client streamer call at method #{method} for call #{call}" 99 ) 100 yield 101 end 102 103 def server_streamer(request:, call:, method:) 104 GRPC.logger.info("Received server streamer call at method #{method} with request" \ 105 " #{request} for call #{call}") 106 call.output_metadata[:interc] = 'from_server_streamer' 107 yield 108 end 109 110 def bidi_streamer(requests:, call:, method:) 111 requests.each do |r| 112 GRPC.logger.info("Bidi request: #{r}") 113 end 114 GRPC.logger.info("Received bidi streamer call at method #{method} with requests" \ 115 " #{requests} for call #{call}") 116 call.output_metadata[:interc] = 'from_bidi_streamer' 117 yield 118 end 119end 120 121# For testing client interceptors 122class TestClientInterceptor < GRPC::ClientInterceptor 123 def request_response(request:, call:, method:, metadata: {}) 124 GRPC.logger.info("Intercepted request/response call at method #{method}" \ 125 " with request #{request} for call #{call}" \ 126 " and metadata: #{metadata}") 127 metadata['foo'] = 'bar_from_request_response' 128 yield 129 end 130 131 def client_streamer(requests:, call:, method:, metadata: {}) 132 GRPC.logger.info("Received client streamer call at method #{method}" \ 133 " with requests #{requests} for call #{call}" \ 134 " and metadata: #{metadata}") 135 requests.each do |r| 136 GRPC.logger.info("In client interceptor: #{r}") 137 end 138 metadata['foo'] = 'bar_from_client_streamer' 139 yield 140 end 141 142 def server_streamer(request:, call:, method:, metadata: {}) 143 GRPC.logger.info("Received server streamer call at method #{method}" \ 144 " with request #{request} for call #{call}" \ 145 " and metadata: #{metadata}") 146 metadata['foo'] = 'bar_from_server_streamer' 147 yield 148 end 149 150 def bidi_streamer(requests:, call:, method:, metadata: {}) 151 GRPC.logger.info("Received bidi streamer call at method #{method}" \ 152 "with requests #{requests} for call #{call}" \ 153 " and metadata: #{metadata}") 154 requests.each do |r| 155 GRPC.logger.info("In client interceptor: #{r}") 156 end 157 metadata['foo'] = 'bar_from_bidi_streamer' 158 yield 159 end 160end 161