protobuf-cucumber 3.10.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.rubocop.yml +70 -0
  4. data/.rubocop_todo.yml +145 -0
  5. data/.travis.yml +40 -0
  6. data/.yardopts +5 -0
  7. data/CHANGES.md +344 -0
  8. data/CONTRIBUTING.md +16 -0
  9. data/Gemfile +3 -0
  10. data/LICENSE.txt +22 -0
  11. data/README.md +33 -0
  12. data/Rakefile +64 -0
  13. data/bin/protoc-gen-ruby +22 -0
  14. data/bin/rpc_server +5 -0
  15. data/install-protobuf.sh +28 -0
  16. data/lib/protobuf.rb +129 -0
  17. data/lib/protobuf/cli.rb +257 -0
  18. data/lib/protobuf/code_generator.rb +120 -0
  19. data/lib/protobuf/decoder.rb +28 -0
  20. data/lib/protobuf/deprecation.rb +117 -0
  21. data/lib/protobuf/descriptors.rb +3 -0
  22. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +62 -0
  23. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +301 -0
  24. data/lib/protobuf/encoder.rb +11 -0
  25. data/lib/protobuf/enum.rb +365 -0
  26. data/lib/protobuf/exceptions.rb +9 -0
  27. data/lib/protobuf/field.rb +74 -0
  28. data/lib/protobuf/field/base_field.rb +380 -0
  29. data/lib/protobuf/field/base_field_object_definitions.rb +504 -0
  30. data/lib/protobuf/field/bool_field.rb +64 -0
  31. data/lib/protobuf/field/bytes_field.rb +78 -0
  32. data/lib/protobuf/field/double_field.rb +25 -0
  33. data/lib/protobuf/field/enum_field.rb +61 -0
  34. data/lib/protobuf/field/field_array.rb +104 -0
  35. data/lib/protobuf/field/field_hash.rb +122 -0
  36. data/lib/protobuf/field/fixed32_field.rb +25 -0
  37. data/lib/protobuf/field/fixed64_field.rb +28 -0
  38. data/lib/protobuf/field/float_field.rb +43 -0
  39. data/lib/protobuf/field/int32_field.rb +21 -0
  40. data/lib/protobuf/field/int64_field.rb +34 -0
  41. data/lib/protobuf/field/integer_field.rb +23 -0
  42. data/lib/protobuf/field/message_field.rb +51 -0
  43. data/lib/protobuf/field/sfixed32_field.rb +27 -0
  44. data/lib/protobuf/field/sfixed64_field.rb +28 -0
  45. data/lib/protobuf/field/signed_integer_field.rb +29 -0
  46. data/lib/protobuf/field/sint32_field.rb +21 -0
  47. data/lib/protobuf/field/sint64_field.rb +21 -0
  48. data/lib/protobuf/field/string_field.rb +51 -0
  49. data/lib/protobuf/field/uint32_field.rb +21 -0
  50. data/lib/protobuf/field/uint64_field.rb +21 -0
  51. data/lib/protobuf/field/varint_field.rb +77 -0
  52. data/lib/protobuf/generators/base.rb +85 -0
  53. data/lib/protobuf/generators/enum_generator.rb +39 -0
  54. data/lib/protobuf/generators/extension_generator.rb +27 -0
  55. data/lib/protobuf/generators/field_generator.rb +193 -0
  56. data/lib/protobuf/generators/file_generator.rb +262 -0
  57. data/lib/protobuf/generators/group_generator.rb +122 -0
  58. data/lib/protobuf/generators/message_generator.rb +104 -0
  59. data/lib/protobuf/generators/option_generator.rb +17 -0
  60. data/lib/protobuf/generators/printable.rb +160 -0
  61. data/lib/protobuf/generators/service_generator.rb +50 -0
  62. data/lib/protobuf/lifecycle.rb +33 -0
  63. data/lib/protobuf/logging.rb +39 -0
  64. data/lib/protobuf/message.rb +260 -0
  65. data/lib/protobuf/message/fields.rb +233 -0
  66. data/lib/protobuf/message/serialization.rb +85 -0
  67. data/lib/protobuf/optionable.rb +70 -0
  68. data/lib/protobuf/rpc/buffer.rb +78 -0
  69. data/lib/protobuf/rpc/client.rb +140 -0
  70. data/lib/protobuf/rpc/connectors/base.rb +221 -0
  71. data/lib/protobuf/rpc/connectors/ping.rb +89 -0
  72. data/lib/protobuf/rpc/connectors/socket.rb +78 -0
  73. data/lib/protobuf/rpc/connectors/zmq.rb +319 -0
  74. data/lib/protobuf/rpc/dynamic_discovery.pb.rb +50 -0
  75. data/lib/protobuf/rpc/env.rb +60 -0
  76. data/lib/protobuf/rpc/error.rb +28 -0
  77. data/lib/protobuf/rpc/error/client_error.rb +31 -0
  78. data/lib/protobuf/rpc/error/server_error.rb +43 -0
  79. data/lib/protobuf/rpc/middleware.rb +25 -0
  80. data/lib/protobuf/rpc/middleware/exception_handler.rb +40 -0
  81. data/lib/protobuf/rpc/middleware/logger.rb +95 -0
  82. data/lib/protobuf/rpc/middleware/request_decoder.rb +79 -0
  83. data/lib/protobuf/rpc/middleware/response_encoder.rb +83 -0
  84. data/lib/protobuf/rpc/middleware/runner.rb +18 -0
  85. data/lib/protobuf/rpc/rpc.pb.rb +64 -0
  86. data/lib/protobuf/rpc/rpc_method.rb +16 -0
  87. data/lib/protobuf/rpc/server.rb +39 -0
  88. data/lib/protobuf/rpc/servers/socket/server.rb +121 -0
  89. data/lib/protobuf/rpc/servers/socket/worker.rb +56 -0
  90. data/lib/protobuf/rpc/servers/socket_runner.rb +46 -0
  91. data/lib/protobuf/rpc/servers/zmq/broker.rb +194 -0
  92. data/lib/protobuf/rpc/servers/zmq/server.rb +321 -0
  93. data/lib/protobuf/rpc/servers/zmq/util.rb +48 -0
  94. data/lib/protobuf/rpc/servers/zmq/worker.rb +105 -0
  95. data/lib/protobuf/rpc/servers/zmq_runner.rb +70 -0
  96. data/lib/protobuf/rpc/service.rb +172 -0
  97. data/lib/protobuf/rpc/service_directory.rb +261 -0
  98. data/lib/protobuf/rpc/service_dispatcher.rb +45 -0
  99. data/lib/protobuf/rpc/service_filters.rb +250 -0
  100. data/lib/protobuf/rpc/stat.rb +119 -0
  101. data/lib/protobuf/socket.rb +21 -0
  102. data/lib/protobuf/tasks.rb +1 -0
  103. data/lib/protobuf/tasks/compile.rake +80 -0
  104. data/lib/protobuf/varint.rb +20 -0
  105. data/lib/protobuf/varint_pure.rb +31 -0
  106. data/lib/protobuf/version.rb +3 -0
  107. data/lib/protobuf/wire_type.rb +10 -0
  108. data/lib/protobuf/zmq.rb +21 -0
  109. data/profile.html +5103 -0
  110. data/proto/dynamic_discovery.proto +44 -0
  111. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  112. data/proto/google/protobuf/descriptor.proto +779 -0
  113. data/proto/rpc.proto +69 -0
  114. data/protobuf-cucumber.gemspec +57 -0
  115. data/spec/benchmark/tasks.rb +143 -0
  116. data/spec/bin/protoc-gen-ruby_spec.rb +23 -0
  117. data/spec/encoding/all_types_spec.rb +103 -0
  118. data/spec/encoding/extreme_values_spec.rb +0 -0
  119. data/spec/functional/class_inheritance_spec.rb +52 -0
  120. data/spec/functional/code_generator_spec.rb +58 -0
  121. data/spec/functional/socket_server_spec.rb +59 -0
  122. data/spec/functional/zmq_server_spec.rb +105 -0
  123. data/spec/lib/protobuf/cli_spec.rb +317 -0
  124. data/spec/lib/protobuf/code_generator_spec.rb +87 -0
  125. data/spec/lib/protobuf/enum_spec.rb +307 -0
  126. data/spec/lib/protobuf/field/bool_field_spec.rb +91 -0
  127. data/spec/lib/protobuf/field/double_field_spec.rb +9 -0
  128. data/spec/lib/protobuf/field/enum_field_spec.rb +44 -0
  129. data/spec/lib/protobuf/field/field_array_spec.rb +105 -0
  130. data/spec/lib/protobuf/field/field_hash_spec.rb +168 -0
  131. data/spec/lib/protobuf/field/fixed32_field_spec.rb +7 -0
  132. data/spec/lib/protobuf/field/fixed64_field_spec.rb +7 -0
  133. data/spec/lib/protobuf/field/float_field_spec.rb +90 -0
  134. data/spec/lib/protobuf/field/int32_field_spec.rb +120 -0
  135. data/spec/lib/protobuf/field/int64_field_spec.rb +7 -0
  136. data/spec/lib/protobuf/field/message_field_spec.rb +132 -0
  137. data/spec/lib/protobuf/field/sfixed32_field_spec.rb +9 -0
  138. data/spec/lib/protobuf/field/sfixed64_field_spec.rb +9 -0
  139. data/spec/lib/protobuf/field/sint32_field_spec.rb +9 -0
  140. data/spec/lib/protobuf/field/sint64_field_spec.rb +9 -0
  141. data/spec/lib/protobuf/field/string_field_spec.rb +79 -0
  142. data/spec/lib/protobuf/field/uint32_field_spec.rb +7 -0
  143. data/spec/lib/protobuf/field/uint64_field_spec.rb +7 -0
  144. data/spec/lib/protobuf/field_spec.rb +192 -0
  145. data/spec/lib/protobuf/generators/base_spec.rb +154 -0
  146. data/spec/lib/protobuf/generators/enum_generator_spec.rb +82 -0
  147. data/spec/lib/protobuf/generators/extension_generator_spec.rb +42 -0
  148. data/spec/lib/protobuf/generators/field_generator_spec.rb +197 -0
  149. data/spec/lib/protobuf/generators/file_generator_spec.rb +119 -0
  150. data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
  151. data/spec/lib/protobuf/generators/service_generator_spec.rb +99 -0
  152. data/spec/lib/protobuf/lifecycle_spec.rb +94 -0
  153. data/spec/lib/protobuf/message_spec.rb +944 -0
  154. data/spec/lib/protobuf/optionable_spec.rb +265 -0
  155. data/spec/lib/protobuf/rpc/client_spec.rb +66 -0
  156. data/spec/lib/protobuf/rpc/connectors/base_spec.rb +226 -0
  157. data/spec/lib/protobuf/rpc/connectors/ping_spec.rb +69 -0
  158. data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +34 -0
  159. data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +110 -0
  160. data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +62 -0
  161. data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +49 -0
  162. data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +115 -0
  163. data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +91 -0
  164. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +38 -0
  165. data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +43 -0
  166. data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +55 -0
  167. data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +35 -0
  168. data/spec/lib/protobuf/rpc/service_directory_spec.rb +293 -0
  169. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +35 -0
  170. data/spec/lib/protobuf/rpc/service_filters_spec.rb +517 -0
  171. data/spec/lib/protobuf/rpc/service_spec.rb +162 -0
  172. data/spec/lib/protobuf/rpc/stat_spec.rb +101 -0
  173. data/spec/lib/protobuf/varint_spec.rb +29 -0
  174. data/spec/lib/protobuf_spec.rb +105 -0
  175. data/spec/spec_helper.rb +42 -0
  176. data/spec/support/all.rb +6 -0
  177. data/spec/support/packed_field.rb +23 -0
  178. data/spec/support/protos/all_types.data.bin +0 -0
  179. data/spec/support/protos/all_types.data.txt +119 -0
  180. data/spec/support/protos/enum.pb.rb +63 -0
  181. data/spec/support/protos/enum.proto +37 -0
  182. data/spec/support/protos/extreme_values.data.bin +0 -0
  183. data/spec/support/protos/google_unittest.bin +0 -0
  184. data/spec/support/protos/google_unittest.pb.rb +798 -0
  185. data/spec/support/protos/google_unittest.proto +884 -0
  186. data/spec/support/protos/google_unittest_custom_options.bin +0 -0
  187. data/spec/support/protos/google_unittest_custom_options.pb.rb +361 -0
  188. data/spec/support/protos/google_unittest_custom_options.proto +424 -0
  189. data/spec/support/protos/google_unittest_import.pb.rb +55 -0
  190. data/spec/support/protos/google_unittest_import.proto +73 -0
  191. data/spec/support/protos/google_unittest_import_public.pb.rb +31 -0
  192. data/spec/support/protos/google_unittest_import_public.proto +41 -0
  193. data/spec/support/protos/map-test.bin +157 -0
  194. data/spec/support/protos/map-test.pb.rb +85 -0
  195. data/spec/support/protos/map-test.proto +68 -0
  196. data/spec/support/protos/multi_field_extensions.pb.rb +59 -0
  197. data/spec/support/protos/multi_field_extensions.proto +35 -0
  198. data/spec/support/protos/resource.pb.rb +172 -0
  199. data/spec/support/protos/resource.proto +137 -0
  200. data/spec/support/resource_service.rb +23 -0
  201. data/spec/support/server.rb +65 -0
  202. data/spec/support/test_app_file.rb +2 -0
  203. data/varint_prof.rb +82 -0
  204. metadata +579 -0
@@ -0,0 +1,265 @@
1
+ require 'spec_helper'
2
+ require 'protobuf/optionable'
3
+ require 'protobuf/field/message_field'
4
+ require PROTOS_PATH.join('resource.pb')
5
+
6
+ RSpec.describe 'Optionable' do
7
+ describe '.{get,get!}_option' do
8
+ before do
9
+ stub_const("OptionableGetOptionTest", Class.new(::Protobuf::Message) do
10
+ set_option :deprecated, true
11
+ set_option :".package.message_field", :field => 33
12
+
13
+ optional :int32, :field, 1
14
+ end)
15
+ end
16
+
17
+ it '.get_option retrieves the option as a symbol' do
18
+ expect(OptionableGetOptionTest.get_option(:deprecated)).to be(true)
19
+ end
20
+
21
+ it '.get_option returns the default value for unset options' do
22
+ expect(OptionableGetOptionTest.get_option(:message_set_wire_format)).to be(false)
23
+ end
24
+
25
+ it '.get_option retrieves the option as a string' do
26
+ expect(OptionableGetOptionTest.get_option('deprecated')).to be(true)
27
+ end
28
+
29
+ it '.get_option errors if the option does not exist' do
30
+ expect { OptionableGetOptionTest.get_option(:baz) }.to raise_error(ArgumentError)
31
+ end
32
+
33
+ it '.get_option errors if the option is not accessed by its fully qualified name' do
34
+ message_field = ::Protobuf::Field::MessageField.new(
35
+ OptionableGetOptionTest, :optional, OptionableGetOptionTest, '.package.message_field', 2, '.message_field', {})
36
+ allow(::Google::Protobuf::MessageOptions).to receive(:get_field).and_return(message_field)
37
+ expect { OptionableGetOptionTest.get_option(message_field.name) }.to raise_error(ArgumentError)
38
+ end
39
+
40
+ it '.get_option can return an option representing a message' do
41
+ message_field = ::Protobuf::Field::MessageField.new(
42
+ OptionableGetOptionTest, :optional, OptionableGetOptionTest, '.package.message_field', 2, 'message_field', {})
43
+ allow(::Google::Protobuf::MessageOptions).to receive(:get_field).and_return(message_field)
44
+ expect(OptionableGetOptionTest.get_option(message_field.fully_qualified_name)).to eq(OptionableGetOptionTest.new(:field => 33))
45
+ end
46
+
47
+ it '.get_option! retrieves explicitly an set option' do
48
+ expect(OptionableGetOptionTest.get_option!(:deprecated)).to be(true)
49
+ end
50
+
51
+ it '.get_option! returns nil for unset options' do
52
+ expect(OptionableGetOptionTest.get_option!(:message_set_wire_format)).to be(nil)
53
+ end
54
+
55
+ it '.get_option! errors if the option does not exist' do
56
+ expect { OptionableGetOptionTest.get_option(:baz) }.to raise_error(ArgumentError)
57
+ end
58
+
59
+ it '#get_option retrieves the option as a symbol' do
60
+ expect(OptionableGetOptionTest.new.get_option(:deprecated)).to be(true)
61
+ end
62
+
63
+ it '#get_option returns the default value for unset options' do
64
+ expect(OptionableGetOptionTest.new.get_option(:message_set_wire_format)).to be(false)
65
+ end
66
+
67
+ it '#get_option retrieves the option as a string' do
68
+ expect(OptionableGetOptionTest.new.get_option('deprecated')).to be(true)
69
+ end
70
+
71
+ it '#get_option errors if the option is not accessed by its fully qualified name' do
72
+ message_field = ::Protobuf::Field::MessageField.new(
73
+ OptionableGetOptionTest, :optional, OptionableGetOptionTest, '.package.message_field', 2, 'message_field', {})
74
+ allow(::Google::Protobuf::MessageOptions).to receive(:get_field).and_return(message_field)
75
+ expect { OptionableGetOptionTest.new.get_option(message_field.name) }.to raise_error(ArgumentError)
76
+ end
77
+
78
+ it '#get_option can return an option representing a message' do
79
+ message_field = ::Protobuf::Field::MessageField.new(
80
+ OptionableGetOptionTest, :optional, OptionableGetOptionTest, '.package.message_field', 2, 'message_field', {})
81
+ allow(::Google::Protobuf::MessageOptions).to receive(:get_field).and_return(message_field)
82
+ expect(OptionableGetOptionTest.new.get_option(message_field.fully_qualified_name)).to eq(OptionableGetOptionTest.new(:field => 33))
83
+ end
84
+
85
+ it '#get_option errors if the option does not exist' do
86
+ expect { OptionableGetOptionTest.new.get_option(:baz) }.to raise_error(ArgumentError)
87
+ end
88
+
89
+ it '#get_option! retrieves explicitly an set option' do
90
+ expect(OptionableGetOptionTest.new.get_option!(:deprecated)).to be(true)
91
+ end
92
+
93
+ it '#get_option! returns nil for unset options' do
94
+ expect(OptionableGetOptionTest.new.get_option!(:message_set_wire_format)).to be(nil)
95
+ end
96
+
97
+ it '#get_option! errors if the option does not exist' do
98
+ expect { OptionableGetOptionTest.new.get_option(:baz) }.to raise_error(ArgumentError)
99
+ end
100
+ end
101
+
102
+ describe '.inject' do
103
+ let(:klass) { Class.new }
104
+
105
+ it 'adds klass.{set,get}_option' do
106
+ expect { klass.get_option(:deprecated) }.to raise_error(NoMethodError)
107
+ expect { klass.__send__(:set_option, :deprecated, true) }.to raise_error(NoMethodError)
108
+ ::Protobuf::Optionable.inject(klass) { ::Google::Protobuf::MessageOptions }
109
+ expect(klass.get_option(:deprecated)).to eq(false)
110
+ expect { klass.set_option(:deprecated, true) }.to raise_error(NoMethodError)
111
+ klass.__send__(:set_option, :deprecated, true)
112
+ expect(klass.get_option(:deprecated)).to eq(true)
113
+ end
114
+
115
+ it 'adds klass#get_option' do
116
+ expect { klass.new.get_option(:deprecated) }.to raise_error(NoMethodError)
117
+ ::Protobuf::Optionable.inject(klass) { ::Google::Protobuf::MessageOptions }
118
+ expect(klass.new.get_option(:deprecated)).to eq(false)
119
+ end
120
+
121
+ it 'adds klass.optionable_descriptor_class' do
122
+ expect { klass.optionable_descriptor_class }.to raise_error(NoMethodError)
123
+ ::Protobuf::Optionable.inject(klass) { ::Google::Protobuf::MessageOptions }
124
+ expect(klass.optionable_descriptor_class).to eq(::Google::Protobuf::MessageOptions)
125
+ end
126
+
127
+ it 'does not add klass.optionable_descriptor_class twice' do
128
+ expect(klass).to receive(:define_singleton_method).once
129
+ ::Protobuf::Optionable.inject(klass) { ::Google::Protobuf::MessageOptions }
130
+ klass.instance_eval do
131
+ def optionable_descriptor_class
132
+ ::Google::Protobuf::MessageOptions
133
+ end
134
+ end
135
+ ::Protobuf::Optionable.inject(klass) { ::Google::Protobuf::MessageOptions }
136
+ end
137
+
138
+ it 'throws error when klass.optionable_descriptor_class defined twice with different args' do
139
+ ::Protobuf::Optionable.inject(klass) { ::Google::Protobuf::MessageOptions }
140
+ expect { ::Protobuf::Optionable.inject(klass) { ::Google::Protobuf::FileOptions } }
141
+ .to raise_error('A class is being defined with two different descriptor classes, something is very wrong')
142
+ end
143
+
144
+ context 'extend_class = false' do
145
+ let(:object) { klass.new }
146
+ it 'adds object.{get,set}_option' do
147
+ expect { object.get_option(:deprecated) }.to raise_error(NoMethodError)
148
+ expect { object.__send__(:set_option, :deprecated, true) }.to raise_error(NoMethodError)
149
+ ::Protobuf::Optionable.inject(klass, false) { ::Google::Protobuf::MessageOptions }
150
+ expect(object.get_option(:deprecated)).to eq(false)
151
+ expect { object.set_option(:deprecated, true) }.to raise_error(NoMethodError)
152
+ object.__send__(:set_option, :deprecated, true)
153
+ expect(object.get_option(:deprecated)).to eq(true)
154
+ end
155
+
156
+ it 'does not add klass.{get,set}_option' do
157
+ expect { object.get_option(:deprecated) }.to raise_error(NoMethodError)
158
+ ::Protobuf::Optionable.inject(klass, false) { ::Google::Protobuf::MessageOptions }
159
+ expect { klass.get_option(:deprecated) }.to raise_error(NoMethodError)
160
+ expect { klass.__send__(:set_option, :deprecated) }.to raise_error(NoMethodError)
161
+ end
162
+
163
+ it 'creates an instance method optionable_descriptor_class' do
164
+ expect { object.optionable_descriptor_class }.to raise_error(NoMethodError)
165
+ ::Protobuf::Optionable.inject(klass, false) { ::Google::Protobuf::MessageOptions }
166
+ expect(object.optionable_descriptor_class).to eq(::Google::Protobuf::MessageOptions)
167
+ end
168
+ end
169
+ end
170
+
171
+ describe 'getting options from generated code' do
172
+ context 'file options' do
173
+ it 'gets base options' do
174
+ expect(::Test.get_option!(:cc_generic_services)).to eq(true)
175
+ end
176
+
177
+ it 'gets unset options' do
178
+ expect(::Test.get_option!(:java_multiple_files)).to eq(nil)
179
+ expect(::Test.get_option(:java_multiple_files)).to eq(false)
180
+ end
181
+
182
+ it 'gets custom options' do
183
+ expect(::Test.get_option!(:".test.file_option")).to eq(9876543210)
184
+ end
185
+ end
186
+
187
+ context 'field options' do
188
+ subject { ::Test::Resource.fields[0] }
189
+
190
+ it 'gets base options' do
191
+ expect(subject.get_option!(:ctype))
192
+ .to eq(::Google::Protobuf::FieldOptions::CType::CORD)
193
+ end
194
+
195
+ it 'gets unset options' do
196
+ expect(subject.get_option!(:lazy)).to eq(nil)
197
+ expect(subject.get_option(:lazy)).to eq(false)
198
+ end
199
+
200
+ it 'gets custom options' do
201
+ expect(subject.get_option!(:".test.field_option")).to eq(8765432109)
202
+ end
203
+ end
204
+
205
+ context 'enum options' do
206
+ subject { ::Test::StatusType }
207
+
208
+ it 'gets base options' do
209
+ expect(subject.get_option!(:allow_alias)).to eq(true)
210
+ end
211
+
212
+ it 'gets unset options' do
213
+ expect(subject.get_option!(:deprecated)).to eq(nil)
214
+ expect(subject.get_option(:deprecated)).to eq(false)
215
+ end
216
+
217
+ it 'gets custom options' do
218
+ expect(subject.get_option!(:".test.enum_option")).to eq(-789)
219
+ end
220
+ end
221
+
222
+ context 'message options' do
223
+ subject { ::Test::Resource }
224
+
225
+ it 'gets base options' do
226
+ expect(subject.get_option!(:map_entry)).to eq(false)
227
+ end
228
+
229
+ it 'gets unset options' do
230
+ expect(subject.get_option!(:deprecated)).to eq(nil)
231
+ expect(subject.get_option(:deprecated)).to eq(false)
232
+ end
233
+
234
+ it 'gets custom options' do
235
+ expect(subject.get_option!(:".test.message_option")).to eq(-56)
236
+ end
237
+ end
238
+
239
+ context 'service options' do
240
+ subject { ::Test::ResourceService }
241
+
242
+ it 'gets unset options' do
243
+ expect(subject.get_option!(:deprecated)).to eq(nil)
244
+ expect(subject.get_option(:deprecated)).to eq(false)
245
+ end
246
+
247
+ it 'gets custom options' do
248
+ expect(subject.get_option!(:".test.service_option")).to eq(-9876543210)
249
+ end
250
+ end
251
+
252
+ context 'method options' do
253
+ subject { ::Test::ResourceService.rpcs[:find] }
254
+
255
+ it 'gets unset options' do
256
+ expect(subject.get_option!(:deprecated)).to eq(nil)
257
+ expect(subject.get_option(:deprecated)).to eq(false)
258
+ end
259
+
260
+ it 'gets custom options' do
261
+ expect(subject.get_option!(:".test.method_option")).to eq(2)
262
+ end
263
+ end
264
+ end
265
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+ require SUPPORT_PATH.join('resource_service')
3
+
4
+ RSpec.describe Protobuf::Rpc::Client do
5
+ before(:each) do
6
+ load 'protobuf/socket.rb'
7
+ end
8
+
9
+ context 'when creating a client from a service' do
10
+ before { reset_service_location(Test::ResourceService) }
11
+
12
+ it 'should be able to get a client through the Service#client helper method' do
13
+ expect(::Test::ResourceService.client(:port => 9191)).to eq(Protobuf::Rpc::Client.new(:service => Test::ResourceService, :port => 9191))
14
+ end
15
+
16
+ it "should be able to override a service location's host and port" do
17
+ Test::ResourceService.located_at 'somewheregreat.com:12345'
18
+ clean_client = Test::ResourceService.client
19
+ expect(clean_client.options[:host]).to eq('somewheregreat.com')
20
+ expect(clean_client.options[:port]).to eq(12345)
21
+
22
+ updated_client = Test::ResourceService.client(:host => 'amazing.com', :port => 54321)
23
+ expect(updated_client.options[:host]).to eq('amazing.com')
24
+ expect(updated_client.options[:port]).to eq(54321)
25
+ end
26
+
27
+ it 'should be able to define which service to create itself for' do
28
+ client = Protobuf::Rpc::Client.new :service => Test::ResourceService
29
+ expect(client.options[:service]).to eq(Test::ResourceService)
30
+ end
31
+
32
+ it 'should have a hard default for host and port on a service that has not been configured' do
33
+ client = Test::ResourceService.client
34
+ expect(client.options[:host]).to eq(Protobuf::Rpc::Service::DEFAULT_HOST)
35
+ expect(client.options[:port]).to eq(Protobuf::Rpc::Service::DEFAULT_PORT)
36
+ end
37
+
38
+ end
39
+
40
+ context 'when calling methods on a service client' do
41
+
42
+ # NOTE: we are assuming the service methods are accurately
43
+ # defined inside spec/proto/test_service.rb,
44
+ # namely the :find method
45
+
46
+ it 'should respond to defined service methods' do
47
+ client = Test::ResourceService.client
48
+ expect(client).to receive(:send_request).and_return(nil)
49
+ expect { client.find(nil) }.to_not raise_error
50
+ end
51
+ end
52
+
53
+ context 'when receiving request objects' do
54
+
55
+ it 'should be able to create the correct request object if passed a hash' do
56
+ client = Test::ResourceService.client
57
+ expect(client).to receive(:send_request)
58
+ client.find(:name => 'Test Name', :active => false)
59
+ expect(client.options[:request]).to be_a(Test::ResourceFindRequest)
60
+ expect(client.options[:request].name).to eq('Test Name')
61
+ expect(client.options[:request].active).to eq(false)
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,226 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Protobuf::Rpc::Connectors::Base do
4
+
5
+ let(:options) do
6
+ { :timeout => 60 }
7
+ end
8
+
9
+ subject { Protobuf::Rpc::Connectors::Base.new(options) }
10
+
11
+ context "API" do
12
+ specify { expect(subject.respond_to?(:any_callbacks?)).to be true }
13
+ specify { expect(subject.respond_to?(:request_caller)).to be true }
14
+ specify { expect(subject.respond_to?(:data_callback)).to be true }
15
+ specify { expect(subject.respond_to?(:error)).to be true }
16
+ specify { expect(subject.respond_to?(:failure)).to be true }
17
+ specify { expect(subject.respond_to?(:complete)).to be true }
18
+ specify { expect(subject.respond_to?(:parse_response)).to be true }
19
+ specify { expect(subject.respond_to?(:verify_options!)).to be true }
20
+ specify { expect(subject.respond_to?(:verify_callbacks)).to be true }
21
+ end
22
+
23
+ describe "#parse_response" do
24
+ let(:options) { { :response_type => Test::Resource, :port => 55589, :host => '127.3.4.5' } }
25
+ it "updates stats#server from the response" do
26
+ allow(subject).to receive(:close_connection)
27
+ subject.instance_variable_set(:@response_data, ::Protobuf::Socketrpc::Response.new(:server => "serverless").encode)
28
+ subject.initialize_stats
29
+ subject.parse_response
30
+ expect(subject.stats.server).to eq("serverless")
31
+ end
32
+ it "does not override stats#server when response.server is missing" do
33
+ allow(subject).to receive(:close_connection)
34
+ subject.instance_variable_set(:@response_data, ::Protobuf::Socketrpc::Response.new.encode)
35
+ subject.initialize_stats
36
+ subject.parse_response
37
+ expect(subject.stats.server).to eq("127.3.4.5:55589")
38
+ end
39
+ it "does not override stats#server when response.server is nil" do
40
+ allow(subject).to receive(:close_connection)
41
+ subject.instance_variable_set(:@response_data, ::Protobuf::Socketrpc::Response.new(:server => nil).encode)
42
+ subject.initialize_stats
43
+ subject.parse_response
44
+ expect(subject.stats.server).to eq("127.3.4.5:55589")
45
+ end
46
+ end
47
+
48
+ describe "#any_callbacks?" do
49
+ [:@complete_cb, :@success_cb, :@failure_cb].each do |cb|
50
+ it "returns true if #{cb} is provided" do
51
+ subject.instance_variable_set(cb, "something")
52
+ expect(subject.any_callbacks?).to be true
53
+ end
54
+ end
55
+
56
+ it "returns false when all callbacks are not provided" do
57
+ subject.instance_variable_set(:@complete_cb, nil)
58
+ subject.instance_variable_set(:@success_cb, nil)
59
+ subject.instance_variable_set(:@failure_cb, nil)
60
+
61
+ expect(subject.any_callbacks?).to be false
62
+ end
63
+ end
64
+
65
+ describe "#data_callback" do
66
+ it "changes state to use the data callback" do
67
+ subject.data_callback("data")
68
+ expect(subject.instance_variable_get(:@used_data_callback)).to be true
69
+ end
70
+
71
+ it "sets the data var when using the data_callback" do
72
+ subject.data_callback("data")
73
+ expect(subject.instance_variable_get(:@data)).to eq("data")
74
+ end
75
+ end
76
+
77
+ describe "#send_request" do
78
+ it "raising an error when 'send_request' is not overridden" do
79
+ expect { subject.send_request }.to raise_error(RuntimeError, /inherit a Connector/)
80
+ end
81
+
82
+ it "does not raise error when 'send_request' is overridden" do
83
+ new_sub = Class.new(subject.class) { def send_request; end }.new(options)
84
+ expect { new_sub.send_request }.to_not raise_error
85
+ end
86
+ end
87
+
88
+ describe '.new' do
89
+ it 'assigns passed options and initializes success/failure callbacks' do
90
+ expect(subject.options).to eq(Protobuf::Rpc::Connectors::DEFAULT_OPTIONS.merge(options))
91
+ expect(subject.success_cb).to be_nil
92
+ expect(subject.failure_cb).to be_nil
93
+ end
94
+ end
95
+
96
+ describe '#success_cb' do
97
+ it 'allows setting the success callback and calling it' do
98
+ expect(subject.success_cb).to be_nil
99
+ cb = proc { |res| fail res }
100
+ subject.success_cb = cb
101
+ expect(subject.success_cb).to eq(cb)
102
+ expect { subject.success_cb.call('an error from cb') }.to raise_error 'an error from cb'
103
+ end
104
+ end
105
+
106
+ describe '#failure_cb' do
107
+ it 'allows setting the failure callback and calling it' do
108
+ expect(subject.failure_cb).to be_nil
109
+ cb = proc { |res| fail res }
110
+ subject.failure_cb = cb
111
+ expect(subject.failure_cb).to eq(cb)
112
+ expect { subject.failure_cb.call('an error from cb') }.to raise_error 'an error from cb'
113
+ end
114
+ end
115
+
116
+ describe '#request_bytes' do
117
+ let(:service) { Test::ResourceService }
118
+ let(:method) { :find }
119
+ let(:request) { '' }
120
+ let(:client_host) { 'myhost.myservice.com' }
121
+ let(:options) do
122
+ {
123
+ :service => service,
124
+ :method => method,
125
+ :request => request,
126
+ :client_host => client_host,
127
+ :timeout => 60,
128
+ }
129
+ end
130
+
131
+ let(:expected) do
132
+ ::Protobuf::Socketrpc::Request.new(
133
+ :service_name => service.name,
134
+ :method_name => 'find',
135
+ :request_proto => '',
136
+ :caller => client_host,
137
+ )
138
+ end
139
+
140
+ before { allow(subject).to receive(:validate_request_type!).and_return(true) }
141
+ before { expect(subject).not_to receive(:failure) }
142
+
143
+ specify { expect(subject.request_bytes).to eq expected.encode }
144
+ end
145
+
146
+ describe '#request_caller' do
147
+ specify { expect(subject.request_caller).to eq ::Protobuf.client_host }
148
+
149
+ context 'when "client_host" option is given to initializer' do
150
+ let(:hostname) { 'myhost.myserver.com' }
151
+ let(:options) { { :client_host => hostname, :timeout => 60 } }
152
+
153
+ specify { expect(subject.request_caller).to_not eq ::Protobuf.client_host }
154
+ specify { expect(subject.request_caller).to eq hostname }
155
+ end
156
+ end
157
+
158
+ describe "#verify_callbacks" do
159
+ it "sets @failure_cb to #data_callback when no callbacks are defined" do
160
+ subject.verify_callbacks
161
+ expect(subject.instance_variable_get(:@failure_cb)).to eq(subject.method(:data_callback))
162
+ end
163
+
164
+ it "sets @success_cb to #data_callback when no callbacks are defined" do
165
+ subject.verify_callbacks
166
+ expect(subject.instance_variable_get(:@success_cb)).to eq(subject.method(:data_callback))
167
+ end
168
+
169
+ it "doesn't set @failure_cb when already defined" do
170
+ set_cb = -> { true }
171
+ subject.instance_variable_set(:@failure_cb, set_cb)
172
+ subject.verify_callbacks
173
+ expect(subject.instance_variable_get(:@failure_cb)).to eq(set_cb)
174
+ expect(subject.instance_variable_get(:@failure_cb)).to_not eq(subject.method(:data_callback))
175
+ end
176
+
177
+ it "doesn't set @success_cb when already defined" do
178
+ set_cb = -> { true }
179
+ subject.instance_variable_set(:@success_cb, set_cb)
180
+ subject.verify_callbacks
181
+ expect(subject.instance_variable_get(:@success_cb)).to eq(set_cb)
182
+ expect(subject.instance_variable_get(:@success_cb)).to_not eq(subject.method(:data_callback))
183
+ end
184
+
185
+ end
186
+
187
+ shared_examples "a ConnectorDisposition" do |meth, cb, *args|
188
+
189
+ it "calls #complete before exit" do
190
+ subject.stats = ::Protobuf::Rpc::Stat.new(:stop => true)
191
+
192
+ expect(subject).to receive(:complete)
193
+ subject.method(meth).call(*args)
194
+ end
195
+
196
+ it "calls the #{cb} callback when provided" do
197
+ stats = ::Protobuf::Rpc::Stat.new
198
+ allow(stats).to receive(:stop).and_return(true)
199
+ subject.stats = stats
200
+ some_cb = double("Object")
201
+
202
+ subject.instance_variable_set("@#{cb}", some_cb)
203
+ expect(some_cb).to receive(:call).and_return(true)
204
+ subject.method(meth).call(*args)
205
+ end
206
+
207
+ it "calls the complete callback when provided" do
208
+ stats = ::Protobuf::Rpc::Stat.new
209
+ allow(stats).to receive(:stop).and_return(true)
210
+ subject.stats = stats
211
+ comp_cb = double("Object")
212
+
213
+ subject.instance_variable_set(:@complete_cb, comp_cb)
214
+ expect(comp_cb).to receive(:call).and_return(true)
215
+ subject.method(meth).call(*args)
216
+ end
217
+
218
+ end
219
+
220
+ it_behaves_like("a ConnectorDisposition", :failure, "failure_cb", :RPC_ERROR, "message")
221
+ it_behaves_like("a ConnectorDisposition", :failure, "complete_cb", :RPC_ERROR, "message")
222
+ it_behaves_like("a ConnectorDisposition", :succeed, "complete_cb", "response")
223
+ it_behaves_like("a ConnectorDisposition", :succeed, "success_cb", "response")
224
+ it_behaves_like("a ConnectorDisposition", :complete, "complete_cb")
225
+
226
+ end