|
| 1 | +require File.expand_path('../helper', __FILE__) |
| 2 | +require 'rake/thread_pool' |
| 3 | +require 'test/unit/assertions' |
| 4 | + |
| 5 | +class TestRakeTestThreadPool < Rake::TestCase |
| 6 | + include Rake |
| 7 | + |
| 8 | + def test_pool_executes_in_current_thread_for_zero_threads |
| 9 | + pool = ThreadPool.new(0) |
| 10 | + f = pool.future{Thread.current} |
| 11 | + pool.join |
| 12 | + assert_equal Thread.current, f.call |
| 13 | + end |
| 14 | + |
| 15 | + def test_pool_executes_in_other_thread_for_pool_of_size_one |
| 16 | + pool = ThreadPool.new(1) |
| 17 | + f = pool.future{Thread.current} |
| 18 | + pool.join |
| 19 | + refute_equal Thread.current, f.call |
| 20 | + end |
| 21 | + |
| 22 | + def test_pool_executes_in_two_other_threads_for_pool_of_size_two |
| 23 | + pool = ThreadPool.new(2) |
| 24 | + threads = 2.times.collect{ pool.future{ sleep 0.1; Thread.current } }.each{|f|f.call} |
| 25 | + |
| 26 | + refute_equal threads[0], threads[1] |
| 27 | + refute_equal Thread.current, threads[0] |
| 28 | + refute_equal Thread.current, threads[1] |
| 29 | + end |
| 30 | + |
| 31 | + def test_pool_creates_the_correct_number_of_threads |
| 32 | + pool = ThreadPool.new(2) |
| 33 | + threads = Set.new |
| 34 | + t_mutex = Mutex.new |
| 35 | + 10.times.each do |
| 36 | + pool.future do |
| 37 | + sleep 0.02 |
| 38 | + t_mutex.synchronize{ threads << Thread.current } |
| 39 | + end |
| 40 | + end |
| 41 | + pool.join |
| 42 | + assert_equal 2, threads.count |
| 43 | + end |
| 44 | + |
| 45 | + def test_pool_future_captures_arguments |
| 46 | + pool = ThreadPool.new(2) |
| 47 | + a = 'a' |
| 48 | + b = 'b' |
| 49 | + c = 5 # 5 throws an execption with 5.dup. It should be ignored |
| 50 | + pool.future(a,c){ |a_var,ignore| a_var.capitalize!; b.capitalize! } |
| 51 | + pool.join |
| 52 | + assert_equal 'a', a |
| 53 | + assert_equal 'b'.capitalize, b |
| 54 | + end |
| 55 | + |
| 56 | + def test_pool_join_empties_queue |
| 57 | + pool = ThreadPool.new(2) |
| 58 | + repeat = 25 |
| 59 | + repeat.times { pool.future do |
| 60 | + repeat.times { pool.future do |
| 61 | + repeat.times { pool.future do |
| 62 | + ; |
| 63 | + end } |
| 64 | + end } |
| 65 | + end } |
| 66 | + |
| 67 | + pool.join |
| 68 | + assert_equal true, pool.__send__(:__queue__).empty? |
| 69 | + end |
| 70 | + |
| 71 | + # test that throwing an exception way down in the blocks propagates |
| 72 | + # to the top |
| 73 | + def test_exceptions |
| 74 | + pool = ThreadPool.new(10) |
| 75 | + |
| 76 | + deep_exception_block = lambda do |count| |
| 77 | + next raise Exception.new if ( count < 1 ) |
| 78 | + pool.future(count-1, &deep_exception_block).call |
| 79 | + end |
| 80 | + |
| 81 | + assert_raises(Exception) do |
| 82 | + pool.future(2, &deep_exception_block).call |
| 83 | + end |
| 84 | + |
| 85 | + end |
| 86 | + |
| 87 | + def test_pool_always_has_max_threads_doing_work |
| 88 | + # here we need to test that even if some threads are halted, there |
| 89 | + # are always at least max_threads that are not sleeping. |
| 90 | + pool = ThreadPool.new(2) |
| 91 | + initial_sleep_time = 0.2 |
| 92 | + future1 = pool.future { sleep initial_sleep_time } |
| 93 | + dependent_futures = 5.times.collect { pool.future{ future1.call } } |
| 94 | + future2 = pool.future { sleep initial_sleep_time } |
| 95 | + future3 = pool.future { sleep 0.01 } |
| 96 | + |
| 97 | + sleep initial_sleep_time / 2.0 # wait for everything to queue up |
| 98 | + |
| 99 | + # at this point, we should have 5 threads sleeping depending on future1, and |
| 100 | + # two threads doing work on future1 and future 2. |
| 101 | + assert_equal pool.__send__(:__threads__).count, 7 |
| 102 | + |
| 103 | + # future 3 is in the queue because there aren't enough active threads to work on it. |
| 104 | + assert_equal pool.__send__(:__queue__).size, 1 |
| 105 | + |
| 106 | + [future1, dependent_futures, future2, future3].flatten.each { |f| f.call } |
| 107 | + pool.join |
| 108 | + end |
| 109 | + |
| 110 | + def test_pool_prevents_deadlock |
| 111 | + pool = ThreadPool.new(5) |
| 112 | + |
| 113 | + common_dependency_a = pool.future { sleep 0.2 } |
| 114 | + futures_a = 10.times.collect { pool.future{ common_dependency_a.call; sleep(rand() * 0.01) } } |
| 115 | + |
| 116 | + common_dependency_b = pool.future { futures_a.each { |f| f.call } } |
| 117 | + futures_b = 10.times.collect { pool.future{ common_dependency_b.call; sleep(rand() * 0.01) } } |
| 118 | + |
| 119 | + (futures_b).each{|f|f.call} |
| 120 | + pool.join |
| 121 | + end |
| 122 | + |
| 123 | +end |
| 124 | + |
0 commit comments