protobuffy 3.6.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,52 +1,35 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'protobuf/rpc/service_dispatcher'
|
3
3
|
|
4
|
-
describe Protobuf::Rpc::ServiceDispatcher do
|
4
|
+
RSpec.describe Protobuf::Rpc::ServiceDispatcher do
|
5
5
|
let(:app) { proc { |env| env } }
|
6
|
-
let(:env)
|
6
|
+
let(:env) do
|
7
7
|
Protobuf::Rpc::Env.new(
|
8
8
|
'method_name' => method_name,
|
9
9
|
'request' => request,
|
10
10
|
'rpc_service' => service_class,
|
11
|
-
'service_name' => service_name
|
11
|
+
'service_name' => service_name
|
12
12
|
)
|
13
|
-
|
13
|
+
end
|
14
14
|
let(:method_name) { :find }
|
15
15
|
let(:request) { request_type.new(:name => 'required') }
|
16
16
|
let(:request_type) { service_class.rpcs[method_name].request_type }
|
17
17
|
let(:response) { response_type.new(:name => 'required') }
|
18
18
|
let(:response_type) { service_class.rpcs[method_name].response_type }
|
19
|
-
|
20
19
|
let(:rpc_service) { service_class.new(env) }
|
21
20
|
let(:service_class) { Test::ResourceService }
|
22
21
|
let(:service_name) { service_class.to_s }
|
23
22
|
|
24
23
|
subject { described_class.new(app) }
|
25
24
|
|
26
|
-
before { subject.
|
25
|
+
before { allow(subject).to receive(:rpc_service).and_return(rpc_service) }
|
27
26
|
|
28
27
|
describe '#call' do
|
29
|
-
before { rpc_service.
|
28
|
+
before { allow(rpc_service).to receive(:response).and_return(response) }
|
30
29
|
|
31
30
|
it "dispatches the request" do
|
32
|
-
stack_env = subject.
|
33
|
-
stack_env.response.
|
34
|
-
end
|
35
|
-
|
36
|
-
context "when the given RPC method is not implemented" do
|
37
|
-
let(:method_name) { :find_not_implemented }
|
38
|
-
|
39
|
-
it "raises a method not found exception" do
|
40
|
-
expect { subject.call(env) }.to raise_exception(Protobuf::Rpc::MethodNotFound)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
context "when the given RPC method is implemented and a NoMethodError is raised" do
|
45
|
-
before { rpc_service.stub(:callable_rpc_method).and_return(lambda { rpc_service.__send__(:foo) }) }
|
46
|
-
|
47
|
-
it "raises the exeception" do
|
48
|
-
expect { subject.call(env) }.to raise_exception(NoMethodError)
|
49
|
-
end
|
31
|
+
stack_env = subject._call(env)
|
32
|
+
expect(stack_env.response).to eq response
|
50
33
|
end
|
51
34
|
end
|
52
35
|
end
|
@@ -27,7 +27,7 @@ class FilterTest
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
describe Protobuf::Rpc::ServiceFilters do
|
30
|
+
RSpec.describe Protobuf::Rpc::ServiceFilters do
|
31
31
|
let(:params) { {} }
|
32
32
|
subject { FilterTest.new(params) }
|
33
33
|
after(:each) { FilterTest.clear_filters! }
|
@@ -38,6 +38,7 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
38
38
|
before(:all) do
|
39
39
|
class FilterTest
|
40
40
|
private
|
41
|
+
|
41
42
|
def verify_before
|
42
43
|
@called << :verify_before
|
43
44
|
@before_filter_calls += 1
|
@@ -55,19 +56,20 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
55
56
|
FilterTest.before_filter(:foo)
|
56
57
|
end
|
57
58
|
|
58
|
-
specify { subject.class.
|
59
|
-
specify { subject.class.
|
59
|
+
specify { expect(subject.class).to respond_to(:before_filter) }
|
60
|
+
specify { expect(subject.class).to respond_to(:before_action) }
|
60
61
|
|
61
62
|
it 'calls filters in the order they were defined' do
|
62
63
|
subject.__send__(:run_filters, :endpoint)
|
63
|
-
subject.called.
|
64
|
-
subject.before_filter_calls.
|
64
|
+
expect(subject.called).to eq [:verify_before, :foo, :endpoint]
|
65
|
+
expect(subject.before_filter_calls).to eq 1
|
65
66
|
end
|
66
67
|
|
67
68
|
context 'when filter is configured with "only"' do
|
68
69
|
before(:all) do
|
69
70
|
class FilterTest
|
70
71
|
private
|
72
|
+
|
71
73
|
def endpoint_with_verify
|
72
74
|
@called << :endpoint_with_verify
|
73
75
|
end
|
@@ -82,14 +84,14 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
82
84
|
context 'when invoking a method defined in "only" option' do
|
83
85
|
it 'invokes the filter' do
|
84
86
|
subject.__send__(:run_filters, :endpoint_with_verify)
|
85
|
-
subject.called.
|
87
|
+
expect(subject.called).to eq [:verify_before, :endpoint_with_verify]
|
86
88
|
end
|
87
89
|
end
|
88
90
|
|
89
91
|
context 'when invoking a method not defined by "only" option' do
|
90
92
|
it 'does not invoke the filter' do
|
91
93
|
subject.__send__(:run_filters, :endpoint)
|
92
|
-
subject.called.
|
94
|
+
expect(subject.called).to eq [:endpoint]
|
93
95
|
end
|
94
96
|
end
|
95
97
|
end
|
@@ -98,6 +100,7 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
98
100
|
before(:all) do
|
99
101
|
class FilterTest
|
100
102
|
private
|
103
|
+
|
101
104
|
def endpoint_without_verify
|
102
105
|
@called << :endpoint_without_verify
|
103
106
|
end
|
@@ -112,14 +115,14 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
112
115
|
context 'when invoking a method not defined in "except" option' do
|
113
116
|
it 'invokes the filter' do
|
114
117
|
subject.__send__(:run_filters, :endpoint)
|
115
|
-
subject.called.
|
118
|
+
expect(subject.called).to eq [:verify_before, :endpoint]
|
116
119
|
end
|
117
120
|
end
|
118
121
|
|
119
122
|
context 'when invoking a method defined by "except" option' do
|
120
123
|
it 'does not invoke the filter' do
|
121
124
|
subject.__send__(:run_filters, :endpoint_without_verify)
|
122
|
-
subject.called.
|
125
|
+
expect(subject.called).to eq [:endpoint_without_verify]
|
123
126
|
end
|
124
127
|
end
|
125
128
|
end
|
@@ -128,9 +131,18 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
128
131
|
before(:all) do
|
129
132
|
class FilterTest
|
130
133
|
private
|
131
|
-
|
132
|
-
def
|
133
|
-
|
134
|
+
|
135
|
+
def check_true
|
136
|
+
true
|
137
|
+
end
|
138
|
+
|
139
|
+
def check_false
|
140
|
+
false
|
141
|
+
end
|
142
|
+
|
143
|
+
def verify_before
|
144
|
+
@called << :verify_before
|
145
|
+
end
|
134
146
|
end
|
135
147
|
end
|
136
148
|
|
@@ -142,19 +154,19 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
142
154
|
|
143
155
|
it 'invokes the filter' do
|
144
156
|
subject.__send__(:run_filters, :endpoint)
|
145
|
-
subject.called.
|
157
|
+
expect(subject.called).to eq [:verify_before, :endpoint]
|
146
158
|
end
|
147
159
|
end
|
148
160
|
|
149
161
|
context 'when "if" option is a callable that returns true' do
|
150
162
|
before do
|
151
163
|
FilterTest.clear_filters!
|
152
|
-
FilterTest.before_filter(:verify_before, :if =>
|
164
|
+
FilterTest.before_filter(:verify_before, :if => ->(_service) { true })
|
153
165
|
end
|
154
166
|
|
155
167
|
it 'invokes the filter' do
|
156
168
|
subject.__send__(:run_filters, :endpoint)
|
157
|
-
subject.called.
|
169
|
+
expect(subject.called).to eq [:verify_before, :endpoint]
|
158
170
|
end
|
159
171
|
end
|
160
172
|
|
@@ -166,19 +178,19 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
166
178
|
|
167
179
|
it 'skips the filter' do
|
168
180
|
subject.__send__(:run_filters, :endpoint)
|
169
|
-
subject.called.
|
181
|
+
expect(subject.called).to eq [:endpoint]
|
170
182
|
end
|
171
183
|
end
|
172
184
|
|
173
185
|
context 'when "if" option is a callable that returns false' do
|
174
186
|
before do
|
175
187
|
FilterTest.clear_filters!
|
176
|
-
FilterTest.before_filter(:verify_before, :if =>
|
188
|
+
FilterTest.before_filter(:verify_before, :if => ->(_service) { false })
|
177
189
|
end
|
178
190
|
|
179
191
|
it 'skips the filter' do
|
180
192
|
subject.__send__(:run_filters, :endpoint)
|
181
|
-
subject.called.
|
193
|
+
expect(subject.called).to eq [:endpoint]
|
182
194
|
end
|
183
195
|
end
|
184
196
|
end
|
@@ -187,9 +199,18 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
187
199
|
before(:all) do
|
188
200
|
class FilterTest
|
189
201
|
private
|
190
|
-
|
191
|
-
def
|
192
|
-
|
202
|
+
|
203
|
+
def check_true
|
204
|
+
true
|
205
|
+
end
|
206
|
+
|
207
|
+
def check_false
|
208
|
+
false
|
209
|
+
end
|
210
|
+
|
211
|
+
def verify_before
|
212
|
+
@called << :verify_before
|
213
|
+
end
|
193
214
|
end
|
194
215
|
end
|
195
216
|
|
@@ -201,19 +222,19 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
201
222
|
|
202
223
|
it 'invokes the filter' do
|
203
224
|
subject.__send__(:run_filters, :endpoint)
|
204
|
-
subject.called.
|
225
|
+
expect(subject.called).to eq [:verify_before, :endpoint]
|
205
226
|
end
|
206
227
|
end
|
207
228
|
|
208
229
|
context 'when "unless" option is a callable that returns true' do
|
209
230
|
before do
|
210
231
|
FilterTest.clear_filters!
|
211
|
-
FilterTest.before_filter(:verify_before, :unless =>
|
232
|
+
FilterTest.before_filter(:verify_before, :unless => ->(_service) { false })
|
212
233
|
end
|
213
234
|
|
214
235
|
it 'invokes the filter' do
|
215
236
|
subject.__send__(:run_filters, :endpoint)
|
216
|
-
subject.called.
|
237
|
+
expect(subject.called).to eq [:verify_before, :endpoint]
|
217
238
|
end
|
218
239
|
end
|
219
240
|
|
@@ -225,19 +246,19 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
225
246
|
|
226
247
|
it 'skips the filter' do
|
227
248
|
subject.__send__(:run_filters, :endpoint)
|
228
|
-
subject.called.
|
249
|
+
expect(subject.called).to eq [:endpoint]
|
229
250
|
end
|
230
251
|
end
|
231
252
|
|
232
253
|
context 'when "unless" option is a callable that returns false' do
|
233
254
|
before do
|
234
255
|
FilterTest.clear_filters!
|
235
|
-
FilterTest.before_filter(:verify_before, :unless =>
|
256
|
+
FilterTest.before_filter(:verify_before, :unless => ->(_service) { true })
|
236
257
|
end
|
237
258
|
|
238
259
|
it 'skips the filter' do
|
239
260
|
subject.__send__(:run_filters, :endpoint)
|
240
|
-
subject.called.
|
261
|
+
expect(subject.called).to eq [:endpoint]
|
241
262
|
end
|
242
263
|
end
|
243
264
|
end
|
@@ -246,9 +267,10 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
246
267
|
before(:all) do
|
247
268
|
class FilterTest
|
248
269
|
private
|
270
|
+
|
249
271
|
def short_circuit_filter
|
250
272
|
@called << :short_circuit_filter
|
251
|
-
|
273
|
+
false
|
252
274
|
end
|
253
275
|
end
|
254
276
|
end
|
@@ -259,9 +281,9 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
259
281
|
end
|
260
282
|
|
261
283
|
it 'does not invoke the rpc method' do
|
262
|
-
subject.
|
284
|
+
expect(subject).not_to receive(:endpoint)
|
263
285
|
subject.__send__(:run_filters, :endpoint)
|
264
|
-
subject.called.
|
286
|
+
expect(subject.called).to eq [:short_circuit_filter]
|
265
287
|
end
|
266
288
|
end
|
267
289
|
end
|
@@ -272,6 +294,7 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
272
294
|
before(:all) do
|
273
295
|
class FilterTest
|
274
296
|
private
|
297
|
+
|
275
298
|
def verify_after
|
276
299
|
@called << :verify_after
|
277
300
|
@after_filter_calls += 1
|
@@ -289,13 +312,13 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
289
312
|
FilterTest.after_filter(:foo)
|
290
313
|
end
|
291
314
|
|
292
|
-
specify { subject.class.
|
293
|
-
specify { subject.class.
|
315
|
+
specify { expect(subject.class).to respond_to(:after_filter) }
|
316
|
+
specify { expect(subject.class).to respond_to(:after_action) }
|
294
317
|
|
295
318
|
it 'calls filters in the order they were defined' do
|
296
319
|
subject.__send__(:run_filters, :endpoint)
|
297
|
-
subject.called.
|
298
|
-
subject.after_filter_calls.
|
320
|
+
expect(subject.called).to eq [:endpoint, :verify_after, :foo]
|
321
|
+
expect(subject.after_filter_calls).to eq 1
|
299
322
|
end
|
300
323
|
end
|
301
324
|
|
@@ -305,6 +328,7 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
305
328
|
before(:all) do
|
306
329
|
class FilterTest
|
307
330
|
private
|
331
|
+
|
308
332
|
def outer_around
|
309
333
|
@called << :outer_around_top
|
310
334
|
yield
|
@@ -326,22 +350,27 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
326
350
|
FilterTest.around_filter(:inner_around)
|
327
351
|
end
|
328
352
|
|
329
|
-
specify { subject.class.
|
330
|
-
specify { subject.class.
|
353
|
+
specify { expect(subject.class).to respond_to(:around_filter) }
|
354
|
+
specify { expect(subject.class).to respond_to(:around_action) }
|
331
355
|
|
332
356
|
it 'calls filters in the order they were defined' do
|
333
357
|
subject.__send__(:run_filters, :endpoint)
|
334
|
-
subject.called.
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
358
|
+
expect(subject.called).to eq(
|
359
|
+
[
|
360
|
+
:outer_around_top,
|
361
|
+
:inner_around_top,
|
362
|
+
:endpoint,
|
363
|
+
:inner_around_bottom,
|
364
|
+
:outer_around_bottom,
|
365
|
+
]
|
366
|
+
)
|
339
367
|
end
|
340
368
|
|
341
369
|
context 'when around_filter does not yield' do
|
342
370
|
before do
|
343
371
|
class FilterTest
|
344
372
|
private
|
373
|
+
|
345
374
|
def inner_around
|
346
375
|
@called << :inner_around
|
347
376
|
end
|
@@ -354,11 +383,15 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
354
383
|
end
|
355
384
|
|
356
385
|
it 'cancels calling the rest of the filters and the endpoint' do
|
357
|
-
subject.
|
386
|
+
expect(subject).not_to receive(:endpoint)
|
358
387
|
subject.__send__(:run_filters, :endpoint)
|
359
|
-
subject.called.
|
360
|
-
|
361
|
-
|
388
|
+
expect(subject.called).to eq(
|
389
|
+
[
|
390
|
+
:outer_around_top,
|
391
|
+
:inner_around,
|
392
|
+
:outer_around_bottom,
|
393
|
+
]
|
394
|
+
)
|
362
395
|
end
|
363
396
|
|
364
397
|
end
|
@@ -377,22 +410,22 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
377
410
|
|
378
411
|
def filter_with_error1
|
379
412
|
@called << :filter_with_error1
|
380
|
-
|
413
|
+
fail CustomError1, 'Filter 1 failed'
|
381
414
|
end
|
382
415
|
|
383
416
|
def filter_with_error2
|
384
417
|
@called << :filter_with_error2
|
385
|
-
|
418
|
+
fail CustomError1, 'Filter 2 failed'
|
386
419
|
end
|
387
420
|
|
388
421
|
def filter_with_error3
|
389
422
|
@called << :filter_with_error3
|
390
|
-
|
423
|
+
fail CustomError3, 'Filter 3 failed'
|
391
424
|
end
|
392
425
|
|
393
426
|
def filter_with_runtime_error
|
394
427
|
@called << :filter_with_runtime_error
|
395
|
-
|
428
|
+
fail 'Filter with runtime error failed'
|
396
429
|
end
|
397
430
|
|
398
431
|
def custom_error_occurred(ex)
|
@@ -411,12 +444,12 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
411
444
|
before { FilterTest.before_filter(:filter_with_error3) }
|
412
445
|
|
413
446
|
it 'short-circuits the call stack' do
|
414
|
-
expect
|
415
|
-
subject.
|
447
|
+
expect do
|
448
|
+
expect(subject).not_to receive(:endpoint)
|
416
449
|
subject.__send__(:run_filters, :endpoint)
|
417
|
-
subject.called.
|
418
|
-
subject.ex_class.
|
419
|
-
|
450
|
+
expect(subject.called).to eq([:filter_with_error3, :custom_error_occurred])
|
451
|
+
expect(subject.ex_class).to eq CustomError3
|
452
|
+
end.not_to raise_error
|
420
453
|
end
|
421
454
|
end
|
422
455
|
|
@@ -432,12 +465,12 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
432
465
|
before { FilterTest.before_filter(:filter_with_error1) }
|
433
466
|
|
434
467
|
it 'short-circuits the call stack' do
|
435
|
-
expect
|
436
|
-
subject.
|
468
|
+
expect do
|
469
|
+
expect(subject).not_to receive(:endpoint)
|
437
470
|
subject.__send__(:run_filters, :endpoint)
|
438
|
-
subject.called.
|
439
|
-
subject.ex_class.
|
440
|
-
|
471
|
+
expect(subject.called).to eq([:filter_with_error1, :custom_error_occurred])
|
472
|
+
expect(subject.ex_class).to eq CustomError1
|
473
|
+
end.not_to raise_error
|
441
474
|
end
|
442
475
|
end
|
443
476
|
end
|
@@ -452,12 +485,12 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
452
485
|
before { FilterTest.before_filter(:filter_with_error1) }
|
453
486
|
|
454
487
|
it 'short-circuits the call stack' do
|
455
|
-
expect
|
456
|
-
subject.
|
488
|
+
expect do
|
489
|
+
expect(subject).not_to receive(:endpoint)
|
457
490
|
subject.__send__(:run_filters, :endpoint)
|
458
|
-
subject.called.
|
459
|
-
subject.ex_class.
|
460
|
-
|
491
|
+
expect(subject.called).to eq([:filter_with_error1, :block_rescue_handler])
|
492
|
+
expect(subject.ex_class).to eq CustomError1
|
493
|
+
end.not_to raise_error
|
461
494
|
end
|
462
495
|
end
|
463
496
|
|
@@ -471,12 +504,12 @@ describe Protobuf::Rpc::ServiceFilters do
|
|
471
504
|
before { FilterTest.before_filter(:filter_with_runtime_error) }
|
472
505
|
|
473
506
|
it 'rescues with the given callable' do
|
474
|
-
expect
|
475
|
-
subject.
|
507
|
+
expect do
|
508
|
+
expect(subject).not_to receive(:endpoint)
|
476
509
|
subject.__send__(:run_filters, :endpoint)
|
477
|
-
subject.called.
|
478
|
-
subject.ex_class.
|
479
|
-
|
510
|
+
expect(subject.called).to eq([:filter_with_runtime_error, :standard_error_rescue_handler])
|
511
|
+
expect(subject.ex_class).to eq RuntimeError
|
512
|
+
end.not_to raise_error
|
480
513
|
end
|
481
514
|
end
|
482
515
|
end
|