asir 0.2.0 → 1.0.1

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.
Files changed (82) hide show
  1. data/Gemfile +1 -2
  2. data/README.textile +4 -2
  3. data/VERSION +1 -1
  4. data/asir.gemspec +1 -4
  5. data/asir.riterate.yml +1 -0
  6. data/bin/asir +2 -1
  7. data/example/asir_control.sh +63 -1
  8. data/example/asir_control_client_http.rb +2 -2
  9. data/example/asir_control_client_resque.rb +16 -0
  10. data/example/asir_control_client_zmq.rb +3 -3
  11. data/example/config/asir_config.rb +20 -8
  12. data/example/ex02.rb +1 -1
  13. data/example/ex03.rb +2 -2
  14. data/example/ex04.rb +2 -2
  15. data/example/ex05.rb +1 -1
  16. data/example/ex06.rb +6 -5
  17. data/example/ex07.rb +2 -2
  18. data/example/ex08.rb +2 -2
  19. data/example/ex09.rb +2 -2
  20. data/example/ex10.rb +2 -2
  21. data/example/ex11.rb +5 -5
  22. data/example/ex12.rb +6 -6
  23. data/example/ex13.rb +4 -4
  24. data/example/ex14.rb +4 -4
  25. data/example/ex15.rb +2 -2
  26. data/example/ex16.rb +8 -8
  27. data/example/ex17.rb +12 -11
  28. data/example/ex18.rb +5 -5
  29. data/example/ex19.rb +3 -3
  30. data/example/ex20.rb +3 -3
  31. data/example/ex21.rb +3 -3
  32. data/example/ex22.rb +1 -1
  33. data/example/ex23.rb +2 -2
  34. data/example/ex24.rb +4 -4
  35. data/example/ex25.rb +41 -0
  36. data/example/example_helper.rb +38 -3
  37. data/example/sample_service.rb +4 -4
  38. data/hack_night/exercise/prob-3.rb +3 -3
  39. data/hack_night/exercise/prob-6.rb +2 -2
  40. data/hack_night/exercise/prob-7.rb +2 -2
  41. data/hack_night/solution/prob-2.rb +2 -2
  42. data/hack_night/solution/prob-3.rb +3 -3
  43. data/hack_night/solution/prob-6.rb +7 -6
  44. data/hack_night/solution/prob-7.rb +6 -6
  45. data/{spec → lab}/const_get_speed_spec.rb +0 -0
  46. data/lib/asir.rb +29 -7
  47. data/lib/asir/additional_data.rb +25 -0
  48. data/lib/asir/channel.rb +4 -5
  49. data/lib/asir/client.rb +29 -13
  50. data/lib/asir/config.rb +8 -0
  51. data/lib/asir/description.rb +34 -0
  52. data/lib/asir/environment.rb +96 -0
  53. data/lib/asir/error.rb +4 -1
  54. data/lib/asir/invoker.rb +14 -0
  55. data/lib/asir/main.rb +84 -103
  56. data/lib/asir/message.rb +1 -1
  57. data/lib/asir/poll_throttle.rb +53 -0
  58. data/lib/asir/retry_behavior.rb +1 -1
  59. data/lib/asir/thread_variable.rb +183 -0
  60. data/lib/asir/transport.rb +36 -23
  61. data/lib/asir/transport/beanstalk.rb +18 -52
  62. data/lib/asir/transport/conduit.rb +42 -0
  63. data/lib/asir/transport/connection_oriented.rb +32 -56
  64. data/lib/asir/transport/delegation.rb +5 -5
  65. data/lib/asir/transport/demux.rb +33 -0
  66. data/lib/asir/transport/file.rb +5 -3
  67. data/lib/asir/transport/payload_io.rb +8 -4
  68. data/lib/asir/transport/resque.rb +212 -0
  69. data/lib/asir/transport/stream.rb +19 -9
  70. data/lib/asir/transport/tcp_socket.rb +3 -2
  71. data/lib/asir/transport/zmq.rb +14 -17
  72. data/lib/asir/uri_config.rb +51 -0
  73. data/lib/asir/version.rb +1 -1
  74. data/spec/client_spec.rb +48 -0
  75. data/spec/demux_spec.rb +38 -0
  76. data/spec/json_spec.rb +0 -2
  77. data/spec/message_spec.rb +68 -0
  78. data/spec/performance_spec.rb +66 -0
  79. data/spec/spec_helper.rb +34 -0
  80. data/spec/thread_variable_spec.rb +135 -0
  81. data/spec/transport_spec.rb +82 -0
  82. metadata +28 -4
@@ -0,0 +1,38 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ require 'asir'
4
+ require 'asir/transport/demux'
5
+
6
+ require 'asir/transport/buffer'
7
+
8
+ describe "ASIR::Transport::Demux" do
9
+ attr_accessor :transport, :object
10
+
11
+ before(:each) do
12
+ self.transport = ASIR::Transport::Demux.new
13
+ self.transport.transport_proc = lambda do | t, m |
14
+ m.arguments[0].size % 2 == 0 ? t[:even] : t[:odd]
15
+ end
16
+ self.transport[:even] = ASIR::Transport::Buffer.new(:transport => ASIR::Transport::Local.new)
17
+ self.transport[:even].pause!
18
+ self.transport[:odd] = ASIR::Transport::Buffer.new(:transport => ASIR::Transport::Local.new)
19
+ self.transport[:odd].pause!
20
+ self.object = ASIR::Test::TestObject.new(self)
21
+ object.class.asir.transport = transport
22
+ end
23
+
24
+ it 'should direct even-sized arg[0] Arrays to transport[:even].' do
25
+ result = object.asir.return_argument [ 1, 2 ]
26
+ transport[:even].size.should == 1
27
+ transport[:odd].size.should == 0
28
+ result.should == nil
29
+ end
30
+
31
+ it 'should direct odd-sized arg[0] Arrays to transport[:odd].' do
32
+ result = object.asir.return_argument [ 1, 2, 3 ]
33
+ transport[:even].size.should == 0
34
+ transport[:odd].size.should == 1
35
+ result.should == nil
36
+ end
37
+ end
38
+
data/spec/json_spec.rb CHANGED
@@ -2,7 +2,6 @@ require File.expand_path('../spec_helper', __FILE__)
2
2
  require 'asir/coder/json'
3
3
 
4
4
  describe "ASIR::Coder::JSON" do
5
- # unless RUBY_PLATFORM =~ /java/
6
5
  before(:each) do
7
6
  @enc = ASIR::Coder::JSON.new
8
7
  @dec = @enc.dup
@@ -123,6 +122,5 @@ FIXME:
123
122
  y.instance_variables.sort { | a, b | a.to_s <=> b.to_s }
124
123
  =end
125
124
  end
126
- #end
127
125
  end
128
126
 
@@ -0,0 +1,68 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ $:.unshift File.expand_path('../../example', __FILE__)
4
+
5
+ require 'asir'
6
+
7
+ describe "ASIR::Message" do
8
+ attr_accessor :message, :data, :object
9
+
10
+ before(:each) do
11
+ self.data = { }
12
+ self.object = ASIR::Test::TestObject.new(self)
13
+ self.message = ASIR::Message.new(object, nil, nil, nil, nil)
14
+ end
15
+
16
+ it 'should return result.' do
17
+ message.receiver.should == object
18
+ message.selector.should == nil
19
+ message.arguments.should == nil
20
+ message.block.should == nil
21
+ message.selector = :return_argument
22
+ message.arguments = [ :this_value ]
23
+ result = message.invoke!
24
+ object.arg.should == :this_value
25
+ result.class.should == ASIR::Result
26
+ result.result.should == :this_value
27
+ result.message.should == message
28
+ result.exception.should == nil
29
+ end
30
+
31
+ it 'should capture exceptions.' do
32
+ cls = ::ASIR::Test::TestError
33
+ msg = "This message".freeze
34
+ message.selector = :raise_exception!
35
+ message.arguments = [ cls, msg ]
36
+ result = message.invoke!
37
+ object.cls.should == cls
38
+ object.msg.should == msg
39
+ result.class.should == ASIR::Result
40
+ result.result.should == nil
41
+ result.message.should == message
42
+ exc = result.exception
43
+ exc.class.should == ASIR::EncapsulatedException
44
+ exc.exception_class.should == cls.name
45
+ exc.exception_message.should == msg
46
+ exc.exception_backtrace.class.should == Array
47
+ end
48
+
49
+ it 'should capture Unforwardable exceptions.' do
50
+ cls = ::ASIR::Error::Unforwardable.unforwardable.first
51
+ cls.should_not == nil
52
+ msg = "This message".freeze
53
+ message.selector = :raise_exception!
54
+ message.arguments = [ cls, msg ]
55
+ result = message.invoke!
56
+ object.cls.should == cls
57
+ object.msg.should == msg
58
+ result.class.should == ASIR::Result
59
+ result.result.should == nil
60
+ result.message.should == message
61
+ exc = result.exception
62
+ exc.class.should == ASIR::EncapsulatedException
63
+ exc.exception_class.should == 'ASIR::Error::Unforwardable'
64
+ exc.exception_message.should == "#{cls.name} #{msg}"
65
+ exc.exception_backtrace.class.should == Array
66
+ end
67
+ end
68
+
@@ -0,0 +1,66 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ require 'asir'
4
+
5
+ describe "ASIR Performance" do
6
+ attr_accessor :transport, :data, :object
7
+ attr_accessor :n
8
+
9
+ before(:each) do
10
+ self.n = 1_000_000
11
+ self.data = { }
12
+ self.transport = ASIR::Transport::Local.new
13
+ self.object = ASIR::Test::TestObject.new(self)
14
+ object.class.asir.transport = transport
15
+ end
16
+
17
+ it 'Raw message time' do
18
+ run! do
19
+ object.return_argument :this_value
20
+ end
21
+ $raw_t = @t # FIXME!
22
+ end
23
+
24
+ it 'Message time using Transport::Local, Coder::Identity' do
25
+ run! do
26
+ object.asir.return_argument :this_value
27
+ end
28
+ that_t = $raw_t # FIXME!
29
+ this_t = @t
30
+ $stderr.puts "\nThis .vs. Raw: #{this_t[:ms_per_n] / that_t[:ms_per_n]} ms/msg / ms/msg"
31
+ end
32
+
33
+ def run! &blk
34
+ $stderr.puts "Warmup: #{desc} ..." if @verbose
35
+ (n / 100 + 100).times &blk
36
+ $stderr.puts "Warmup: DONE." if @verbose
37
+ elapsed do
38
+ n.times &blk
39
+ end
40
+ end
41
+
42
+ def elapsed
43
+ result = nil
44
+ @t = { }
45
+ $stderr.puts "Measuring: #{desc} ..." if @verbose
46
+ @t[:n] = n
47
+ @t[:t0] = Time.now
48
+ result = yield
49
+ @t[:t1] = Time.now
50
+ @t[:dt] = @t[:t1] - @t[:t0]
51
+ $stderr.puts "Measuring: DONE." if @verbose
52
+ $stderr.puts "\n#{desc}:"
53
+ $stderr.puts " n = #{@t[:n]}"
54
+ $stderr.puts " elapsed = #{@t[:dt]} s"
55
+ $stderr.puts " rate = #{@t[:n_per_s] = @t[:n] / @t[:dt]} n/s"
56
+ $stderr.puts " time = #{@t[:ms_per_n] = @t[:dt] / @t[:n] * 1000} ms/n"
57
+ result
58
+ end
59
+
60
+ def desc
61
+ @desc ||=
62
+ @example.metadata[:description_args] * " "
63
+ end
64
+
65
+ end
66
+
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,37 @@
1
1
  require 'rubygems'
2
2
  require 'debug_helper'
3
3
  $: << File.expand_path('../../lib', __FILE__)
4
+
5
+ require 'asir'
6
+
7
+ module ASIR
8
+ module Test
9
+ class TestError < ::Exception; end
10
+ class TestObject
11
+ include ASIR::Client
12
+ attr_accessor :spec, :arg, :cls, :msg, :transport, :message
13
+ def initialize spec
14
+ @spec = spec
15
+ end
16
+
17
+ def get_data!
18
+ @transport = ASIR::Transport.current
19
+ @message = @transport && @transport.message
20
+ end
21
+
22
+ def return_argument arg
23
+ get_data!
24
+ @arg = arg
25
+ arg
26
+ end
27
+
28
+ def raise_exception! cls, msg
29
+ get_data!
30
+ @cls = cls
31
+ @msg = msg
32
+ raise cls, msg
33
+ end
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,135 @@
1
+ require 'asir/thread_variable'
2
+
3
+ class ASIR::ThreadVariable::Test
4
+ include ASIR::ThreadVariable
5
+ cattr_accessor_thread :tv1
6
+ cattr_accessor_thread :tv2, :initialize => '1'
7
+ cattr_accessor_thread :tv3, :default => '2'
8
+ cattr_accessor_thread :tv4, :transform => '__val.to_s'
9
+ attr_accessor_thread :iv1
10
+ attr_accessor :iv2
11
+ end
12
+
13
+ describe 'ASIR::ThreadVariable' do
14
+ def tc
15
+ @tc ||=
16
+ ASIR::ThreadVariable::Test
17
+ end
18
+ def ti
19
+ @ti ||=
20
+ tc.new
21
+ end
22
+
23
+ it 'cattr_accessor_thread handles concurrency' do
24
+ th1 = Thread.new {
25
+ tc.tv1.should == nil
26
+ tc.tv1 = 1
27
+ tc.tv1.should == 1
28
+ tc.with_attr! :tv1, 2 do
29
+ tc.tv1.should == 2
30
+ end
31
+ tc.tv1.should == 1
32
+ tc.clear_tv1.should == tc
33
+ tc.tv1.should == nil
34
+ }
35
+ th2 = Thread.new {
36
+ tc.tv1.should == nil
37
+ tc.tv1 = 2
38
+ tc.tv1.should == 2
39
+ tc.clear_tv1.should == tc
40
+ tc.tv1.should == nil
41
+ }
42
+ th1.join
43
+ th2.join
44
+ end
45
+
46
+ it 'attr_accessor_thread handles concurrency' do
47
+ th1 = Thread.new do
48
+ begin
49
+ ti.iv1.should == nil
50
+ ti.iv1 = 1
51
+ ti.iv1.should == 1
52
+ ti.with_attr! :iv1, 2 do
53
+ ti.iv1.should == 2
54
+ end
55
+ ti.iv1.should == 1
56
+ ti.clear_iv1.should == ti
57
+ ti.iv1.should == nil
58
+ rescue Exception => exc
59
+ raise exc.class, "In th1: #{exc.message}", exc.backtrace
60
+ end
61
+ end
62
+ th2 = Thread.new do
63
+ begin
64
+ ti.iv1.should == nil
65
+ ti.iv1 = 2
66
+ ti.iv1.should == 2
67
+ ti.clear_iv1.should == ti
68
+ ti.iv1.should == nil
69
+ rescue Exception => exc
70
+ raise exc.class, "In th2: #{exc.message}", exc.backtrace
71
+ end
72
+ end
73
+ th1.join
74
+ th2.join
75
+ end
76
+
77
+ it 'cattr_accessor_thread basic options' do
78
+ tc.tv1.should == nil
79
+ tc.tv1 = 1
80
+ tc.tv1.should == 1
81
+ tc.clear_tv1
82
+ tc.tv1.should == nil
83
+ end
84
+
85
+ it 'cattr_accessor_thread :initialize option' do
86
+ tc.tv2.should == 1
87
+ tc.tv2 = 2
88
+ tc.tv2.should == 2
89
+ tc.clear_tv2
90
+ tc.tv2.should == 1
91
+ end
92
+
93
+ it 'cattr_accessor_thread :default option' do
94
+ tc.tv3.should == 2
95
+ tc.tv3 = 3
96
+ tc.tv3.should == 3
97
+ tc.clear_tv3
98
+ tc.tv3.should == 2
99
+ end
100
+
101
+ it 'cattr_accessor_thread :transform option' do
102
+ tc.tv4.should == ''
103
+ tc.tv4 = 101
104
+ tc.tv4.should == '101'
105
+ tc.tv4 = '102'
106
+ tc.tv4.should == '102'
107
+ tc.clear_tv4
108
+ tc.tv4.should == ''
109
+ end
110
+
111
+ it 'attr_accessor_thread :with_attr!' do
112
+ ti.iv1.should == nil
113
+ ti.with_attr! :iv1, :value do
114
+ ti.iv1.should == :value
115
+ ti.with_attr! :iv1, :value2 do
116
+ ti.iv1.should == :value2
117
+ end
118
+ ti.iv1.should == :value
119
+ end
120
+ ti.iv1.should == nil
121
+ end
122
+
123
+ it 'attr_accessor :with_attr!' do
124
+ ti.iv2.should == nil
125
+ ti.with_attr! :iv2, :value do
126
+ ti.iv2.should == :value
127
+ ti.with_attr! :iv2, :value2 do
128
+ ti.iv2.should == :value2
129
+ end
130
+ ti.iv2.should == :value
131
+ end
132
+ ti.iv2.should == nil
133
+ end
134
+ end
135
+
@@ -0,0 +1,82 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ require 'asir'
4
+
5
+ describe "ASIR::Transport" do
6
+ attr_accessor :transport, :data, :object
7
+
8
+ before(:each) do
9
+ self.data = { }
10
+ self.transport = ASIR::Transport::Local.new
11
+ self.object = ASIR::Test::TestObject.new(self)
12
+ object.class.asir.transport = transport
13
+ object.class.asir.transport.should == self.transport
14
+ end
15
+
16
+ it 'should return result.' do
17
+ result = object.asir.return_argument :this_value
18
+ object.arg.should == :this_value
19
+ result.should == :this_value
20
+ end
21
+
22
+ it 'should set ASIR::Transport only during processing.' do
23
+ ASIR::Transport.current.should == nil
24
+ object.asir.return_argument :this_value
25
+ ASIR::Transport.current.should == nil
26
+ object.transport.class.should == transport.class
27
+ end
28
+
29
+ it 'should not create clone of ASIR::Transport during processing.' do
30
+ object.asir.return_argument :this_value
31
+ object.transport.object_id.should == transport.object_id
32
+ end
33
+
34
+ it 'should set ASIR::Transport#message during processing.' do
35
+ transport.message.should == nil
36
+ object.asir.return_argument :this_value
37
+ transport.message.should == nil
38
+ object.transport.message.should == nil
39
+ object.message.class.should == ASIR::Message
40
+ end
41
+
42
+ it 'should handle on_result_exception callbacks' do
43
+ _transport, _result = nil, nil
44
+ p = lambda do | transport, result |
45
+ _transport = transport
46
+ _result = result
47
+ end
48
+ transport.on_result_exception = p
49
+
50
+ cls = ::ASIR::Test::TestError
51
+ msg = 'This Message'.freeze
52
+
53
+ object.asir.transport.on_result_exception.should == p
54
+
55
+ expect {
56
+ object.asir.raise_exception! cls, msg
57
+ }.to raise_error
58
+
59
+ object.msg.should == msg
60
+ object.message.class.should == ASIR::Message
61
+ object.transport.on_result_exception.class.should == Proc
62
+
63
+ result = object.message.result
64
+ result.class.should == ASIR::Result
65
+
66
+ exc = result.exception
67
+ exc.class.should == ASIR::EncapsulatedException
68
+ exc.exception_class.should == cls.name
69
+ exc.exception_message.should == msg
70
+ exc.exception_backtrace.class.should == Array
71
+
72
+ _transport.should == object.transport
73
+
74
+ _result.class.should == ASIR::Result
75
+ _result.object_id.should == result.object_id
76
+
77
+ _result.message.class.should == ASIR::Message
78
+ _result.message.object_id.should == result.message.object_id
79
+ _result.exception.should == exc
80
+ end
81
+ end
82
+