Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/ezmobius/nanite
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael committed Mar 18, 2009
2 parents 2b02a5c + a3c7db5 commit 7c51e1a
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 20 deletions.
7 changes: 4 additions & 3 deletions lib/nanite/job.rb
Expand Up @@ -54,7 +54,7 @@ def process(msg)

end
end
end
end # JobWarden

class Job
attr_reader :results, :request, :token, :targets, :completed, :intermediate_state, :pending_keys, :intermediate_handler
Expand Down Expand Up @@ -100,5 +100,6 @@ def reset_pending_intermediate_state_keys
def completed?
targets.empty?
end
end
end
end # Job

end # Nanite
28 changes: 16 additions & 12 deletions lib/nanite/local_state.rb
Expand Up @@ -5,26 +5,30 @@ def initialize(hsh={})
self[k] = v
end
end

def all_services
map{|n,s| s[:services] }.flatten.uniq
all(:services)
end

def all_tags
map{|n,s| s[:tags] }.flatten.uniq
all(:tags)
end

def nanites_for(service, *tags)
tags = tags.dup.flatten
res = select { |name, state| state[:services].include?(service) }
nanites = select { |name, state| state[:services].include?(service) }
unless tags.empty?
res.select {|a|
p(a[1][:tags] & tags)
!(a[1][:tags] & tags).empty?
}
nanites.select { |a| !(a[1][:tags] & tags).empty? }
else
res
nanites
end
end
end
end

private

def all(key)
map { |n,s| s[key] }.flatten.uniq.compact
end

end # LocalState
end # Nanite
10 changes: 5 additions & 5 deletions spec/job_spec.rb
Expand Up @@ -15,20 +15,20 @@
end

it "should instantiate a new Job" do
Nanite::Job.should_receive(:new).with(@request, @targets, nil).and_return(@job)
Nanite::Job.should_receive(:new).with(@request, @targets, nil, nil).and_return(@job)
@warden.new_job(@request, @targets)
end

it "should add the job to the job list" do
Nanite::Job.should_receive(:new).with(@request, @targets, nil).and_return(@job)
Nanite::Job.should_receive(:new).with(@request, @targets, nil, nil).and_return(@job)
@warden.jobs.size.should == 0
@warden.new_job(@request, @targets)
@warden.jobs.size.should == 1
@warden.jobs["3faba24fcc"].should == @job
end

it "return the newly crated job" do
Nanite::Job.should_receive(:new).with(@request, @targets, nil).and_return(@job)
Nanite::Job.should_receive(:new).with(@request, @targets, nil, nil).and_return(@job)
@warden.new_job(@request, @targets).should == @job
end

Expand All @@ -41,7 +41,7 @@
@message = mock("Message", :token => "3faba24fcc")
@serializer = mock("Serializer", :load => @message)
@warden = Nanite::JobWarden.new(@serializer)
@job = mock("Job", :token => "3faba24fcc", :process => true, :completed? => false, :results => 42)
@job = mock("Job", :token => "3faba24fcc", :process => true, :completed? => false, :results => 42, :pending_keys => [], :intermediate_handler => true)

Nanite::Log.stub!(:debug)
end
Expand Down Expand Up @@ -158,7 +158,7 @@
end

it "should initialize the job block" do
job = Nanite::Job.new(@request, nil, "my block")
job = Nanite::Job.new(@request, nil, nil, "my block")
job.completed.should == "my block"
end

Expand Down
112 changes: 112 additions & 0 deletions spec/local_state_spec.rb
@@ -0,0 +1,112 @@
require File.dirname(__FILE__) + '/spec_helper'
require 'nanite/local_state'

describe "Nanite::LocalState: " do

describe "Class" do

it "should a Hash" do
Nanite::LocalState.new({}).should be_kind_of(Hash)
end

it "should create empty hash if no hash passed in" do
Nanite::LocalState.new.should == {}
end

it "should initialize hash with value passed in" do
state = Nanite::LocalState.new({:a => 1, :b => 2, :c => 3})
state.should == {:a => 1, :b => 2, :c => 3}
end

end # Class


describe "All services" do

it "should return empty array if no services are defined" do
state = Nanite::LocalState.new({:f => { :foo => 1 }, :b => { :bar => 2 }})
state.all_services.should == []
end

it "should return all :services values" do
state = Nanite::LocalState.new({:f => { :foo => 1 }, :b => { :services => "b's services" }, :c => { :services => "c's services" }})
state.all_services.should include("b's services")
state.all_services.should include("c's services")
end

it "should only return one entry for each service" do
state = Nanite::LocalState.new({:f => { :services => "services" }, :b => { :services => "services" }})
state.all_services.size == 1
state.all_services.should == ["services"]
end

end # All services


describe "All tags" do

it "should return empty array if no tags are defined" do
state = Nanite::LocalState.new({:f => { :foo => 1 }, :b => { :bar => 2 }})
state.all_tags.should == []
end

it "should return all :tags values" do
state = Nanite::LocalState.new({:f => { :foo => 1 }, :b => { :tags => ["a", "b"] }, :c => { :tags => ["c", "d"] }})
state.all_tags.should include("a")
state.all_tags.should include("b")
state.all_tags.should include("c")
state.all_tags.should include("d")
end

it "should only return one entry for each tag" do
state = Nanite::LocalState.new({:f => { :foo => 1 }, :b => { :tags => ["a", "b"] }, :c => { :tags => ["a", "c"] }})
state.all_tags.size == 3
state.all_tags.should include("a")
state.all_tags.should include("b")
state.all_tags.should include("c")
end

end # All tags


describe "Nanites lookup" do

it "should find services matching the service criteria if no tags criteria is specified" do
state = Nanite::LocalState.new({:a => { :services => "a's services" }, :b => { :services => "b's services" }})
state.nanites_for("b's services").should == [[:b, {:services => "b's services"}]]
end

it "should find all services matching the service criteria if no tags criteria is specified" do
state = Nanite::LocalState.new({:a => { :services => "services" }, :b => { :services => "services" }, :c => { :services => "other services" }})
state.nanites_for("services").should include([:a, {:services => "services"}])
state.nanites_for("services").should include([:b, {:services => "services"}])
end

it "should only services matching the service criteria that also match the tags criteria" do
state = Nanite::LocalState.new({:a => { :services => "a's services", :tags => ["a_1", "a_2"] }, :b => { :services => "b's services", :tags => ["b_1", "b_2"] }})
state.nanites_for("b's services").should == [[:b, {:tags=>["b_1", "b_2"], :services=>"b's services"}]]
end

it "should also return all tags for services matching the service criteria that also match a single tags criterium" do
state = Nanite::LocalState.new({:a => { :services => "services", :tags => ["t_1", "t_2"] }})
state.nanites_for("services", ["t_1"]).should == [[:a, {:tags=>["t_1", "t_2"], :services=>"services"}]]
end

it "should return services matching the service criteria and also match the tags criterium" do
state = Nanite::LocalState.new({:a => { :services => "a's services", :tags => ["a_1", "a_2"] }, :b => { :services => "b's services", :tags => ["b_1", "b_2"] }})
state.nanites_for("b's services", ["b_1"]).should == [[:b, {:tags=>["b_1", "b_2"], :services=>"b's services"}]]
end

it "should ignore services matching the service criteria and but not the tags criteria" do
state = Nanite::LocalState.new({:a => { :services => "services", :tags => ["t_1", "t_2"] }, :b => { :services => "services", :tags => ["t_3", "t_4"] }})
state.nanites_for("services", ["t_1"]).should == [[:a, {:services => "services", :tags => ["t_1", "t_2"]}]]
end

it "should lookup services matching the service criteria and and any of the tags criteria" do
state = Nanite::LocalState.new({:a => { :services => "services", :tags => ["t_1", "t_2"] }, :b => { :services => "services", :tags => ["t_2", "t_3"] }})
state.nanites_for("services", ["t_1", "t_3"]).should == [[:a, {:services => "services", :tags => ["t_1", "t_2"]}], [:b, {:services => "services", :tags => ["t_2", "t_3"]}]]
end

end # Nanites lookup

end # Nanite::LocalState

0 comments on commit 7c51e1a

Please sign in to comment.