From ba7ad5bcb10ba33ffd0e4825a34148056006fdeb Mon Sep 17 00:00:00 2001 From: ck Date: Sat, 14 Mar 2009 08:05:27 -0400 Subject: [PATCH 1/3] Nanite::LocalState cleanup and spec'ing. DRY'd up some code and removed print statement in Nanite::LocalState. Started specs for Nanite::LocalState. --- lib/nanite/local_state.rb | 24 ++++++++----- spec/local_state_spec.rb | 71 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 spec/local_state_spec.rb diff --git a/lib/nanite/local_state.rb b/lib/nanite/local_state.rb index f56eb05..cfbc2c4 100644 --- a/lib/nanite/local_state.rb +++ b/lib/nanite/local_state.rb @@ -5,26 +5,32 @@ 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) } unless tags.empty? - res.select {|a| - p(a[1][:tags] & tags) - !(a[1][:tags] & tags).empty? + res.select {|a| + !(a[1][:tags] & tags).empty? } else res end end - end -end \ No newline at end of file + + private + + def all(key) + map { |n,s| s[key] }.flatten.uniq.compact + end + + end # LocalState +end # Nanite diff --git a/spec/local_state_spec.rb b/spec/local_state_spec.rb new file mode 100644 index 0000000..49106df --- /dev/null +++ b/spec/local_state_spec.rb @@ -0,0 +1,71 @@ +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 + +end # Nanite::LocalState From 7017c60b67ff4d5c820ff881de5a95cc3fdfff9d Mon Sep 17 00:00:00 2001 From: ck Date: Sat, 14 Mar 2009 10:02:08 -0400 Subject: [PATCH 2/3] Nanite::LocalState: Minor (cosmetic) work and additional specs. --- lib/nanite/local_state.rb | 8 +++----- spec/local_state_spec.rb | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/lib/nanite/local_state.rb b/lib/nanite/local_state.rb index cfbc2c4..27a4a98 100644 --- a/lib/nanite/local_state.rb +++ b/lib/nanite/local_state.rb @@ -16,13 +16,11 @@ def all_tags 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| - !(a[1][:tags] & tags).empty? - } + nanites.select { |a| !(a[1][:tags] & tags).empty? } else - res + nanites end end diff --git a/spec/local_state_spec.rb b/spec/local_state_spec.rb index 49106df..1f4bcbc 100644 --- a/spec/local_state_spec.rb +++ b/spec/local_state_spec.rb @@ -68,4 +68,45 @@ 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 From a3c7db58161f82d11db468a8443775de7e3781db Mon Sep 17 00:00:00 2001 From: ck Date: Tue, 17 Mar 2009 21:36:22 -0400 Subject: [PATCH 3/3] Fixed specs for Nanite::Job and Nanite::JobWarden. Minor formating. --- lib/nanite/job.rb | 7 ++++--- spec/job_spec.rb | 10 +++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/nanite/job.rb b/lib/nanite/job.rb index 4405a4d..6e189cb 100644 --- a/lib/nanite/job.rb +++ b/lib/nanite/job.rb @@ -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 @@ -100,5 +100,6 @@ def reset_pending_intermediate_state_keys def completed? targets.empty? end - end -end \ No newline at end of file + end # Job + +end # Nanite diff --git a/spec/job_spec.rb b/spec/job_spec.rb index 5c3e556..ca6e070 100644 --- a/spec/job_spec.rb +++ b/spec/job_spec.rb @@ -15,12 +15,12 @@ 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 @@ -28,7 +28,7 @@ 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 @@ -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 @@ -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