zactor 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'active_support/log_subscriber'
3
+ module Zactor
4
+ class LogSubscriber < ActiveSupport::LogSubscriber
5
+ def logger
6
+ Zactor.logger
7
+ end
8
+
9
+ def merge(event)
10
+ payload = event.payload
11
+
12
+ message = "[Zactor](%.0fms) sending request '#{payload[:event]}' to '#{payload[:actor]}'' with params #{paylaod[:args].inspect}" % event.duration
13
+ debug message
14
+ end
15
+
16
+ def send_to(event)
17
+ payload = event.payload
18
+
19
+ message = "[Zactor] sending messages to '#{payload[:actor]}'" % event.duration
20
+ debug message
21
+ end
22
+ end
23
+ end
24
+
25
+ Zactor::LogSubscriber.attach_to :zactor
@@ -0,0 +1,23 @@
1
+ module Zactor
2
+ class Message
3
+ attr_accessor :actor, :params
4
+ attr_accessor :callback_id, :args
5
+ def initialize(actor, params = {})
6
+ @actor = actor
7
+ @params = params
8
+ end
9
+
10
+ def sender
11
+ params[:sender]
12
+ end
13
+
14
+ def args
15
+ params[:args]
16
+ end
17
+
18
+ def reply(*args)
19
+ return false unless params[:callback_id]
20
+ actor.send_reply sender, params[:callback_id], *args
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Zactor
2
+ VERSION = "0.0.6"
3
+ end
@@ -0,0 +1,136 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe "Zactor actor" do
6
+ class A
7
+ include Zactor
8
+ end
9
+ before do
10
+ stub(Zactor::Broker).new
11
+ Zactor.start 8000
12
+ end
13
+ describe "init" do
14
+ let(:actor) { A.new.zactor }
15
+ let(:sub) { Object.new }
16
+ let(:local_pub) { Object.new }
17
+ before do
18
+ stub(actor).make_sub { sub }
19
+ stub(actor).make_pub { local_pub }
20
+ end
21
+ it "должен зарегистрировать себя" do
22
+ mock(Zactor).register actor
23
+ actor.init
24
+ end
25
+
26
+ it "должен создать sub сокет" do
27
+ mock(actor).make_sub { sub }
28
+ actor.init
29
+ actor.instance_eval { @sub }.should eq(sub)
30
+ end
31
+
32
+ it "должен создать pub сокет для локальных вызовов" do
33
+ mock(actor).make_pub("inproc://zactor_broker_sub") { local_pub }
34
+ actor.init
35
+ actor.instance_eval { @pubs['0.0.0.0:8000'] }.should eq(local_pub)
36
+ end
37
+
38
+ describe "identity" do
39
+ it "по-умолчанию" do
40
+ actor.init
41
+ actor.actor.should eq({ 'identity' => "actor.#{actor.owner.object_id}-0.0.0.0:8000", 'host' => '0.0.0.0:8000' })
42
+ end
43
+
44
+ it "с глобальным указанием" do
45
+ A.zactor.identity "a"
46
+ actor.init
47
+ actor.actor.should eq({ 'identity' => "a", 'host' => '0.0.0.0:8000' })
48
+ end
49
+
50
+ it "с указанием для этого объекта" do
51
+ actor.identity = "b"
52
+ actor.init
53
+ actor.actor.should eq({ 'identity' => "b", 'host' => '0.0.0.0:8000' })
54
+ end
55
+ end
56
+
57
+ describe "after init" do
58
+ include Zactor::ZMQMEssages
59
+
60
+ def assert_messages(got, expected)
61
+ got.map(&:copy_out_string).should eq(expected.map(&:copy_out_string))
62
+ end
63
+
64
+ before do
65
+ actor.init
66
+ end
67
+ describe "send_to" do
68
+ it "должен отправить в локальный pub для локального объекта" do
69
+ mes = messages { |m| }
70
+ stub(local_pub).send_messages do |mes|
71
+ assert_messages mes, messages { |m|
72
+ m.str('b')
73
+ m.str(actor.bson_actor)
74
+ }
75
+ end
76
+ actor.send_to Zactor.get_actor('b')
77
+ end
78
+
79
+ it "должен отправить в удаленный pub для удаленного объекта, перед этим создав его" do
80
+ remote_pub = Object.new
81
+ mock(actor).make_pub("tcp://192.168.1.1:3000") { remote_pub }
82
+ stub(remote_pub).send_messages do |mes|
83
+ assert_messages mes, messages { |m| m.str('b'); m.str(actor.bson_actor) }
84
+ end
85
+ actor.send_to Zactor.get_actor('b', :host => '192.168.1.1:3000')
86
+ end
87
+ end
88
+
89
+ describe "send_request" do
90
+ it "должен отправлять сообщение типа request" do
91
+ stub(local_pub).send_messages do |mes|
92
+ assert_messages mes, messages { |m|
93
+ m.str('b')
94
+ m.str(actor.bson_actor)
95
+
96
+ m.str 'request'
97
+ m.str ""
98
+ m.str "show"
99
+ m.str BSON.serialize({ 'args' => ['foo', :bar] })
100
+ }
101
+ end
102
+ actor.send_request Zactor.get_actor('b'), :show, 'foo', :bar
103
+ end
104
+ end
105
+
106
+ describe "send_reply" do
107
+ it "должен отправлять сообщение типа reply" do
108
+ stub(local_pub).send_messages do |mes|
109
+ assert_messages mes, messages { |m|
110
+ m.str('b')
111
+ m.str(actor.bson_actor)
112
+
113
+ m.str 'reply'
114
+ m.str 5
115
+ m.str BSON.serialize({ 'args' => ['foo', :bar] })
116
+ }
117
+ end
118
+ actor.send_reply Zactor.get_actor('b'), 5, 'foo', :bar
119
+ end
120
+ end
121
+
122
+ describe "receive_reply" do
123
+
124
+ end
125
+
126
+ describe "receive_request" do
127
+
128
+ end
129
+
130
+ describe "finish" do
131
+
132
+ end
133
+ end
134
+
135
+ end
136
+ end
@@ -0,0 +1,81 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require "em-spec/rspec"
5
+
6
+ describe "Zactor" do
7
+ include EM::SpecHelper
8
+ module Exchange
9
+ class A
10
+ include Zactor
11
+
12
+ def initialize
13
+ zactor.init
14
+ end
15
+
16
+ def ping(actor = Zactor.get_actor("b"))
17
+ zactor.send_request actor, :ping do |res|
18
+ reply
19
+ end
20
+ end
21
+
22
+ def reply
23
+
24
+ end
25
+ end
26
+
27
+ class B
28
+ include Zactor
29
+
30
+ zactor do
31
+ identity "b"
32
+
33
+ event(:ping) do |o, msg|
34
+ o.receive
35
+ msg.reply "Pong!"
36
+ end
37
+ end
38
+
39
+ def initialize
40
+ zactor.init
41
+ end
42
+
43
+ def receive
44
+
45
+ end
46
+ end
47
+ end
48
+
49
+ after do
50
+ Zactor.clear
51
+ Zactor.finish
52
+ end
53
+
54
+ it "B должен получить сообщение" do
55
+ em do
56
+ Zactor.start 8000, :debug => true
57
+ Exchange::A.new.ping
58
+ b = Exchange::B.new
59
+ mock.proxy(b).receive { done }
60
+ end
61
+ end
62
+
63
+ it "A должен получить ответ" do
64
+ em do
65
+ Zactor.start 8000, :debug => true
66
+ a = Exchange::A.new
67
+ a.ping
68
+ Exchange::B.new
69
+ mock.proxy(a).reply { done }
70
+ end
71
+ end
72
+
73
+ it "Если задан таймаут, то он должен срабатывать" do
74
+ em(7) do
75
+ Zactor.start 8000, :debug => true
76
+ a = Exchange::A.new.ping.timeout(5) do
77
+ done
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,67 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require "em-spec/rspec"
5
+
6
+ describe "Zactor" do
7
+ include EM::SpecHelper
8
+ module Link
9
+ class A
10
+ include Zactor
11
+
12
+ def initialize
13
+ zactor.init
14
+ end
15
+ end
16
+
17
+ class B
18
+ include Zactor
19
+
20
+ def initialize
21
+ zactor.init
22
+ end
23
+ end
24
+ end
25
+
26
+ after do
27
+ puts "FINISH ZACTOR"
28
+ Zactor.finish
29
+ end
30
+
31
+ it "A должен уведомляться о смерти B" do
32
+ em do
33
+ Zactor.start 8000, :debug => true
34
+ a = Link::A.new
35
+ b = Link::B.new
36
+ a.zactor.link b.zactor.actor do
37
+ done
38
+ end
39
+ EM.add_timer(0.5) do
40
+ b.zactor.finish
41
+ end
42
+ end
43
+ end
44
+
45
+ # it "A должен уведомляться о смерти B в случае удаления объекта гарбэйдж коллектором" do
46
+ #
47
+ # end
48
+
49
+ it "A должен уведомляться о смерти B, даже если B закончил выполнение неожиданно" do
50
+ em(12) do
51
+ EM.add_timer(0.5) do #FIXME ZMQ-сокетам нужно время чтобы закрыться. Нужно придумать, что с этим делать
52
+ Zactor.start 8000, :debug => true
53
+ a = Link::A.new
54
+ b = Link::B.new
55
+ a.zactor.link b.zactor.actor do
56
+ done
57
+ end
58
+ EM.add_timer(0.5) do
59
+ b.zactor.instance_eval do
60
+ @linked = []
61
+ end
62
+ b.zactor.finish
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,40 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe "Zactor" do
6
+ describe "start" do
7
+ before do
8
+ stub(Zactor::Broker).new
9
+ end
10
+ it "должен выставлять хост по-умолчанию 0.0.0.0" do
11
+ Zactor.start 8000
12
+ Zactor.host.should eq('0.0.0.0:8000')
13
+ end
14
+
15
+ it "должен выставлять хост с учетом переданного" do
16
+ Zactor.start 8000, :host => '192.168.1.1'
17
+ Zactor.host.should eq('192.168.1.1:8000')
18
+ end
19
+
20
+ it "должен создавать новый брокер с указаным балансером" do
21
+ mock(Zactor::Broker).new :balancer => '0.0.0.0:4000'
22
+ Zactor.start 8000, :balancer => '0.0.0.0:4000'
23
+ end
24
+ end
25
+
26
+ describe "get_actor" do
27
+ before do
28
+ stub(Zactor::Broker).new
29
+ Zactor.start 8000
30
+ end
31
+ it "в качестве хоста по-умолчанию ставит себя же" do
32
+ Zactor.get_actor("actor1").should eq({ 'identity' => 'actor1', 'host' => '0.0.0.0:8000' })
33
+ end
34
+
35
+
36
+ it "в качестве хоста ставит переданный" do
37
+ Zactor.get_actor("actor1", :host => '192.168.1.1:3000').should eq({ 'identity' => 'actor1', 'host' => '192.168.1.1:3000' })
38
+ end
39
+ end
40
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  require 'rspec'
4
+ require 'zactor'
4
5
  require 'rr'
5
- require 'ruby_interface'
6
6
 
7
7
  # Requires supporting files with custom matchers and macros, etc,
8
8
  # in ./support/ and its subdirectories.
@@ -1,15 +1,15 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "ruby-interface/version"
3
+ require "zactor/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = %q{zactor}
7
- s.version = RubyInterface::VERSION
8
- s.summary = "Ruby interface"
7
+ s.version = Zactor::VERSION
8
+ s.summary = "Zactor"
9
9
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
- s.authors = ["Andrew Rudenko", "Nick Recobra"]
10
+ s.authors = ["Andrew Rudenko"]
11
11
  s.date = %q{2011-03-24}
12
- s.description = %q{Ruby interface}
12
+ s.description = %q{Zactor}
13
13
  s.email = %q{ceo@prepor.ru}
14
14
 
15
15
  s.files = `git ls-files`.split("\n")
@@ -17,6 +17,12 @@ Gem::Specification.new do |s|
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
+ s.add_dependency('ffi', ["> 0.1"])
21
+ s.add_dependency('ruby-interface', ["> 0"])
22
+ s.add_dependency('ffi-rzmq', ["> 0.1"])
23
+ s.add_dependency('em-zeromq', ["> 0.1"])
24
+ s.add_dependency('bson', ["> 0.1"])
25
+ s.add_dependency('bson_ext', ["> 0.1"])
20
26
  s.add_dependency('activesupport', ["> 0.1"])
21
- s.add_dependency(%q<i18n>, ["> 0.1"])
22
- end
27
+ end
28
+
metadata CHANGED
@@ -5,12 +5,11 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 5
9
- version: 0.0.5
8
+ - 6
9
+ version: 0.0.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andrew Rudenko
13
- - Nick Recobra
14
13
  autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
@@ -19,7 +18,7 @@ date: 2011-03-24 00:00:00 +03:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
- name: activesupport
21
+ name: ffi
23
22
  requirement: &id001 !ruby/object:Gem::Requirement
24
23
  none: false
25
24
  requirements:
@@ -33,8 +32,21 @@ dependencies:
33
32
  prerelease: false
34
33
  version_requirements: *id001
35
34
  - !ruby/object:Gem::Dependency
36
- name: i18n
35
+ name: ruby-interface
37
36
  requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">"
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: ffi-rzmq
49
+ requirement: &id003 !ruby/object:Gem::Requirement
38
50
  none: false
39
51
  requirements:
40
52
  - - ">"
@@ -45,8 +57,64 @@ dependencies:
45
57
  version: "0.1"
46
58
  type: :runtime
47
59
  prerelease: false
48
- version_requirements: *id002
49
- description: Ruby interface
60
+ version_requirements: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ name: em-zeromq
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">"
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ - 1
71
+ version: "0.1"
72
+ type: :runtime
73
+ prerelease: false
74
+ version_requirements: *id004
75
+ - !ruby/object:Gem::Dependency
76
+ name: bson
77
+ requirement: &id005 !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">"
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ - 1
85
+ version: "0.1"
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: *id005
89
+ - !ruby/object:Gem::Dependency
90
+ name: bson_ext
91
+ requirement: &id006 !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">"
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ - 1
99
+ version: "0.1"
100
+ type: :runtime
101
+ prerelease: false
102
+ version_requirements: *id006
103
+ - !ruby/object:Gem::Dependency
104
+ name: activesupport
105
+ requirement: &id007 !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">"
109
+ - !ruby/object:Gem::Version
110
+ segments:
111
+ - 0
112
+ - 1
113
+ version: "0.1"
114
+ type: :runtime
115
+ prerelease: false
116
+ version_requirements: *id007
117
+ description: Zactor
50
118
  email: ceo@prepor.ru
51
119
  executables: []
52
120
 
@@ -57,18 +125,32 @@ extra_rdoc_files: []
57
125
  files:
58
126
  - .document
59
127
  - .gitignore
128
+ - .rake_tasks~
60
129
  - .rspec
130
+ - .yardopts
61
131
  - Gemfile
62
132
  - Gemfile.lock
133
+ - Guardfile
63
134
  - LICENSE.txt
64
135
  - README.md
65
136
  - Rakefile
66
- - lib/ruby-interface/version.rb
67
- - lib/ruby-interface/yard.rb
68
- - lib/ruby_interface.rb
69
- - ruby-interface.gemspec
70
- - spec/ruby_interface_spec.rb
137
+ - VERSION
138
+ - examples/chat/client.rb
139
+ - examples/chat/server.rb
140
+ - examples/ping/inproc.rb
141
+ - lib/zactor.rb
142
+ - lib/zactor/actor_pub.rb
143
+ - lib/zactor/actor_sub.rb
144
+ - lib/zactor/broker.rb
145
+ - lib/zactor/log_subscriber.rb
146
+ - lib/zactor/message.rb
147
+ - lib/zactor/version.rb
148
+ - spec/lib/actor_spec.rb
149
+ - spec/lib/exchange_spec.rb
150
+ - spec/lib/link_spec.rb
151
+ - spec/lib/zactor_spec.rb
71
152
  - spec/spec_helper.rb
153
+ - zactor.gemspec
72
154
  has_rdoc: true
73
155
  homepage:
74
156
  licenses: []
@@ -83,7 +165,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
165
  requirements:
84
166
  - - ">="
85
167
  - !ruby/object:Gem::Version
86
- hash: -442756119075028289
168
+ hash: -3870401536511786688
87
169
  segments:
88
170
  - 0
89
171
  version: "0"
@@ -101,7 +183,10 @@ rubyforge_project:
101
183
  rubygems_version: 1.3.7
102
184
  signing_key:
103
185
  specification_version: 3
104
- summary: Ruby interface
186
+ summary: Zactor
105
187
  test_files:
106
- - spec/ruby_interface_spec.rb
188
+ - spec/lib/actor_spec.rb
189
+ - spec/lib/exchange_spec.rb
190
+ - spec/lib/link_spec.rb
191
+ - spec/lib/zactor_spec.rb
107
192
  - spec/spec_helper.rb