Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed problem with readvertisements and added tests for the same

  • Loading branch information...
commit 11025ad5b62acee50db6528807b3d86f80767081 1 parent c28a7ee
@outerim outerim authored
Showing with 126 additions and 11 deletions.
  1. +15 −6 lib/pelvis/agent.rb
  2. +90 −0 spec/actor_spec.rb
  3. +21 −5 spec/helpers.rb
View
21 lib/pelvis/agent.rb
@@ -59,6 +59,9 @@ def actors
private :actors
class ConfiguredActor < SimpleDelegator
+ extend Callbacks
+ callbacks :readvertising
+
def initialize(klass, block)
super(klass)
@block = block || proc {|actor|}
@@ -73,22 +76,28 @@ def start(*a)
def operations_for(job)
super.collect {|_klass, op| [self, op]}
end
+
+ def readvertised_with(a)
+ readvertising(a)
+ end
end
def add_actor(klass, &block)
- if block_given?
- actors << ConfiguredActor.new(klass, block)
- else
- actors << klass
- end
+ actors << ConfiguredActor.new(klass, block)
it = actors.last
it.added_to_agent(self)
it.on_resources_changed {
logger.debug "got resources changed for #{it}, readvertising"
- Advertiser.new(self, [it]).on_completed {
+ a = Advertiser.new(self, [it])
+ a.on_succeeded {
logger.debug "readvertisement successful"
}
+ a.on_failed {
+ logger.debug "readvertisement failed"
+ }
+ it.readvertised_with(a)
}
+ it
end
def advertise
View
90 spec/actor_spec.rb
@@ -0,0 +1,90 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+class FakeHerault < Pelvis::Actor
+ class <<self
+ def reset
+ ads.clear
+ end
+
+ def ads
+ @ads ||= []
+ end
+ end
+
+ operation '/security/advertise'
+ def advertise
+ self.class.ads << params
+ finish
+ end
+end
+
+class FakeActor < Pelvis::Actor
+ class << self
+ def readvertise
+ resources_changed
+ end
+
+ def reset
+ @_on_resources_changed = []
+ end
+
+ def resources_for(op)
+ ["/resource#{op}"]
+ end
+ end
+
+ operation '/test'
+ def test
+ end
+end
+
+describe "An agent" do
+ include Pelvis::Helpers
+
+ before(:each) do
+ FakeHerault.reset; FakeActor.reset
+
+ @agents = [
+ [:herault, [FakeHerault]],
+ [:foo, [FakeActor]],
+ [:bar, []]
+ ]
+ @ad = {"identity"=>identity_for(:foo), "operations"=>{"/test"=>["/resource/test"]}}
+ end
+
+ describe "should advertise it's resources" do
+ it "when loaded into an agent" do
+ start_agents do |agent|
+ FakeHerault.ads.should == [@ad]
+ EM.stop
+ end
+ end
+
+ it "when resources_changed is called" do
+ after = lambda {
+ FakeHerault.ads.should == [@ad, @ad]
+ }
+
+ start_em(after) do
+ connect(:herault) do |agent|
+ agent.add_actor FakeHerault
+ end
+ connect(:foo) do |agent|
+ actor = agent.add_actor(FakeActor)
+ agent.on_advertised {
+ FakeActor.readvertise
+ actor.on_readvertising { |a|
+ a.on_succeeded {
+ EM.stop
+ }
+ a.on_failed {
+ EM.stop
+ }
+ }
+ }
+ end
+ end
+ end
+ end
+end
+
View
26 spec/helpers.rb
@@ -23,17 +23,27 @@ def identity_for(name)
PROTOCOL.new(CONFIGS[name]).identity
end
- def start_agents(&block)
- EM.run do
+ def start_em(after=nil)
+ EM.run(nil, after) do
Protocols::Local::SET.clear
+ yield
+ end
+ end
+
+ def start_agents(&block)
+ start_em do
agent_connect(@agents, &block)
end
end
+ def connect(agent, &block)
+ options = CONFIGS[agent].dup
+ Pelvis.connect(PROTOCOL.registered_as, options, &block)
+ end
+
def agent_connect(agents, &block)
agent, actors = *agents.shift
- options = CONFIGS[agent].dup
- connection = Pelvis.connect(PROTOCOL.registered_as, options) do |agent|
+ connection = connect(agent) do |agent|
actors.each do |actor|
agent.add_actor actor
end
@@ -56,7 +66,13 @@ def should_be_good(results, exp_response=nil)
results.should be_completed
results.should_not be_failed
- results.data.should == exp_response
+ # We need to make sure each expected response is in the dataset
+ # nothing more, order is unpredictable
+ exp_response.each do |data|
+ results.data.should include(data)
+ results.data.delete_at results.data.index(data)
+ end
+ results.data.should be_empty #by now
end
def should_not_be_good(results)
Please sign in to comment.
Something went wrong with that request. Please try again.