Permalink
Browse files

fix a bug in Tests#balance! that breaks projects with less tests then…

… there are workers
  • Loading branch information...
1 parent e3230ef commit 4f57f35b7fa36f9f670449d3293e738f1184fbe8 Jared Grippe committed with May 16, 2012
@@ -73,9 +73,10 @@ def duration
end
def jobs
- @jobs ||= keys \
- .inject([]){|indices, key| key =~ /^job:(\d+):.*/ and indices << $1.to_i; indices } \
- .uniq.sort.map{|index| Job.new(self, index) }
+ @jobs ||= tests.map(&:job).uniq.sort.inject([]){|jobs, index|
+ jobs[index] ||= Job.new(self, index)
+ jobs
+ }
end
def logger
@@ -65,6 +65,9 @@ def balance_for! number_of_jobs
# calculate estimates runtimes
each(&:calculate_estimated_runtime!)
+ # celing the number of jobs at the number of tests we have to run
+ number_of_jobs = self.size if number_of_jobs > self.size
+
# one job is easy
return each{|test| test.job = 0 } if number_of_jobs == 1
@@ -170,75 +170,86 @@ def job_est_runtimes
end
- end
+ describe "balance_for!" do
- describe "balance_for!" do
+ def balance_for! est_runtimes, number_of_jobs
+ tests = Factory.tests
+ est_runtimes.each_with_index{|est_runtime, index|
+ tests.add("scenario:features/#{index}.feature")
+ tests["scenario:features/#{index}.feature"].est_runtime = est_runtime
+ }
+ tests.balance_for! number_of_jobs
+ tests.map(&:job).sort
+ end
- def balance_for! est_runtimes, number_of_jobs
- tests = Factory.tests
- est_runtimes.each_with_index{|est_runtime, index|
- tests.add("scenario:features/#{index}.feature")
- tests["scenario:features/#{index}.feature"].est_runtime = est_runtime
- }
- tests.balance_for! number_of_jobs
- tests.map(&:job).sort
- end
+ context "when we have never run these tests before" do
+ it "should give each job an equal number of tests" do
+ balance_for!([nil,nil,nil,nil,nil,nil,nil,nil,nil,nil], 1).should == [0,0,0,0,0,0,0,0,0,0]
+ balance_for!([nil,nil,nil,nil,nil,nil,nil,nil,nil,nil], 2).should == [0,0,0,0,0,1,1,1,1,1]
+ balance_for!([nil,nil,nil,nil,nil,nil,nil,nil,nil,nil], 3).should == [0,0,0,0,1,1,1,2,2,2]
+ balance_for!([nil,nil,nil,nil,nil,nil,nil,nil,nil,nil], 4).should == [0,0,0,1,1,2,2,3,3,3]
+ end
+ end
- context "when we have never run these tests before" do
- it "should give each job an equal number of tests" do
- balance_for!([nil,nil,nil,nil,nil,nil,nil,nil,nil,nil], 1).should == [0,0,0,0,0,0,0,0,0,0]
- balance_for!([nil,nil,nil,nil,nil,nil,nil,nil,nil,nil], 2).should == [0,0,0,0,0,1,1,1,1,1]
- balance_for!([nil,nil,nil,nil,nil,nil,nil,nil,nil,nil], 3).should == [0,0,0,0,1,1,1,2,2,2]
- balance_for!([nil,nil,nil,nil,nil,nil,nil,nil,nil,nil], 4).should == [0,0,0,1,1,2,2,3,3,3]
+ context "when we have run these tests before" do
+ it "should give each job a balanced number of tests" do
+ balance_for!([ 1, 1, 1, 1, 1, 1, 1, 1, 1,10], 1).should == [0,0,0,0,0,0,0,0,0,0]
+ balance_for!([ 1, 1, 1, 1, 1, 1, 1, 1, 1,10], 2).should == [0,1,1,1,1,1,1,1,1,1]
+ end
end
- end
- context "when we have run these tests before" do
- it "should give each job a balanced number of tests" do
- balance_for!([ 1, 1, 1, 1, 1, 1, 1, 1, 1,10], 1).should == [0,0,0,0,0,0,0,0,0,0]
- balance_for!([ 1, 1, 1, 1, 1, 1, 1, 1, 1,10], 2).should == [0,1,1,1,1,1,1,1,1,1]
+ context "when there are more workers then tests" do
+ it "should schedule only as many jobs as their are tests" do
+ test_run = Factory.test_run
+ test_run.tests.add("spec:models/user_spec.rb")
+ test_run.tests.add("spec:models/post_spec.rb")
+ test_run.tests.balance_for! 60
+ test_run.jobs.size.should == 2
+ test_run.tests.map(&:job).sort.should == [0,1]
+ end
end
+
+ # subject{
+ # Hobson::Project::TestRun::Tests.new(stub(
+ # :[] => nil, :[]= => nil, :data => {}
+ # ))
+ # }
+
+ # before{
+ # 10.times{|i| tests["features/#{i}.feature"].status = "waiting" }
+ # }
+
+ # context "when we have never run these tests before" do
+ # it "should give each job an equal number of tests" do
+ # tests.map(&:job).should == [nil,nil,nil,nil,nil,nil,nil,nil,nil,nil]
+ # tests.balance_for! 1
+ # tests.map(&:job).should == [0,0,0,0,0,0,0,0,0,0]
+ # tests.balance_for! 2
+ # tests.map(&:job).sort.should == [0,0,0,0,0,1,1,1,1,1]
+ # tests.balance_for! 3
+ # tests.map(&:job).sort.should == [0,0,0,0,1,1,1,2,2,2]
+ # end
+ # end
+
+ # context "when we have run these tests before" do
+ # before{
+ # [1,1,1,1,1,5,10].each_with_index{|est_runtime, index|
+ # tests.to_a[index].est_runtime = est_runtime
+ # }
+ # }
+ # it "should give each job a balanced number of tests" do
+ # debugger;1
+ # tests.map(&:job).should == [nil,nil,nil,nil,nil,nil,nil,nil,nil,nil]
+ # tests.balance_for! 1
+ # tests.map(&:job).should == [0,0,0,0,0,0,0,0,0,0]
+ # tests.balance_for! 2
+ # tests.map(&:job).sort.should == [0,0,0,0,0,0,0,0,0,1]
+ # # tests.balance_for! 3
+ # # tests.map(&:job).sort.should == [0,0,0,0,1,1,1,2,2,2]
+ # end
+ # end
end
- # subject{
- # Hobson::Project::TestRun::Tests.new(stub(
- # :[] => nil, :[]= => nil, :data => {}
- # ))
- # }
-
- # before{
- # 10.times{|i| tests["features/#{i}.feature"].status = "waiting" }
- # }
-
- # context "when we have never run these tests before" do
- # it "should give each job an equal number of tests" do
- # tests.map(&:job).should == [nil,nil,nil,nil,nil,nil,nil,nil,nil,nil]
- # tests.balance_for! 1
- # tests.map(&:job).should == [0,0,0,0,0,0,0,0,0,0]
- # tests.balance_for! 2
- # tests.map(&:job).sort.should == [0,0,0,0,0,1,1,1,1,1]
- # tests.balance_for! 3
- # tests.map(&:job).sort.should == [0,0,0,0,1,1,1,2,2,2]
- # end
- # end
-
- # context "when we have run these tests before" do
- # before{
- # [1,1,1,1,1,5,10].each_with_index{|est_runtime, index|
- # tests.to_a[index].est_runtime = est_runtime
- # }
- # }
- # it "should give each job a balanced number of tests" do
- # debugger;1
- # tests.map(&:job).should == [nil,nil,nil,nil,nil,nil,nil,nil,nil,nil]
- # tests.balance_for! 1
- # tests.map(&:job).should == [0,0,0,0,0,0,0,0,0,0]
- # tests.balance_for! 2
- # tests.map(&:job).sort.should == [0,0,0,0,0,0,0,0,0,1]
- # # tests.balance_for! 3
- # # tests.map(&:job).sort.should == [0,0,0,0,1,1,1,2,2,2]
- # end
- # end
end
end
@@ -77,7 +77,8 @@
describe "status" do
it "should accurately reflect the test run's status" do
test_run = Factory.test_run
- Factory.job(test_run, 0)
+ test_run.tests.add('spec:models/user_spec.rb')
+ test_run.tests.first.job = 0
test_run.status.should == 'waiting...'

0 comments on commit 4f57f35

Please sign in to comment.