mojombo-bertrpc 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/VERSION +1 -1
- data/bertrpc.gemspec +13 -8
- data/lib/bertrpc/call.rb +33 -0
- data/lib/bertrpc/call_proxy.rb +11 -0
- data/lib/bertrpc/{fun.rb → encodes.rb} +1 -29
- data/lib/bertrpc/mod.rb +4 -2
- data/lib/bertrpc/service.rb +2 -2
- data/lib/bertrpc.rb +3 -1
- data/test/call_proxy_test.rb +24 -0
- data/test/call_test.rb +85 -0
- data/test/encodes_test.rb +80 -0
- data/test/mod_test.rb +12 -6
- data/test/service_test.rb +2 -2
- data/test/test_helper.rb +3 -2
- metadata +15 -8
- data/test/fun_test.rb +0 -148
data/README.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/bertrpc.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{bertrpc}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.2.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Tom Preston-Werner"]
|
9
|
-
s.date = %q{2009-
|
9
|
+
s.date = %q{2009-08-13}
|
10
10
|
s.email = %q{tom@mojombo.com}
|
11
11
|
s.extra_rdoc_files = [
|
12
12
|
"LICENSE",
|
@@ -21,23 +21,28 @@ Gem::Specification.new do |s|
|
|
21
21
|
"VERSION",
|
22
22
|
"bertrpc.gemspec",
|
23
23
|
"lib/bertrpc.rb",
|
24
|
+
"lib/bertrpc/call.rb",
|
25
|
+
"lib/bertrpc/call_proxy.rb",
|
26
|
+
"lib/bertrpc/encodes.rb",
|
24
27
|
"lib/bertrpc/errors.rb",
|
25
|
-
"lib/bertrpc/fun.rb",
|
26
28
|
"lib/bertrpc/mod.rb",
|
27
29
|
"lib/bertrpc/service.rb",
|
28
|
-
"test/
|
30
|
+
"test/call_proxy_test.rb",
|
31
|
+
"test/call_test.rb",
|
32
|
+
"test/encodes_test.rb",
|
29
33
|
"test/mod_test.rb",
|
30
34
|
"test/service_test.rb",
|
31
35
|
"test/test_helper.rb"
|
32
36
|
]
|
33
|
-
s.has_rdoc = true
|
34
37
|
s.homepage = %q{http://github.com/mojombo/bertrpc}
|
35
38
|
s.rdoc_options = ["--charset=UTF-8"]
|
36
39
|
s.require_paths = ["lib"]
|
37
|
-
s.rubygems_version = %q{1.3.
|
40
|
+
s.rubygems_version = %q{1.3.5}
|
38
41
|
s.summary = %q{BERTRPC is a Ruby BERT-RPC client library.}
|
39
42
|
s.test_files = [
|
40
|
-
"test/
|
43
|
+
"test/call_proxy_test.rb",
|
44
|
+
"test/call_test.rb",
|
45
|
+
"test/encodes_test.rb",
|
41
46
|
"test/mod_test.rb",
|
42
47
|
"test/service_test.rb",
|
43
48
|
"test/test_helper.rb"
|
@@ -45,7 +50,7 @@ Gem::Specification.new do |s|
|
|
45
50
|
|
46
51
|
if s.respond_to? :specification_version then
|
47
52
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
-
s.specification_version =
|
53
|
+
s.specification_version = 3
|
49
54
|
|
50
55
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
56
|
s.add_runtime_dependency(%q<erlectricity>, [">= 1.0.1"])
|
data/lib/bertrpc/call.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module BERTRPC
|
2
|
+
class Call
|
3
|
+
include Encodes
|
4
|
+
|
5
|
+
def initialize(svc, mod, fun, args)
|
6
|
+
@svc = svc
|
7
|
+
@mod = mod
|
8
|
+
@fun = fun
|
9
|
+
@args = args
|
10
|
+
end
|
11
|
+
|
12
|
+
def execute
|
13
|
+
bert_request = encode_ruby_request([:call, @mod, @fun, @args])
|
14
|
+
bert_response = sync_request(bert_request)
|
15
|
+
decode_bert_response(bert_response)
|
16
|
+
end
|
17
|
+
|
18
|
+
#private
|
19
|
+
|
20
|
+
def sync_request(bert_request)
|
21
|
+
sock = TCPSocket.new(@svc.host, @svc.port)
|
22
|
+
sock.write([bert_request.length].pack("N"))
|
23
|
+
sock.write(bert_request)
|
24
|
+
lenheader = sock.read(4)
|
25
|
+
raise ProtocolError.new("Unable to read length header from server.") unless lenheader
|
26
|
+
len = lenheader.unpack('N').first
|
27
|
+
bert_response = sock.read(len)
|
28
|
+
raise ProtocolError.new("Unable to read data from server.") unless bert_response
|
29
|
+
sock.close
|
30
|
+
bert_response
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,33 +1,5 @@
|
|
1
1
|
module BERTRPC
|
2
|
-
|
3
|
-
def initialize(svc, mod, fun)
|
4
|
-
@svc = svc
|
5
|
-
@mod = mod
|
6
|
-
@fun = fun
|
7
|
-
end
|
8
|
-
|
9
|
-
def call(*args)
|
10
|
-
args = [*args]
|
11
|
-
bert_request = encode_ruby_request([:call, @mod, @fun, args])
|
12
|
-
bert_response = sync_request(bert_request)
|
13
|
-
decode_bert_response(bert_response)
|
14
|
-
end
|
15
|
-
|
16
|
-
# private
|
17
|
-
|
18
|
-
def sync_request(bert_request)
|
19
|
-
sock = TCPSocket.new(@svc.host, @svc.port)
|
20
|
-
sock.write([bert_request.length].pack("N"))
|
21
|
-
sock.write(bert_request)
|
22
|
-
lenheader = sock.read(4)
|
23
|
-
raise ProtocolError.new("Unable to read length header from server.") unless lenheader
|
24
|
-
len = lenheader.unpack('N').first
|
25
|
-
bert_response = sock.read(len)
|
26
|
-
raise ProtocolError.new("Unable to read data from server.") unless bert_response
|
27
|
-
sock.close
|
28
|
-
bert_response
|
29
|
-
end
|
30
|
-
|
2
|
+
module Encodes
|
31
3
|
def encode_ruby_request(ruby_request)
|
32
4
|
ruby_payload = convert(ruby_request)
|
33
5
|
Erlectricity::Encoder.encode(ruby_payload)
|
data/lib/bertrpc/mod.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
module BERTRPC
|
2
2
|
class Mod
|
3
|
-
def initialize(svc, mod)
|
3
|
+
def initialize(svc, type, mod)
|
4
4
|
@svc = svc
|
5
|
+
@type = type
|
5
6
|
@mod = mod
|
6
7
|
end
|
7
8
|
|
8
9
|
def method_missing(cmd, *args)
|
9
|
-
|
10
|
+
args = [*args]
|
11
|
+
@type.new(@svc, @mod, cmd, args).execute
|
10
12
|
end
|
11
13
|
end
|
12
14
|
end
|
data/lib/bertrpc/service.rb
CHANGED
data/lib/bertrpc.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CallProxyTest < Test::Unit::TestCase
|
4
|
+
context "A CallProxy" do
|
5
|
+
setup do
|
6
|
+
@svc = BERTRPC::Service.new('localhost', 9941)
|
7
|
+
end
|
8
|
+
|
9
|
+
should "be created with a Service" do
|
10
|
+
assert BERTRPC::CallProxy.new(@svc).is_a?(BERTRPC::CallProxy)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "A CallProxy instance" do
|
15
|
+
setup do
|
16
|
+
svc = BERTRPC::Service.new('localhost', 9941)
|
17
|
+
@cp = BERTRPC::CallProxy.new(@svc)
|
18
|
+
end
|
19
|
+
|
20
|
+
should "return a Mod instance" do
|
21
|
+
assert @cp.myfun.is_a?(BERTRPC::Mod)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/test/call_test.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CallTest < Test::Unit::TestCase
|
4
|
+
context "A Call" do
|
5
|
+
setup do
|
6
|
+
@svc = BERTRPC::Service.new('localhost', 9941)
|
7
|
+
end
|
8
|
+
|
9
|
+
should "be created with a Service, module name, fun name, and args" do
|
10
|
+
assert BERTRPC::Call.new(@svc, :mymod, :myfun, [1, 2]).is_a?(BERTRPC::Call)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "A Call instance" do
|
15
|
+
setup do
|
16
|
+
@svc = BERTRPC::Service.new('localhost', 9941)
|
17
|
+
@enc = Enc.new
|
18
|
+
end
|
19
|
+
|
20
|
+
should "call with single-arity" do
|
21
|
+
req = @enc.encode_ruby_request([:call, :mymod, :myfun, [1]])
|
22
|
+
res = @enc.encode_ruby_request([:reply, 2])
|
23
|
+
call = BERTRPC::Call.new(@svc, :mymod, :myfun, [1])
|
24
|
+
call.expects(:sync_request).with(req).returns(res)
|
25
|
+
assert_equal 2, call.execute
|
26
|
+
end
|
27
|
+
|
28
|
+
should "call with single-arity array" do
|
29
|
+
req = @enc.encode_ruby_request([:call, :mymod, :myfun, [[1, 2, 3]]])
|
30
|
+
res = @enc.encode_ruby_request([:reply, [4, 5, 6]])
|
31
|
+
call = BERTRPC::Call.new(@svc, :mymod, :myfun, [[1, 2, 3]])
|
32
|
+
call.expects(:sync_request).with(req).returns(res)
|
33
|
+
assert_equal [4, 5, 6], call.execute
|
34
|
+
end
|
35
|
+
|
36
|
+
should "call with multi-arity" do
|
37
|
+
req = @enc.encode_ruby_request([:call, :mymod, :myfun, [1, 2, 3]])
|
38
|
+
res = @enc.encode_ruby_request([:reply, [4, 5, 6]])
|
39
|
+
call = BERTRPC::Call.new(@svc, :mymod, :myfun, [1, 2, 3])
|
40
|
+
call.expects(:sync_request).with(req).returns(res)
|
41
|
+
assert_equal [4, 5, 6], call.execute
|
42
|
+
end
|
43
|
+
|
44
|
+
context "sync_request" do
|
45
|
+
setup do
|
46
|
+
@svc = BERTRPC::Service.new('localhost', 9941)
|
47
|
+
@call = BERTRPC::Call.new(@svc, :mymod, :myfun, [])
|
48
|
+
end
|
49
|
+
|
50
|
+
should "read and write BERT-Ps from the socket" do
|
51
|
+
io = stub()
|
52
|
+
io.expects(:write).with("\000\000\000\003")
|
53
|
+
io.expects(:write).with("foo")
|
54
|
+
io.expects(:read).with(4).returns("\000\000\000\003")
|
55
|
+
io.expects(:read).with(3).returns("bar")
|
56
|
+
io.expects(:close)
|
57
|
+
TCPSocket.expects(:new).returns(io)
|
58
|
+
assert_equal "bar", @call.sync_request("foo")
|
59
|
+
end
|
60
|
+
|
61
|
+
should "raise a ProtocolError when the length is invalid" do
|
62
|
+
io = stub()
|
63
|
+
io.expects(:write).with("\000\000\000\003")
|
64
|
+
io.expects(:write).with("foo")
|
65
|
+
io.expects(:read).with(4).returns(nil)
|
66
|
+
TCPSocket.expects(:new).returns(io)
|
67
|
+
assert_raises(BERTRPC::ProtocolError) do
|
68
|
+
@call.sync_request("foo")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
should "raise a ProtocolError when the data is invalid" do
|
73
|
+
io = stub()
|
74
|
+
io.expects(:write).with("\000\000\000\003")
|
75
|
+
io.expects(:write).with("foo")
|
76
|
+
io.expects(:read).with(4).returns("\000\000\000\003")
|
77
|
+
io.expects(:read).with(3).returns(nil)
|
78
|
+
TCPSocket.expects(:new).returns(io)
|
79
|
+
assert_raises(BERTRPC::ProtocolError) do
|
80
|
+
@call.sync_request("foo")
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class EncodesTest < Test::Unit::TestCase
|
4
|
+
context "An Encodes includer" do
|
5
|
+
setup do
|
6
|
+
@enc = Enc.new
|
7
|
+
end
|
8
|
+
|
9
|
+
context "converter" do
|
10
|
+
should "convert top level hashes to BERT-RPC dict form" do
|
11
|
+
assert_equal([:dict, [:foo, 1], [:bar, 2]], @enc.convert({:foo => 1, :bar => 2}))
|
12
|
+
end
|
13
|
+
|
14
|
+
should "convert nested hashes in the same way" do
|
15
|
+
assert_equal([1, 2, [:dict, [:foo, 1]]], @enc.convert([1, 2, {:foo => 1}]))
|
16
|
+
end
|
17
|
+
|
18
|
+
should "keep everything else the same" do
|
19
|
+
assert_equal [1, 2, "foo", :bar, 3.14], @enc.convert([1, 2, "foo", :bar, 3.14])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "deconverter" do
|
24
|
+
should "convert top level BERT-RPC dict forms to hashes" do
|
25
|
+
assert_equal({:foo => 1, :bar => 2}, @enc.deconvert([:dict, [:foo, 1], [:bar, 2]]))
|
26
|
+
end
|
27
|
+
|
28
|
+
should "convert nested dicts in the same way" do
|
29
|
+
assert_equal([1, 2, {:foo => 1}], @enc.deconvert([1, 2, [:dict, [:foo, 1]]]))
|
30
|
+
end
|
31
|
+
|
32
|
+
should "keep everything else the same" do
|
33
|
+
assert_equal [1, 2, "foo", :bar, 3.14], @enc.deconvert([1, 2, "foo", :bar, 3.14])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "ruby request encoder" do
|
38
|
+
should "return BERT-RPC encoded request" do
|
39
|
+
bert = "\203h\004d\000\004calld\000\005mymodd\000\005myfunh\003a\001a\002a\003"
|
40
|
+
assert_equal bert, @enc.encode_ruby_request([:call, :mymod, :myfun, [1, 2, 3]])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "bert response decoder" do
|
45
|
+
should "return response when successful" do
|
46
|
+
req = @enc.encode_ruby_request([:reply, [1, 2, 3]])
|
47
|
+
res = @enc.decode_bert_response(req)
|
48
|
+
assert_equal [1, 2, 3], res
|
49
|
+
end
|
50
|
+
|
51
|
+
should "raise a ProtocolError error when protocol level error is returned" do
|
52
|
+
req = @enc.encode_ruby_request([:error, [:protocol, 1, "invalid"]])
|
53
|
+
assert_raises(BERTRPC::ProtocolError) do
|
54
|
+
@enc.decode_bert_response(req)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
should "raise a ServerError error when server level error is returned" do
|
59
|
+
req = @enc.encode_ruby_request([:error, [:server, 1, "invalid"]])
|
60
|
+
assert_raises(BERTRPC::ServerError) do
|
61
|
+
@enc.decode_bert_response(req)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
should "raise a UserError error when user level error is returned" do
|
66
|
+
req = @enc.encode_ruby_request([:error, [:user, 1, "invalid"]])
|
67
|
+
assert_raises(BERTRPC::UserError) do
|
68
|
+
@enc.decode_bert_response(req)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
should "raise a ProxyError error when proxy level error is returned" do
|
73
|
+
req = @enc.encode_ruby_request([:error, [:proxy, 1, "invalid"]])
|
74
|
+
assert_raises(BERTRPC::ProxyError) do
|
75
|
+
@enc.decode_bert_response(req)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/test/mod_test.rb
CHANGED
@@ -6,19 +6,25 @@ class ModTest < Test::Unit::TestCase
|
|
6
6
|
@svc = BERTRPC::Service.new('localhost', 9941)
|
7
7
|
end
|
8
8
|
|
9
|
-
should "be created with a Service and module name" do
|
10
|
-
assert BERTRPC::Mod.new(@svc, :mymod).is_a?(BERTRPC::Mod)
|
9
|
+
should "be created with a Service and type and module name" do
|
10
|
+
assert BERTRPC::Mod.new(@svc, BERTRPC::Call, :mymod).is_a?(BERTRPC::Mod)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
context "A Mod instance" do
|
15
15
|
setup do
|
16
|
-
svc = BERTRPC::Service.new('localhost', 9941)
|
17
|
-
@mod = BERTRPC::Mod.new(@svc, :mymod)
|
16
|
+
@svc = BERTRPC::Service.new('localhost', 9941)
|
17
|
+
@mod = BERTRPC::Mod.new(@svc, BERTRPC::Call, :mymod)
|
18
18
|
end
|
19
19
|
|
20
|
-
should "
|
21
|
-
|
20
|
+
should "call execute on the type" do
|
21
|
+
m = mock(:execute => nil)
|
22
|
+
BERTRPC::Call.expects(:new).with(@svc, :mymod, :myfun, [1, 2, 3]).returns(m)
|
23
|
+
@mod.myfun(1, 2, 3)
|
24
|
+
|
25
|
+
m = mock(:execute => nil)
|
26
|
+
BERTRPC::Call.expects(:new).with(@svc, :mymod, :myfun, [1]).returns(m)
|
27
|
+
@mod.myfun(1)
|
22
28
|
end
|
23
29
|
end
|
24
30
|
end
|
data/test/service_test.rb
CHANGED
@@ -21,8 +21,8 @@ class ServiceTest < Test::Unit::TestCase
|
|
21
21
|
assert_equal 9941, @svc.port
|
22
22
|
end
|
23
23
|
|
24
|
-
should "return a
|
25
|
-
assert @svc.
|
24
|
+
should "return a Call instance" do
|
25
|
+
assert @svc.call.is_a?(BERTRPC::CallProxy)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mojombo-bertrpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Preston-Werner
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-08-13 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -40,16 +40,21 @@ files:
|
|
40
40
|
- VERSION
|
41
41
|
- bertrpc.gemspec
|
42
42
|
- lib/bertrpc.rb
|
43
|
+
- lib/bertrpc/call.rb
|
44
|
+
- lib/bertrpc/call_proxy.rb
|
45
|
+
- lib/bertrpc/encodes.rb
|
43
46
|
- lib/bertrpc/errors.rb
|
44
|
-
- lib/bertrpc/fun.rb
|
45
47
|
- lib/bertrpc/mod.rb
|
46
48
|
- lib/bertrpc/service.rb
|
47
|
-
- test/
|
49
|
+
- test/call_proxy_test.rb
|
50
|
+
- test/call_test.rb
|
51
|
+
- test/encodes_test.rb
|
48
52
|
- test/mod_test.rb
|
49
53
|
- test/service_test.rb
|
50
54
|
- test/test_helper.rb
|
51
|
-
has_rdoc:
|
55
|
+
has_rdoc: false
|
52
56
|
homepage: http://github.com/mojombo/bertrpc
|
57
|
+
licenses:
|
53
58
|
post_install_message:
|
54
59
|
rdoc_options:
|
55
60
|
- --charset=UTF-8
|
@@ -70,12 +75,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
75
|
requirements: []
|
71
76
|
|
72
77
|
rubyforge_project:
|
73
|
-
rubygems_version: 1.
|
78
|
+
rubygems_version: 1.3.5
|
74
79
|
signing_key:
|
75
|
-
specification_version:
|
80
|
+
specification_version: 3
|
76
81
|
summary: BERTRPC is a Ruby BERT-RPC client library.
|
77
82
|
test_files:
|
78
|
-
- test/
|
83
|
+
- test/call_proxy_test.rb
|
84
|
+
- test/call_test.rb
|
85
|
+
- test/encodes_test.rb
|
79
86
|
- test/mod_test.rb
|
80
87
|
- test/service_test.rb
|
81
88
|
- test/test_helper.rb
|
data/test/fun_test.rb
DELETED
@@ -1,148 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class FunTest < Test::Unit::TestCase
|
4
|
-
context "A Fun" do
|
5
|
-
setup do
|
6
|
-
@svc = BERTRPC::Service.new('localhost', 9941)
|
7
|
-
end
|
8
|
-
|
9
|
-
should "be created with a Service, module name, and fun name" do
|
10
|
-
assert BERTRPC::Fun.new(@svc, :mymod, :myfun).is_a?(BERTRPC::Fun)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
context "A Fun instance" do
|
15
|
-
setup do
|
16
|
-
svc = BERTRPC::Service.new('localhost', 9941)
|
17
|
-
@fun = BERTRPC::Fun.new(svc, :mymod, :myfun)
|
18
|
-
end
|
19
|
-
|
20
|
-
should "call with single-arity" do
|
21
|
-
req = @fun.encode_ruby_request([:call, :mymod, :myfun, [1]])
|
22
|
-
res = @fun.encode_ruby_request([:reply, 2])
|
23
|
-
@fun.expects(:sync_request).with(req).returns(res)
|
24
|
-
assert_equal 2, @fun.call(1)
|
25
|
-
end
|
26
|
-
|
27
|
-
should "call with single-arity array" do
|
28
|
-
req = @fun.encode_ruby_request([:call, :mymod, :myfun, [[1, 2, 3]]])
|
29
|
-
res = @fun.encode_ruby_request([:reply, [4, 5, 6]])
|
30
|
-
@fun.expects(:sync_request).with(req).returns(res)
|
31
|
-
assert_equal [4, 5, 6], @fun.call([1, 2, 3])
|
32
|
-
end
|
33
|
-
|
34
|
-
should "call with multi-arity" do
|
35
|
-
req = @fun.encode_ruby_request([:call, :mymod, :myfun, [1, 2, 3]])
|
36
|
-
res = @fun.encode_ruby_request([:reply, [4, 5, 6]])
|
37
|
-
@fun.expects(:sync_request).with(req).returns(res)
|
38
|
-
assert_equal [4, 5, 6], @fun.call(1, 2, 3)
|
39
|
-
end
|
40
|
-
|
41
|
-
context "converter" do
|
42
|
-
should "convert top level hashes to BERT-RPC dict form" do
|
43
|
-
assert_equal([:dict, [:foo, 1], [:bar, 2]], @fun.convert({:foo => 1, :bar => 2}))
|
44
|
-
end
|
45
|
-
|
46
|
-
should "convert nested hashes in the same way" do
|
47
|
-
assert_equal([1, 2, [:dict, [:foo, 1]]], @fun.convert([1, 2, {:foo => 1}]))
|
48
|
-
end
|
49
|
-
|
50
|
-
should "keep everything else the same" do
|
51
|
-
assert_equal [1, 2, "foo", :bar, 3.14], @fun.convert([1, 2, "foo", :bar, 3.14])
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
context "deconverter" do
|
56
|
-
should "convert top level BERT-RPC dict forms to hashes" do
|
57
|
-
assert_equal({:foo => 1, :bar => 2}, @fun.deconvert([:dict, [:foo, 1], [:bar, 2]]))
|
58
|
-
end
|
59
|
-
|
60
|
-
should "convert nested dicts in the same way" do
|
61
|
-
assert_equal([1, 2, {:foo => 1}], @fun.deconvert([1, 2, [:dict, [:foo, 1]]]))
|
62
|
-
end
|
63
|
-
|
64
|
-
should "keep everything else the same" do
|
65
|
-
assert_equal [1, 2, "foo", :bar, 3.14], @fun.deconvert([1, 2, "foo", :bar, 3.14])
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
context "sync_request" do
|
70
|
-
should "read and write BERT-Ps from the socket" do
|
71
|
-
io = stub()
|
72
|
-
io.expects(:write).with("\000\000\000\003")
|
73
|
-
io.expects(:write).with("foo")
|
74
|
-
io.expects(:read).with(4).returns("\000\000\000\003")
|
75
|
-
io.expects(:read).with(3).returns("bar")
|
76
|
-
io.expects(:close)
|
77
|
-
TCPSocket.expects(:new).returns(io)
|
78
|
-
assert_equal "bar", @fun.sync_request("foo")
|
79
|
-
end
|
80
|
-
|
81
|
-
should "raise a ProxyError when the length is invalid" do
|
82
|
-
io = stub()
|
83
|
-
io.expects(:write).with("\000\000\000\003")
|
84
|
-
io.expects(:write).with("foo")
|
85
|
-
io.expects(:read).with(4).returns(nil)
|
86
|
-
TCPSocket.expects(:new).returns(io)
|
87
|
-
assert_raises(BERTRPC::ProtocolError) do
|
88
|
-
@fun.sync_request("foo")
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
should "raise a ProxyError when the data is invalid" do
|
93
|
-
io = stub()
|
94
|
-
io.expects(:write).with("\000\000\000\003")
|
95
|
-
io.expects(:write).with("foo")
|
96
|
-
io.expects(:read).with(4).returns("\000\000\000\003")
|
97
|
-
io.expects(:read).with(3).returns(nil)
|
98
|
-
TCPSocket.expects(:new).returns(io)
|
99
|
-
assert_raises(BERTRPC::ProtocolError) do
|
100
|
-
@fun.sync_request("foo")
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
context "ruby request encoder" do
|
106
|
-
should "return BERT-RPC encoded request" do
|
107
|
-
bert = "\203h\004d\000\004calld\000\005mymodd\000\005myfunh\003a\001a\002a\003"
|
108
|
-
assert_equal bert, @fun.encode_ruby_request([:call, :mymod, :myfun, [1, 2, 3]])
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
context "bert response decoder" do
|
113
|
-
should "return response when successful" do
|
114
|
-
req = @fun.encode_ruby_request([:reply, [1, 2, 3]])
|
115
|
-
res = @fun.decode_bert_response(req)
|
116
|
-
assert_equal [1, 2, 3], res
|
117
|
-
end
|
118
|
-
|
119
|
-
should "raise a ProtocolError error when protocol level error is returned" do
|
120
|
-
req = @fun.encode_ruby_request([:error, [:protocol, 1, "invalid"]])
|
121
|
-
assert_raises(BERTRPC::ProtocolError) do
|
122
|
-
@fun.decode_bert_response(req)
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
should "raise a ServerError error when server level error is returned" do
|
127
|
-
req = @fun.encode_ruby_request([:error, [:server, 1, "invalid"]])
|
128
|
-
assert_raises(BERTRPC::ServerError) do
|
129
|
-
@fun.decode_bert_response(req)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
should "raise a UserError error when user level error is returned" do
|
134
|
-
req = @fun.encode_ruby_request([:error, [:user, 1, "invalid"]])
|
135
|
-
assert_raises(BERTRPC::UserError) do
|
136
|
-
@fun.decode_bert_response(req)
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
should "raise a ProxyError error when proxy level error is returned" do
|
141
|
-
req = @fun.encode_ruby_request([:error, [:proxy, 1, "invalid"]])
|
142
|
-
assert_raises(BERTRPC::ProxyError) do
|
143
|
-
@fun.decode_bert_response(req)
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|