\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef.upgrade/embedded/lib/ruby/gems/2.3.0/gems/rake-10.4.2/test/

Viewing File: test_rake_thread_pool.rb

require File.expand_path('../helper', __FILE__)
require 'rake/thread_pool'

class TestRakeTestThreadPool < Rake::TestCase
  include Rake

  def test_pool_executes_in_current_thread_for_zero_threads
    pool = ThreadPool.new(0)
    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 }
    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.map {
      pool.future {
        sleep 0.1
        Thread.current
      }
    }.each { |f|
      f.value
    }

    refute_equal threads[0], threads[1]
    refute_equal Thread.current, threads[0]
    refute_equal Thread.current, threads[1]
  ensure
    pool.join
  end

  def test_pool_creates_the_correct_number_of_threads
    pool = ThreadPool.new(2)
    threads = Set.new
    t_mutex = Mutex.new
    10.times.each do
      pool.future do
        sleep 0.02
        t_mutex.synchronize { threads << Thread.current }
      end
    end
    pool.join
    assert_equal 2, threads.count
  end

  def test_pool_future_does_not_duplicate_arguments
    pool = ThreadPool.new(2)
    obj = Object.new
    captured = nil
    pool.future(obj) { |var| captured = var }
    pool.join
    assert_equal obj, captured
  end

  def test_pool_join_empties_queue
    pool = ThreadPool.new(2)
    repeat = 25
    repeat.times {
      pool.future do
        repeat.times {
          pool.future do
            repeat.times {
              pool.future do end
            }
          end
        }
      end
    }

    pool.join
    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|
      raise CustomError if count < 1
      pool.future(count - 1, &deep_exception_block).value
    end

    assert_raises(CustomError) do
      pool.future(2, &deep_exception_block).value
    end
  ensure
    pool.join
  end

  def test_pool_prevents_deadlock
    pool = ThreadPool.new(5)

    common_dependency_a = pool.future { sleep 0.2 }
    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.map {
      pool.future {
        common_dependency_b.value
        sleep(rand() * 0.01)
      }
    }

    futures_b.each { |f| f.value }
    pool.join
  end

  def test_pool_reports_correct_results
    pool = ThreadPool.new(7)

    a = 18
    b = 5
    c = 3

    result = a.times.map do
      pool.future do
        b.times.map do
          pool.future { sleep rand * 0.001; c }
        end.reduce(0) { |m, f| m + f.value }
      end
    end.reduce(0) { |m, f| m + f.value }

    assert_equal a * b * c, result
    pool.join
  end

end