Skip to content

Commit

Permalink
Add enable_parallel and disable_parallel.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Jul 28, 2011
1 parent 4aa7ba8 commit 8ee9598
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/failures.rb
Expand Up @@ -31,7 +31,7 @@ def test_another_test_yes
end

def test_inc_test
assert_includes %w[a b c], 'd'
assert_include %w[a b c], 'd'
end

def test_equal
Expand Down
15 changes: 15 additions & 0 deletions examples/parallel.rb
@@ -0,0 +1,15 @@
require File.expand_path('../../lib/para', __FILE__)

class Foo < Para::Test
enable_parallel

test "foo" do
assert true
sleep 1
end

test "foo again" do
assert true
sleep 1
end
end
25 changes: 23 additions & 2 deletions lib/para/base.rb
Expand Up @@ -6,14 +6,31 @@ def thread_count() (ENV['THREADS'] || 1).to_i; end
def printer() @printer ||= Printer.new; end

# Worker
def worker!(q, i) while q.any?; run_test i, *q.shift; end; end
def worker!(q, i)
if i == 0
# For the first thread, just take the next test.
while q.any?
run_test i, *q.shift
end
else
# For other threads, only work on the ones that are enabled for parallel.
while true
test = q.detect { |(testcase, _)| testcase.parallel? } or break
q.delete test
run_test i, *test
end
end
end

# Spawn multiple threads, each doing #work!
def start!
Signal.trap("INT") { printer.done; exit }

queue = Test.all_tests
thread_count.times.map { |i| Thread.new { worker!(queue, i) } }.each { |t| t.join }
threads = (thread_count-1).times.map { |i| Thread.new { worker!(queue, i+1) } }

worker!(queue, 0)
threads.join

printer.done
end
Expand Down Expand Up @@ -46,6 +63,10 @@ def failures() @failures ||= Array.new; end
def teardown(); end
def setup(); end

def self.enable_parallel() @parallel = true; end
def self.disable_parallel() @parallel = false; end
def self.parallel?() !! @parallel; end

def assert(what, msg=nil)
if what
@runner.printer.success
Expand Down

0 comments on commit 8ee9598

Please sign in to comment.