protobuffy 3.6.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +67 -0
- data/.rubocop_todo.yml +145 -0
- data/.travis.yml +25 -5
- data/CHANGES.md +55 -0
- data/CONTRIBUTING.md +1 -1
- data/LICENSE.txt +17 -9
- data/README.md +13 -12
- data/Rakefile +15 -11
- data/bin/protoc-gen-ruby +8 -3
- data/bin/rpc_server +1 -0
- data/examples/lib/example/reverse-client.rb +2 -2
- data/install-protobuf.sh +28 -0
- data/lib/protobuf.rb +57 -53
- data/lib/protobuf/cli.rb +94 -74
- data/lib/protobuf/code_generator.rb +60 -9
- data/lib/protobuf/decoder.rb +19 -65
- data/lib/protobuf/deprecation.rb +117 -0
- data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +11 -1
- data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +55 -3
- data/lib/protobuf/encoder.rb +13 -53
- data/lib/protobuf/enum.rb +58 -63
- data/lib/protobuf/field.rb +4 -4
- data/lib/protobuf/field/base_field.rb +101 -173
- data/lib/protobuf/field/bool_field.rb +17 -11
- data/lib/protobuf/field/bytes_field.rb +21 -35
- data/lib/protobuf/field/double_field.rb +0 -1
- data/lib/protobuf/field/enum_field.rb +23 -22
- data/lib/protobuf/field/field_array.rb +5 -4
- data/lib/protobuf/field/fixed32_field.rb +1 -1
- data/lib/protobuf/field/fixed64_field.rb +0 -1
- data/lib/protobuf/field/float_field.rb +4 -1
- data/lib/protobuf/field/int32_field.rb +0 -1
- data/lib/protobuf/field/int64_field.rb +0 -1
- data/lib/protobuf/field/integer_field.rb +0 -1
- data/lib/protobuf/field/message_field.rb +13 -28
- data/lib/protobuf/field/sfixed32_field.rb +0 -1
- data/lib/protobuf/field/sfixed64_field.rb +0 -1
- data/lib/protobuf/field/signed_integer_field.rb +0 -1
- data/lib/protobuf/field/sint32_field.rb +0 -1
- data/lib/protobuf/field/sint64_field.rb +0 -1
- data/lib/protobuf/field/string_field.rb +2 -4
- data/lib/protobuf/field/uint32_field.rb +0 -1
- data/lib/protobuf/field/uint64_field.rb +0 -1
- data/lib/protobuf/field/varint_field.rb +30 -13
- data/lib/protobuf/generators/base.rb +30 -16
- data/lib/protobuf/generators/enum_generator.rb +6 -9
- data/lib/protobuf/generators/extension_generator.rb +1 -2
- data/lib/protobuf/generators/field_generator.rb +25 -13
- data/lib/protobuf/generators/file_generator.rb +157 -35
- data/lib/protobuf/generators/group_generator.rb +22 -17
- data/lib/protobuf/generators/message_generator.rb +13 -14
- data/lib/protobuf/generators/option_generator.rb +17 -0
- data/lib/protobuf/generators/printable.rb +12 -13
- data/lib/protobuf/generators/service_generator.rb +2 -3
- data/lib/protobuf/http.rb +2 -2
- data/lib/protobuf/lifecycle.rb +20 -33
- data/lib/protobuf/logging.rb +39 -0
- data/lib/protobuf/message.rb +114 -47
- data/lib/protobuf/message/fields.rb +170 -88
- data/lib/protobuf/message/serialization.rb +19 -18
- data/lib/protobuf/optionable.rb +53 -6
- data/lib/protobuf/rpc/buffer.rb +18 -19
- data/lib/protobuf/rpc/client.rb +22 -50
- data/lib/protobuf/rpc/connectors/base.rb +177 -12
- data/lib/protobuf/rpc/connectors/http.rb +14 -9
- data/lib/protobuf/rpc/connectors/ping.rb +89 -0
- data/lib/protobuf/rpc/connectors/socket.rb +13 -8
- data/lib/protobuf/rpc/connectors/zmq.rb +178 -73
- data/lib/protobuf/rpc/dynamic_discovery.pb.rb +4 -1
- data/lib/protobuf/rpc/env.rb +12 -12
- data/lib/protobuf/rpc/error.rb +3 -3
- data/lib/protobuf/rpc/error/client_error.rb +4 -4
- data/lib/protobuf/rpc/error/server_error.rb +9 -9
- data/lib/protobuf/rpc/middleware/exception_handler.rb +6 -2
- data/lib/protobuf/rpc/middleware/logger.rb +8 -4
- data/lib/protobuf/rpc/middleware/request_decoder.rb +17 -21
- data/lib/protobuf/rpc/middleware/response_encoder.rb +22 -27
- data/lib/protobuf/rpc/middleware/statsd.rb +3 -3
- data/lib/protobuf/rpc/rpc.pb.rb +4 -1
- data/lib/protobuf/rpc/server.rb +1 -1
- data/lib/protobuf/rpc/servers/http/server.rb +19 -17
- data/lib/protobuf/rpc/servers/socket/server.rb +78 -70
- data/lib/protobuf/rpc/servers/socket/worker.rb +4 -4
- data/lib/protobuf/rpc/servers/socket_runner.rb +27 -15
- data/lib/protobuf/rpc/servers/zmq/broker.rb +70 -31
- data/lib/protobuf/rpc/servers/zmq/server.rb +55 -47
- data/lib/protobuf/rpc/servers/zmq/util.rb +14 -13
- data/lib/protobuf/rpc/servers/zmq/worker.rb +16 -16
- data/lib/protobuf/rpc/servers/zmq_runner.rb +26 -7
- data/lib/protobuf/rpc/service.rb +21 -27
- data/lib/protobuf/rpc/service_directory.rb +43 -27
- data/lib/protobuf/rpc/service_dispatcher.rb +9 -10
- data/lib/protobuf/rpc/service_filters.rb +32 -55
- data/lib/protobuf/rpc/stat.rb +4 -8
- data/lib/protobuf/socket.rb +1 -2
- data/lib/protobuf/tasks/compile.rake +3 -4
- data/lib/protobuf/varint.rb +9 -0
- data/lib/protobuf/varint_pure.rb +13 -0
- data/lib/protobuf/version.rb +1 -1
- data/lib/protobuf/zmq.rb +2 -2
- data/proto/google/protobuf/descriptor.proto +190 -31
- data/protobuffy.gemspec +30 -17
- data/spec/benchmark/tasks.rb +27 -19
- data/spec/bin/protoc-gen-ruby_spec.rb +11 -6
- data/spec/encoding/all_types_spec.rb +96 -84
- data/spec/encoding/extreme_values_spec.rb +0 -0
- data/spec/functional/class_inheritance_spec.rb +52 -0
- data/spec/functional/code_generator_spec.rb +38 -0
- data/spec/functional/socket_server_spec.rb +15 -15
- data/spec/functional/zmq_server_spec.rb +29 -27
- data/spec/lib/protobuf/cli_spec.rb +82 -67
- data/spec/lib/protobuf/code_generator_spec.rb +37 -10
- data/spec/lib/protobuf/enum_spec.rb +77 -46
- data/spec/lib/protobuf/field/bool_field_spec.rb +91 -0
- data/spec/lib/protobuf/field/double_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/enum_field_spec.rb +26 -0
- data/spec/lib/protobuf/field/field_array_spec.rb +69 -0
- data/spec/lib/protobuf/field/fixed32_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/fixed64_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/float_field_spec.rb +90 -0
- data/spec/lib/protobuf/field/int32_field_spec.rb +114 -1
- data/spec/lib/protobuf/field/int64_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/message_field_spec.rb +132 -0
- data/spec/lib/protobuf/field/sfixed32_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/sfixed64_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/sint32_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/sint64_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/string_field_spec.rb +44 -11
- data/spec/lib/protobuf/field/uint32_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/uint64_field_spec.rb +7 -0
- data/spec/lib/protobuf/field_spec.rb +4 -6
- data/spec/lib/protobuf/generators/base_spec.rb +80 -13
- data/spec/lib/protobuf/generators/enum_generator_spec.rb +35 -21
- data/spec/lib/protobuf/generators/extension_generator_spec.rb +12 -13
- data/spec/lib/protobuf/generators/field_generator_spec.rb +73 -21
- data/spec/lib/protobuf/generators/file_generator_spec.rb +89 -6
- data/spec/lib/protobuf/generators/service_generator_spec.rb +25 -13
- data/spec/lib/protobuf/lifecycle_spec.rb +25 -20
- data/spec/lib/protobuf/message_spec.rb +578 -79
- data/spec/lib/protobuf/optionable_spec.rb +202 -26
- data/spec/lib/protobuf/rpc/client_spec.rb +16 -16
- data/spec/lib/protobuf/rpc/connectors/base_spec.rb +167 -13
- data/spec/lib/protobuf/rpc/connectors/connector_spec.rb +4 -5
- data/spec/lib/protobuf/rpc/connectors/http_spec.rb +13 -11
- data/spec/lib/protobuf/rpc/connectors/ping_spec.rb +69 -0
- data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +6 -7
- data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +35 -52
- data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +10 -10
- data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +11 -11
- data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +23 -23
- data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +11 -11
- data/spec/lib/protobuf/rpc/middleware/statsd_spec.rb +6 -6
- data/spec/lib/protobuf/rpc/servers/http/server_spec.rb +47 -44
- data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +6 -6
- data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +12 -10
- data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +11 -11
- data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +7 -7
- data/spec/lib/protobuf/rpc/service_directory_spec.rb +47 -49
- data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +8 -25
- data/spec/lib/protobuf/rpc/service_filters_spec.rb +102 -69
- data/spec/lib/protobuf/rpc/service_spec.rb +37 -36
- data/spec/lib/protobuf/rpc/stat_spec.rb +7 -9
- data/spec/lib/protobuf/varint_spec.rb +29 -0
- data/spec/lib/protobuf_spec.rb +55 -28
- data/spec/spec_helper.rb +12 -27
- data/spec/support/all.rb +0 -1
- data/spec/support/packed_field.rb +4 -3
- data/spec/support/{test → protos}/all_types.data.bin +0 -0
- data/spec/support/{test → protos}/all_types.data.txt +0 -0
- data/spec/support/{test → protos}/enum.pb.rb +8 -4
- data/spec/support/{test → protos}/enum.proto +4 -1
- data/spec/support/{test → protos}/extreme_values.data.bin +0 -0
- data/spec/support/protos/google_unittest.bin +0 -0
- data/spec/support/protos/google_unittest.pb.rb +798 -0
- data/spec/support/{test → protos}/google_unittest.proto +237 -66
- data/spec/support/protos/google_unittest_custom_options.bin +0 -0
- data/spec/support/protos/google_unittest_custom_options.pb.rb +268 -0
- data/spec/support/protos/google_unittest_custom_options.proto +424 -0
- data/spec/support/protos/google_unittest_import.pb.rb +55 -0
- data/spec/support/{test → protos}/google_unittest_import.proto +19 -10
- data/spec/support/protos/google_unittest_import_public.pb.rb +31 -0
- data/spec/support/{test → protos}/google_unittest_import_public.proto +8 -5
- data/spec/support/{test → protos}/multi_field_extensions.pb.rb +5 -2
- data/spec/support/{test → protos}/multi_field_extensions.proto +2 -0
- data/spec/support/{test → protos}/resource.pb.rb +47 -11
- data/spec/support/{test → protos}/resource.proto +24 -1
- data/spec/support/resource_service.rb +23 -0
- data/spec/support/server.rb +32 -61
- metadata +119 -59
- data/lib/protobuf/deprecator.rb +0 -42
- data/lib/protobuf/logger.rb +0 -93
- data/lib/protobuf/rpc/connector.rb +0 -21
- data/lib/protobuf/rpc/connectors/common.rb +0 -172
- data/spec/data/data.bin +0 -3
- data/spec/data/types.bin +0 -0
- data/spec/lib/protobuf/logger_spec.rb +0 -145
- data/spec/lib/protobuf/rpc/connector_spec.rb +0 -26
- data/spec/lib/protobuf/rpc/connectors/common_spec.rb +0 -170
- data/spec/support/test/defaults.pb.rb +0 -25
- data/spec/support/test/defaults.proto +0 -9
- data/spec/support/test/extended.pb.rb +0 -22
- data/spec/support/test/extended.proto +0 -10
- data/spec/support/test/google_unittest.pb.rb +0 -543
- data/spec/support/test/google_unittest_import.pb.rb +0 -37
- data/spec/support/test/google_unittest_import_public.pb.rb +0 -8
- data/spec/support/test/resource_service.rb +0 -26
- data/spec/support/tolerance_matcher.rb +0 -40
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'protobuf'
|
2
|
-
|
3
|
-
module Protobuf
|
4
|
-
module Rpc
|
5
|
-
class Connector
|
6
|
-
|
7
|
-
# Returns a connector class for the pre-defined connector_type.
|
8
|
-
def self.connector_for_client
|
9
|
-
case ::Protobuf.connector_type
|
10
|
-
when :zmq then
|
11
|
-
::Protobuf::Rpc::Connectors::Zmq
|
12
|
-
when :http then
|
13
|
-
::Protobuf::Rpc::Connectors::Http
|
14
|
-
else
|
15
|
-
::Protobuf::Rpc::Connectors::Socket
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,172 +0,0 @@
|
|
1
|
-
require 'protobuf'
|
2
|
-
|
3
|
-
module Protobuf
|
4
|
-
module Rpc
|
5
|
-
module Connectors
|
6
|
-
module Common
|
7
|
-
|
8
|
-
attr_reader :error
|
9
|
-
|
10
|
-
def any_callbacks?
|
11
|
-
return [@complete_cb, @failure_cb, @success_cb].inject(false) do |reduction, cb|
|
12
|
-
reduction = (reduction || !cb.nil?)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def request_caller
|
17
|
-
@options[:client_host] || ::Protobuf.client_host
|
18
|
-
end
|
19
|
-
|
20
|
-
def complete
|
21
|
-
@stats.stop
|
22
|
-
log_info { @stats.to_s }
|
23
|
-
log_debug { sign_message('Response proceessing complete') }
|
24
|
-
@complete_cb.call(self) unless @complete_cb.nil?
|
25
|
-
rescue => e
|
26
|
-
log_error { sign_message('Complete callback error encountered') }
|
27
|
-
log_exception(e)
|
28
|
-
raise
|
29
|
-
end
|
30
|
-
|
31
|
-
def data_callback(data)
|
32
|
-
log_debug { sign_message('Using data_callback') }
|
33
|
-
@used_data_callback = true
|
34
|
-
@data = data
|
35
|
-
end
|
36
|
-
|
37
|
-
# All failures should be routed through this method.
|
38
|
-
#
|
39
|
-
# @param [Symbol] code The code we're using (see ::Protobuf::Socketrpc::ErrorReason)
|
40
|
-
# @param [String] message The error message
|
41
|
-
def fail(code, message)
|
42
|
-
@error = ClientError.new
|
43
|
-
@error.code = Protobuf::Socketrpc::ErrorReason.fetch(code)
|
44
|
-
@error.message = message
|
45
|
-
log_debug { sign_message("Server failed request (invoking on_failure): #{@error.inspect}") }
|
46
|
-
|
47
|
-
@stats.failure(code)
|
48
|
-
@failure_cb.call(@error) unless @failure_cb.nil?
|
49
|
-
rescue => e
|
50
|
-
log_error { sign_message("Failure callback error encountered") }
|
51
|
-
log_exception(e)
|
52
|
-
raise
|
53
|
-
ensure
|
54
|
-
complete
|
55
|
-
end
|
56
|
-
|
57
|
-
def initialize_stats
|
58
|
-
@stats = Protobuf::Rpc::Stat.new(:CLIENT)
|
59
|
-
@stats.server = [@options[:port], @options[:host]]
|
60
|
-
@stats.service = @options[:service].name
|
61
|
-
@stats.method_name = @options[:method].to_s
|
62
|
-
rescue => ex
|
63
|
-
log_exception(ex)
|
64
|
-
fail(:RPC_ERROR, "Invalid stats configuration. #{ex.message}")
|
65
|
-
end
|
66
|
-
|
67
|
-
def log_signature
|
68
|
-
@_log_signature ||= "[client-#{self.class}]"
|
69
|
-
end
|
70
|
-
|
71
|
-
def parse_response
|
72
|
-
# Close up the connection as we no longer need it
|
73
|
-
close_connection
|
74
|
-
|
75
|
-
log_debug { sign_message("Parsing response from server (connection closed)") }
|
76
|
-
|
77
|
-
# Parse out the raw response
|
78
|
-
@stats.response_size = @response_data.size unless @response_data.nil?
|
79
|
-
response_wrapper = Protobuf::Socketrpc::Response.decode(@response_data)
|
80
|
-
|
81
|
-
# Determine success or failure based on parsed data
|
82
|
-
if response_wrapper.has_field?(:error_reason)
|
83
|
-
log_debug { sign_message("Error response parsed") }
|
84
|
-
|
85
|
-
# fail the call if we already know the client is failed
|
86
|
-
# (don't try to parse out the response payload)
|
87
|
-
fail(response_wrapper.error_reason, response_wrapper.error)
|
88
|
-
else
|
89
|
-
log_debug { sign_message("Successful response parsed") }
|
90
|
-
|
91
|
-
# Ensure client_response is an instance
|
92
|
-
parsed = @options[:response_type].decode(response_wrapper.response_proto.to_s)
|
93
|
-
|
94
|
-
if parsed.nil? and not response_wrapper.has_field?(:error_reason)
|
95
|
-
fail(:BAD_RESPONSE_PROTO, 'Unable to parse response from server')
|
96
|
-
else
|
97
|
-
verify_callbacks
|
98
|
-
succeed(parsed)
|
99
|
-
return @data if @used_data_callback
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
def post_init
|
105
|
-
send_data unless error?
|
106
|
-
rescue => e
|
107
|
-
fail(:RPC_ERROR, "Connection error: #{e.message}")
|
108
|
-
end
|
109
|
-
|
110
|
-
def request_bytes
|
111
|
-
validate_request_type!
|
112
|
-
fields = { :service_name => @options[:service].name,
|
113
|
-
:method_name => @options[:method].to_s,
|
114
|
-
:request_proto => @options[:request],
|
115
|
-
:caller => request_caller }
|
116
|
-
|
117
|
-
return ::Protobuf::Socketrpc::Request.encode(fields)
|
118
|
-
rescue => e
|
119
|
-
fail(:INVALID_REQUEST_PROTO, "Could not set request proto: #{e.message}")
|
120
|
-
end
|
121
|
-
|
122
|
-
def setup_connection
|
123
|
-
initialize_stats
|
124
|
-
@request_data = request_bytes
|
125
|
-
@stats.request_size = request_bytes.size
|
126
|
-
end
|
127
|
-
|
128
|
-
def succeed(response)
|
129
|
-
log_debug { sign_message("Server succeeded request (invoking on_success)") }
|
130
|
-
@stats.success
|
131
|
-
@success_cb.call(response) unless @success_cb.nil?
|
132
|
-
rescue => e
|
133
|
-
log_error { sign_message("Success callback error encountered") }
|
134
|
-
log_exception(e)
|
135
|
-
fail(:RPC_ERROR, "An exception occurred while calling on_success: #{e.message}")
|
136
|
-
ensure
|
137
|
-
complete
|
138
|
-
end
|
139
|
-
|
140
|
-
# Wrap the given block in a timeout of the configured number of seconds.
|
141
|
-
#
|
142
|
-
def timeout_wrap(&block)
|
143
|
-
::Timeout.timeout(options[:timeout], &block)
|
144
|
-
rescue ::Timeout::Error
|
145
|
-
fail(:RPC_FAILED, "The server took longer than #{options[:timeout]} seconds to respond")
|
146
|
-
end
|
147
|
-
|
148
|
-
def validate_request_type!
|
149
|
-
unless @options[:request].class == @options[:request_type]
|
150
|
-
expected = @options[:request_type].name
|
151
|
-
actual = @options[:request].class.name
|
152
|
-
fail(:INVALID_REQUEST_PROTO, "Expected request type to be type of #{expected}, got #{actual} instead")
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
def verify_callbacks
|
157
|
-
unless any_callbacks?
|
158
|
-
log_debug { sign_message("No callbacks set, using data_callback") }
|
159
|
-
@success_cb = @failure_cb = self.method(:data_callback)
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
def verify_options!
|
164
|
-
# Verify the options that are necessary and merge them in
|
165
|
-
[:service, :method, :host, :port].each do |opt|
|
166
|
-
fail(:RPC_ERROR, "Invalid client connection configuration. #{opt} must be a defined option.") if @options[opt].nil?
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
172
|
-
end
|
data/spec/data/data.bin
DELETED
data/spec/data/types.bin
DELETED
Binary file
|
@@ -1,145 +0,0 @@
|
|
1
|
-
require 'protobuf/logger'
|
2
|
-
require 'stringio'
|
3
|
-
require 'fileutils'
|
4
|
-
|
5
|
-
describe Protobuf::Logger do
|
6
|
-
|
7
|
-
subject { Protobuf::Logger }
|
8
|
-
|
9
|
-
before(:each) do
|
10
|
-
Protobuf::Logger.reset_device!
|
11
|
-
Protobuf::Logger.file = '/dev/null'
|
12
|
-
Protobuf::Logger.level = ::Logger::INFO
|
13
|
-
end
|
14
|
-
|
15
|
-
after(:all) do
|
16
|
-
::FileUtils.rm_f('myfile.log')
|
17
|
-
end
|
18
|
-
|
19
|
-
describe '.instance' do
|
20
|
-
|
21
|
-
it 'doesn\'t create a logger if the file was not set' do
|
22
|
-
subject.file = nil
|
23
|
-
subject.instance.should be_nil
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'doesn\'t create a logger if the level was not set' do
|
27
|
-
subject.level = nil
|
28
|
-
subject.instance.should be_nil
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'gets a new instance of the logger when file and level are set' do
|
32
|
-
subject.file.should_not be_nil
|
33
|
-
subject.level.should_not be_nil
|
34
|
-
subject.instance.should_not be_nil
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'keeps the same object from multiple calls to instance' do
|
38
|
-
subject.instance === subject.instance
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
describe '.configure' do
|
44
|
-
before(:each) { subject.reset_device! }
|
45
|
-
it 'sets the file and level in one call' do
|
46
|
-
subject.file.should_not be
|
47
|
-
subject.level.should_not be
|
48
|
-
subject.instance.should_not be
|
49
|
-
subject.configure :file => 'myfile.log', :level => ::Logger::WARN
|
50
|
-
subject.file.should == 'myfile.log'
|
51
|
-
subject.level.should == ::Logger::WARN
|
52
|
-
subject.instance.level.should == ::Logger::WARN
|
53
|
-
end
|
54
|
-
|
55
|
-
context 'specifying logger' do
|
56
|
-
let(:logger) { double('Logger') }
|
57
|
-
|
58
|
-
it 'sets the logger instance' do
|
59
|
-
subject.instance.should_not be
|
60
|
-
subject.configure :logger => logger
|
61
|
-
subject.instance.should == logger
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
describe '.reset_device!' do
|
67
|
-
|
68
|
-
it 'resets the logger instance, file, and level' do
|
69
|
-
subject.instance.should be
|
70
|
-
subject.file.should be
|
71
|
-
subject.level.should be
|
72
|
-
subject.reset_device!
|
73
|
-
subject.instance.should_not be
|
74
|
-
subject.file.should_not be
|
75
|
-
subject.level.should_not be
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'when logging' do
|
81
|
-
|
82
|
-
it 'doesn\'t raise errors when log instance is nil' do
|
83
|
-
subject.reset_device!
|
84
|
-
subject.instance.should be_nil
|
85
|
-
expect {
|
86
|
-
subject.debug 'No errors here'
|
87
|
-
subject.info 'No errors here'
|
88
|
-
subject.warn 'No errors here'
|
89
|
-
subject.error 'No errors here'
|
90
|
-
subject.fatal 'No errors here'
|
91
|
-
subject.add 'No errors here'
|
92
|
-
subject.log 'No errors here'
|
93
|
-
}.to_not raise_error
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'logs correctly when instance is valid' do
|
97
|
-
subject.instance.should_not be_nil
|
98
|
-
subject.instance.should_receive(:info).with('Should log great')
|
99
|
-
subject.info 'Should log great'
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
103
|
-
|
104
|
-
describe Protobuf::Logger::LogMethods do
|
105
|
-
|
106
|
-
context 'when included in another class' do
|
107
|
-
|
108
|
-
before(:all) do
|
109
|
-
class MyTestClass
|
110
|
-
include Protobuf::Logger::LogMethods
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
subject { MyTestClass.new }
|
115
|
-
|
116
|
-
it { should respond_to(:log_debug) }
|
117
|
-
it { should respond_to(:log_info) }
|
118
|
-
it { should respond_to(:log_warn) }
|
119
|
-
it { should respond_to(:log_error) }
|
120
|
-
it { should respond_to(:log_fatal) }
|
121
|
-
it { should respond_to(:log_add) }
|
122
|
-
it { should respond_to(:log_log) }
|
123
|
-
|
124
|
-
context '#log_exception' do
|
125
|
-
it 'logs the exception message as an error and backtrace as debug' do
|
126
|
-
subject.should_receive(:log_error).twice
|
127
|
-
subject.should_receive(:log_debug)
|
128
|
-
subject.log_exception(RuntimeError.new('this is an exception'))
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
its(:log_signature) { should eq "[MyTestClass]" }
|
133
|
-
describe '#sign_message' do
|
134
|
-
specify { subject.sign_message("this is a test").should eq "[MyTestClass] this is a test" }
|
135
|
-
specify { subject.class.sign_message("this is a test").should eq "[MyTestClass] this is a test" }
|
136
|
-
end
|
137
|
-
|
138
|
-
it 'passes all embedded log calls to Logger instance' do
|
139
|
-
Protobuf::Logger.instance.should_receive(:debug).with('[MyTestClass] log this')
|
140
|
-
subject.log_debug('log this')
|
141
|
-
end
|
142
|
-
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'protobuf/socket'
|
3
|
-
require 'protobuf/zmq'
|
4
|
-
|
5
|
-
describe ::Protobuf::Rpc::Connector do
|
6
|
-
|
7
|
-
describe '.connector_for_client(true)' do
|
8
|
-
subject { described_class.connector_for_client }
|
9
|
-
|
10
|
-
context 'Protobuf.connector_type is socket' do
|
11
|
-
before { ::Protobuf.connector_type = :socket }
|
12
|
-
it { should eq ::Protobuf::Rpc::Connectors::Socket }
|
13
|
-
end
|
14
|
-
|
15
|
-
context 'Protobuf.connector_type is not a known value' do
|
16
|
-
before { ::Protobuf.stub(:connector_type) { :foo } }
|
17
|
-
it { should eq ::Protobuf::Rpc::Connectors::Socket }
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'Protobuf.connector_type is zmq' do
|
21
|
-
before { ::Protobuf.connector_type = :zmq }
|
22
|
-
it { should eq ::Protobuf::Rpc::Connectors::Zmq }
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
@@ -1,170 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'protobuf/rpc/service'
|
3
|
-
|
4
|
-
describe Protobuf::Rpc::Connectors::Common do
|
5
|
-
let(:common_class) do
|
6
|
-
Class.new(Protobuf::Rpc::Connectors::Base) do
|
7
|
-
include Protobuf::Rpc::Connectors::Common
|
8
|
-
attr_accessor :options
|
9
|
-
attr_accessor :stats
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
let(:subject_options) { {} }
|
14
|
-
|
15
|
-
subject { @subject ||= common_class.new(subject_options) }
|
16
|
-
|
17
|
-
context "API" do
|
18
|
-
specify { subject.respond_to?(:any_callbacks?).should be_true }
|
19
|
-
specify { subject.respond_to?(:request_caller).should be_true }
|
20
|
-
specify { subject.respond_to?(:data_callback).should be_true }
|
21
|
-
specify { subject.respond_to?(:error).should be_true }
|
22
|
-
specify { subject.respond_to?(:fail).should be_true }
|
23
|
-
specify { subject.respond_to?(:complete).should be_true }
|
24
|
-
specify { subject.respond_to?(:parse_response).should be_true }
|
25
|
-
specify { subject.respond_to?(:verify_options!).should be_true }
|
26
|
-
specify { subject.respond_to?(:verify_callbacks).should be_true }
|
27
|
-
end
|
28
|
-
|
29
|
-
describe "#any_callbacks?" do
|
30
|
-
|
31
|
-
[:@complete_cb, :@success_cb, :@failure_cb].each do |cb|
|
32
|
-
it "returns true if #{cb} is provided" do
|
33
|
-
subject.instance_variable_set(cb, "something")
|
34
|
-
subject.any_callbacks?.should be_true
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
it "returns false when all callbacks are not provided" do
|
39
|
-
subject.instance_variable_set(:@complete_cb, nil)
|
40
|
-
subject.instance_variable_set(:@success_cb, nil)
|
41
|
-
subject.instance_variable_set(:@failure_cb, nil)
|
42
|
-
|
43
|
-
subject.any_callbacks?.should be_false
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
describe '#request_caller' do
|
49
|
-
its(:request_caller) { should eq ::Protobuf.client_host }
|
50
|
-
|
51
|
-
context 'when "client_host" option is given to initializer' do
|
52
|
-
let(:hostname) { 'myhost.myserver.com' }
|
53
|
-
let(:subject_options) { { :client_host => hostname } }
|
54
|
-
|
55
|
-
its(:request_caller) { should_not eq ::Protobuf.client_host }
|
56
|
-
its(:request_caller) { should eq hostname }
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
describe "#data_callback" do
|
61
|
-
it "changes state to use the data callback" do
|
62
|
-
subject.data_callback("data")
|
63
|
-
subject.instance_variable_get(:@used_data_callback).should be_true
|
64
|
-
end
|
65
|
-
|
66
|
-
it "sets the data var when using the data_callback" do
|
67
|
-
subject.data_callback("data")
|
68
|
-
subject.instance_variable_get(:@data).should eq("data")
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
describe '#request_bytes' do
|
73
|
-
let(:service) { Test::ResourceService }
|
74
|
-
let(:method) { :find }
|
75
|
-
let(:request) { '' }
|
76
|
-
let(:client_host) { 'myhost.myservice.com' }
|
77
|
-
let(:subject_options) { { :service => service,
|
78
|
-
:method => method,
|
79
|
-
:request => request,
|
80
|
-
:client_host => client_host } }
|
81
|
-
|
82
|
-
let(:expected) { ::Protobuf::Socketrpc::Request.new({ :service_name => service.name,
|
83
|
-
:method_name => 'find',
|
84
|
-
:request_proto => '',
|
85
|
-
:caller => client_host }) }
|
86
|
-
|
87
|
-
before { subject.stub(:validate_request_type!).and_return(true) }
|
88
|
-
before { subject.should_not_receive(:fail) }
|
89
|
-
|
90
|
-
its(:request_bytes) { should eq expected.encode }
|
91
|
-
end
|
92
|
-
|
93
|
-
describe "#verify_callbacks" do
|
94
|
-
|
95
|
-
it "sets @failure_cb to #data_callback when no callbacks are defined" do
|
96
|
-
subject.verify_callbacks
|
97
|
-
subject.instance_variable_get(:@failure_cb).should eq(subject.method(:data_callback))
|
98
|
-
end
|
99
|
-
|
100
|
-
it "sets @success_cb to #data_callback when no callbacks are defined" do
|
101
|
-
subject.verify_callbacks
|
102
|
-
subject.instance_variable_get(:@success_cb).should eq(subject.method(:data_callback))
|
103
|
-
end
|
104
|
-
|
105
|
-
it "doesn't set @failure_cb when already defined" do
|
106
|
-
set_cb = lambda{ true }
|
107
|
-
subject.instance_variable_set(:@failure_cb, set_cb)
|
108
|
-
subject.verify_callbacks
|
109
|
-
subject.instance_variable_get(:@failure_cb).should eq(set_cb)
|
110
|
-
subject.instance_variable_get(:@failure_cb).should_not eq(subject.method(:data_callback))
|
111
|
-
end
|
112
|
-
|
113
|
-
it "doesn't set @success_cb when already defined" do
|
114
|
-
set_cb = lambda{ true }
|
115
|
-
subject.instance_variable_set(:@success_cb, set_cb)
|
116
|
-
subject.verify_callbacks
|
117
|
-
subject.instance_variable_get(:@success_cb).should eq(set_cb)
|
118
|
-
subject.instance_variable_get(:@success_cb).should_not eq(subject.method(:data_callback))
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|
122
|
-
|
123
|
-
shared_examples "a ConnectorDisposition" do |meth, cb, *args|
|
124
|
-
|
125
|
-
it "calls #complete before exit" do
|
126
|
-
stats = double("Object")
|
127
|
-
stats.stub(:success) { true }
|
128
|
-
stats.stub(:failure) { true }
|
129
|
-
stats.stub(:stop) { true }
|
130
|
-
subject.stats = stats
|
131
|
-
|
132
|
-
subject.should_receive(:complete)
|
133
|
-
subject.method(meth).call(*args)
|
134
|
-
end
|
135
|
-
|
136
|
-
it "calls the #{cb} callback when provided" do
|
137
|
-
stats = double("Object")
|
138
|
-
stats.stub(:success) { true }
|
139
|
-
stats.stub(:failure) { true }
|
140
|
-
stats.stub(:stop) { true }
|
141
|
-
subject.stats = stats
|
142
|
-
_cb = double("Object")
|
143
|
-
|
144
|
-
subject.instance_variable_set("@#{cb}", _cb)
|
145
|
-
_cb.should_receive(:call).and_return(true)
|
146
|
-
subject.method(meth).call(*args)
|
147
|
-
end
|
148
|
-
|
149
|
-
it "calls the complete callback when provided" do
|
150
|
-
stats = double("Object")
|
151
|
-
stats.stub(:success) { true }
|
152
|
-
stats.stub(:failure) { true }
|
153
|
-
stats.stub(:stop) { true }
|
154
|
-
subject.stats = stats
|
155
|
-
comp_cb = double("Object")
|
156
|
-
|
157
|
-
subject.instance_variable_set(:@complete_cb, comp_cb)
|
158
|
-
comp_cb.should_receive(:call).and_return(true)
|
159
|
-
subject.method(meth).call(*args)
|
160
|
-
end
|
161
|
-
|
162
|
-
end
|
163
|
-
|
164
|
-
it_behaves_like("a ConnectorDisposition", :fail, "failure_cb", "code", "message")
|
165
|
-
it_behaves_like("a ConnectorDisposition", :fail, "complete_cb", "code", "message")
|
166
|
-
it_behaves_like("a ConnectorDisposition", :succeed, "complete_cb", "response")
|
167
|
-
it_behaves_like("a ConnectorDisposition", :succeed, "success_cb", "response")
|
168
|
-
it_behaves_like("a ConnectorDisposition", :complete, "complete_cb")
|
169
|
-
|
170
|
-
end
|