protobuffy 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.travis.yml +12 -0
  4. data/.yardopts +5 -0
  5. data/CHANGES.md +261 -0
  6. data/CONTRIBUTING.md +16 -0
  7. data/Gemfile +3 -0
  8. data/LICENSE.txt +14 -0
  9. data/README.md +58 -0
  10. data/Rakefile +61 -0
  11. data/bin/protoc-gen-ruby +17 -0
  12. data/bin/rpc_server +4 -0
  13. data/examples/bin/reverse-client-http +4 -0
  14. data/examples/bin/reverse-client-socket +4 -0
  15. data/examples/bin/reverse-client-zmq +4 -0
  16. data/examples/config.ru +6 -0
  17. data/examples/definitions/example/reverse.proto +12 -0
  18. data/examples/lib/example/reverse-client.rb +23 -0
  19. data/examples/lib/example/reverse-service.rb +9 -0
  20. data/examples/lib/example/reverse.pb.rb +36 -0
  21. data/lib/protobuf.rb +106 -0
  22. data/lib/protobuf/cli.rb +249 -0
  23. data/lib/protobuf/code_generator.rb +41 -0
  24. data/lib/protobuf/decoder.rb +74 -0
  25. data/lib/protobuf/deprecator.rb +42 -0
  26. data/lib/protobuf/descriptors.rb +3 -0
  27. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +52 -0
  28. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +249 -0
  29. data/lib/protobuf/encoder.rb +62 -0
  30. data/lib/protobuf/enum.rb +319 -0
  31. data/lib/protobuf/exceptions.rb +9 -0
  32. data/lib/protobuf/field.rb +74 -0
  33. data/lib/protobuf/field/base_field.rb +280 -0
  34. data/lib/protobuf/field/bool_field.rb +53 -0
  35. data/lib/protobuf/field/bytes_field.rb +81 -0
  36. data/lib/protobuf/field/double_field.rb +26 -0
  37. data/lib/protobuf/field/enum_field.rb +57 -0
  38. data/lib/protobuf/field/field_array.rb +86 -0
  39. data/lib/protobuf/field/fixed32_field.rb +25 -0
  40. data/lib/protobuf/field/fixed64_field.rb +29 -0
  41. data/lib/protobuf/field/float_field.rb +38 -0
  42. data/lib/protobuf/field/int32_field.rb +22 -0
  43. data/lib/protobuf/field/int64_field.rb +22 -0
  44. data/lib/protobuf/field/integer_field.rb +24 -0
  45. data/lib/protobuf/field/message_field.rb +66 -0
  46. data/lib/protobuf/field/sfixed32_field.rb +28 -0
  47. data/lib/protobuf/field/sfixed64_field.rb +29 -0
  48. data/lib/protobuf/field/signed_integer_field.rb +30 -0
  49. data/lib/protobuf/field/sint32_field.rb +22 -0
  50. data/lib/protobuf/field/sint64_field.rb +22 -0
  51. data/lib/protobuf/field/string_field.rb +35 -0
  52. data/lib/protobuf/field/uint32_field.rb +22 -0
  53. data/lib/protobuf/field/uint64_field.rb +22 -0
  54. data/lib/protobuf/field/varint_field.rb +68 -0
  55. data/lib/protobuf/generators/base.rb +71 -0
  56. data/lib/protobuf/generators/enum_generator.rb +42 -0
  57. data/lib/protobuf/generators/extension_generator.rb +28 -0
  58. data/lib/protobuf/generators/field_generator.rb +132 -0
  59. data/lib/protobuf/generators/file_generator.rb +140 -0
  60. data/lib/protobuf/generators/group_generator.rb +113 -0
  61. data/lib/protobuf/generators/message_generator.rb +99 -0
  62. data/lib/protobuf/generators/printable.rb +161 -0
  63. data/lib/protobuf/generators/service_generator.rb +27 -0
  64. data/lib/protobuf/http.rb +20 -0
  65. data/lib/protobuf/lifecycle.rb +46 -0
  66. data/lib/protobuf/logger.rb +86 -0
  67. data/lib/protobuf/message.rb +182 -0
  68. data/lib/protobuf/message/fields.rb +122 -0
  69. data/lib/protobuf/message/serialization.rb +84 -0
  70. data/lib/protobuf/optionable.rb +23 -0
  71. data/lib/protobuf/rpc/buffer.rb +79 -0
  72. data/lib/protobuf/rpc/client.rb +168 -0
  73. data/lib/protobuf/rpc/connector.rb +21 -0
  74. data/lib/protobuf/rpc/connectors/base.rb +54 -0
  75. data/lib/protobuf/rpc/connectors/common.rb +172 -0
  76. data/lib/protobuf/rpc/connectors/http.rb +90 -0
  77. data/lib/protobuf/rpc/connectors/socket.rb +73 -0
  78. data/lib/protobuf/rpc/connectors/zmq.rb +205 -0
  79. data/lib/protobuf/rpc/dynamic_discovery.pb.rb +47 -0
  80. data/lib/protobuf/rpc/env.rb +58 -0
  81. data/lib/protobuf/rpc/error.rb +28 -0
  82. data/lib/protobuf/rpc/error/client_error.rb +31 -0
  83. data/lib/protobuf/rpc/error/server_error.rb +43 -0
  84. data/lib/protobuf/rpc/middleware.rb +25 -0
  85. data/lib/protobuf/rpc/middleware/exception_handler.rb +36 -0
  86. data/lib/protobuf/rpc/middleware/logger.rb +91 -0
  87. data/lib/protobuf/rpc/middleware/request_decoder.rb +83 -0
  88. data/lib/protobuf/rpc/middleware/response_encoder.rb +88 -0
  89. data/lib/protobuf/rpc/middleware/runner.rb +18 -0
  90. data/lib/protobuf/rpc/rpc.pb.rb +53 -0
  91. data/lib/protobuf/rpc/server.rb +39 -0
  92. data/lib/protobuf/rpc/servers/http/server.rb +101 -0
  93. data/lib/protobuf/rpc/servers/http_runner.rb +34 -0
  94. data/lib/protobuf/rpc/servers/socket/server.rb +113 -0
  95. data/lib/protobuf/rpc/servers/socket/worker.rb +56 -0
  96. data/lib/protobuf/rpc/servers/socket_runner.rb +34 -0
  97. data/lib/protobuf/rpc/servers/zmq/broker.rb +155 -0
  98. data/lib/protobuf/rpc/servers/zmq/server.rb +313 -0
  99. data/lib/protobuf/rpc/servers/zmq/util.rb +47 -0
  100. data/lib/protobuf/rpc/servers/zmq/worker.rb +105 -0
  101. data/lib/protobuf/rpc/servers/zmq_runner.rb +51 -0
  102. data/lib/protobuf/rpc/service.rb +179 -0
  103. data/lib/protobuf/rpc/service_directory.rb +245 -0
  104. data/lib/protobuf/rpc/service_dispatcher.rb +46 -0
  105. data/lib/protobuf/rpc/service_filters.rb +273 -0
  106. data/lib/protobuf/rpc/stat.rb +148 -0
  107. data/lib/protobuf/socket.rb +22 -0
  108. data/lib/protobuf/tasks.rb +1 -0
  109. data/lib/protobuf/tasks/compile.rake +61 -0
  110. data/lib/protobuf/version.rb +3 -0
  111. data/lib/protobuf/wire_type.rb +10 -0
  112. data/lib/protobuf/zmq.rb +21 -0
  113. data/proto/dynamic_discovery.proto +44 -0
  114. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  115. data/proto/google/protobuf/descriptor.proto +620 -0
  116. data/proto/rpc.proto +62 -0
  117. data/protobuffy.gemspec +37 -0
  118. data/spec/benchmark/tasks.rb +113 -0
  119. data/spec/bin/protoc-gen-ruby_spec.rb +18 -0
  120. data/spec/data/data.bin +3 -0
  121. data/spec/data/types.bin +0 -0
  122. data/spec/encoding/all_types_spec.rb +91 -0
  123. data/spec/encoding/extreme_values_spec.rb +0 -0
  124. data/spec/functional/socket_server_spec.rb +59 -0
  125. data/spec/functional/zmq_server_spec.rb +103 -0
  126. data/spec/lib/protobuf/cli_spec.rb +267 -0
  127. data/spec/lib/protobuf/code_generator_spec.rb +60 -0
  128. data/spec/lib/protobuf/enum_spec.rb +239 -0
  129. data/spec/lib/protobuf/field/int32_field_spec.rb +7 -0
  130. data/spec/lib/protobuf/field/string_field_spec.rb +46 -0
  131. data/spec/lib/protobuf/field_spec.rb +194 -0
  132. data/spec/lib/protobuf/generators/base_spec.rb +87 -0
  133. data/spec/lib/protobuf/generators/enum_generator_spec.rb +68 -0
  134. data/spec/lib/protobuf/generators/extension_generator_spec.rb +43 -0
  135. data/spec/lib/protobuf/generators/field_generator_spec.rb +99 -0
  136. data/spec/lib/protobuf/generators/file_generator_spec.rb +29 -0
  137. data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
  138. data/spec/lib/protobuf/generators/service_generator_spec.rb +43 -0
  139. data/spec/lib/protobuf/lifecycle_spec.rb +89 -0
  140. data/spec/lib/protobuf/logger_spec.rb +136 -0
  141. data/spec/lib/protobuf/message_spec.rb +368 -0
  142. data/spec/lib/protobuf/optionable_spec.rb +46 -0
  143. data/spec/lib/protobuf/rpc/client_spec.rb +66 -0
  144. data/spec/lib/protobuf/rpc/connector_spec.rb +26 -0
  145. data/spec/lib/protobuf/rpc/connectors/base_spec.rb +50 -0
  146. data/spec/lib/protobuf/rpc/connectors/common_spec.rb +170 -0
  147. data/spec/lib/protobuf/rpc/connectors/connector_spec.rb +13 -0
  148. data/spec/lib/protobuf/rpc/connectors/http_spec.rb +61 -0
  149. data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +24 -0
  150. data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +129 -0
  151. data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +62 -0
  152. data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +49 -0
  153. data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +115 -0
  154. data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +75 -0
  155. data/spec/lib/protobuf/rpc/servers/http/server_spec.rb +104 -0
  156. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +38 -0
  157. data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +41 -0
  158. data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +55 -0
  159. data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +35 -0
  160. data/spec/lib/protobuf/rpc/service_directory_spec.rb +295 -0
  161. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +52 -0
  162. data/spec/lib/protobuf/rpc/service_filters_spec.rb +484 -0
  163. data/spec/lib/protobuf/rpc/service_spec.rb +161 -0
  164. data/spec/lib/protobuf/rpc/stat_spec.rb +151 -0
  165. data/spec/lib/protobuf_spec.rb +78 -0
  166. data/spec/spec_helper.rb +57 -0
  167. data/spec/support/all.rb +7 -0
  168. data/spec/support/packed_field.rb +22 -0
  169. data/spec/support/server.rb +94 -0
  170. data/spec/support/test/all_types.data.bin +0 -0
  171. data/spec/support/test/all_types.data.txt +119 -0
  172. data/spec/support/test/defaults.pb.rb +25 -0
  173. data/spec/support/test/defaults.proto +9 -0
  174. data/spec/support/test/enum.pb.rb +59 -0
  175. data/spec/support/test/enum.proto +34 -0
  176. data/spec/support/test/extended.pb.rb +22 -0
  177. data/spec/support/test/extended.proto +10 -0
  178. data/spec/support/test/extreme_values.data.bin +0 -0
  179. data/spec/support/test/google_unittest.pb.rb +543 -0
  180. data/spec/support/test/google_unittest.proto +713 -0
  181. data/spec/support/test/google_unittest_import.pb.rb +37 -0
  182. data/spec/support/test/google_unittest_import.proto +64 -0
  183. data/spec/support/test/google_unittest_import_public.pb.rb +8 -0
  184. data/spec/support/test/google_unittest_import_public.proto +38 -0
  185. data/spec/support/test/multi_field_extensions.pb.rb +56 -0
  186. data/spec/support/test/multi_field_extensions.proto +33 -0
  187. data/spec/support/test/resource.pb.rb +117 -0
  188. data/spec/support/test/resource.proto +94 -0
  189. data/spec/support/test/resource_service.rb +26 -0
  190. data/spec/support/test_app_file.rb +2 -0
  191. data/spec/support/tolerance_matcher.rb +40 -0
  192. metadata +367 -0
data/proto/rpc.proto ADDED
@@ -0,0 +1,62 @@
1
+ // Copyright (c) 2009 Shardul Deo
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ // THE SOFTWARE.
20
+
21
+ // Authors: Shardul Deo, BJ Neilsen
22
+ //
23
+ // Protobufs needed for socket rpcs.
24
+
25
+ package protobuf.socketrpc;
26
+
27
+ message Request
28
+ {
29
+ required string service_name = 1; // Fully- qualified Service class name
30
+ required string method_name = 2; // Service method to invoke
31
+ optional bytes request_proto = 3; // Serialized request bytes
32
+ optional string caller = 4; // Calling hostname or address
33
+ }
34
+
35
+ message Response
36
+ {
37
+ optional bytes response_proto = 1; // Serialized response
38
+ optional string error = 2; // Error message, if any
39
+ optional bool callback = 3 [default = false]; // Was callback invoked (not sure what this is for)
40
+ optional ErrorReason error_reason = 4; // Error Reason
41
+ }
42
+
43
+ // Possible error reasons
44
+ // The server-side errors are returned in the response from the server.
45
+ // The client-side errors are returned by the client-side code when it doesn't
46
+ // have a response from the server.
47
+ enum ErrorReason
48
+ {
49
+ // Server-side errors
50
+ BAD_REQUEST_DATA = 0; // Server received bad request data
51
+ BAD_REQUEST_PROTO = 1; // Server received bad request proto
52
+ SERVICE_NOT_FOUND = 2; // Service not found on server
53
+ METHOD_NOT_FOUND = 3; // Method not found on server
54
+ RPC_ERROR = 4; // Rpc threw exception on server
55
+ RPC_FAILED = 5; // Rpc failed on server
56
+
57
+ // Client-side errors (these are returned by the client-side code)
58
+ INVALID_REQUEST_PROTO = 6; // Rpc was called with invalid request proto
59
+ BAD_RESPONSE_PROTO = 7; // Server returned a bad response proto
60
+ UNKNOWN_HOST = 8; // Could not find supplied host
61
+ IO_ERROR = 9; // I/O error while communicating with server
62
+ }
@@ -0,0 +1,37 @@
1
+ # encoding: UTF-8
2
+ $:.push ::File.expand_path("../lib", __FILE__)
3
+ require "protobuf/version"
4
+
5
+ ::Gem::Specification.new do |s|
6
+ s.name = 'protobuffy'
7
+ s.version = ::Protobuf::VERSION
8
+ s.date = ::Time.now.strftime('%Y-%m-%d')
9
+ s.license = 'WTFPL'
10
+
11
+ s.authors = ['BJ Neilsen', 'Brandon Dewitt', 'Devin Christensen', 'Adam Hutchison', 'R. Tyler Croy',]
12
+ s.email = ['bj.neilsen+protobuf@gmail.com', 'brandonsdewitt+protobuf@gmail.com', 'quixoten@gmail.com', 'liveh2o@gmail.com', 'tyler@monkeypox.org']
13
+ s.homepage = 'https://github.com/lookout/protobuffy'
14
+ s.summary = "Google Protocol Buffers serialization and RPC implementation for Ruby."
15
+ s.description = s.summary
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'activesupport', '>= 3.2'
23
+ s.add_dependency 'middleware'
24
+ s.add_dependency 'multi_json'
25
+ s.add_dependency 'thor'
26
+
27
+ s.add_development_dependency 'rack'
28
+ s.add_development_dependency 'faraday'
29
+ s.add_development_dependency 'ffi-rzmq'
30
+ s.add_development_dependency 'pry-nav'
31
+ s.add_development_dependency 'rake'
32
+ s.add_development_dependency 'rspec', '2.99.0'
33
+ s.add_development_dependency 'simplecov'
34
+ s.add_development_dependency 'yard'
35
+ s.add_development_dependency 'timecop'
36
+ s.add_development_dependency 'perftools.rb'
37
+ end
@@ -0,0 +1,113 @@
1
+ require 'benchmark'
2
+ require 'protobuf/socket'
3
+ require 'support/all'
4
+ require 'support/test/resource_service'
5
+
6
+ begin
7
+ require 'perftools'
8
+ rescue LoadError
9
+ $stderr.puts 'perftools must be uncommented in the gemspec before you can run this benchmark task'
10
+ exit(1)
11
+ end
12
+
13
+ # Including a way to turn on debug logger for spec runs
14
+ if ENV["DEBUG"]
15
+ puts 'debugging'
16
+ debug_log = File.expand_path('../debug_bench.log', File.dirname(__FILE__) )
17
+ Protobuf::Logger.configure(:file => debug_log, :level => ::Logger::DEBUG)
18
+ end
19
+
20
+ namespace :benchmark do
21
+
22
+ def benchmark_wrapper(global_bench = nil)
23
+ if global_bench
24
+ yield global_bench
25
+ else
26
+ Benchmark.bm(10) do |bench|
27
+ yield bench
28
+ end
29
+ end
30
+ end
31
+
32
+ def sock_client_sock_server(number_tests, test_length, global_bench = nil)
33
+ load "protobuf/socket.rb"
34
+
35
+ StubServer.new(:server => Protobuf::Rpc::Socket::Server, :port => 9399) do |server|
36
+ client = ::Test::ResourceService.client(:port => 9399)
37
+
38
+ benchmark_wrapper(global_bench) do |bench|
39
+ bench.report("SS / SC") do
40
+ (1..number_tests.to_i).each { client.find(:name => "Test Name" * test_length.to_i, :active => true) }
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ def zmq_client_zmq_server(number_tests, test_length, global_bench = nil)
47
+ load "protobuf/zmq.rb"
48
+ StubServer.new(:port => 9399, :server => Protobuf::Rpc::Zmq::Server) do |server|
49
+ client = ::Test::ResourceService.client(:port => 9399)
50
+
51
+ benchmark_wrapper(global_bench) do |bench|
52
+ bench.report("ZS / ZC") do
53
+ (1..number_tests.to_i).each { client.find(:name => "Test Name" * test_length.to_i, :active => true) }
54
+ end
55
+ end
56
+ server.stop
57
+ end
58
+ end
59
+
60
+ desc "benchmark ZMQ client with ZMQ server"
61
+ task :zmq_client_zmq_server, [:number, :length] do |t, args|
62
+ args.with_defaults(:number => 1000, :length => 100)
63
+ zmq_client_zmq_server(args[:number], args[:length])
64
+ end
65
+
66
+ desc "benchmark ZMQ client with ZMQ server and profile"
67
+ task :zmq_profile, [:number, :length, :profile_output] do |t, args|
68
+ args.with_defaults(:number => 1000, :length => 100, :profile_output => "/tmp/zmq_profiler_#{Time.now.to_i}")
69
+ ::PerfTools::CpuProfiler.start(args[:profile_output]) do
70
+ zmq_client_zmq_server(args[:number], args[:length])
71
+ end
72
+
73
+ puts args[:profile_output]
74
+ end
75
+
76
+ desc "benchmark Protobuf Message #new"
77
+ task :profile_protobuf_new, [:number, :profile_output] do |t, args|
78
+ args.with_defaults(:number => 1000, :profile_output => "/tmp/profiler_new_#{Time.now.to_i}")
79
+ create_params = { :name => "The name that we set", :date_created => Time.now.to_i, :status => 2 }
80
+ ::PerfTools::CpuProfiler.start(args[:profile_output]) do
81
+ args[:number].to_i.times { Test::Resource.new(create_params) }
82
+ end
83
+
84
+ puts args[:profile_output]
85
+ end
86
+
87
+ desc "benchmark Protobuf Message #serialize"
88
+ task :profile_protobuf_serialize, [:number, :profile_output] do |t, args|
89
+ args.with_defaults(:number => 1000, :profile_output => "/tmp/profiler_new_#{Time.now.to_i}")
90
+ create_params = { :name => "The name that we set", :date_created => Time.now.to_i, :status => 2 }
91
+ ::PerfTools::CpuProfiler.start(args[:profile_output]) do
92
+ args[:number].to_i.times { Test::Resource.new(create_params).serialize }
93
+ end
94
+
95
+ puts args[:profile_output]
96
+ end
97
+
98
+ desc "benchmark Socket client with Socket server"
99
+ task :sock_client_sock_server, [:number, :length] do |t, args|
100
+ args.with_defaults(:number => 1000, :length => 100)
101
+ sock_client_sock_server(args[:number], args[:length])
102
+ end
103
+
104
+ desc "benchmark server performance"
105
+ task :servers, [:number, :length] do |t, args|
106
+ args.with_defaults(:number => 1000, :length => 100)
107
+
108
+ Benchmark.bm(10) do |bench|
109
+ zmq_client_zmq_server(args[:number], args[:length], bench)
110
+ sock_client_sock_server(args[:number], args[:length], bench)
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ require 'protobuf/code_generator'
4
+
5
+ describe 'protoc-gen-ruby' do
6
+ let(:binpath) { ::File.expand_path('../../../bin/protoc-gen-ruby', __FILE__) }
7
+ let(:request_bytes) { ::Google::Protobuf::Compiler::CodeGeneratorRequest.new(:file_to_generate => [ "test/foo.proto" ]) }
8
+ let(:expected_file) { ::Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(:name => 'test/foo.pb.rb') }
9
+ let(:expected_response_bytes) { ::Google::Protobuf::Compiler::CodeGeneratorRequest.encode(:files => [ expected_file ]) }
10
+
11
+ it 'reads the serialized request bytes and outputs serialized response bytes' do
12
+ ::IO.popen(binpath, 'w+') do |pipe|
13
+ pipe.write(request_bytes)
14
+ pipe.read(expected_response_bytes.size).should eq expected_response_bytes
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,3 @@
1
+
2
+ John Doe� jdoe@example.com"
3
+ 555-4321
Binary file
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Protobuf do
4
+ it "correctly encodes all types" do
5
+ message = GoogleUnittest::TestAllTypes.new(
6
+ optional_int32: 101,
7
+ optional_int64: 102,
8
+ optional_uint32: 103,
9
+ optional_uint64: 104,
10
+ optional_sint32: 105,
11
+ optional_sint64: 106,
12
+ optional_fixed32: 107,
13
+ optional_fixed64: 108,
14
+ optional_sfixed32: 109,
15
+ optional_sfixed64: 110,
16
+ optional_float: 111,
17
+ optional_double: 112,
18
+ optional_bool: true,
19
+ optional_string: "115",
20
+ optional_bytes: "116",
21
+ optional_nested_message: GoogleUnittest::TestAllTypes::NestedMessage.new(bb: 118),
22
+ optional_foreign_message: GoogleUnittest::ForeignMessage.new(c: 119),
23
+ optional_import_message: GoogleUnittestImport::ImportMessage.new(d: 120),
24
+ optional_nested_enum: GoogleUnittest::TestAllTypes::NestedEnum::BAZ,
25
+ optional_foreign_enum: GoogleUnittest::ForeignEnum::FOREIGN_BAZ,
26
+ optional_import_enum: GoogleUnittestImport::ImportEnum::IMPORT_BAZ,
27
+ optional_string_piece: "124",
28
+ optional_cord: "125",
29
+ optional_public_import_message: GoogleUnittestImport::PublicImportMessage.new(e: 126),
30
+ optional_lazy_message: GoogleUnittest::TestAllTypes::NestedMessage.new(bb: 127),
31
+ repeated_int32: [201, 301],
32
+ repeated_int64: [202, 302],
33
+ repeated_uint32: [203, 303],
34
+ repeated_uint64: [204, 304],
35
+ repeated_sint32: [205, 305],
36
+ repeated_sint64: [206, 306],
37
+ repeated_fixed32: [207, 307],
38
+ repeated_fixed64: [208, 308],
39
+ repeated_sfixed32: [209, 309],
40
+ repeated_sfixed64: [210, 310],
41
+ repeated_float: [211, 311],
42
+ repeated_double: [212, 312],
43
+ repeated_bool: [true, false],
44
+ repeated_string: ["215", "315"],
45
+ repeated_bytes: ["216", "316"],
46
+ repeated_nested_message: [::GoogleUnittest::TestAllTypes::NestedMessage.new(bb: 218),
47
+ ::GoogleUnittest::TestAllTypes::NestedMessage.new(bb: 318)],
48
+ repeated_foreign_message: [::GoogleUnittest::ForeignMessage.new(c: 219),
49
+ ::GoogleUnittest::ForeignMessage.new(c: 319)],
50
+ repeated_import_message: [::GoogleUnittestImport::ImportMessage.new(d: 220),
51
+ ::GoogleUnittestImport::ImportMessage.new(d: 320)],
52
+ repeated_nested_enum: [::GoogleUnittest::TestAllTypes::NestedEnum::BAR,
53
+ ::GoogleUnittest::TestAllTypes::NestedEnum::BAZ],
54
+ repeated_foreign_enum: [::GoogleUnittest::ForeignEnum::FOREIGN_BAR,
55
+ ::GoogleUnittest::ForeignEnum::FOREIGN_BAZ],
56
+ repeated_import_enum: [::GoogleUnittestImport::ImportEnum::IMPORT_BAR,
57
+ ::GoogleUnittestImport::ImportEnum::IMPORT_BAZ],
58
+ repeated_string_piece: ["224", "324"],
59
+ repeated_cord: ["225", "325"],
60
+ repeated_lazy_message: [::GoogleUnittest::TestAllTypes::NestedMessage.new(bb:227),
61
+ ::GoogleUnittest::TestAllTypes::NestedMessage.new(bb:327)],
62
+ default_int32: 401,
63
+ default_int64: 402,
64
+ default_uint32: 403,
65
+ default_uint64: 404,
66
+ default_sint32: 405,
67
+ default_sint64: 406,
68
+ default_fixed32: 407,
69
+ default_fixed64: 408,
70
+ default_sfixed32: 409,
71
+ default_sfixed64: 410,
72
+ default_float: 411,
73
+ default_double: 412,
74
+ default_bool: false,
75
+ default_string: "415",
76
+ default_bytes: "416",
77
+ default_nested_enum: ::GoogleUnittest::TestAllTypes::NestedEnum::FOO,
78
+ default_foreign_enum: ::GoogleUnittest::ForeignEnum::FOREIGN_FOO,
79
+ default_import_enum: ::GoogleUnittestImport::ImportEnum::IMPORT_FOO,
80
+ default_string_piece: "424",
81
+ default_cord: "425",
82
+ )
83
+
84
+ data_file_path = File.expand_path('../../support/test/all_types.data.bin', __FILE__)
85
+ data = File.open(data_file_path, 'rb') do |file|
86
+ file.read
87
+ end
88
+
89
+ data.should eq message.serialize_to_string
90
+ end
91
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+ require 'spec/support/test/resource_service'
3
+
4
+ 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) { |runner| 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 {
20
+ client = ::Test::ResourceService.client
21
+
22
+ client.find(:name => 'Test Name', :active => true) do |c|
23
+ c.on_success do |succ|
24
+ succ.name.should eq("Test Name")
25
+ succ.status.should eq(::Test::StatusType::ENABLED)
26
+ end
27
+
28
+ c.on_failure do |err|
29
+ raise err.inspect
30
+ end
31
+ end
32
+ }.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 { raise "shouldn't pass"}
42
+ c.on_failure {|e| error = e}
43
+ end
44
+ error.message.should =~ /name.*required/
45
+ end
46
+
47
+ it 'calls the on_failure callback when the request type is wrong' do
48
+ error = nil
49
+ request = ::Test::Resource.new(:name => 'Test Name')
50
+ client = ::Test::ResourceService.client
51
+
52
+ client.find(request) do |c|
53
+ c.on_success { raise "shouldn't pass"}
54
+ c.on_failure {|e| error = e}
55
+ end
56
+ error.message.should =~ /expected request.*ResourceFindRequest.*Resource instead/i
57
+ end
58
+ end
59
+
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+
3
+ require 'spec/support/test/resource_service'
4
+ require 'protobuf/rpc/service_directory'
5
+
6
+ describe 'Functional ZMQ Client' do
7
+ before(:all) do
8
+ load "protobuf/zmq.rb"
9
+ @runner = ::Protobuf::Rpc::ZmqRunner.new({ :host => "127.0.0.1",
10
+ :port => 9399,
11
+ :worker_port => 9408,
12
+ :backlog => 100,
13
+ :threshold => 100,
14
+ :threads => 5 })
15
+ @server_thread = Thread.new(@runner) { |runner| runner.run }
16
+ Thread.pass until @runner.running?
17
+ end
18
+
19
+ after(:all) do
20
+ @runner.stop
21
+ @server_thread.join
22
+ end
23
+
24
+ it 'runs fine when required fields are set' do
25
+ expect {
26
+ client = ::Test::ResourceService.client
27
+
28
+ client.find(:name => 'Test Name', :active => true) do |c|
29
+ c.on_success do |succ|
30
+ succ.name.should eq("Test Name")
31
+ succ.status.should eq(::Test::StatusType::ENABLED)
32
+ end
33
+
34
+ c.on_failure do |err|
35
+ raise err.inspect
36
+ end
37
+ end
38
+ }.to_not raise_error
39
+ end
40
+
41
+ it 'runs under heavy load' do
42
+ 10.times do |x|
43
+ 5.times.map do |y|
44
+ Thread.new do
45
+ client = ::Test::ResourceService.client
46
+
47
+ client.find(:name => 'Test Name', :active => true) do |c|
48
+ c.on_success do |succ|
49
+ succ.name.should eq("Test Name")
50
+ succ.status.should eq(::Test::StatusType::ENABLED)
51
+ end
52
+
53
+ c.on_failure do |err|
54
+ raise err.inspect
55
+ end
56
+ end
57
+ end
58
+ end.each(&:join)
59
+ end
60
+ end
61
+
62
+ context 'when a message is malformed' do
63
+ it 'calls the on_failure callback' do
64
+ error = nil
65
+ request = ::Test::ResourceFindRequest.new(:active => true)
66
+ client = ::Test::ResourceService.client
67
+
68
+ client.find(request) do |c|
69
+ c.on_success { raise "shouldn't pass" }
70
+ c.on_failure {|e| error = e }
71
+ end
72
+ error.message.should match(/name.*required/)
73
+ end
74
+ end
75
+
76
+ context 'when the request type is wrong' do
77
+ it 'calls the on_failure callback' do
78
+ error = nil
79
+ request = ::Test::Resource.new(:name => 'Test Name')
80
+ client = ::Test::ResourceService.client
81
+
82
+ client.find(request) do |c|
83
+ c.on_success { raise "shouldn't pass" }
84
+ c.on_failure {|e| error = e}
85
+ end
86
+ error.message.should match(/expected request.*ResourceFindRequest.*Resource instead/i)
87
+ end
88
+ end
89
+
90
+ context 'when the server takes too long to respond' do
91
+ it 'responds with a timeout error' do
92
+ error = nil
93
+ client = ::Test::ResourceService.client(:timeout => 1)
94
+
95
+ client.find_with_sleep(:sleep => 2) do |c|
96
+ c.on_success { raise "shouldn't pass" }
97
+ c.on_failure { |e| error = e }
98
+ end
99
+ error.message.should match(/The server repeatedly failed to respond/)
100
+ end
101
+ end
102
+
103
+ end