Browse files

initial commit of the library

  • Loading branch information...
0 parents commit 7c17fe783cbe779c6e938a3b80232ccfd5ce9eb8 Nick Gauthier committed Dec 17, 2010
Showing with 105 additions and 0 deletions.
  1. +25 −0 README.md
  2. +5 −0 Rakefile
  3. +33 −0 lib/minitest/parallel.rb
  4. +42 −0 test/minitest/parallel_test.rb
25 README.md
@@ -0,0 +1,25 @@
+= MiniTest::Parallel
+
+== Usage
+
+ require 'minitest/parallel'
+
+ # optionally set the number of processors
+ MiniTest::Parallel.processor_count = 2
+ # if you don't set the number of processors, it
+ # defaults to the number of processors in your
+ # system
+
+You MUST require minitest/parallel before you require any other minitest
+files. MiniTest::Parallel is a monkeypatch, and therefore requires a
+specific version of the MiniTest gem to be activated.
+
+== Gotchas
+
+MiniTest::Parallel runs your tests in parallel. That means if you try to access
+a shared object (like a database, or a third-party service) you need to
+make sure that you can interact with that services in a parallel manner.
+
+I suggest making a separate directory of serial tests. Then, create two
+rake tasks, one for serial, and one for parallel. In the parallel task,
+require MiniTest::Parallel.
5 Rakefile
@@ -0,0 +1,5 @@
+task :test do
+ system %{ruby -Ilib test/minitest/parallel_test.rb}
+end
+
+task :default => :test
33 lib/minitest/parallel.rb
@@ -0,0 +1,33 @@
+if defined?(MiniTest)
+ raise "Do not require minitest before minitest/parallel\n"
+end
+require 'rubygems'
+gem 'minitest', '=2.0.1'
+gem 'parallel', '=0.5.1'
+require 'parallel'
+require 'minitest/unit'
+
+module MiniTest::Parallel
+ def self.included(base)
+ base.class_eval do
+ alias_method :_run_suites_in_series, :_run_suites
+ alias_method :_run_suites, :_run_suites_in_parallel
+ end
+ end
+
+ def self.processor_count=(procs)
+ @processor_count = procs
+ end
+
+ def self.processor_count
+ @processor_count ||= Parallel.processor_count
+ end
+
+ def _run_suites_in_parallel(suites, type)
+ Parallel.map(suites, :in_processes => MiniTest::Parallel.processor_count) do |suite|
+ _run_suite(suite, type)
+ end
+ end
+end
+
+MiniTest::Unit.send(:include, MiniTest::Parallel)
42 test/minitest/parallel_test.rb
@@ -0,0 +1,42 @@
+require 'minitest/parallel'
+require 'stringio'
+class MiniTest::FirstTest < MiniTest::Unit::TestCase
+ def test_sleep_one
+ sleep(1)
+ end
+end
+
+class MiniTest::SecondTest < MiniTest::Unit::TestCase
+ def test_sleep_two
+ sleep(1)
+ end
+end
+
+$stdout.write "Beginning MiniTest::Parallel Tests\n"
+
+# Stifle
+MiniTest::Unit.output = StringIO.new
+
+# On one processor, this should take at least two seconds
+MiniTest::Parallel.processor_count = 1
+start = Time.now
+MiniTest::Unit.new.run
+if Time.now-start < 2
+ raise "MiniTest::Parallel went faster than it should have"
+else
+ $stdout.write "."
+end
+
+# On two processors, this should take less than 1.25 seconds
+MiniTest::Parallel.processor_count = 2
+start = Time.now
+MiniTest::Unit.new.run
+if Time.now-start > 1.25
+ raise "MiniTest::Parallel did not accelerate the test suite"
+else
+ $stdout.write "."
+end
+
+$stdout.write "\n"
+
+$stdout.write "All tests passed\n"

0 comments on commit 7c17fe7

Please sign in to comment.