summaryrefslogtreecommitdiff
path: root/test/rake/test_rake_thread_pool.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/rake/test_rake_thread_pool.rb')
-rw-r--r--test/rake/test_rake_thread_pool.rb67
1 files changed, 43 insertions, 24 deletions
diff --git a/test/rake/test_rake_thread_pool.rb b/test/rake/test_rake_thread_pool.rb
index cc8163a..93f32fb 100644
--- a/test/rake/test_rake_thread_pool.rb
+++ b/test/rake/test_rake_thread_pool.rb
@@ -7,21 +7,28 @@ class TestRakeTestThreadPool < Rake::TestCase
def test_pool_executes_in_current_thread_for_zero_threads
pool = ThreadPool.new(0)
- f = pool.future{Thread.current}
+ f = pool.future { Thread.current }
pool.join
assert_equal Thread.current, f.value
end
def test_pool_executes_in_other_thread_for_pool_of_size_one
pool = ThreadPool.new(1)
- f = pool.future{Thread.current}
+ f = pool.future { Thread.current }
pool.join
refute_equal Thread.current, f.value
end
def test_pool_executes_in_two_other_threads_for_pool_of_size_two
pool = ThreadPool.new(2)
- threads = 2.times.collect{ pool.future{ sleep 0.1; Thread.current } }.each{|f|f.value}
+ threads = 2.times.map {
+ pool.future {
+ sleep 0.1
+ Thread.current
+ }
+ }.each { |f|
+ f.value
+ }
refute_equal threads[0], threads[1]
refute_equal Thread.current, threads[0]
@@ -35,22 +42,20 @@ class TestRakeTestThreadPool < Rake::TestCase
10.times.each do
pool.future do
sleep 0.02
- t_mutex.synchronize{ threads << Thread.current }
+ t_mutex.synchronize { threads << Thread.current }
end
end
pool.join
assert_equal 2, threads.count
end
- def test_pool_future_captures_arguments
+ def test_pool_future_does_not_duplicate_arguments
pool = ThreadPool.new(2)
- a = 'a'
- b = 'b'
- c = 5 # 5 throws an exception with 5.dup. It should be ignored
- pool.future(a,c){ |a_var,ignore| a_var.capitalize!; b.capitalize! }
+ obj = Object.new
+ captured = nil
+ pool.future(obj) { |var| captured = var }
pool.join
- assert_equal 'a', a
- assert_equal 'b'.capitalize, b
+ assert_equal obj, captured
end
def test_pool_join_empties_queue
@@ -69,35 +74,49 @@ class TestRakeTestThreadPool < Rake::TestCase
}
pool.join
- assert_equal true, pool.__send__(:__queue__).empty?, "queue should be empty"
+ assert_equal(
+ true,
+ pool.__send__(:__queue__).empty?,
+ "queue should be empty")
end
+ CustomError = Class.new(StandardError)
+
# test that throwing an exception way down in the blocks propagates
# to the top
def test_exceptions
pool = ThreadPool.new(10)
deep_exception_block = lambda do |count|
- next raise Exception.new if ( count < 1 )
- pool.future(count-1, &deep_exception_block).value
+ raise CustomError if count < 1
+ pool.future(count - 1, &deep_exception_block).value
end
- assert_raises(Exception) do
+ assert_raises(CustomError) do
pool.future(2, &deep_exception_block).value
end
-
end
def test_pool_prevents_deadlock
pool = ThreadPool.new(5)
common_dependency_a = pool.future { sleep 0.2 }
- futures_a = 10.times.collect { pool.future{ common_dependency_a.value; sleep(rand() * 0.01) } }
+ futures_a = 10.times.map {
+ pool.future {
+ common_dependency_a.value
+ sleep(rand() * 0.01)
+ }
+ }
common_dependency_b = pool.future { futures_a.each { |f| f.value } }
- futures_b = 10.times.collect { pool.future{ common_dependency_b.value; sleep(rand() * 0.01) } }
+ futures_b = 10.times.map {
+ pool.future {
+ common_dependency_b.value
+ sleep(rand() * 0.01)
+ }
+ }
- futures_b.each{|f|f.value}
+ futures_b.each { |f| f.value }
pool.join
end
@@ -108,15 +127,15 @@ class TestRakeTestThreadPool < Rake::TestCase
b = 5
c = 3
- result = a.times.collect do
+ result = a.times.map do
pool.future do
- b.times.collect do
+ b.times.map do
pool.future { sleep rand * 0.001; c }
- end.inject(0) { |m,f| m+f.value }
+ end.reduce(0) { |m, f| m + f.value }
end
- end.inject(0) { |m,f| m+f.value }
+ end.reduce(0) { |m, f| m + f.value }
- assert_equal( (a*b*c), result )
+ assert_equal a * b * c, result
pool.join
end