ia-redis-rpc 2.0.0.pre.dev → 2.1.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/examples/calc.rb +7 -2
- data/lib/redis-rpc/version.rb +1 -1
- data/lib/redis-rpc.rb +14 -4
- data/spec/calculator_spec.rb +5 -0
- data/spec/kwargs_spec.rb +13 -0
- data/spec/spec_helper.rb +2 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6429f97b99f8b0a63a1db9ac8834efd273718d7ddd797a9558408caf8ec2e41c
|
4
|
+
data.tar.gz: 87ce089cfa6e86f6270a02742f6e613c741faabd189e0c517b644da9a44f2103
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cc6c900ac1299e205372a1876d8e3884a310310c6624f3ab6aacc22fc5c433ddc175b0dfd35522082fec4655adf4e5724a449209e9216c66d4f1890e5b02a44
|
7
|
+
data.tar.gz: e287474f5df739d7cb16c400097ccae92272df4842c55ed38fc501f577e3ba028d9a6e3c1c3d08482b53d514025479c2496f719f8d82a4ba42b24f8e50305a3d
|
data/examples/calc.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
class Calculator
|
2
2
|
# A simple, mutable calculator used for testing.
|
3
3
|
# referenced explicitly in ../spec/calculator.spec.rb
|
4
|
-
|
4
|
+
|
5
5
|
def initialize
|
6
6
|
@acc = 0.0
|
7
7
|
end
|
@@ -27,6 +27,11 @@ class Calculator
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def val
|
30
|
-
|
30
|
+
@acc
|
31
|
+
end
|
32
|
+
|
33
|
+
def sleep(time)
|
34
|
+
# expose this method to test response timeouts
|
35
|
+
super
|
31
36
|
end
|
32
37
|
end
|
data/lib/redis-rpc/version.rb
CHANGED
data/lib/redis-rpc.rb
CHANGED
@@ -69,7 +69,7 @@ module RedisRpc
|
|
69
69
|
'timeout_at' => get_timeout_at,
|
70
70
|
}
|
71
71
|
|
72
|
-
rpc_raw_request =
|
72
|
+
rpc_raw_request = rpc_request.to_json
|
73
73
|
|
74
74
|
# transport
|
75
75
|
@redis_server.rpush @message_queue, rpc_raw_request
|
@@ -116,7 +116,12 @@ module RedisRpc
|
|
116
116
|
def initialize(redis_server, message_queue, local_object, timeout: nil, response_expiry: 1, verbose: false, logger: nil)
|
117
117
|
@redis_server = redis_server
|
118
118
|
@message_queue = message_queue
|
119
|
-
@local_object = local_object
|
119
|
+
@local_object = local_object.tap do |o|
|
120
|
+
# Override #respond_to? so it only exposes methods which are directly defined by the receiver
|
121
|
+
def o.respond_to?(method_name)
|
122
|
+
self.public_methods(false).include?(method_name.to_sym)
|
123
|
+
end
|
124
|
+
end
|
120
125
|
@timeout = timeout || 0
|
121
126
|
@response_expiry = response_expiry
|
122
127
|
@verbose = verbose
|
@@ -176,8 +181,13 @@ module RedisRpc
|
|
176
181
|
|
177
182
|
logger&.info("[#{Time.now}] #{self.class.name} : action=run_one rpc_call=#{@local_object.class.name}##{function_call['name']}(#{function_call['args']})")
|
178
183
|
|
184
|
+
function_call_name = function_call['name'].to_sym
|
185
|
+
unless @local_object.public_methods(false).include?(function_call_name)
|
186
|
+
raise "Forbidden RPC call '#{function_call_name}'"
|
187
|
+
end
|
188
|
+
|
179
189
|
function_call['kwargs'].transform_keys!(&:to_sym) if function_call['kwargs']&.kind_of?(Hash)
|
180
|
-
return_value = @local_object.
|
190
|
+
return_value = @local_object.public_send(function_call_name, *function_call['args'], **function_call['kwargs'])
|
181
191
|
rpc_response = { 'return_value' => return_value }
|
182
192
|
rescue StandardError => err
|
183
193
|
rpc_response = { 'exception' => err.to_s, 'backtrace' => err.backtrace }
|
@@ -192,7 +202,7 @@ module RedisRpc
|
|
192
202
|
end
|
193
203
|
|
194
204
|
# response transport
|
195
|
-
rpc_raw_response =
|
205
|
+
rpc_raw_response = rpc_response.to_json
|
196
206
|
@redis_server.multi do |pipeline|
|
197
207
|
pipeline.rpush response_queue, rpc_raw_response
|
198
208
|
pipeline.expire response_queue, @response_expiry
|
data/spec/calculator_spec.rb
CHANGED
@@ -48,6 +48,11 @@ describe Calculator do
|
|
48
48
|
expect { calculator.a_missing_method }.to raise_error(RedisRpc::RemoteException)
|
49
49
|
end
|
50
50
|
|
51
|
+
it 'should raise when method is not implemented by receiver' do
|
52
|
+
# noinspection RubyResolve
|
53
|
+
expect { calculator.send(:instance_eval, "puts :remote_exec") }.to raise_error(RedisRpc::RemoteException, "Forbidden RPC call 'instance_eval'")
|
54
|
+
end
|
55
|
+
|
51
56
|
it 'should raise timeout when execution expires' do
|
52
57
|
expect { calculator.send(:sleep, 3) }.to raise_error RedisRpc::TimeoutException
|
53
58
|
end
|
data/spec/kwargs_spec.rb
CHANGED
@@ -11,6 +11,7 @@ describe KwargsEcho do
|
|
11
11
|
let(:hall) { KwargsEcho.new }
|
12
12
|
|
13
13
|
it 'should echo' do
|
14
|
+
expect(hall.public_methods(true).include?(:echo)).to eq(true)
|
14
15
|
expect(hall.echo(1, kwarg1: :kw1)).to be == {args1: 1, kwarg1: :kw1, kwarg2: :kwarg2}
|
15
16
|
end
|
16
17
|
end
|
@@ -25,6 +26,18 @@ describe KwargsEcho do
|
|
25
26
|
after(:each) { rpc_server_builder.call.stop! && @rpc_server.kill; rpc_server_builder.call.flush_queue! }
|
26
27
|
let(:hall) { RedisRpc::Client.new($REDIS, 'hall', timeout: 2) }
|
27
28
|
|
29
|
+
it 'should respond_to?(:echo)' do
|
30
|
+
expect(hall.respond_to?(:echo)).to eq(true)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should not respond_to?(:sleep)' do
|
34
|
+
expect(hall.respond_to?(:sleep)).to eq(false)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should not respond_to?(:instance_eval)' do
|
38
|
+
expect(hall.respond_to?(:instance_eval)).to eq(false)
|
39
|
+
end
|
40
|
+
|
28
41
|
it 'should echo' do
|
29
42
|
expect(hall.echo(1, kwarg1: :kw1)).to be == { "args1" => 1, "kwarg1" => "kw1", "kwarg2" => "kwarg2" }
|
30
43
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ia-redis-rpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Phuong Nguyen
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2024-02-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
@@ -144,9 +144,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
144
144
|
version: '0'
|
145
145
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
146
|
requirements:
|
147
|
-
- - "
|
147
|
+
- - ">="
|
148
148
|
- !ruby/object:Gem::Version
|
149
|
-
version:
|
149
|
+
version: '0'
|
150
150
|
requirements: []
|
151
151
|
rubygems_version: 3.3.3
|
152
152
|
signing_key:
|