• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  def self.marshal(_o)
21    ''
22  end
23
24  def self.unmarshal(_o)
25    EchoMsg.new
26  end
27end
28
29# A test service with an echo implementation.
30class EchoService
31  include GRPC::GenericService
32  rpc :an_rpc, EchoMsg, EchoMsg
33  rpc :a_client_streaming_rpc, stream(EchoMsg), EchoMsg
34  rpc :a_server_streaming_rpc, EchoMsg, stream(EchoMsg)
35  rpc :a_bidi_rpc, stream(EchoMsg), stream(EchoMsg)
36  attr_reader :received_md
37
38  def initialize(**kw)
39    @trailing_metadata = kw
40    @received_md = []
41  end
42
43  def an_rpc(req, call)
44    GRPC.logger.info('echo service received a request')
45    call.output_metadata.update(@trailing_metadata)
46    @received_md << call.metadata unless call.metadata.nil?
47    req
48  end
49
50  def a_client_streaming_rpc(call)
51    # iterate through requests so call can complete
52    call.output_metadata.update(@trailing_metadata)
53    call.each_remote_read.each do |r|
54      GRPC.logger.info(r)
55    end
56    EchoMsg.new
57  end
58
59  def a_server_streaming_rpc(_req, call)
60    call.output_metadata.update(@trailing_metadata)
61    [EchoMsg.new, EchoMsg.new]
62  end
63
64  def a_bidi_rpc(requests, call)
65    call.output_metadata.update(@trailing_metadata)
66    requests.each do |r|
67      GRPC.logger.info(r)
68    end
69    [EchoMsg.new, EchoMsg.new]
70  end
71end
72
73EchoStub = EchoService.rpc_stub_class
74
75# For testing server interceptors
76class TestServerInterceptor < GRPC::ServerInterceptor
77  def request_response(request:, call:, method:)
78    GRPC.logger.info("Received request/response call at method #{method}" \
79      " with request #{request} for call #{call}")
80    call.output_metadata[:interc] = 'from_request_response'
81    GRPC.logger.info("[GRPC::Ok] (#{method.owner.name}.#{method.name})")
82    yield
83  end
84
85  def client_streamer(call:, method:)
86    call.output_metadata[:interc] = 'from_client_streamer'
87    call.each_remote_read.each do |r|
88      GRPC.logger.info("In interceptor: #{r}")
89    end
90    GRPC.logger.info(
91      "Received client streamer call at method #{method} for call #{call}"
92    )
93    yield
94  end
95
96  def server_streamer(request:, call:, method:)
97    GRPC.logger.info("Received server streamer call at method #{method} with request" \
98      " #{request} for call #{call}")
99    call.output_metadata[:interc] = 'from_server_streamer'
100    yield
101  end
102
103  def bidi_streamer(requests:, call:, method:)
104    requests.each do |r|
105      GRPC.logger.info("Bidi request: #{r}")
106    end
107    GRPC.logger.info("Received bidi streamer call at method #{method} with requests" \
108      " #{requests} for call #{call}")
109    call.output_metadata[:interc] = 'from_bidi_streamer'
110    yield
111  end
112end
113
114# For testing client interceptors
115class TestClientInterceptor < GRPC::ClientInterceptor
116  def request_response(request:, call:, method:, metadata: {})
117    GRPC.logger.info("Intercepted request/response call at method #{method}" \
118      " with request #{request} for call #{call}" \
119      " and metadata: #{metadata}")
120    metadata['foo'] = 'bar_from_request_response'
121    yield
122  end
123
124  def client_streamer(requests:, call:, method:, metadata: {})
125    GRPC.logger.info("Received client streamer call at method #{method}" \
126      " with requests #{requests} for call #{call}" \
127      " and metadata: #{metadata}")
128    requests.each do |r|
129      GRPC.logger.info("In client interceptor: #{r}")
130    end
131    metadata['foo'] = 'bar_from_client_streamer'
132    yield
133  end
134
135  def server_streamer(request:, call:, method:, metadata: {})
136    GRPC.logger.info("Received server streamer call at method #{method}" \
137      " with request #{request} for call #{call}" \
138      " and metadata: #{metadata}")
139    metadata['foo'] = 'bar_from_server_streamer'
140    yield
141  end
142
143  def bidi_streamer(requests:, call:, method:, metadata: {})
144    GRPC.logger.info("Received bidi streamer call at method #{method}" \
145      "with requests #{requests} for call #{call}" \
146      " and metadata: #{metadata}")
147    requests.each do |r|
148      GRPC.logger.info("In client interceptor: #{r}")
149    end
150    metadata['foo'] = 'bar_from_bidi_streamer'
151    yield
152  end
153end
154