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,46 +1,222 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'protobuf/optionable'
|
3
|
+
require 'protobuf/field/message_field'
|
4
|
+
require PROTOS_PATH.join('resource.pb')
|
3
5
|
|
4
|
-
describe 'Optionable' do
|
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
|
5
12
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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)
|
11
53
|
end
|
12
54
|
|
13
|
-
it '
|
14
|
-
expect(
|
15
|
-
expect(OptionableSetOptionTest.method(:set_option).arity).to eq(-2)
|
16
|
-
expect {
|
17
|
-
OptionableSetOptionTest.set_option(:foo, :bar)
|
18
|
-
}.to_not raise_error
|
55
|
+
it '.get_option! errors if the option does not exist' do
|
56
|
+
expect { OptionableGetOptionTest.get_option(:baz) }.to raise_error(ArgumentError)
|
19
57
|
end
|
20
58
|
|
21
|
-
it '
|
22
|
-
|
23
|
-
|
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)
|
24
99
|
end
|
25
100
|
end
|
26
101
|
|
27
|
-
describe '.
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
32
134
|
end
|
135
|
+
::Protobuf::Optionable.inject(klass) { ::Google::Protobuf::MessageOptions }
|
33
136
|
end
|
34
137
|
|
35
|
-
it '
|
36
|
-
|
37
|
-
expect
|
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')
|
38
142
|
end
|
39
143
|
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
43
168
|
end
|
44
169
|
end
|
45
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
|
+
end
|
46
222
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require '
|
2
|
+
require SUPPORT_PATH.join('resource_service')
|
3
3
|
|
4
|
-
describe Protobuf::Rpc::Client do
|
4
|
+
RSpec.describe Protobuf::Rpc::Client do
|
5
5
|
before(:each) do
|
6
6
|
load 'protobuf/socket.rb'
|
7
7
|
end
|
@@ -10,29 +10,29 @@ describe Protobuf::Rpc::Client do
|
|
10
10
|
before { reset_service_location(Test::ResourceService) }
|
11
11
|
|
12
12
|
it 'should be able to get a client through the Service#client helper method' do
|
13
|
-
Test::ResourceService.client(:port => 9191).
|
13
|
+
expect(::Test::ResourceService.client(:port => 9191)).to eq(Protobuf::Rpc::Client.new(:service => Test::ResourceService, :port => 9191))
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should be able to override a service location's host and port" do
|
17
17
|
Test::ResourceService.located_at 'somewheregreat.com:12345'
|
18
18
|
clean_client = Test::ResourceService.client
|
19
|
-
clean_client.options[:host].
|
20
|
-
clean_client.options[:port].
|
19
|
+
expect(clean_client.options[:host]).to eq('somewheregreat.com')
|
20
|
+
expect(clean_client.options[:port]).to eq(12345)
|
21
21
|
|
22
22
|
updated_client = Test::ResourceService.client(:host => 'amazing.com', :port => 54321)
|
23
|
-
updated_client.options[:host].
|
24
|
-
updated_client.options[:port].
|
23
|
+
expect(updated_client.options[:host]).to eq('amazing.com')
|
24
|
+
expect(updated_client.options[:port]).to eq(54321)
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'should be able to define which service to create itself for' do
|
28
28
|
client = Protobuf::Rpc::Client.new :service => Test::ResourceService
|
29
|
-
client.options[:service].
|
29
|
+
expect(client.options[:service]).to eq(Test::ResourceService)
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'should have a hard default for host and port on a service that has not been configured' do
|
33
33
|
client = Test::ResourceService.client
|
34
|
-
client.options[:host].
|
35
|
-
client.options[:port].
|
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
36
|
end
|
37
37
|
|
38
38
|
end
|
@@ -45,7 +45,7 @@ describe Protobuf::Rpc::Client do
|
|
45
45
|
|
46
46
|
it 'should respond to defined service methods' do
|
47
47
|
client = Test::ResourceService.client
|
48
|
-
client.
|
48
|
+
expect(client).to receive(:send_request).and_return(nil)
|
49
49
|
expect { client.find(nil) }.to_not raise_error
|
50
50
|
end
|
51
51
|
end
|
@@ -54,11 +54,11 @@ describe Protobuf::Rpc::Client do
|
|
54
54
|
|
55
55
|
it 'should be able to create the correct request object if passed a hash' do
|
56
56
|
client = Test::ResourceService.client
|
57
|
-
client.
|
58
|
-
client.find(
|
59
|
-
client.options[:request].
|
60
|
-
client.options[:request].name.
|
61
|
-
client.options[:request].active.
|
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
62
|
end
|
63
63
|
|
64
64
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Protobuf::Rpc::Connectors::Base do
|
3
|
+
RSpec.describe Protobuf::Rpc::Connectors::Base do
|
4
4
|
|
5
5
|
let(:options) do
|
6
6
|
{ :timeout => 60 }
|
@@ -8,43 +8,197 @@ describe Protobuf::Rpc::Connectors::Base do
|
|
8
8
|
|
9
9
|
subject { Protobuf::Rpc::Connectors::Base.new(options) }
|
10
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 "#any_callbacks?" do
|
24
|
+
[:@complete_cb, :@success_cb, :@failure_cb].each do |cb|
|
25
|
+
it "returns true if #{cb} is provided" do
|
26
|
+
subject.instance_variable_set(cb, "something")
|
27
|
+
expect(subject.any_callbacks?).to be true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "returns false when all callbacks are not provided" do
|
32
|
+
subject.instance_variable_set(:@complete_cb, nil)
|
33
|
+
subject.instance_variable_set(:@success_cb, nil)
|
34
|
+
subject.instance_variable_set(:@failure_cb, nil)
|
35
|
+
|
36
|
+
expect(subject.any_callbacks?).to be false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#data_callback" do
|
41
|
+
it "changes state to use the data callback" do
|
42
|
+
subject.data_callback("data")
|
43
|
+
expect(subject.instance_variable_get(:@used_data_callback)).to be true
|
44
|
+
end
|
45
|
+
|
46
|
+
it "sets the data var when using the data_callback" do
|
47
|
+
subject.data_callback("data")
|
48
|
+
expect(subject.instance_variable_get(:@data)).to eq("data")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
11
52
|
describe "#send_request" do
|
12
53
|
it "raising an error when 'send_request' is not overridden" do
|
13
|
-
expect{ subject.send_request }.to raise_error(RuntimeError, /inherit a Connector/)
|
54
|
+
expect { subject.send_request }.to raise_error(RuntimeError, /inherit a Connector/)
|
14
55
|
end
|
15
56
|
|
16
57
|
it "does not raise error when 'send_request' is overridden" do
|
17
|
-
new_sub = Class.new(subject.class){ def send_request; end }.new(options)
|
18
|
-
expect{ new_sub.send_request }.to_not raise_error
|
58
|
+
new_sub = Class.new(subject.class) { def send_request; end }.new(options)
|
59
|
+
expect { new_sub.send_request }.to_not raise_error
|
19
60
|
end
|
20
61
|
end
|
21
62
|
|
22
63
|
describe '.new' do
|
23
64
|
it 'assigns passed options and initializes success/failure callbacks' do
|
24
|
-
subject.options.
|
25
|
-
subject.success_cb.
|
26
|
-
subject.failure_cb.
|
65
|
+
expect(subject.options).to eq(Protobuf::Rpc::Connectors::DEFAULT_OPTIONS.merge(options))
|
66
|
+
expect(subject.success_cb).to be_nil
|
67
|
+
expect(subject.failure_cb).to be_nil
|
27
68
|
end
|
28
69
|
end
|
29
70
|
|
30
71
|
describe '#success_cb' do
|
31
72
|
it 'allows setting the success callback and calling it' do
|
32
|
-
subject.success_cb.
|
33
|
-
cb = proc {|res|
|
73
|
+
expect(subject.success_cb).to be_nil
|
74
|
+
cb = proc { |res| fail res }
|
34
75
|
subject.success_cb = cb
|
35
|
-
subject.success_cb.
|
76
|
+
expect(subject.success_cb).to eq(cb)
|
36
77
|
expect { subject.success_cb.call('an error from cb') }.to raise_error 'an error from cb'
|
37
78
|
end
|
38
79
|
end
|
39
80
|
|
40
81
|
describe '#failure_cb' do
|
41
82
|
it 'allows setting the failure callback and calling it' do
|
42
|
-
subject.failure_cb.
|
43
|
-
cb = proc {|res|
|
83
|
+
expect(subject.failure_cb).to be_nil
|
84
|
+
cb = proc { |res| fail res }
|
44
85
|
subject.failure_cb = cb
|
45
|
-
subject.failure_cb.
|
86
|
+
expect(subject.failure_cb).to eq(cb)
|
46
87
|
expect { subject.failure_cb.call('an error from cb') }.to raise_error 'an error from cb'
|
47
88
|
end
|
48
89
|
end
|
49
90
|
|
91
|
+
describe '#request_bytes' do
|
92
|
+
let(:service) { Test::ResourceService }
|
93
|
+
let(:method) { :find }
|
94
|
+
let(:request) { '' }
|
95
|
+
let(:client_host) { 'myhost.myservice.com' }
|
96
|
+
let(:options) do
|
97
|
+
{
|
98
|
+
:service => service,
|
99
|
+
:method => method,
|
100
|
+
:request => request,
|
101
|
+
:client_host => client_host,
|
102
|
+
:timeout => 60,
|
103
|
+
}
|
104
|
+
end
|
105
|
+
|
106
|
+
let(:expected) do
|
107
|
+
::Protobuf::Socketrpc::Request.new(
|
108
|
+
:service_name => service.name,
|
109
|
+
:method_name => 'find',
|
110
|
+
:request_proto => '',
|
111
|
+
:caller => client_host
|
112
|
+
)
|
113
|
+
end
|
114
|
+
|
115
|
+
before { allow(subject).to receive(:validate_request_type!).and_return(true) }
|
116
|
+
before { expect(subject).not_to receive(:failure) }
|
117
|
+
|
118
|
+
specify { expect(subject.request_bytes).to eq expected.encode }
|
119
|
+
end
|
120
|
+
|
121
|
+
describe '#request_caller' do
|
122
|
+
specify { expect(subject.request_caller).to eq ::Protobuf.client_host }
|
123
|
+
|
124
|
+
context 'when "client_host" option is given to initializer' do
|
125
|
+
let(:hostname) { 'myhost.myserver.com' }
|
126
|
+
let(:options) { { :client_host => hostname, :timeout => 60 } }
|
127
|
+
|
128
|
+
specify { expect(subject.request_caller).to_not eq ::Protobuf.client_host }
|
129
|
+
specify { expect(subject.request_caller).to eq hostname }
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe "#verify_callbacks" do
|
134
|
+
it "sets @failure_cb to #data_callback when no callbacks are defined" do
|
135
|
+
subject.verify_callbacks
|
136
|
+
expect(subject.instance_variable_get(:@failure_cb)).to eq(subject.method(:data_callback))
|
137
|
+
end
|
138
|
+
|
139
|
+
it "sets @success_cb to #data_callback when no callbacks are defined" do
|
140
|
+
subject.verify_callbacks
|
141
|
+
expect(subject.instance_variable_get(:@success_cb)).to eq(subject.method(:data_callback))
|
142
|
+
end
|
143
|
+
|
144
|
+
it "doesn't set @failure_cb when already defined" do
|
145
|
+
set_cb = -> { true }
|
146
|
+
subject.instance_variable_set(:@failure_cb, set_cb)
|
147
|
+
subject.verify_callbacks
|
148
|
+
expect(subject.instance_variable_get(:@failure_cb)).to eq(set_cb)
|
149
|
+
expect(subject.instance_variable_get(:@failure_cb)).to_not eq(subject.method(:data_callback))
|
150
|
+
end
|
151
|
+
|
152
|
+
it "doesn't set @success_cb when already defined" do
|
153
|
+
set_cb = -> { true }
|
154
|
+
subject.instance_variable_set(:@success_cb, set_cb)
|
155
|
+
subject.verify_callbacks
|
156
|
+
expect(subject.instance_variable_get(:@success_cb)).to eq(set_cb)
|
157
|
+
expect(subject.instance_variable_get(:@success_cb)).to_not eq(subject.method(:data_callback))
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
161
|
+
|
162
|
+
shared_examples "a ConnectorDisposition" do |meth, stats_meth, cb, *args|
|
163
|
+
let(:stats) { double("Object", :stop => true, :success => true, :failure => true) }
|
164
|
+
before { subject.stats = stats }
|
165
|
+
|
166
|
+
it "calls #complete before exit" do
|
167
|
+
expect(subject).to receive(:complete)
|
168
|
+
subject.method(meth).call(*args)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "calls the #{cb} callback when provided" do
|
172
|
+
some_cb = double("Object")
|
173
|
+
|
174
|
+
subject.instance_variable_set("@#{cb}", some_cb)
|
175
|
+
expect(some_cb).to receive(:call).and_return(true)
|
176
|
+
subject.method(meth).call(*args)
|
177
|
+
end
|
178
|
+
|
179
|
+
it "calls the complete callback when provided" do
|
180
|
+
comp_cb = double("Object")
|
181
|
+
|
182
|
+
subject.instance_variable_set(:@complete_cb, comp_cb)
|
183
|
+
expect(comp_cb).to receive(:call).and_return(true)
|
184
|
+
subject.method(meth).call(*args)
|
185
|
+
end
|
186
|
+
|
187
|
+
it "calls method on stats" do
|
188
|
+
subject.stats = Class.new
|
189
|
+
expected_args = stats_meth == :failure ? args[0] : no_args
|
190
|
+
expect(subject.stats).to receive(stats_meth).once.with(expected_args)
|
191
|
+
allow(subject.stats).to receive(:stop)
|
192
|
+
|
193
|
+
subject.method(meth).call(*args)
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
197
|
+
|
198
|
+
it_behaves_like("a ConnectorDisposition", :failure, :failure, "failure_cb", "code", "message")
|
199
|
+
it_behaves_like("a ConnectorDisposition", :failure, :failure, "complete_cb", "code", "message")
|
200
|
+
it_behaves_like("a ConnectorDisposition", :succeed, :success, "complete_cb", "response")
|
201
|
+
it_behaves_like("a ConnectorDisposition", :succeed, :success, "success_cb", "response")
|
202
|
+
it_behaves_like("a ConnectorDisposition", :complete, :stop, "complete_cb")
|
203
|
+
|
50
204
|
end
|