services.rb 4.3 KB

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