hessian2 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +10 -10
  3. data/README.md +197 -197
  4. data/hessian2.gemspec +0 -2
  5. data/lib/hessian2/client.rb +57 -50
  6. data/lib/hessian2/constants.rb +164 -164
  7. data/lib/hessian2/fault.rb +3 -3
  8. data/lib/hessian2/handler.rb +18 -18
  9. data/lib/hessian2/hessian_client.rb +3 -3
  10. data/lib/hessian2/parser.rb +619 -619
  11. data/lib/hessian2/type_wrapper.rb +49 -49
  12. data/lib/hessian2/version.rb +3 -3
  13. data/lib/hessian2/writer.rb +499 -499
  14. data/lib/hessian2.rb +14 -14
  15. data/spec/binary_spec.rb +51 -51
  16. data/spec/boolean_spec.rb +26 -26
  17. data/spec/class_wrapper_spec.rb +52 -52
  18. data/spec/create_monkeys.rb +14 -14
  19. data/spec/date_spec.rb +45 -45
  20. data/spec/double_spec.rb +78 -78
  21. data/spec/int_spec.rb +54 -54
  22. data/spec/list_spec.rb +66 -66
  23. data/spec/long_spec.rb +68 -68
  24. data/spec/map_spec.rb +36 -36
  25. data/spec/null_spec.rb +17 -17
  26. data/spec/object_spec.rb +78 -78
  27. data/spec/ref_spec.rb +43 -43
  28. data/spec/spec_helper.rb +23 -23
  29. data/spec/string_spec.rb +61 -61
  30. data/spec/struct_wrapper_spec.rb +47 -47
  31. data/spec/type_wrapper_spec.rb +102 -102
  32. data/test/app.rb +24 -24
  33. data/test/async/em_http_asleep.rb +25 -25
  34. data/test/async/em_http_sleep.rb +25 -25
  35. data/test/async/monkey.asleep.rb +27 -27
  36. data/test/async/mysql2_aquery.rb +37 -37
  37. data/test/fault/monkey.undefined_method.rb +5 -5
  38. data/test/fault/monkey.wrong_arguments.rb +5 -5
  39. data/test/fiber_concurrency/em_http_asleep.rb +17 -17
  40. data/test/fiber_concurrency/em_http_sleep.rb +17 -17
  41. data/test/fiber_concurrency/monkey.asleep.fiber_aware.rb +18 -18
  42. data/test/fiber_concurrency/mysql2_query.rb +29 -29
  43. data/test/fiber_concurrency/net_http_asleep.rb +19 -19
  44. data/test/fiber_concurrency/net_http_sleep.rb +19 -19
  45. data/test/fibered_rainbows/Gemfile +15 -15
  46. data/test/fibered_rainbows/config.ru +11 -11
  47. data/test/fibered_rainbows/rainbows.rb +13 -13
  48. data/test/monkey_service.rb +16 -16
  49. data/test/prepare.rb +7 -7
  50. data/test/thread_concurrency/active_record_execute.rb +29 -29
  51. data/test/thread_concurrency/monkey.asleep.rb +22 -22
  52. data/test/thread_concurrency/net_http_asleep.rb +24 -24
  53. data/test/thread_concurrency/net_http_sleep.rb +24 -24
  54. data/test/threaded_rainbows/Gemfile +13 -13
  55. data/test/threaded_rainbows/config.ru +9 -9
  56. data/test/threaded_rainbows/rainbows.rb +13 -13
  57. metadata +4 -74
data/spec/ref_spec.rb CHANGED
@@ -1,43 +1,43 @@
1
- require File.expand_path('../spec_helper', __FILE__)
2
-
3
- module Hessian2
4
- describe Writer do
5
- context "when ref" do
6
- hash1 = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
7
-
8
- it "should write reference to map/list/object - integer ('Q') ::= x51 int" do
9
- monkey1 = Monkey.new(hash1)
10
- monkey2 = Hessian2::ClassWrapper.new("com.sun.java.Monkey", hash1)
11
- monkey3 = Hessian2::TypeWrapper.new("com.sun.java.Monkey", hash1)
12
- monkeys1 = [ monkey1, monkey2, monkey3 ]
13
- monkeys2 = Hessian2::ClassWrapper.new("[com.sun.java.Monkey", monkeys1)
14
- monkeys3 = Hessian2::TypeWrapper.new("[com.sun.java.Monkey", monkeys1)
15
-
16
- arr = [ hash1, monkey1, monkey2, monkey3, monkeys1, monkeys2, monkeys3 ] * 2
17
-
18
- bin = Hessian2.write(arr)
19
-
20
- _hash1, _monkey1, _monkey2, _monkey3, _monkeys1, _monkeys2, _monkeys3, \
21
- _hash1r, _monkey1r, _monkey2r, _monkey3r, _monkeys1r, _monkeys2r, _monkeys3r = Hessian2.parse(bin, nil, symbolize_keys: true)
22
-
23
- [ _hash1, _hash1r, _monkey3, _monkey3r ].each do |_hash|
24
- expect([ _hash[:born_at], _hash[:name], _hash[:price] ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
25
- end
26
-
27
- [ _monkey1, _monkey2, _monkey1r, _monkey2r ].each do |_monkey|
28
- expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
29
- end
30
-
31
- [ _monkeys1, _monkeys2, _monkeys3, _monkeys1r, _monkeys2r, _monkeys3r ].each do |_monkeys|
32
- [ _monkeys[0], _monkeys[1] ].each do |_monkey|
33
- expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
34
- end
35
- last = _monkeys.last
36
- expect([ last[:born_at], last[:name], last[:price] ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
37
- end
38
-
39
- end
40
-
41
- end
42
- end
43
- end
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ module Hessian2
4
+ describe Writer do
5
+ context "when ref" do
6
+ hash1 = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
7
+
8
+ it "should write reference to map/list/object - integer ('Q') ::= x51 int" do
9
+ monkey1 = Monkey.new(hash1)
10
+ monkey2 = Hessian2::ClassWrapper.new("com.sun.java.Monkey", hash1)
11
+ monkey3 = Hessian2::TypeWrapper.new("com.sun.java.Monkey", hash1)
12
+ monkeys1 = [ monkey1, monkey2, monkey3 ]
13
+ monkeys2 = Hessian2::ClassWrapper.new("[com.sun.java.Monkey", monkeys1)
14
+ monkeys3 = Hessian2::TypeWrapper.new("[com.sun.java.Monkey", monkeys1)
15
+
16
+ arr = [ hash1, monkey1, monkey2, monkey3, monkeys1, monkeys2, monkeys3 ] * 2
17
+
18
+ bin = Hessian2.write(arr)
19
+
20
+ _hash1, _monkey1, _monkey2, _monkey3, _monkeys1, _monkeys2, _monkeys3, \
21
+ _hash1r, _monkey1r, _monkey2r, _monkey3r, _monkeys1r, _monkeys2r, _monkeys3r = Hessian2.parse(bin, nil, symbolize_keys: true)
22
+
23
+ [ _hash1, _hash1r, _monkey3, _monkey3r ].each do |_hash|
24
+ expect([ _hash[:born_at], _hash[:name], _hash[:price] ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
25
+ end
26
+
27
+ [ _monkey1, _monkey2, _monkey1r, _monkey2r ].each do |_monkey|
28
+ expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
29
+ end
30
+
31
+ [ _monkeys1, _monkeys2, _monkeys3, _monkeys1r, _monkeys2r, _monkeys3r ].each do |_monkeys|
32
+ [ _monkeys[0], _monkeys[1] ].each do |_monkey|
33
+ expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
34
+ end
35
+ last = _monkeys.last
36
+ expect([ last[:born_at], last[:name], last[:price] ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+ end
43
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,23 +1,23 @@
1
- lib_path = File.expand_path('../../lib', __FILE__)
2
- $:.unshift(lib_path)
3
-
4
- require 'hessian2'
5
-
6
- require 'active_record'
7
- require 'mysql2'
8
-
9
- MonkeyStruct = Struct.new(:born_at, :id, :name, :price)
10
-
11
- options = YAML.load_file(File.expand_path('../database.yml', __FILE__))
12
- ActiveRecord::Base.establish_connection(options)
13
- ActiveRecord::Base.default_timezone = :local
14
-
15
- class Monkey < ActiveRecord::Base; end
16
-
17
- class AnotherMonkey
18
- attr_accessor :born_at, :id, :name, :price
19
-
20
- def initialize(attrs = {})
21
- attrs.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
22
- end
23
- end
1
+ lib_path = File.expand_path('../../lib', __FILE__)
2
+ $:.unshift(lib_path)
3
+
4
+ require 'hessian2'
5
+
6
+ require 'active_record'
7
+ require 'mysql2'
8
+
9
+ MonkeyStruct = Struct.new(:born_at, :id, :name, :price)
10
+
11
+ options = YAML.load_file(File.expand_path('../database.yml', __FILE__))
12
+ ActiveRecord::Base.establish_connection(options)
13
+ ActiveRecord::Base.default_timezone = :local
14
+
15
+ class Monkey < ActiveRecord::Base; end
16
+
17
+ class AnotherMonkey
18
+ attr_accessor :born_at, :id, :name, :price
19
+
20
+ def initialize(attrs = {})
21
+ attrs.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
22
+ end
23
+ end
data/spec/string_spec.rb CHANGED
@@ -1,61 +1,61 @@
1
- require File.expand_path('../spec_helper', __FILE__)
2
-
3
- module Hessian2
4
- describe Writer do
5
- context "when string" do
6
-
7
- it "should write utf-8 string length 0-31 ::= [x00-x1f] <utf8-data>" do
8
- (0..31).each do |len|
9
- val = '啦' * len
10
- bin = Hessian2.write(val)
11
-
12
- expect(bin[0].unpack('C').first).to eq(val.size)
13
- expect(bin[1..-1]).to eq(val.b)
14
- expect(Hessian2.parse(bin)).to eq(val)
15
- end
16
- end
17
-
18
-
19
- it "should write utf-8 string length 0-1023 ::= [x30-x33] b0 <utf8-data>" do
20
- [ 33, 256, 512, 1023 ].each do |len|
21
- val = '啦' * len
22
- bin = Hessian2.write(val)
23
-
24
- b1, b0 = bin[0, 2].unpack('CC')
25
- expect(256 * (b1 - 0x30) + b0).to eq(val.size)
26
- expect(bin[2..-1]).to eq(val.b)
27
- expect(Hessian2.parse(bin)).to eq(val)
28
- end
29
- end
30
-
31
- it "should write utf-8 string final chunk ('S') ::= S b1 b0 <utf8-data>" do
32
- val = '啦' * 0x400
33
- bin = Hessian2.write(val)
34
-
35
- expect(bin[0]).to eq('S')
36
- expect(bin[1, 2].unpack('n').first).to eq(0x400)
37
- expect(Hessian2.parse(bin)).to eq(val)
38
- end
39
-
40
-
41
- it "should write utf-8 string non-final chunk ('R') ::= x52 b1 b0 <utf8-data>" do
42
- val = '啦' * 0x10400
43
- bin = Hessian2.write(val)
44
-
45
- chunks = val.size / 0x8000
46
- i = 0
47
-
48
- chunks.times do |c|
49
- expect(bin[i]).to eq('R')
50
- expect(bin[i + 1, 2].unpack('n').first).to eq(0x8000)
51
- i += (3 + val[c * 0x8000, 0x8000].bytesize)
52
- end
53
-
54
- expect(bin[i]).to eq('S')
55
- expect(bin[i + 1, 2].unpack('n').first).to eq(val.size - 0x8000 * chunks)
56
- expect(Hessian2.parse(bin)).to eq(val)
57
- end
58
-
59
- end
60
- end
61
- end
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ module Hessian2
4
+ describe Writer do
5
+ context "when string" do
6
+
7
+ it "should write utf-8 string length 0-31 ::= [x00-x1f] <utf8-data>" do
8
+ (0..31).each do |len|
9
+ val = '啦' * len
10
+ bin = Hessian2.write(val)
11
+
12
+ expect(bin[0].unpack('C').first).to eq(val.size)
13
+ expect(bin[1..-1]).to eq(val.b)
14
+ expect(Hessian2.parse(bin)).to eq(val)
15
+ end
16
+ end
17
+
18
+
19
+ it "should write utf-8 string length 0-1023 ::= [x30-x33] b0 <utf8-data>" do
20
+ [ 33, 256, 512, 1023 ].each do |len|
21
+ val = '啦' * len
22
+ bin = Hessian2.write(val)
23
+
24
+ b1, b0 = bin[0, 2].unpack('CC')
25
+ expect(256 * (b1 - 0x30) + b0).to eq(val.size)
26
+ expect(bin[2..-1]).to eq(val.b)
27
+ expect(Hessian2.parse(bin)).to eq(val)
28
+ end
29
+ end
30
+
31
+ it "should write utf-8 string final chunk ('S') ::= S b1 b0 <utf8-data>" do
32
+ val = '啦' * 0x400
33
+ bin = Hessian2.write(val)
34
+
35
+ expect(bin[0]).to eq('S')
36
+ expect(bin[1, 2].unpack('n').first).to eq(0x400)
37
+ expect(Hessian2.parse(bin)).to eq(val)
38
+ end
39
+
40
+
41
+ it "should write utf-8 string non-final chunk ('R') ::= x52 b1 b0 <utf8-data>" do
42
+ val = '啦' * 0x10400
43
+ bin = Hessian2.write(val)
44
+
45
+ chunks = val.size / 0x8000
46
+ i = 0
47
+
48
+ chunks.times do |c|
49
+ expect(bin[i]).to eq('R')
50
+ expect(bin[i + 1, 2].unpack('n').first).to eq(0x8000)
51
+ i += (3 + val[c * 0x8000, 0x8000].bytesize)
52
+ end
53
+
54
+ expect(bin[i]).to eq('S')
55
+ expect(bin[i + 1, 2].unpack('n').first).to eq(val.size - 0x8000 * chunks)
56
+ expect(Hessian2.parse(bin)).to eq(val)
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -1,47 +1,47 @@
1
- require File.expand_path('../spec_helper', __FILE__)
2
-
3
- module Hessian2
4
- describe StructWrapper do
5
- hash = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
6
-
7
- # it "should raise error" do
8
- # expect(lambda{ Hessian2::StructWrapper.new(MonkeyStruct) }).to raise_error
9
- # end
10
-
11
-
12
- # it "should wrap nil" do
13
- # bin = Hessian2.write(Hessian2::StructWrapper.new(MonkeyStruct, nil))
14
-
15
- # _monkey = Hessian2.parse(bin, MonkeyStruct)
16
- # expect(_monkey).to eq(nil)
17
- # end
18
-
19
-
20
- # it "should wrap hash, monkey, another monkey" do
21
- # [ MonkeyStruct, 'MonkeyStruct' ].each do |klass|
22
- # [ hash, Monkey.new(hash), AnotherMonkey.new(hash) ].each do |val|
23
- # bin = Hessian2.write(Hessian2::StructWrapper.new(klass, val))
24
-
25
- # _monkey = Hessian2.parse(bin, klass)
26
- # expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
27
- # end
28
- # end
29
- # end
30
-
31
-
32
- it "should wrap array" do
33
- [ [MonkeyStruct], '[MonkeyStruct' ].each do |klass|
34
- arr = [ nil, hash, Monkey.new(hash), AnotherMonkey.new(hash) ]
35
- bin = Hessian2.write(Hessian2::StructWrapper.new(klass, arr))
36
-
37
- _monkey1, _monkey2, _monkey3, _monkey4 = Hessian2.parse(bin, klass, symbolize_keys: true)
38
- expect(_monkey1).to eq(nil)
39
- expect([ _monkey2[:born_at], _monkey2[:name], _monkey2[:price] ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
40
- [ _monkey3, _monkey4 ].each do |_monkey|
41
- expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
42
- end
43
- end
44
- end
45
-
46
- end
47
- end
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ module Hessian2
4
+ describe StructWrapper do
5
+ hash = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
6
+
7
+ # it "should raise error" do
8
+ # expect(lambda{ Hessian2::StructWrapper.new(MonkeyStruct) }).to raise_error
9
+ # end
10
+
11
+
12
+ # it "should wrap nil" do
13
+ # bin = Hessian2.write(Hessian2::StructWrapper.new(MonkeyStruct, nil))
14
+
15
+ # _monkey = Hessian2.parse(bin, MonkeyStruct)
16
+ # expect(_monkey).to eq(nil)
17
+ # end
18
+
19
+
20
+ # it "should wrap hash, monkey, another monkey" do
21
+ # [ MonkeyStruct, 'MonkeyStruct' ].each do |klass|
22
+ # [ hash, Monkey.new(hash), AnotherMonkey.new(hash) ].each do |val|
23
+ # bin = Hessian2.write(Hessian2::StructWrapper.new(klass, val))
24
+
25
+ # _monkey = Hessian2.parse(bin, klass)
26
+ # expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
27
+ # end
28
+ # end
29
+ # end
30
+
31
+
32
+ it "should wrap array" do
33
+ [ [MonkeyStruct], '[MonkeyStruct' ].each do |klass|
34
+ arr = [ nil, hash, Monkey.new(hash), AnotherMonkey.new(hash) ]
35
+ bin = Hessian2.write(Hessian2::StructWrapper.new(klass, arr))
36
+
37
+ _monkey1, _monkey2, _monkey3, _monkey4 = Hessian2.parse(bin, klass, symbolize_keys: true)
38
+ expect(_monkey1).to eq(nil)
39
+ expect([ _monkey2[:born_at], _monkey2[:name], _monkey2[:price] ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
40
+ [ _monkey3, _monkey4 ].each do |_monkey|
41
+ expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -1,102 +1,102 @@
1
- require File.expand_path('../spec_helper', __FILE__)
2
-
3
- module Hessian2
4
- describe TypeWrapper do
5
- hash = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
6
-
7
- it "should raise error" do
8
- expect(lambda{ Hessian2::TypeWrapper.new(:unknown) }).to raise_error
9
- end
10
-
11
-
12
- it "should wrap nil" do
13
- bin = Hessian2.write(Hessian2::TypeWrapper.new(:unknown, nil))
14
-
15
- expect(bin).to eq('N')
16
- expect(Hessian2.parse(bin)).to eq(nil)
17
- end
18
-
19
-
20
- it "should wrap long" do
21
- [ 59, '59' ].each do |val|
22
- [ 'L', 'l', 'Long', 'long', :long ].each do |type|
23
- bin = Hessian2.write(Hessian2::TypeWrapper.new(type, val))
24
-
25
- b1, b0 = bin[0, 2].unpack('CC')
26
- expect(((b1 - 0xf8) << 8) + b0).to eq(Integer(val))
27
- expect(Hessian2.parse(bin)).to eq(Integer(val))
28
- end
29
- end
30
- end
31
-
32
-
33
- it "should wrap int" do
34
- [ 0x7f_fff_fff, '0x7f_fff_fff' ].each do |val|
35
- [ 'I', 'i', 'Integer', 'int', :int ].each do |type|
36
- bin = Hessian2.write(Hessian2::TypeWrapper.new(type, val))
37
-
38
- expect(bin[0]).to eq('I')
39
- expect(bin[1, 4].unpack('l>').first).to eq(Integer(val))
40
- expect(Hessian2.parse(bin)).to eq(Integer(val))
41
- end
42
- end
43
- end
44
-
45
-
46
- it "should wrap binary" do
47
- val = 'b' * 59
48
- [ 'B', 'b', 'Binary', 'bin', :bin ].each do |type|
49
- bin = Hessian2.write(Hessian2::TypeWrapper.new(type, val))
50
-
51
- b1, b0 = bin[0, 2].unpack('CC')
52
- expect(256 * (b1 - 0x34) + b0).to eq(val.size)
53
- expect(bin.size).to eq(2 + val.size)
54
- expect(Hessian2.parse(bin)).to eq(val)
55
- end
56
- end
57
-
58
-
59
- it "should wrap monkey" do
60
- [ Monkey.new(hash), AnotherMonkey.new(hash) ].each do |val|
61
- bin = Hessian2.write(Hessian2::TypeWrapper.new('com.sun.java.Monkey', val))
62
-
63
- _hash = Hessian2.parse(bin, nil, symbolize_keys: true)
64
- expect([ _hash[:born_at], _hash[:name], _hash[:price] ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
65
- end
66
- end
67
-
68
-
69
- it "should wrap long array" do
70
- bin = Hessian2.write(Hessian2::TypeWrapper.new('[long', [ 59, 69, 79, 89, 99 ]))
71
-
72
- expect(Hessian2.parse(bin)).to eq([ 59, 69, 79, 89, 99 ])
73
- end
74
-
75
-
76
- it "should wrap int array" do
77
- bin = Hessian2.write(Hessian2::TypeWrapper.new('[int', [ 0x7f_fff_ffb, 0x7f_fff_ffc, 0x7f_fff_ffd, 0x7f_fff_ffe, 0x7f_fff_fff ]))
78
-
79
- expect(Hessian2.parse(bin)).to eq([ 0x7f_fff_ffb, 0x7f_fff_ffc, 0x7f_fff_ffd, 0x7f_fff_ffe, 0x7f_fff_fff ])
80
- end
81
-
82
-
83
- it "should wrap binary array" do
84
- bin = Hessian2.write(Hessian2::TypeWrapper.new('[bin', [ 'b' * 59, 'b' * 69, 'b' * 79, 'b' * 89, 'b' * 99 ]))
85
-
86
- expect(Hessian2.parse(bin)).to eq([ 'b' * 59, 'b' * 69, 'b' * 79, 'b' * 89, 'b' * 99 ])
87
- end
88
-
89
-
90
- it "should wrap monkey array" do
91
- bin = Hessian2.write(Hessian2::TypeWrapper.new('[com.sun.java.Monkey', [ nil, Monkey.new(hash), AnotherMonkey.new(hash) ]))
92
-
93
- _hash1, _hash2, _hash3 = Hessian2.parse(bin, nil, symbolize_keys: true)
94
- expect(_hash1).to eq(nil)
95
- [ _hash2, _hash3 ].each do |_hash|
96
- expect([ _hash[:born_at], _hash[:name], _hash[:price] ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
97
- end
98
- end
99
-
100
-
101
- end
102
- end
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ module Hessian2
4
+ describe TypeWrapper do
5
+ hash = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
6
+
7
+ it "should raise error" do
8
+ expect(lambda{ Hessian2::TypeWrapper.new(:unknown) }).to raise_error
9
+ end
10
+
11
+
12
+ it "should wrap nil" do
13
+ bin = Hessian2.write(Hessian2::TypeWrapper.new(:unknown, nil))
14
+
15
+ expect(bin).to eq('N')
16
+ expect(Hessian2.parse(bin)).to eq(nil)
17
+ end
18
+
19
+
20
+ it "should wrap long" do
21
+ [ 59, '59' ].each do |val|
22
+ [ 'L', 'l', 'Long', 'long', :long ].each do |type|
23
+ bin = Hessian2.write(Hessian2::TypeWrapper.new(type, val))
24
+
25
+ b1, b0 = bin[0, 2].unpack('CC')
26
+ expect(((b1 - 0xf8) << 8) + b0).to eq(Integer(val))
27
+ expect(Hessian2.parse(bin)).to eq(Integer(val))
28
+ end
29
+ end
30
+ end
31
+
32
+
33
+ it "should wrap int" do
34
+ [ 0x7f_fff_fff, '0x7f_fff_fff' ].each do |val|
35
+ [ 'I', 'i', 'Integer', 'int', :int ].each do |type|
36
+ bin = Hessian2.write(Hessian2::TypeWrapper.new(type, val))
37
+
38
+ expect(bin[0]).to eq('I')
39
+ expect(bin[1, 4].unpack('l>').first).to eq(Integer(val))
40
+ expect(Hessian2.parse(bin)).to eq(Integer(val))
41
+ end
42
+ end
43
+ end
44
+
45
+
46
+ it "should wrap binary" do
47
+ val = 'b' * 59
48
+ [ 'B', 'b', 'Binary', 'bin', :bin ].each do |type|
49
+ bin = Hessian2.write(Hessian2::TypeWrapper.new(type, val))
50
+
51
+ b1, b0 = bin[0, 2].unpack('CC')
52
+ expect(256 * (b1 - 0x34) + b0).to eq(val.size)
53
+ expect(bin.size).to eq(2 + val.size)
54
+ expect(Hessian2.parse(bin)).to eq(val)
55
+ end
56
+ end
57
+
58
+
59
+ it "should wrap monkey" do
60
+ [ Monkey.new(hash), AnotherMonkey.new(hash) ].each do |val|
61
+ bin = Hessian2.write(Hessian2::TypeWrapper.new('com.sun.java.Monkey', val))
62
+
63
+ _hash = Hessian2.parse(bin, nil, symbolize_keys: true)
64
+ expect([ _hash[:born_at], _hash[:name], _hash[:price] ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
65
+ end
66
+ end
67
+
68
+
69
+ it "should wrap long array" do
70
+ bin = Hessian2.write(Hessian2::TypeWrapper.new('[long', [ 59, 69, 79, 89, 99 ]))
71
+
72
+ expect(Hessian2.parse(bin)).to eq([ 59, 69, 79, 89, 99 ])
73
+ end
74
+
75
+
76
+ it "should wrap int array" do
77
+ bin = Hessian2.write(Hessian2::TypeWrapper.new('[int', [ 0x7f_fff_ffb, 0x7f_fff_ffc, 0x7f_fff_ffd, 0x7f_fff_ffe, 0x7f_fff_fff ]))
78
+
79
+ expect(Hessian2.parse(bin)).to eq([ 0x7f_fff_ffb, 0x7f_fff_ffc, 0x7f_fff_ffd, 0x7f_fff_ffe, 0x7f_fff_fff ])
80
+ end
81
+
82
+
83
+ it "should wrap binary array" do
84
+ bin = Hessian2.write(Hessian2::TypeWrapper.new('[bin', [ 'b' * 59, 'b' * 69, 'b' * 79, 'b' * 89, 'b' * 99 ]))
85
+
86
+ expect(Hessian2.parse(bin)).to eq([ 'b' * 59, 'b' * 69, 'b' * 79, 'b' * 89, 'b' * 99 ])
87
+ end
88
+
89
+
90
+ it "should wrap monkey array" do
91
+ bin = Hessian2.write(Hessian2::TypeWrapper.new('[com.sun.java.Monkey', [ nil, Monkey.new(hash), AnotherMonkey.new(hash) ]))
92
+
93
+ _hash1, _hash2, _hash3 = Hessian2.parse(bin, nil, symbolize_keys: true)
94
+ expect(_hash1).to eq(nil)
95
+ [ _hash2, _hash3 ].each do |_hash|
96
+ expect([ _hash[:born_at], _hash[:name], _hash[:price] ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
97
+ end
98
+ end
99
+
100
+
101
+ end
102
+ end
data/test/app.rb CHANGED
@@ -1,24 +1,24 @@
1
- require File.expand_path('../monkey_service', __FILE__)
2
-
3
- set :logging, false
4
-
5
- get '/' do
6
- status 405
7
- 'post me'
8
- end
9
-
10
- post '/' do
11
- MonkeyService.handle(request.body.read)
12
- end
13
-
14
- route :get, :post, '/sleep' do
15
- sleep 1
16
-
17
- 'wake'
18
- end
19
-
20
- route :get, :post, '/asleep' do
21
- EM::Synchrony.sleep(1)
22
-
23
- 'awake'
24
- end
1
+ require File.expand_path('../monkey_service', __FILE__)
2
+
3
+ set :logging, false
4
+
5
+ get '/' do
6
+ status 405
7
+ 'post me'
8
+ end
9
+
10
+ post '/' do
11
+ MonkeyService.handle(request.body.read)
12
+ end
13
+
14
+ route :get, :post, '/sleep' do
15
+ sleep 1
16
+
17
+ 'wake'
18
+ end
19
+
20
+ route :get, :post, '/asleep' do
21
+ EM::Synchrony.sleep(1)
22
+
23
+ 'awake'
24
+ end
@@ -1,25 +1,25 @@
1
- require File.expand_path('../../prepare', __FILE__)
2
- require 'eventmachine'
3
- require 'em-synchrony/em-http'
4
-
5
- EM.run do
6
- @number_of.times do |i|
7
- puts i
8
- http = EM::HttpRequest.new("http://127.0.0.1:8080/asleep").apost
9
- http.callback do |r|
10
- puts 'callback'
11
- @results << r.response
12
- EM.stop if @results.size >= @number_of
13
- end
14
-
15
- http.errback do |r|
16
- puts "errback #{r.error}"
17
- EM.stop
18
- end
19
- end
20
-
21
- puts "results.size #{@results.size}"
22
- end
23
-
24
- puts @results.inspect
25
- puts "results.size #{@results.size}"
1
+ require File.expand_path('../../prepare', __FILE__)
2
+ require 'eventmachine'
3
+ require 'em-synchrony/em-http'
4
+
5
+ EM.run do
6
+ @number_of.times do |i|
7
+ puts i
8
+ http = EM::HttpRequest.new("http://127.0.0.1:8080/asleep").apost
9
+ http.callback do |r|
10
+ puts 'callback'
11
+ @results << r.response
12
+ EM.stop if @results.size >= @number_of
13
+ end
14
+
15
+ http.errback do |r|
16
+ puts "errback #{r.error}"
17
+ EM.stop
18
+ end
19
+ end
20
+
21
+ puts "results.size #{@results.size}"
22
+ end
23
+
24
+ puts @results.inspect
25
+ puts "results.size #{@results.size}"