Skip to content

Commit

Permalink
better failure handling, all tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
lee committed May 16, 2009
1 parent 194f084 commit a00d1ab
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
23 changes: 16 additions & 7 deletions lib/pelvis/actor.rb
Expand Up @@ -66,13 +66,8 @@ def initialize(invocation, operation) #, config, deployment_resources)

def start
@started_at = Time.now
if self.class.resources
my_resources = [params[:resources]].flatten.compact & self.class.resources
if my_resources.empty?
fail :code => 404, :message => "invalid resources #{params[:resources]} supplied"
end
params[:resources] = my_resources
end
@orig_resources = [params.delete(:resources)].flatten.compact
validate_resources

send(@operation)
rescue => e
Expand All @@ -83,6 +78,20 @@ def start
end
end

def validate_resources
if self.class.resources
if @orig_resources.empty?
fail :code => 500, :message => "A resource is required for this operation"
elsif allowed_resources.empty?
fail :code => 404, :message => "Invalid resources #{@orig_resources} for this operation"
end
end
end

def allowed_resources
@allowed_resources ||= @orig_resources & self.class.resources
end

def finish
@finished_at = Time.now
completed "Duration: #{duration}"
Expand Down
4 changes: 3 additions & 1 deletion lib/pelvis/protocols/local.rb
Expand Up @@ -19,7 +19,9 @@ def evoke(identity, job)
if remote_agent = agent_for(identity)
remote_agent.invoke(agent.identity, job)
else
raise "Could not find an agent found #{identity.inspect}"
i = Incall.new(nil, nil, nil)
i.failed(:code => 500, :message =>"Could not find an agent found #{identity.inspect}")
i
end
end

Expand Down
6 changes: 5 additions & 1 deletion lib/pelvis/protocols/xmpp/incall.rb
Expand Up @@ -14,7 +14,11 @@ def initialize(agent, job)

def start
@agent.send_job_init(job.token, job.scope, job.operation, job.args) do |reply|
initialized
if reply["type"] == 'error'
failed(:message => "Got jabber error", :data => reply)
else
initialized
end
end
end

Expand Down
14 changes: 10 additions & 4 deletions spec/request_spec.rb
Expand Up @@ -29,13 +29,13 @@
end

it "errors when the remote end is down" do
pending "This should call into the 'failed' callback"
#pending "This should call into the 'failed' callback"

results = TestDelegate.new
start_agents do |agent|
agent.request(:direct, "/echo", {:number => 1, :hash => { :one => 2 }}, :identities => ["broken"], :delegate => results)
agent.request(:direct, "/echo", {:number => 1, :hash => { :one => 2 }}, :identities => ["broken@localhost"], :delegate => results)
end
should_be_good(results)
should_not_be_good(results)
end

describe "with scope" do
Expand Down Expand Up @@ -87,7 +87,13 @@
end

describe "that don't have a resource argument" do
it "should fail when sent to an actor that uses resources"
it "should fail when sent to an actor that uses resources" do
results = TestDelegate.new
start_agents do |agent|
agent.request(:direct, '/w_resource', {:test => 'bla'}, :identities => [identity_for(:foo)], :delegate => results)
end
should_not_be_good(results)
end
end

end

0 comments on commit a00d1ab

Please sign in to comment.