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,58 @@
1
+ # encoding: binary
2
+
3
+ require 'spec_helper'
4
+ require 'protobuf/code_generator'
5
+
6
+ RSpec.describe 'code generation' do
7
+ it "generates code for google's unittest.proto" do
8
+ bytes = IO.read(PROTOS_PATH.join('google_unittest.bin'), :mode => 'rb')
9
+
10
+ expected_files =
11
+ ["google_unittest_import_public.pb.rb", "google_unittest_import.pb.rb", "google_unittest.pb.rb"]
12
+
13
+ expected_file_descriptors = expected_files.map do |file_name|
14
+ file_content = File.open(PROTOS_PATH.join(file_name), "r:UTF-8", &:read)
15
+ ::Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(
16
+ :name => "protos/" + file_name, :content => file_content)
17
+ end
18
+
19
+ expected_output =
20
+ ::Google::Protobuf::Compiler::CodeGeneratorResponse.encode(:file => expected_file_descriptors)
21
+
22
+ code_generator = ::Protobuf::CodeGenerator.new(bytes)
23
+ code_generator.eval_unknown_extensions!
24
+ expect(code_generator.response_bytes).to eq(expected_output)
25
+ end
26
+
27
+ it "generates code for map types" do
28
+ input_descriptor = ::Google::Protobuf::FileDescriptorSet.decode(
29
+ IO.read(PROTOS_PATH.join('map-test.bin'), :mode => 'rb'))
30
+ request = ::Google::Protobuf::Compiler::CodeGeneratorRequest.new(:file_to_generate => ['map-test.proto'],
31
+ :proto_file => input_descriptor.file)
32
+
33
+ file_name = "map-test.pb.rb"
34
+ file_content = File.open(PROTOS_PATH.join(file_name), "r:UTF-8", &:read)
35
+ expected_file_output =
36
+ ::Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(
37
+ :name => file_name, :content => file_content)
38
+
39
+ expected_response =
40
+ ::Google::Protobuf::Compiler::CodeGeneratorResponse.encode(:file => [expected_file_output])
41
+
42
+ code_generator = ::Protobuf::CodeGenerator.new(request.encode)
43
+ code_generator.eval_unknown_extensions!
44
+ expect(code_generator.response_bytes).to eq(expected_response)
45
+ end
46
+
47
+ it "generates code (including service stubs) with custom field and method options" do
48
+ expected_unittest_custom_options =
49
+ File.open(PROTOS_PATH.join('google_unittest_custom_options.pb.rb'), "r:UTF-8", &:read)
50
+
51
+ bytes = IO.read(PROTOS_PATH.join('google_unittest_custom_options.bin'), :mode => 'rb')
52
+ code_generator = ::Protobuf::CodeGenerator.new(bytes)
53
+ code_generator.eval_unknown_extensions!
54
+ response = ::Google::Protobuf::Compiler::CodeGeneratorResponse.decode(code_generator.response_bytes)
55
+ expect(response.file.find { |f| f.name == 'protos/google_unittest_custom_options.pb.rb' }.content)
56
+ .to eq(expected_unittest_custom_options)
57
+ end
58
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+ require SUPPORT_PATH.join('resource_service')
3
+
4
+ RSpec.describe 'Functional Socket Client' do
5
+ before(:all) do
6
+ load "protobuf/socket.rb"
7
+ @options = OpenStruct.new(:host => "127.0.0.1", :port => 9399, :backlog => 100, :threshold => 100)
8
+ @runner = ::Protobuf::Rpc::SocketRunner.new(@options)
9
+ @server_thread = Thread.new(@runner, &:run)
10
+ Thread.pass until @runner.running?
11
+ end
12
+
13
+ after(:all) do
14
+ @runner.stop
15
+ @server_thread.join
16
+ end
17
+
18
+ it 'runs fine when required fields are set' do
19
+ expect do
20
+ client = ::Test::ResourceService.client
21
+
22
+ client.find(:name => 'Test Name', :active => true) do |c|
23
+ c.on_success do |succ|
24
+ expect(succ.name).to eq("Test Name")
25
+ expect(succ.status).to eq(::Test::StatusType::ENABLED)
26
+ end
27
+
28
+ c.on_failure do |err|
29
+ fail err.inspect
30
+ end
31
+ end
32
+ end.to_not raise_error
33
+ end
34
+
35
+ it 'calls the on_failure callback when a message is malformed' do
36
+ error = nil
37
+ request = ::Test::ResourceFindRequest.new(:active => true)
38
+ client = ::Test::ResourceService.client
39
+
40
+ client.find(request) do |c|
41
+ c.on_success { fail "shouldn't pass" }
42
+ c.on_failure { |e| error = e }
43
+ end
44
+
45
+ expect(error.message).to match(/Required field.*does not have a value/)
46
+ end
47
+
48
+ it 'calls the on_failure callback when the request type is wrong' do
49
+ error = nil
50
+ request = ::Test::Resource.new(:name => 'Test Name')
51
+ client = ::Test::ResourceService.client
52
+
53
+ client.find(request) do |c|
54
+ c.on_success { fail "shouldn't pass" }
55
+ c.on_failure { |e| error = e }
56
+ end
57
+ expect(error.message).to match(/expected request.*ResourceFindRequest.*Resource instead/i)
58
+ end
59
+ end
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+
3
+ require 'protobuf/rpc/service_directory'
4
+ require SUPPORT_PATH.join('resource_service')
5
+
6
+ RSpec.describe 'Functional ZMQ Client' do
7
+ before(:all) do
8
+ load "protobuf/zmq.rb"
9
+ @runner = ::Protobuf::Rpc::ZmqRunner.new(
10
+ 'host' => '127.0.0.1',
11
+ 'port' => 9399,
12
+ 'worker_port' => 9408,
13
+ 'backlog' => 100,
14
+ 'threshold' => 100,
15
+ 'threads' => 5,
16
+ )
17
+ @server_thread = Thread.new(@runner, &:run)
18
+ Thread.pass until @runner.running?
19
+ end
20
+
21
+ after(:all) do
22
+ @runner.stop
23
+ @server_thread.join
24
+ end
25
+
26
+ it 'runs fine when required fields are set' do
27
+ expect do
28
+ client = ::Test::ResourceService.client
29
+
30
+ client.find(:name => 'Test Name', :active => true) do |c|
31
+ c.on_success do |succ|
32
+ expect(succ.name).to eq("Test Name")
33
+ expect(succ.status).to eq(::Test::StatusType::ENABLED)
34
+ end
35
+
36
+ c.on_failure do |err|
37
+ fail err.inspect
38
+ end
39
+ end
40
+ end.to_not raise_error
41
+ end
42
+
43
+ it 'runs under heavy load' do
44
+ 10.times do
45
+ 5.times.map do
46
+ Thread.new do
47
+ client = ::Test::ResourceService.client
48
+
49
+ client.find(:name => 'Test Name', :active => true) do |c|
50
+ c.on_success do |succ|
51
+ expect(succ.name).to eq("Test Name")
52
+ expect(succ.status).to eq(::Test::StatusType::ENABLED)
53
+ end
54
+
55
+ c.on_failure do |err|
56
+ fail err.inspect
57
+ end
58
+ end
59
+ end
60
+ end.each(&:join)
61
+ end
62
+ end
63
+
64
+ context 'when a message is malformed' do
65
+ it 'calls the on_failure callback' do
66
+ error = nil
67
+ request = ::Test::ResourceFindRequest.new(:active => true)
68
+ client = ::Test::ResourceService.client
69
+
70
+ client.find(request) do |c|
71
+ c.on_success { fail "shouldn't pass" }
72
+ c.on_failure { |e| error = e }
73
+ end
74
+ expect(error.message).to match(/Required field.*does not have a value/)
75
+ end
76
+ end
77
+
78
+ context 'when the request type is wrong' do
79
+ it 'calls the on_failure callback' do
80
+ error = nil
81
+ request = ::Test::Resource.new(:name => 'Test Name')
82
+ client = ::Test::ResourceService.client
83
+
84
+ client.find(request) do |c|
85
+ c.on_success { fail "shouldn't pass" }
86
+ c.on_failure { |e| error = e }
87
+ end
88
+ expect(error.message).to match(/expected request.*ResourceFindRequest.*Resource instead/i)
89
+ end
90
+ end
91
+
92
+ context 'when the server takes too long to respond' do
93
+ it 'responds with a timeout error' do
94
+ error = nil
95
+ client = ::Test::ResourceService.client(:timeout => 1)
96
+
97
+ client.find_with_sleep(:sleep => 2) do |c|
98
+ c.on_success { fail "shouldn't pass" }
99
+ c.on_failure { |e| error = e }
100
+ end
101
+ expect(error.message).to match(/The server repeatedly failed to respond/)
102
+ end
103
+ end
104
+
105
+ end
@@ -0,0 +1,317 @@
1
+ require 'spec_helper'
2
+ require 'protobuf/cli'
3
+
4
+ RSpec.describe ::Protobuf::CLI do
5
+
6
+ let(:app_file) do
7
+ File.expand_path('../../../support/test_app_file.rb', __FILE__)
8
+ end
9
+
10
+ let(:sock_runner) do
11
+ runner = double("SocketRunner", :register_signals => nil)
12
+ allow(runner).to receive(:run).and_return(::ActiveSupport::Notifications.publish("after_server_bind"))
13
+ runner
14
+ end
15
+
16
+ let(:zmq_runner) do
17
+ runner = double("ZmqRunner", :register_signals => nil)
18
+ allow(runner).to receive(:run).and_return(::ActiveSupport::Notifications.publish("after_server_bind"))
19
+ runner
20
+ end
21
+
22
+ around(:each) do |example|
23
+ logger = ::Protobuf::Logging.logger
24
+ example.run
25
+ ::Protobuf::Logging.logger = logger
26
+ end
27
+
28
+ before(:each) do
29
+ allow(::Protobuf::Rpc::SocketRunner).to receive(:new).and_return(sock_runner)
30
+ allow(::Protobuf::Rpc::ZmqRunner).to receive(:new).and_return(zmq_runner)
31
+ end
32
+
33
+ describe '#start' do
34
+ let(:base_args) { ['start', app_file] }
35
+ let(:test_args) { [] }
36
+ let(:args) { base_args + test_args }
37
+
38
+ context 'host option' do
39
+ let(:test_args) { ['--host=123.123.123.123'] }
40
+
41
+ it 'sends the host option to the runner' do
42
+ expect(::Protobuf::Rpc::SocketRunner).to receive(:new) do |options|
43
+ expect(options[:host]).to eq '123.123.123.123'
44
+ end.and_return(sock_runner)
45
+ described_class.start(args)
46
+ end
47
+ end
48
+
49
+ context 'port option' do
50
+ let(:test_args) { ['--port=12345'] }
51
+
52
+ it 'sends the port option to the runner' do
53
+ expect(::Protobuf::Rpc::SocketRunner).to receive(:new) do |options|
54
+ expect(options[:port]).to eq 12345
55
+ end.and_return(sock_runner)
56
+ described_class.start(args)
57
+ end
58
+ end
59
+
60
+ context 'threads option' do
61
+ let(:test_args) { ['--threads=500'] }
62
+
63
+ it 'sends the threads option to the runner' do
64
+ expect(::Protobuf::Rpc::SocketRunner).to receive(:new) do |options|
65
+ expect(options[:threads]).to eq 500
66
+ end.and_return(sock_runner)
67
+ described_class.start(args)
68
+ end
69
+ end
70
+
71
+ context 'backlog option' do
72
+ let(:test_args) { ['--backlog=500'] }
73
+
74
+ it 'sends the backlog option to the runner' do
75
+ expect(::Protobuf::Rpc::SocketRunner).to receive(:new) do |options|
76
+ expect(options[:backlog]).to eq 500
77
+ end.and_return(sock_runner)
78
+ described_class.start(args)
79
+ end
80
+ end
81
+
82
+ context 'threshold option' do
83
+ let(:test_args) { ['--threshold=500'] }
84
+
85
+ it 'sends the backlog option to the runner' do
86
+ expect(::Protobuf::Rpc::SocketRunner).to receive(:new) do |options|
87
+ expect(options[:threshold]).to eq 500
88
+ end.and_return(sock_runner)
89
+ described_class.start(args)
90
+ end
91
+ end
92
+
93
+ context 'log options' do
94
+ let(:test_args) { ['--log=mylog.log', '--level=0'] }
95
+
96
+ it 'sends the log file and level options to the runner' do
97
+ expect(::Protobuf::Logging).to receive(:initialize_logger) do |file, level|
98
+ expect(file).to eq 'mylog.log'
99
+ expect(level).to eq 0
100
+ end
101
+ described_class.start(args)
102
+ end
103
+ end
104
+
105
+ context 'gc options' do
106
+
107
+ context 'when gc options are not present' do
108
+ let(:test_args) { [] }
109
+
110
+ it 'sets both request and serialization pausing to false' do
111
+ described_class.start(args)
112
+ expect(::Protobuf).to_not be_gc_pause_server_request
113
+ end
114
+ end
115
+
116
+ unless defined?(JRUBY_VERSION)
117
+ context 'request pausing' do
118
+ let(:test_args) { ['--gc_pause_request'] }
119
+
120
+ it 'sets the configuration option to GC pause server request' do
121
+ described_class.start(args)
122
+ expect(::Protobuf).to be_gc_pause_server_request
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ context 'deprecation options' do
129
+ context 'when not given' do
130
+ let(:test_args) { [] }
131
+
132
+ context 'when no ENV is present and no command line option' do
133
+ before { ENV.delete("PB_IGNORE_DEPRECATIONS") }
134
+
135
+ it 'sets the deprecation warning flag to true' do
136
+ described_class.start(args)
137
+ expect(::Protobuf.print_deprecation_warnings?).to be true
138
+ end
139
+ end
140
+
141
+ context 'if ENV["PB_IGNORE_DEPRECATIONS"] is present' do
142
+ before { ENV["PB_IGNORE_DEPRECATIONS"] = "1" }
143
+ after { ENV.delete("PB_IGNORE_DEPRECATIONS") }
144
+
145
+ it 'sets the deprecation warning flag to false ' do
146
+ described_class.start(args)
147
+ expect(::Protobuf.print_deprecation_warnings?).to be false
148
+ end
149
+ end
150
+ end
151
+
152
+ context 'when enabled' do
153
+ let(:test_args) { ['--print_deprecation_warnings'] }
154
+
155
+ it 'sets the deprecation warning flag to true' do
156
+ described_class.start(args)
157
+ expect(::Protobuf.print_deprecation_warnings?).to be true
158
+ end
159
+ end
160
+
161
+ context 'when disabled' do
162
+ let(:test_args) { ['--no-print_deprecation_warnings'] }
163
+
164
+ it 'sets the deprecation warning flag to false' do
165
+ described_class.start(args)
166
+ expect(::Protobuf.print_deprecation_warnings?).to be false
167
+ end
168
+ end
169
+ end
170
+
171
+ context 'run modes' do
172
+
173
+ context "extension" do
174
+ let(:runner) { ::Protobuf::Rpc::Servers::SocketRunner }
175
+
176
+ it "loads the runner specified by PB_SERVER_TYPE" do
177
+ ENV['PB_SERVER_TYPE'] = "protobuf/rpc/servers/socket_runner"
178
+ expect(runner).to receive(:new).and_return(sock_runner)
179
+ described_class.start(args)
180
+ ENV.delete('PB_SERVER_TYPE')
181
+ end
182
+
183
+ context "without extension loaded" do
184
+ it "will throw a LoadError when extension is not loaded" do
185
+ ENV['PB_SERVER_TYPE'] = "socket_to_load"
186
+ expect { described_class.start(args) }.to raise_error(LoadError, /socket_to_load/)
187
+ ENV.delete("PB_SERVER_TYPE")
188
+ end
189
+ end
190
+ end
191
+
192
+ context 'socket' do
193
+ let(:test_args) { ['--socket'] }
194
+ let(:runner) { ::Protobuf::Rpc::SocketRunner }
195
+
196
+ before do
197
+ expect(::Protobuf::Rpc::ZmqRunner).not_to receive(:new)
198
+ end
199
+
200
+ it 'is activated by the --socket switch' do
201
+ expect(runner).to receive(:new)
202
+ described_class.start(args)
203
+ end
204
+
205
+ it 'is activated by PB_SERVER_TYPE=Socket ENV variable' do
206
+ ENV['PB_SERVER_TYPE'] = "Socket"
207
+ expect(runner).to receive(:new).and_return(sock_runner)
208
+ described_class.start(args)
209
+ ENV.delete('PB_SERVER_TYPE')
210
+ end
211
+ end
212
+
213
+ context 'zmq workers only' do
214
+ let(:test_args) { ['--workers_only', '--zmq'] }
215
+ let(:runner) { ::Protobuf::Rpc::ZmqRunner }
216
+
217
+ before do
218
+ expect(::Protobuf::Rpc::SocketRunner).not_to receive(:new)
219
+ end
220
+
221
+ it 'is activated by the --workers_only switch' do
222
+ expect(runner).to receive(:new) do |options|
223
+ expect(options[:workers_only]).to be true
224
+ end.and_return(zmq_runner)
225
+
226
+ described_class.start(args)
227
+ end
228
+
229
+ it 'is activated by PB_WORKERS_ONLY=1 ENV variable' do
230
+ ENV['PB_WORKERS_ONLY'] = "1"
231
+ expect(runner).to receive(:new) do |options|
232
+ expect(options[:workers_only]).to be true
233
+ end.and_return(zmq_runner)
234
+
235
+ described_class.start(args)
236
+ ENV.delete('PB_WORKERS_ONLY')
237
+ end
238
+ end
239
+
240
+ context 'zmq worker port' do
241
+ let(:test_args) { ['--worker_port=1234', '--zmq'] }
242
+ let(:runner) { ::Protobuf::Rpc::ZmqRunner }
243
+
244
+ before do
245
+ expect(::Protobuf::Rpc::SocketRunner).not_to receive(:new)
246
+ end
247
+
248
+ it 'is activated by the --worker_port switch' do
249
+ expect(runner).to receive(:new) do |options|
250
+ expect(options[:worker_port]).to eq(1234)
251
+ end.and_return(zmq_runner)
252
+
253
+ described_class.start(args)
254
+ end
255
+ end
256
+
257
+ context 'zmq' do
258
+ let(:test_args) { ['--zmq'] }
259
+ let(:runner) { ::Protobuf::Rpc::ZmqRunner }
260
+
261
+ before do
262
+ expect(::Protobuf::Rpc::SocketRunner).not_to receive(:new)
263
+ end
264
+
265
+ it 'is activated by the --zmq switch' do
266
+ expect(runner).to receive(:new)
267
+ described_class.start(args)
268
+ end
269
+
270
+ it 'is activated by PB_SERVER_TYPE=Zmq ENV variable' do
271
+ ENV['PB_SERVER_TYPE'] = "Zmq"
272
+ expect(runner).to receive(:new)
273
+ described_class.start(args)
274
+ ENV.delete('PB_SERVER_TYPE')
275
+ end
276
+ end
277
+
278
+ context 'after server bind' do
279
+ let(:sock_runner) { double("FakeRunner", :run => nil) }
280
+
281
+ before { allow(sock_runner).to receive(:run).and_yield }
282
+
283
+ it 'publishes when using the lib/protobuf callback' do
284
+ message_after_bind = false
285
+ ::Protobuf.after_server_bind do
286
+ message_after_bind = true
287
+ end
288
+ described_class.start(args)
289
+ expect(message_after_bind).to eq(true)
290
+ end
291
+ end
292
+
293
+ context 'before server bind' do
294
+ it 'publishes a message before the runner runs' do
295
+ message_before_bind = false
296
+ ::ActiveSupport::Notifications.subscribe('before_server_bind') do
297
+ message_before_bind = true
298
+ end
299
+ described_class.start(args)
300
+ expect(message_before_bind).to eq(true)
301
+ end
302
+
303
+ it 'publishes when using the lib/protobuf callback' do
304
+ message_before_bind = false
305
+ ::Protobuf.before_server_bind do
306
+ message_before_bind = true
307
+ end
308
+ described_class.start(args)
309
+ expect(message_before_bind).to eq(true)
310
+ end
311
+ end
312
+
313
+ end
314
+
315
+ end
316
+
317
+ end