Skip to content

Commit

Permalink
backport r48127 from ruby/ruby trunk.
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbt committed Oct 25, 2014
1 parent 5fc3fe0 commit b769417
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 28 deletions.
12 changes: 12 additions & 0 deletions lib/rake/cpu_counter.rb
Expand Up @@ -44,6 +44,18 @@ def count
end end
end end


begin
require 'etc'
rescue LoadError
else
if Etc.respond_to?(:nprocessors)
undef count
def count
return Etc.nprocessors
end
end
end

def count_via_java_runtime def count_via_java_runtime
Java::Java.lang.Runtime.getRuntime.availableProcessors Java::Java.lang.Runtime.getRuntime.availableProcessors
rescue StandardError rescue StandardError
Expand Down
74 changes: 46 additions & 28 deletions test/test_rake_cpu_counter.rb
Expand Up @@ -8,43 +8,61 @@ def setup
@cpu_counter = Rake::CpuCounter.new @cpu_counter = Rake::CpuCounter.new
end end


def test_count_via_win32 def test_count
if Rake::Win32.windows? then num = @cpu_counter.count
assert_kind_of Numeric, @cpu_counter.count_via_win32 skip 'cannot count CPU' if num == nil
else assert_kind_of Numeric, num
assert_nil @cpu_counter.count_via_win32 assert_operator num, :>=, 1
end
end end


def test_in_path_command def test_count_with_default_nil
with_ruby_in_path do |ruby| def @cpu_counter.count; nil; end
assert_equal ruby, @cpu_counter.in_path_command(ruby) assert_equal(8, @cpu_counter.count_with_default(8))
end assert_equal(4, @cpu_counter.count_with_default)
rescue Errno::ENOENT => e end
raise unless e.message =~ /\bwhich\b/


skip 'cannot find which for this test' def test_count_with_default_raise
def @cpu_counter.count; raise; end
assert_equal(8, @cpu_counter.count_with_default(8))
assert_equal(4, @cpu_counter.count_with_default)
end end


def test_run class TestClassMethod < Rake::TestCase
with_ruby_in_path do |ruby| def setup
assert_equal 7, @cpu_counter.run(ruby, '-e', 'puts 3 + 4') super

@klass = Class.new(Rake::CpuCounter)
end end
end


def with_ruby_in_path def test_count
ruby = File.basename Gem.ruby @klass.class_eval do
ruby_dir = File.dirname Gem.ruby def count; 8; end
end
assert_equal(8, @klass.count)
end


begin def test_count_nil
orig_path, ENV['PATH'] = counted = false
ENV['PATH'], [ruby_dir, *ENV['PATH']].join(File::PATH_SEPARATOR) @klass.class_eval do
define_method(:count) do
counted = true
nil
end
end
assert_equal(4, @klass.count)
assert_equal(true, counted)
end


yield ruby def test_count_raise
ensure counted = false
ENV['PATH'] = orig_path @klass.class_eval do
define_method(:count) do
counted = true
raise
end
end
assert_equal(4, @klass.count)
assert_equal(true, counted)
end end
end end

end end

0 comments on commit b769417

Please sign in to comment.