nofxx-nanite 0.4.1.2 → 0.4.1.15
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +3 -2
- data/bin/nanite-agent +3 -0
- data/lib/nanite.rb +8 -2
- data/lib/nanite/agent.rb +19 -11
- data/lib/nanite/amqp.rb +18 -1
- data/lib/nanite/cluster.rb +34 -14
- data/lib/nanite/config.rb +1 -1
- data/lib/nanite/daemonize.rb +4 -4
- data/lib/nanite/local_state.rb +4 -0
- data/lib/nanite/log.rb +1 -1
- data/lib/nanite/mapper.rb +34 -21
- data/lib/nanite/mapper_proxy.rb +4 -4
- data/lib/nanite/{dispatcher.rb → nanite_dispatcher.rb} +1 -1
- data/lib/nanite/packets.rb +4 -2
- data/lib/nanite/reaper.rb +15 -14
- data/lib/nanite/security/signature.rb +1 -1
- data/lib/nanite/serializer.rb +31 -18
- data/lib/nanite/state.rb +7 -3
- metadata +63 -59
- data/spec/actor_registry_spec.rb +0 -60
- data/spec/actor_spec.rb +0 -77
- data/spec/agent_spec.rb +0 -240
- data/spec/cached_certificate_store_proxy_spec.rb +0 -34
- data/spec/certificate_cache_spec.rb +0 -49
- data/spec/certificate_spec.rb +0 -27
- data/spec/cluster_spec.rb +0 -485
- data/spec/dispatcher_spec.rb +0 -136
- data/spec/distinguished_name_spec.rb +0 -24
- data/spec/encrypted_document_spec.rb +0 -21
- data/spec/job_spec.rb +0 -251
- data/spec/local_state_spec.rb +0 -112
- data/spec/packet_spec.rb +0 -220
- data/spec/rsa_key_pair_spec.rb +0 -33
- data/spec/secure_serializer_spec.rb +0 -41
- data/spec/serializer_spec.rb +0 -107
- data/spec/signature_spec.rb +0 -30
- data/spec/spec_helper.rb +0 -33
- data/spec/static_certificate_store_spec.rb +0 -30
- data/spec/util_spec.rb +0 -63
data/spec/actor_spec.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
-
|
3
|
-
describe Nanite::Actor do
|
4
|
-
class ::WebDocumentImporter
|
5
|
-
include Nanite::Actor
|
6
|
-
expose :import, :cancel
|
7
|
-
|
8
|
-
def import
|
9
|
-
1
|
10
|
-
end
|
11
|
-
def cancel
|
12
|
-
0
|
13
|
-
end
|
14
|
-
def continue
|
15
|
-
1
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
module ::Actors
|
20
|
-
class ComedyActor
|
21
|
-
include Nanite::Actor
|
22
|
-
expose :fun_tricks
|
23
|
-
def fun_tricks
|
24
|
-
:rabbit_in_the_hat
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class ::Actors::InvalidActor
|
30
|
-
include Nanite::Actor
|
31
|
-
expose :non_existing
|
32
|
-
end
|
33
|
-
|
34
|
-
describe ".expose" do
|
35
|
-
before :each do
|
36
|
-
@exposed = WebDocumentImporter.instance_variable_get(:@exposed).dup
|
37
|
-
end
|
38
|
-
|
39
|
-
after :each do
|
40
|
-
WebDocumentImporter.instance_variable_set(:@exposed, @exposed)
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
|
-
it "should single expose method only once" do
|
45
|
-
3.times { WebDocumentImporter.expose(:continue) }
|
46
|
-
WebDocumentImporter.provides_for("webfiles").should == ["/webfiles/import", "/webfiles/cancel", "/webfiles/continue"]
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
describe ".default_prefix" do
|
51
|
-
it "is calculated as default prefix as const path of class name" do
|
52
|
-
Actors::ComedyActor.default_prefix.should == "actors/comedy_actor"
|
53
|
-
WebDocumentImporter.default_prefix.should == "web_document_importer"
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
describe ".provides_for(prefix)" do
|
58
|
-
before :each do
|
59
|
-
@provides = Actors::ComedyActor.provides_for("money")
|
60
|
-
end
|
61
|
-
|
62
|
-
it "returns an array" do
|
63
|
-
@provides.should be_kind_of(Array)
|
64
|
-
end
|
65
|
-
|
66
|
-
it "maps exposed service methods to prefix" do
|
67
|
-
@provides.should == ["/money/fun_tricks"]
|
68
|
-
wdi_provides = WebDocumentImporter.provides_for("webfiles")
|
69
|
-
wdi_provides.should include("/webfiles/import")
|
70
|
-
wdi_provides.should include("/webfiles/cancel")
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should not include methods not existing in the actor class" do
|
74
|
-
Actors::InvalidActor.provides_for("money").should_not include("/money/non_existing")
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
data/spec/agent_spec.rb
DELETED
@@ -1,240 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
-
|
3
|
-
describe "Agent:" do
|
4
|
-
|
5
|
-
describe "Default Option" do
|
6
|
-
|
7
|
-
before(:all) do
|
8
|
-
EM.stub!(:add_periodic_timer)
|
9
|
-
AMQP.stub!(:connect)
|
10
|
-
@amq = mock("AMQueue", :queue => mock("queue", :subscribe => {}), :fanout => mock("fanout", :publish => nil))
|
11
|
-
MQ.stub!(:new).and_return(@amq)
|
12
|
-
@agent = Nanite::Agent.start
|
13
|
-
end
|
14
|
-
|
15
|
-
it "for daemonize is false" do
|
16
|
-
@agent.options.should include(:daemonize)
|
17
|
-
@agent.options[:daemonize].should == false
|
18
|
-
end
|
19
|
-
|
20
|
-
it "for format is marshal" do
|
21
|
-
@agent.options.should include(:format)
|
22
|
-
@agent.options[:format].should == :marshal
|
23
|
-
end
|
24
|
-
|
25
|
-
it "for console is false" do
|
26
|
-
@agent.options.should include(:console)
|
27
|
-
@agent.options[:console].should == false
|
28
|
-
end
|
29
|
-
|
30
|
-
it "for user is nanite" do
|
31
|
-
@agent.options.should include(:user)
|
32
|
-
@agent.options[:user].should == "nanite"
|
33
|
-
end
|
34
|
-
|
35
|
-
it "for pass(word) is testing" do
|
36
|
-
@agent.options.should include(:pass)
|
37
|
-
@agent.options[:pass].should == "testing"
|
38
|
-
end
|
39
|
-
|
40
|
-
it "for secure is false" do
|
41
|
-
@agent.options.should include(:secure)
|
42
|
-
@agent.options[:secure].should == false
|
43
|
-
end
|
44
|
-
|
45
|
-
it "for host is 0.0.0.0" do
|
46
|
-
@agent.options.should include(:host)
|
47
|
-
@agent.options[:host].should == "0.0.0.0"
|
48
|
-
end
|
49
|
-
|
50
|
-
it "for log_level is info" do
|
51
|
-
@agent.options.should include(:log_level)
|
52
|
-
@agent.options[:log_level].should == :info
|
53
|
-
end
|
54
|
-
|
55
|
-
it "for vhost is /nanite" do
|
56
|
-
@agent.options.should include(:vhost)
|
57
|
-
@agent.options[:vhost].should == "/nanite"
|
58
|
-
end
|
59
|
-
|
60
|
-
it "for ping_time is 15" do
|
61
|
-
@agent.options.should include(:ping_time)
|
62
|
-
@agent.options[:ping_time].should == 15
|
63
|
-
end
|
64
|
-
|
65
|
-
it "for default_services is []" do
|
66
|
-
@agent.options.should include(:default_services)
|
67
|
-
@agent.options[:default_services].should == []
|
68
|
-
end
|
69
|
-
|
70
|
-
it "for root is #{File.expand_path(File.join(File.dirname(__FILE__), '..'))}" do
|
71
|
-
@agent.options.should include(:root)
|
72
|
-
@agent.options[:root].should == File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
73
|
-
end
|
74
|
-
|
75
|
-
it "for file_root is #{File.expand_path(File.join(File.dirname(__FILE__), '..', 'files'))}" do
|
76
|
-
@agent.options.should include(:file_root)
|
77
|
-
@agent.options[:file_root].should == File.expand_path(File.join(File.dirname(__FILE__), '..', 'files'))
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
|
-
|
82
|
-
describe "Options from config.yml" do
|
83
|
-
|
84
|
-
before(:all) do
|
85
|
-
@agent = Nanite::Agent.start
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
|
90
|
-
describe "Passed in Options" do
|
91
|
-
|
92
|
-
before(:each) do
|
93
|
-
EM.stub!(:add_periodic_timer)
|
94
|
-
AMQP.stub!(:connect)
|
95
|
-
@amq = mock("AMQueue", :queue => mock("queue", :subscribe => {}), :fanout => mock("fanout", :publish => nil))
|
96
|
-
MQ.stub!(:new).and_return(@amq)
|
97
|
-
end
|
98
|
-
|
99
|
-
# TODO figure out how to stub call to daemonize
|
100
|
-
# it "for daemonize should override default (false)" do
|
101
|
-
# agent = Nanite::Agent.start(:daemonize => true)
|
102
|
-
# agent.options.should include(:daemonize)
|
103
|
-
# agent.options[:daemonize].should == true
|
104
|
-
# end
|
105
|
-
|
106
|
-
it "for format should override default (marshal)" do
|
107
|
-
agent = Nanite::Agent.start(:format => :json)
|
108
|
-
agent.options.should include(:format)
|
109
|
-
agent.options[:format].should == :json
|
110
|
-
end
|
111
|
-
|
112
|
-
# TODO figure out how to avoid console output
|
113
|
-
# it "for console should override default (false)" do
|
114
|
-
# agent = Nanite::Agent.start(:console => true)
|
115
|
-
# agent.options.should include(:console)
|
116
|
-
# agent.options[:console].should == true
|
117
|
-
# end
|
118
|
-
|
119
|
-
it "for user should override default (nanite)" do
|
120
|
-
agent = Nanite::Agent.start(:user => "me")
|
121
|
-
agent.options.should include(:user)
|
122
|
-
agent.options[:user].should == "me"
|
123
|
-
end
|
124
|
-
|
125
|
-
it "for pass(word) should override default (testing)" do
|
126
|
-
agent = Nanite::Agent.start(:pass => "secret")
|
127
|
-
agent.options.should include(:pass)
|
128
|
-
agent.options[:pass].should == "secret"
|
129
|
-
end
|
130
|
-
|
131
|
-
it "for secure should override default (false)" do
|
132
|
-
agent = Nanite::Agent.start(:secure => true)
|
133
|
-
agent.options.should include(:secure)
|
134
|
-
agent.options[:secure].should == true
|
135
|
-
end
|
136
|
-
|
137
|
-
it "for host should override default (0.0.0.0)" do
|
138
|
-
agent = Nanite::Agent.start(:host => "127.0.0.1")
|
139
|
-
agent.options.should include(:host)
|
140
|
-
agent.options[:host].should == "127.0.0.1"
|
141
|
-
end
|
142
|
-
|
143
|
-
it "for log_level should override default (info)" do
|
144
|
-
agent = Nanite::Agent.start(:log_level => :debug)
|
145
|
-
agent.options.should include(:log_level)
|
146
|
-
agent.options[:log_level].should == :debug
|
147
|
-
end
|
148
|
-
|
149
|
-
it "for vhost should override default (/nanite)" do
|
150
|
-
agent = Nanite::Agent.start(:vhost => "/virtual_host")
|
151
|
-
agent.options.should include(:vhost)
|
152
|
-
agent.options[:vhost].should == "/virtual_host"
|
153
|
-
end
|
154
|
-
|
155
|
-
it "for ping_time should override default (15)" do
|
156
|
-
agent = Nanite::Agent.start(:ping_time => 5)
|
157
|
-
agent.options.should include(:ping_time)
|
158
|
-
agent.options[:ping_time].should == 5
|
159
|
-
end
|
160
|
-
|
161
|
-
it "for default_services should override default ([])" do
|
162
|
-
agent = Nanite::Agent.start(:default_services => [:test])
|
163
|
-
agent.options.should include(:default_services)
|
164
|
-
agent.options[:default_services].should == [:test]
|
165
|
-
end
|
166
|
-
|
167
|
-
it "for root should override default (#{File.expand_path(File.join(File.dirname(__FILE__), '..'))})" do
|
168
|
-
agent = Nanite::Agent.start(:root => File.expand_path(File.dirname(__FILE__)))
|
169
|
-
agent.options.should include(:root)
|
170
|
-
agent.options[:root].should == File.expand_path(File.dirname(__FILE__))
|
171
|
-
end
|
172
|
-
|
173
|
-
it "for file_root should override default (#{File.expand_path(File.join(File.dirname(__FILE__), '..', 'files'))})" do
|
174
|
-
agent = Nanite::Agent.start(:file_root => File.expand_path(File.dirname(__FILE__)))
|
175
|
-
agent.options.should include(:file_root)
|
176
|
-
agent.options[:file_root].should == File.expand_path(File.dirname(__FILE__))
|
177
|
-
end
|
178
|
-
|
179
|
-
it "for a single tag should result in the agent's tags being set" do
|
180
|
-
agent = Nanite::Agent.start(:tag => "sample_tag")
|
181
|
-
agent.tags.should include("sample_tag")
|
182
|
-
end
|
183
|
-
|
184
|
-
it "for multiple tags should result in the agent's tags being set" do
|
185
|
-
agent = Nanite::Agent.start(:tag => ["sample_tag_1", "sample_tag_2"])
|
186
|
-
agent.tags.should include("sample_tag_1")
|
187
|
-
agent.tags.should include("sample_tag_2")
|
188
|
-
end
|
189
|
-
|
190
|
-
it "for threadpool_size" do
|
191
|
-
agent = Nanite::Agent.start(:threadpool_size => 5)
|
192
|
-
agent.dispatcher.evmclass.threadpool_size.should == 5
|
193
|
-
end
|
194
|
-
|
195
|
-
end
|
196
|
-
|
197
|
-
describe "Security" do
|
198
|
-
|
199
|
-
before(:each) do
|
200
|
-
EM.stub!(:add_periodic_timer)
|
201
|
-
AMQP.stub!(:connect)
|
202
|
-
@amq = mock("AMQueue", :queue => mock("queue", :subscribe => {}, :publish => {}), :fanout => mock("fanout", :publish => nil))
|
203
|
-
MQ.stub!(:new).and_return(@amq)
|
204
|
-
serializer = Nanite::Serializer.new
|
205
|
-
@request = Nanite::Request.new('/foo/bar', '')
|
206
|
-
@push = Nanite::Push.new('/foo/bar', '')
|
207
|
-
@agent = Nanite::Agent.start
|
208
|
-
end
|
209
|
-
|
210
|
-
it 'should correctly deny requests' do
|
211
|
-
security = mock("Security")
|
212
|
-
@agent.register_security(security)
|
213
|
-
|
214
|
-
security.should_receive(:authorize).twice.and_return(false)
|
215
|
-
@agent.dispatcher.should_not_receive(:dispatch)
|
216
|
-
@agent.__send__(:receive, @request)
|
217
|
-
@agent.__send__(:receive, @push)
|
218
|
-
end
|
219
|
-
|
220
|
-
it 'should correctly authorize requests' do
|
221
|
-
security = mock("Security")
|
222
|
-
@agent.register_security(security)
|
223
|
-
|
224
|
-
security.should_receive(:authorize).twice.and_return(true)
|
225
|
-
@agent.dispatcher.stub!(:dispatch)
|
226
|
-
@agent.dispatcher.should_receive(:dispatch).twice
|
227
|
-
@agent.__send__(:receive, @request)
|
228
|
-
@agent.__send__(:receive, @push)
|
229
|
-
end
|
230
|
-
|
231
|
-
it 'should be ignored when not specified' do
|
232
|
-
@agent.dispatcher.stub!(:dispatch)
|
233
|
-
@agent.dispatcher.should_receive(:dispatch).twice
|
234
|
-
@agent.__send__(:receive, @request)
|
235
|
-
@agent.__send__(:receive, @push)
|
236
|
-
end
|
237
|
-
|
238
|
-
end
|
239
|
-
|
240
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
-
|
3
|
-
describe Nanite::CachedCertificateStoreProxy do
|
4
|
-
|
5
|
-
include SpecHelpers
|
6
|
-
|
7
|
-
before(:all) do
|
8
|
-
@signer, key = issue_cert
|
9
|
-
@recipient, key = issue_cert
|
10
|
-
@store = mock("Store")
|
11
|
-
@proxy = Nanite::CachedCertificateStoreProxy.new(@store)
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'should not raise and return nil for non existent certificates' do
|
15
|
-
res = nil
|
16
|
-
@store.should_receive(:get_recipients).with(nil).and_return(nil)
|
17
|
-
lambda { res = @proxy.get_recipients(nil) }.should_not raise_error
|
18
|
-
res.should == nil
|
19
|
-
@store.should_receive(:get_signer).with(nil).and_return(nil)
|
20
|
-
lambda { res = @proxy.get_signer(nil) }.should_not raise_error
|
21
|
-
res.should == nil
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'should return recipient certificates' do
|
25
|
-
@store.should_receive(:get_recipients).with('anything').and_return(@recipient)
|
26
|
-
@proxy.get_recipients('anything').should == @recipient
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'should return signer certificates' do
|
30
|
-
@store.should_receive(:get_signer).with('anything').and_return(@signer)
|
31
|
-
@proxy.get_signer('anything').should == @signer
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
-
|
3
|
-
describe Nanite::CertificateCache do
|
4
|
-
|
5
|
-
before(:each) do
|
6
|
-
@cache = Nanite::CertificateCache.new(2)
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'should allow storing and retrieving objects' do
|
10
|
-
@cache['some_id'].should be_nil
|
11
|
-
@cache['some_id'] = 'some_value'
|
12
|
-
@cache['some_id'].should == 'some_value'
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'should not store more than required' do
|
16
|
-
@cache[1] = 'oldest'
|
17
|
-
@cache[2] = 'older'
|
18
|
-
@cache[1].should == 'oldest'
|
19
|
-
@cache[2].should == 'older'
|
20
|
-
|
21
|
-
@cache[3] = 'new'
|
22
|
-
@cache[3].should == 'new'
|
23
|
-
|
24
|
-
@cache[1].should be_nil
|
25
|
-
@cache[2].should == 'older'
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'should use LRU to remove entries' do
|
29
|
-
@cache[1] = 'oldest'
|
30
|
-
@cache[2] = 'older'
|
31
|
-
@cache[1].should == 'oldest'
|
32
|
-
@cache[2].should == 'older'
|
33
|
-
|
34
|
-
@cache[1] = 'new'
|
35
|
-
@cache[3] = 'newer'
|
36
|
-
@cache[1].should == 'new'
|
37
|
-
@cache[3].should == 'newer'
|
38
|
-
|
39
|
-
@cache[2].should be_nil
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'should store items returned by block' do
|
43
|
-
@cache[1].should be_nil
|
44
|
-
item = @cache.get(1) { 'item' }
|
45
|
-
item.should == 'item'
|
46
|
-
@cache[1].should == 'item'
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
data/spec/certificate_spec.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
-
|
3
|
-
describe Nanite::Certificate do
|
4
|
-
|
5
|
-
include SpecHelpers
|
6
|
-
|
7
|
-
before(:all) do
|
8
|
-
@certificate, key = issue_cert
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'should save' do
|
12
|
-
filename = File.join(File.dirname(__FILE__), "cert.pem")
|
13
|
-
@certificate.save(filename)
|
14
|
-
File.size(filename).should be > 0
|
15
|
-
File.delete(filename)
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'should load' do
|
19
|
-
filename = File.join(File.dirname(__FILE__), "cert.pem")
|
20
|
-
@certificate.save(filename)
|
21
|
-
cert = Nanite::Certificate.load(filename)
|
22
|
-
File.delete(filename)
|
23
|
-
cert.should_not be_nil
|
24
|
-
cert.data.should == @certificate.data
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
data/spec/cluster_spec.rb
DELETED
@@ -1,485 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
-
|
3
|
-
describe Nanite::Cluster do
|
4
|
-
|
5
|
-
include SpecHelpers
|
6
|
-
|
7
|
-
describe "Intialization" do
|
8
|
-
|
9
|
-
before(:each) do
|
10
|
-
@fanout = mock("fanout")
|
11
|
-
@binding = mock("binding", :subscribe => true)
|
12
|
-
@queue = mock("queue", :bind => @binding)
|
13
|
-
@amq = mock("AMQueue", :queue => @queue, :fanout => @fanout)
|
14
|
-
@serializer = mock("Serializer")
|
15
|
-
@reaper = mock("Reaper")
|
16
|
-
@mapper = mock("Mapper")
|
17
|
-
Nanite::Reaper.stub!(:new).and_return(@reaper)
|
18
|
-
end
|
19
|
-
|
20
|
-
describe "of Heartbeat (Queue)" do
|
21
|
-
|
22
|
-
it "should setup the heartbeat (queue) for id" do
|
23
|
-
@amq.should_receive(:queue).with("heartbeat-the_identity", anything()).and_return(@queue)
|
24
|
-
cluster = Nanite::Cluster.new(@amq, 10, "the_identity", @serializer, @mapper)
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should make the heartbeat (queue) exclusive" do
|
28
|
-
@amq.should_receive(:queue).with("heartbeat-the_identity", { :exclusive => true }).and_return(@queue)
|
29
|
-
cluster = Nanite::Cluster.new(@amq, 10, "the_identity", @serializer, @mapper)
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should bind the heartbeat (queue) to 'heartbeat' fanout" do
|
33
|
-
@amq.should_receive(:fanout).with("heartbeat", { :durable => true }).and_return(@fanout)
|
34
|
-
@queue.should_receive(:bind).with(@fanout).and_return(@binding)
|
35
|
-
cluster = Nanite::Cluster.new(@amq, 10, "the_identity", @serializer, @mapper)
|
36
|
-
end
|
37
|
-
|
38
|
-
end # of Heartbeat (Queue)
|
39
|
-
|
40
|
-
|
41
|
-
describe "of Registration (Queue)" do
|
42
|
-
|
43
|
-
it "should setup the registration (queue) for id" do
|
44
|
-
@amq.should_receive(:queue).with("registration-the_identity", anything()).and_return(@queue)
|
45
|
-
cluster = Nanite::Cluster.new(@amq, 10, "the_identity", @serializer, @mapper)
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should make the registration (queue) exclusive" do
|
49
|
-
@amq.should_receive(:queue).with("registration-the_identity", { :exclusive => true }).and_return(@queue)
|
50
|
-
cluster = Nanite::Cluster.new(@amq, 10, "the_identity", @serializer, @mapper)
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should bind the registration (queue) to 'registration' fanout" do
|
54
|
-
@amq.should_receive(:fanout).with("registration", { :durable => true }).and_return(@fanout)
|
55
|
-
@queue.should_receive(:bind).with(@fanout).and_return(@binding)
|
56
|
-
cluster = Nanite::Cluster.new(@amq, 10, "the_identity", @serializer, @mapper)
|
57
|
-
end
|
58
|
-
|
59
|
-
end # of Registration (Queue)
|
60
|
-
|
61
|
-
describe "of Request (Queue)" do
|
62
|
-
|
63
|
-
it "should setup the request (queue) for id" do
|
64
|
-
@amq.should_receive(:queue).with("request-the_identity", anything()).and_return(@queue)
|
65
|
-
cluster = Nanite::Cluster.new(@amq, 10, "the_identity", @serializer, @mapper)
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should make the request (queue) exclusive" do
|
69
|
-
@amq.should_receive(:queue).with("request-the_identity", { :exclusive => true }).and_return(@queue)
|
70
|
-
cluster = Nanite::Cluster.new(@amq, 10, "the_identity", @serializer, @mapper)
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should bind the request (queue) to 'request' fanout" do
|
74
|
-
@amq.should_receive(:fanout).with("request", { :durable => true }).and_return(@fanout)
|
75
|
-
@queue.should_receive(:bind).with(@fanout).and_return(@binding)
|
76
|
-
cluster = Nanite::Cluster.new(@amq, 10, "the_identity", @serializer, @mapper)
|
77
|
-
end
|
78
|
-
|
79
|
-
end # of Request (Queue)
|
80
|
-
|
81
|
-
|
82
|
-
describe "Reaper" do
|
83
|
-
|
84
|
-
it "should be created" do
|
85
|
-
Nanite::Reaper.should_receive(:new).with(anything()).and_return(@reaper)
|
86
|
-
cluster = Nanite::Cluster.new(@amq, 32, "the_identity", @serializer, @mapper)
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should use the agent timeout" do
|
90
|
-
Nanite::Reaper.should_receive(:new).with(443).and_return(@reaper)
|
91
|
-
cluster = Nanite::Cluster.new(@amq, 443, "the_identity", @serializer, @mapper)
|
92
|
-
end
|
93
|
-
|
94
|
-
end # Reaper
|
95
|
-
|
96
|
-
describe "State" do
|
97
|
-
begin
|
98
|
-
require 'nanite/state'
|
99
|
-
rescue LoadError
|
100
|
-
end
|
101
|
-
|
102
|
-
if defined?(Redis)
|
103
|
-
it "should use a local state by default" do
|
104
|
-
cluster = Nanite::Cluster.new(@amq, 443, "the_identity", @serializer, @mapper)
|
105
|
-
cluster.nanites.instance_of?(Nanite::LocalState).should == true
|
106
|
-
end
|
107
|
-
|
108
|
-
it "should set up a redis state when requested" do
|
109
|
-
state = Nanite::State.new("")
|
110
|
-
Nanite::State.should_receive(:new).with("localhost:1234").and_return(state)
|
111
|
-
cluster = Nanite::Cluster.new(@amq, 443, "the_identity", @serializer, @mapper, "localhost:1234")
|
112
|
-
cluster.nanites.instance_of?(Nanite::State).should == true
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end # Intialization
|
117
|
-
|
118
|
-
|
119
|
-
describe "Target Selection" do
|
120
|
-
|
121
|
-
before(:each) do
|
122
|
-
@fanout = mock("fanout")
|
123
|
-
@binding = mock("binding", :subscribe => true)
|
124
|
-
@queue = mock("queue", :bind => @binding)
|
125
|
-
@amq = mock("AMQueue", :queue => @queue, :fanout => @fanout)
|
126
|
-
@serializer = mock("Serializer")
|
127
|
-
@reaper = mock("Reaper")
|
128
|
-
Nanite::Reaper.stub!(:new).and_return(@reaper)
|
129
|
-
@cluster = Nanite::Cluster.new(@amq, 32, "the_identity", @serializer, @mapper)
|
130
|
-
end
|
131
|
-
|
132
|
-
it "should return array containing targets for request" do
|
133
|
-
target = mock("Supplied Target")
|
134
|
-
request = mock("Request", :target => target)
|
135
|
-
@cluster.targets_for(request).should be_instance_of(Array)
|
136
|
-
end
|
137
|
-
|
138
|
-
it "should use target from request" do
|
139
|
-
target = mock("Supplied Target")
|
140
|
-
request = mock("Request", :target => target)
|
141
|
-
@cluster.targets_for(request).should == [target]
|
142
|
-
end
|
143
|
-
|
144
|
-
it "should use targets choosen by least loaded selector (:least_loaded)" do
|
145
|
-
targets = { "target 3" => 3 }
|
146
|
-
request = mock("Request", :target => nil, :selector => :least_loaded, :type => "service", :tags => [])
|
147
|
-
@cluster.should_receive(:least_loaded).with("service", []).and_return(targets)
|
148
|
-
@cluster.targets_for(request).should == ["target 3"]
|
149
|
-
end
|
150
|
-
|
151
|
-
it "should use targets choosen by all selector (:all)" do
|
152
|
-
targets = { "target 1" => 1, "target 2" => 2, "target 3" => 3 }
|
153
|
-
request = mock("Request", :target => nil, :selector => :all, :type => "service", :tags => [])
|
154
|
-
@cluster.should_receive(:all).with("service", []).and_return(targets)
|
155
|
-
@cluster.targets_for(request).should == ["target 1", "target 2", "target 3"]
|
156
|
-
end
|
157
|
-
|
158
|
-
it "should use targets choosen by random selector (:random)" do
|
159
|
-
targets = { "target 3" => 3 }
|
160
|
-
request = mock("Request", :target => nil, :selector => :random, :type => "service", :tags => [])
|
161
|
-
@cluster.should_receive(:random).with("service", []).and_return(targets)
|
162
|
-
@cluster.targets_for(request).should == ["target 3"]
|
163
|
-
end
|
164
|
-
|
165
|
-
it "should use targets choosen by round-robin selector (:rr)" do
|
166
|
-
targets = { "target 2" => 2 }
|
167
|
-
request = mock("Request", :target => nil, :selector => :rr, :type => "service", :tags => [])
|
168
|
-
@cluster.should_receive(:rr).with("service", []).and_return(targets)
|
169
|
-
@cluster.targets_for(request).should == ["target 2"]
|
170
|
-
end
|
171
|
-
|
172
|
-
end # Target Selection
|
173
|
-
|
174
|
-
|
175
|
-
describe "Nanite Registration" do
|
176
|
-
|
177
|
-
before(:each) do
|
178
|
-
@fanout = mock("fanout")
|
179
|
-
@binding = mock("binding", :subscribe => true)
|
180
|
-
@queue = mock("queue", :bind => @binding)
|
181
|
-
@amq = mock("AMQueue", :queue => @queue, :fanout => @fanout)
|
182
|
-
@serializer = mock("Serializer")
|
183
|
-
@reaper = mock("Reaper", :timeout => true)
|
184
|
-
Nanite::Log.stub!(:info)
|
185
|
-
Nanite::Reaper.stub!(:new).and_return(@reaper)
|
186
|
-
@cluster = Nanite::Cluster.new(@amq, 32, "the_identity", @serializer, @mapper)
|
187
|
-
@register_packet = Nanite::Register.new("nanite_id", ["the_nanite_services"], "nanite_status",[])
|
188
|
-
end
|
189
|
-
|
190
|
-
it "should add the Nanite to the nanites map" do
|
191
|
-
@cluster.register(@register_packet)
|
192
|
-
@cluster.nanites['nanite_id'].should_not be_nil
|
193
|
-
end
|
194
|
-
|
195
|
-
it "should use hash of the Nanite's services and status as value" do
|
196
|
-
@cluster.register(@register_packet)
|
197
|
-
@cluster.nanites['nanite_id'].keys.size == 2
|
198
|
-
@cluster.nanites['nanite_id'].keys.should include(:services)
|
199
|
-
@cluster.nanites['nanite_id'].keys.should include(:status)
|
200
|
-
@cluster.nanites['nanite_id'][:services].should == ["the_nanite_services"]
|
201
|
-
@cluster.nanites['nanite_id'][:status].should == "nanite_status"
|
202
|
-
end
|
203
|
-
|
204
|
-
it "should add nanite to reaper" do
|
205
|
-
@reaper.should_receive(:timeout).with('nanite_id', 33)
|
206
|
-
@cluster.register(@register_packet)
|
207
|
-
end
|
208
|
-
|
209
|
-
it "should log info message that nanite was registered" do
|
210
|
-
Nanite::Log.should_receive(:info)
|
211
|
-
@cluster.register(@register_packet)
|
212
|
-
end
|
213
|
-
|
214
|
-
describe "with registered callbacks" do
|
215
|
-
before(:each) do
|
216
|
-
@register_callback = lambda {|request, mapper|}
|
217
|
-
@cluster = Nanite::Cluster.new(@amq, 32, "the_identity", @serializer, @mapper, nil, :register => @register_callback)
|
218
|
-
end
|
219
|
-
|
220
|
-
it "should call the registration callback" do
|
221
|
-
@register_callback.should_receive(:call).with("nanite_id", @mapper)
|
222
|
-
@cluster.register(@register_packet)
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
describe "when sending an invalid packet to the registration queue" do
|
227
|
-
it "should log a message statement" do
|
228
|
-
Nanite::Log.logger.should_receive(:warn).with("RECV [register] Invalid packet type: Nanite::Ping")
|
229
|
-
@cluster.register(Nanite::Ping.new(nil, nil))
|
230
|
-
end
|
231
|
-
end
|
232
|
-
end # Nanite Registration
|
233
|
-
|
234
|
-
describe "Unregister" do
|
235
|
-
before(:each) do
|
236
|
-
@fanout = mock("fanout")
|
237
|
-
@binding = mock("binding", :subscribe => true)
|
238
|
-
@queue = mock("queue", :bind => @binding)
|
239
|
-
@amq = mock("AMQueue", :queue => @queue, :fanout => @fanout)
|
240
|
-
@serializer = mock("Serializer")
|
241
|
-
@reaper = mock("Reaper", :timeout => true)
|
242
|
-
Nanite::Log.stub!(:info)
|
243
|
-
Nanite::Reaper.stub!(:new).and_return(@reaper)
|
244
|
-
@cluster = Nanite::Cluster.new(@amq, 32, "the_identity", @serializer, @mapper)
|
245
|
-
@cluster.nanites["nanite_id"] = "nanite_id"
|
246
|
-
@unregister_packet = Nanite::UnRegister.new("nanite_id")
|
247
|
-
end
|
248
|
-
|
249
|
-
it "should delete the nanite" do
|
250
|
-
@cluster.register(@unregister_packet)
|
251
|
-
@cluster.nanites["nanite_id"].should == nil
|
252
|
-
end
|
253
|
-
|
254
|
-
describe "with registered callbacks" do
|
255
|
-
before(:each) do
|
256
|
-
@unregister_callback = lambda {|request, mapper| }
|
257
|
-
@cluster = Nanite::Cluster.new(@amq, 32, "the_identity", @serializer, @mapper, nil, :unregister => @unregister_callback)
|
258
|
-
@cluster.nanites["nanite_id"] = "nanite_id"
|
259
|
-
end
|
260
|
-
|
261
|
-
it "should call the unregister callback" do
|
262
|
-
@unregister_callback.should_receive(:call).with("nanite_id", @mapper)
|
263
|
-
@cluster.register(@unregister_packet)
|
264
|
-
end
|
265
|
-
end
|
266
|
-
end
|
267
|
-
|
268
|
-
describe "Nanite timed out" do
|
269
|
-
before(:each) do
|
270
|
-
@fanout = mock("fanout")
|
271
|
-
@binding = mock("binding", :subscribe => true)
|
272
|
-
@queue = mock("queue", :bind => @binding)
|
273
|
-
@amq = mock("AMQueue", :queue => @queue, :fanout => @fanout)
|
274
|
-
@serializer = mock("Serializer")
|
275
|
-
Nanite::Log.stub!(:info)
|
276
|
-
@register_packet = Nanite::Register.new("nanite_id", ["the_nanite_services"], "nanite_status",[])
|
277
|
-
end
|
278
|
-
|
279
|
-
it "should remove the nanite when timed out" do
|
280
|
-
EM.run do
|
281
|
-
@cluster = Nanite::Cluster.new(@amq, 0.01, "the_identity", @serializer, @mapper)
|
282
|
-
@cluster.register(@register_packet)
|
283
|
-
EM.add_timer(1.1) {
|
284
|
-
@cluster.nanites["nanite_id"].should == nil
|
285
|
-
EM.stop_event_loop
|
286
|
-
}
|
287
|
-
end
|
288
|
-
end
|
289
|
-
|
290
|
-
it "should call the timed out callback handler when registered" do
|
291
|
-
EM.run do
|
292
|
-
@cluster = Nanite::Cluster.new(@amq, 0.01, "the_identity", @serializer, @mapper)
|
293
|
-
@cluster.register(@register_packet)
|
294
|
-
EM.add_timer(1.1) {
|
295
|
-
@cluster.nanites["nanite_id"].should == nil
|
296
|
-
EM.stop_event_loop
|
297
|
-
}
|
298
|
-
end
|
299
|
-
end
|
300
|
-
end
|
301
|
-
|
302
|
-
describe "Route" do
|
303
|
-
|
304
|
-
before(:each) do
|
305
|
-
@fanout = mock("fanout")
|
306
|
-
@binding = mock("binding", :subscribe => true)
|
307
|
-
@queue = mock("queue", :bind => @binding)
|
308
|
-
@amq = mock("AMQueue", :queue => @queue, :fanout => @fanout)
|
309
|
-
@serializer = mock("Serializer")
|
310
|
-
@reaper = mock("Reaper")
|
311
|
-
Nanite::Reaper.stub!(:new).and_return(@reaper)
|
312
|
-
@cluster = Nanite::Cluster.new(@amq, 32, "the_identity", @serializer, @mapper)
|
313
|
-
@request = mock("Request")
|
314
|
-
end
|
315
|
-
|
316
|
-
it "should publish request to all targets" do
|
317
|
-
target1 = mock("Target 1")
|
318
|
-
target2 = mock("Target 2")
|
319
|
-
@cluster.should_receive(:publish).with(@request, target1)
|
320
|
-
@cluster.should_receive(:publish).with(@request, target2)
|
321
|
-
EM.run {
|
322
|
-
@cluster.route(@request, [target1, target2])
|
323
|
-
EM.stop
|
324
|
-
}
|
325
|
-
end
|
326
|
-
|
327
|
-
end # Route
|
328
|
-
|
329
|
-
|
330
|
-
describe "Publish" do
|
331
|
-
|
332
|
-
before(:each) do
|
333
|
-
@fanout = mock("fanout")
|
334
|
-
@binding = mock("binding", :subscribe => true)
|
335
|
-
@queue = mock("queue", :bind => @binding, :publish => true)
|
336
|
-
@amq = mock("AMQueue", :queue => @queue, :fanout => @fanout)
|
337
|
-
@serializer = mock("Serializer", :dump => "dumped_value")
|
338
|
-
@reaper = mock("Reaper")
|
339
|
-
Nanite::Reaper.stub!(:new).and_return(@reaper)
|
340
|
-
@cluster = Nanite::Cluster.new(@amq, 32, "the_identity", @serializer, @mapper)
|
341
|
-
@request = mock("Request", :persistent => true, :target => nil, :target= => nil, :to_s => nil)
|
342
|
-
@target = mock("Target of Request")
|
343
|
-
end
|
344
|
-
|
345
|
-
it "should serialize request before publishing it" do
|
346
|
-
@request.should_receive(:target=).with(@target)
|
347
|
-
@request.should_receive(:target=)
|
348
|
-
@request.should_receive(:target)
|
349
|
-
@serializer.should_receive(:dump).with(@request).and_return("serialized_request")
|
350
|
-
@cluster.publish(@request, @target)
|
351
|
-
end
|
352
|
-
|
353
|
-
it "should publish request to target queue" do
|
354
|
-
@request.should_receive(:target=).with(@target)
|
355
|
-
@request.should_receive(:target=)
|
356
|
-
@request.should_receive(:target)
|
357
|
-
@queue.should_receive(:publish).with("dumped_value", anything())
|
358
|
-
@cluster.publish(@request, @target)
|
359
|
-
end
|
360
|
-
|
361
|
-
it "should persist request based on request setting" do
|
362
|
-
@request.should_receive(:target=).with(@target)
|
363
|
-
@request.should_receive(:target=)
|
364
|
-
@request.should_receive(:target)
|
365
|
-
@request.should_receive(:persistent).and_return(false)
|
366
|
-
@queue.should_receive(:publish).with(anything(), { :persistent => false })
|
367
|
-
@cluster.publish(@request, @target)
|
368
|
-
end
|
369
|
-
|
370
|
-
end # Publish
|
371
|
-
|
372
|
-
describe "Agent Request Handling" do
|
373
|
-
|
374
|
-
before(:each) do
|
375
|
-
@fanout = mock("fanout")
|
376
|
-
@binding = mock("binding", :subscribe => true)
|
377
|
-
@queue = mock("queue", :bind => @binding, :publish => true)
|
378
|
-
@amq = mock("AMPQueue", :queue => @queue, :fanout => @fanout)
|
379
|
-
@serializer = mock("Serializer", :dump => "dumped_value")
|
380
|
-
@target = mock("Target of Request")
|
381
|
-
@reaper = mock("Reaper")
|
382
|
-
Nanite::Reaper.stub!(:new).and_return(@reaper)
|
383
|
-
@request_without_target = mock("Request", :target => nil, :token => "Token",
|
384
|
-
:reply_to => "Reply To", :from => "From", :persistent => true, :identity => "Identity",
|
385
|
-
:payload => "Payload", :to_s => nil)
|
386
|
-
@request_with_target = mock("Request", :target => "Target", :token => "Token",
|
387
|
-
:reply_to => "Reply To", :from => "From", :persistent => true, :payload => "Payload", :to_s => nil)
|
388
|
-
@mapper_with_target = mock("Mapper", :identity => "id")
|
389
|
-
@mapper_without_target = mock("Mapper", :request => false, :identity => @request_without_target.identity)
|
390
|
-
@cluster_with_target = Nanite::Cluster.new(@amq, 32, "the_identity", @serializer, @mapper_with_target)
|
391
|
-
@cluster_without_target = Nanite::Cluster.new(@amq, 32, "the_identity", @serializer, @mapper_without_target)
|
392
|
-
Nanite::Cluster.stub!(:mapper).and_return(@mapper)
|
393
|
-
end
|
394
|
-
|
395
|
-
it "should forward requests with targets" do
|
396
|
-
@mapper_with_target.should_receive(:send_request).with(@request_with_target, anything())
|
397
|
-
@cluster_with_target.__send__(:handle_request, @request_with_target)
|
398
|
-
end
|
399
|
-
|
400
|
-
it "should reply back with nil results for requests with no target when offline queue is disabled" do
|
401
|
-
@mapper_without_target.should_receive(:send_request).with(@request_without_target, anything())
|
402
|
-
Nanite::Result.should_receive(:new).with(@request_without_target.token, @request_without_target.from, nil, @request_without_target.identity)
|
403
|
-
@cluster_without_target.__send__(:handle_request, @request_without_target)
|
404
|
-
end
|
405
|
-
|
406
|
-
it "should hand in an intermediate handler" do
|
407
|
-
@mapper_with_target.should_receive(:send_request) do |request, opts|
|
408
|
-
opts[:intermediate_handler].should be_instance_of(Proc)
|
409
|
-
end
|
410
|
-
|
411
|
-
@cluster_with_target.__send__(:handle_request, @request_with_target)
|
412
|
-
end
|
413
|
-
|
414
|
-
it "should forward the message when send_request failed" do
|
415
|
-
@mapper_with_target.stub!(:send_request).and_return(false)
|
416
|
-
@cluster_with_target.should_receive(:forward_response)
|
417
|
-
@cluster_with_target.__send__(:handle_request, @request_with_target)
|
418
|
-
end
|
419
|
-
|
420
|
-
describe "when getting push requests from an agent" do
|
421
|
-
it "should send the push message through the mapper" do
|
422
|
-
push = Nanite::Push.new(nil, nil)
|
423
|
-
@mapper_with_target.should_receive(:send_push).with(push)
|
424
|
-
@cluster_with_target.__send__(:handle_request, push)
|
425
|
-
end
|
426
|
-
end
|
427
|
-
end # Agent Request Handling
|
428
|
-
|
429
|
-
describe "Heartbeat" do
|
430
|
-
before(:each) do
|
431
|
-
@fanout = mock("fanout")
|
432
|
-
@binding = mock("binding", :subscribe => true)
|
433
|
-
@queue = mock("queue", :bind => @binding, :publish => true)
|
434
|
-
@amq = mock("AMQueue", :queue => @queue, :fanout => @fanout)
|
435
|
-
@serializer = mock("Serializer", :dump => "dumped_value")
|
436
|
-
Nanite::Log.stub!(:info)
|
437
|
-
@ping = stub("ping", :status => 0.3, :identity => "nanite_id")
|
438
|
-
end
|
439
|
-
|
440
|
-
it "should update the nanite status" do
|
441
|
-
run_in_em do
|
442
|
-
@cluster = Nanite::Cluster.new(@amq, 32, "the_identity", @serializer, @mapper)
|
443
|
-
@cluster.nanites["nanite_id"] = {:status => "nanite_status"}
|
444
|
-
@cluster.send :handle_ping, @ping
|
445
|
-
@cluster.nanites["nanite_id"][:status].should == 0.3
|
446
|
-
end
|
447
|
-
end
|
448
|
-
|
449
|
-
it "should reset the agent time out" do
|
450
|
-
run_in_em do
|
451
|
-
@cluster = Nanite::Cluster.new(@amq, 32, "the_identity", @serializer, @mapper)
|
452
|
-
@cluster.reaper.should_receive(:reset_with_autoregister_hack).with("nanite_id", 33)
|
453
|
-
@cluster.nanites["nanite_id"] = {:status => "nanite_status"}
|
454
|
-
@cluster.send :handle_ping, @ping
|
455
|
-
end
|
456
|
-
end
|
457
|
-
|
458
|
-
describe "when timing out after a heartbeat" do
|
459
|
-
it "should remove the nanite" do
|
460
|
-
run_in_em(false) do
|
461
|
-
@cluster = Nanite::Cluster.new(@amq, 0.1, "the_identity", @serializer, @mapper)
|
462
|
-
@cluster.nanites["nanite_id"] = {:status => "nanite_status"}
|
463
|
-
@cluster.send :handle_ping, @ping
|
464
|
-
EM.add_timer(1.5) do
|
465
|
-
@cluster.nanites["nanite_id"].should == nil
|
466
|
-
EM.stop_event_loop
|
467
|
-
end
|
468
|
-
end
|
469
|
-
end
|
470
|
-
|
471
|
-
it "should call the timeout callback when defined" do
|
472
|
-
run_in_em(false) do
|
473
|
-
@timeout_callback = lambda {|nanite, mapper| }
|
474
|
-
@timeout_callback.should_receive(:call).with("nanite_id", @mapper)
|
475
|
-
@cluster = Nanite::Cluster.new(@amq, 0.1, "the_identity", @serializer, @mapper, nil, :timeout => @timeout_callback)
|
476
|
-
@cluster.nanites["nanite_id"] = {:status => "nanite_status"}
|
477
|
-
@cluster.send :handle_ping, @ping
|
478
|
-
EM.add_timer(1.5) do
|
479
|
-
EM.stop_event_loop
|
480
|
-
end
|
481
|
-
end
|
482
|
-
end
|
483
|
-
end
|
484
|
-
end
|
485
|
-
end # Nanite::Cluster
|