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 attr_accessor :on_call_started 45 46 def initialize(**kw) 47 @trailing_metadata = kw 48 @received_md = [] 49 @on_call_started = nil 50 end 51 52 def an_rpc(req, call) 53 GRPC.logger.info('echo service received a request') 54 on_call_started&.call(call) 55 call.output_metadata.update(@trailing_metadata) 56 @received_md << call.metadata unless call.metadata.nil? 57 req 58 end 59 60 def a_client_streaming_rpc(call) 61 # iterate through requests so call can complete 62 call.output_metadata.update(@trailing_metadata) 63 call.each_remote_read.each do |r| 64 GRPC.logger.info(r) 65 end 66 EchoMsg.new 67 end 68 69 def a_server_streaming_rpc(_req, call) 70 call.output_metadata.update(@trailing_metadata) 71 [EchoMsg.new, EchoMsg.new] 72 end 73 74 def a_bidi_rpc(requests, call) 75 call.output_metadata.update(@trailing_metadata) 76 requests.each do |r| 77 GRPC.logger.info(r) 78 end 79 [EchoMsg.new, EchoMsg.new] 80 end 81end 82 83EchoStub = EchoService.rpc_stub_class 84 85# For testing server interceptors 86class TestServerInterceptor < GRPC::ServerInterceptor 87 def request_response(request:, call:, method:) 88 GRPC.logger.info("Received request/response call at method #{method}" \ 89 " with request #{request} for call #{call}") 90 call.output_metadata[:interc] = 'from_request_response' 91 GRPC.logger.info("[GRPC::Ok] (#{method.owner.name}.#{method.name})") 92 yield 93 end 94 95 def client_streamer(call:, method:) 96 call.output_metadata[:interc] = 'from_client_streamer' 97 call.each_remote_read.each do |r| 98 GRPC.logger.info("In interceptor: #{r}") 99 end 100 GRPC.logger.info( 101 "Received client streamer call at method #{method} for call #{call}" 102 ) 103 yield 104 end 105 106 def server_streamer(request:, call:, method:) 107 GRPC.logger.info("Received server streamer call at method #{method} with request" \ 108 " #{request} for call #{call}") 109 call.output_metadata[:interc] = 'from_server_streamer' 110 yield 111 end 112 113 def bidi_streamer(requests:, call:, method:) 114 requests.each do |r| 115 GRPC.logger.info("Bidi request: #{r}") 116 end 117 GRPC.logger.info("Received bidi streamer call at method #{method} with requests" \ 118 " #{requests} for call #{call}") 119 call.output_metadata[:interc] = 'from_bidi_streamer' 120 yield 121 end 122end 123 124# For testing client interceptors 125class TestClientInterceptor < GRPC::ClientInterceptor 126 def request_response(request:, call:, method:, metadata: {}) 127 GRPC.logger.info("Intercepted request/response call at method #{method}" \ 128 " with request #{request} for call #{call}" \ 129 " and metadata: #{metadata}") 130 metadata['foo'] = 'bar_from_request_response' 131 yield 132 end 133 134 def client_streamer(requests:, call:, method:, metadata: {}) 135 GRPC.logger.info("Received client streamer call at method #{method}" \ 136 " with requests #{requests} for call #{call}" \ 137 " and metadata: #{metadata}") 138 requests.each do |r| 139 GRPC.logger.info("In client interceptor: #{r}") 140 end 141 metadata['foo'] = 'bar_from_client_streamer' 142 yield 143 end 144 145 def server_streamer(request:, call:, method:, metadata: {}) 146 GRPC.logger.info("Received server streamer call at method #{method}" \ 147 " with request #{request} for call #{call}" \ 148 " and metadata: #{metadata}") 149 metadata['foo'] = 'bar_from_server_streamer' 150 yield 151 end 152 153 def bidi_streamer(requests:, call:, method:, metadata: {}) 154 GRPC.logger.info("Received bidi streamer call at method #{method}" \ 155 "with requests #{requests} for call #{call}" \ 156 " and metadata: #{metadata}") 157 requests.each do |r| 158 GRPC.logger.info("In client interceptor: #{r}") 159 end 160 metadata['foo'] = 'bar_from_bidi_streamer' 161 yield 162 end 163end 164