ngauthier-multitest 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Nick Gauthier
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,58 @@
1
+ = multitest
2
+
3
+ == Caveats
4
+
5
+ Multitest currently is not really tested and working with databases all that well. I have a project in Postgres with all of my data done transactionally during the test via factory_girl. It works there. But it didn't work on a quick test in a MySQL project.
6
+
7
+ Multitest is still very young, it is in active development and is no where near complete or robust.
8
+
9
+ == Installation
10
+
11
+ gem install ngauthier-multitest
12
+
13
+ == Usage
14
+
15
+ in your Rakefile:
16
+
17
+ require 'multitest'
18
+ require 'multitest/tasks'
19
+
20
+ on the commandline:
21
+
22
+ rake multitest
23
+ rake multitest:units multitest:functionals multitest:integration
24
+
25
+ == Configuration
26
+
27
+ in your Rakefile:
28
+
29
+ Multitest.cores = 8
30
+
31
+ == Examples
32
+
33
+ On a small app that I have:
34
+
35
+ Normal suite
36
+ time rake test
37
+ 16.38user 2.99system 0:20.38elapsed 95%CPU
38
+
39
+ 2 Cores:
40
+ time rake multitest
41
+ 8.35user 1.64system 0:08.51elapsed 117%CPU
42
+
43
+ Note the wall-time differences of 20.38 vs 8.51. 239% speedup on two cores.
44
+
45
+ == Note on Patches/Pull Requests
46
+
47
+ * Fork the project.
48
+ * Make your feature addition or bug fix.
49
+ * Add tests for it. This is important so I don't break it in a
50
+ future version unintentionally.
51
+ * Commit, do not mess with rakefile, version, or history.
52
+ (if you want to have your own version, that is fine but
53
+ bump version in a commit by itself I can ignore when I pull)
54
+ * Send me a pull request. Bonus points for topic branches.
55
+
56
+ == Copyright
57
+
58
+ Copyright (c) 2009 Nick Gauthier. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "multitest"
8
+ gem.summary = "Run your tests in parallel"
9
+ gem.description = "Run your tests across multiple cores automatically."
10
+ gem.email = "nick@smartlogicsolutions.com"
11
+ gem.homepage = "http://github.com/ngauthier/multitest"
12
+ gem.authors = ["Nick Gauthier"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ gem.files.include(['lib/**/*.rb'])
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/multitest_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "multitest #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,127 @@
1
+ require File.join(File.dirname(__FILE__), 'pipe_dream')
2
+ require 'test/unit/testresult'
3
+ require 'test/unit'
4
+
5
+ # The ability to test multiple files across multiple cores
6
+ # simultaneously
7
+ class Multitest
8
+ @@cores = 2
9
+ def self.cores
10
+ @@cores
11
+ end
12
+ def self.cores=(c)
13
+ @@cores = c
14
+ end
15
+ # Takes an options hash.
16
+ # :files => files to test
17
+ # :cores => number of cores to use
18
+ def initialize(files, cores = Multitest.cores)
19
+ @files = files
20
+ @cores = cores
21
+ @children = []
22
+ @threads = []
23
+ @pipes = []
24
+
25
+ Test::Unit.run = true
26
+ @files.each{|f| load f}
27
+ end
28
+
29
+ # Run the tests that have been setup
30
+ def run
31
+ return if @files.empty?
32
+ $stderr.write @files.inspect+"\n"; $stderr.flush
33
+ @cores.times do |c|
34
+ @pipes << PipeDream.new
35
+ @children << Process.fork do
36
+ Signal.trap("TERM") { exit }
37
+ Signal.trap("HUP") { exit }
38
+ pipe = @pipes.last
39
+ pipe.identify_as_child
40
+ pipe.write("[Worker #{c}] Booted\n")
41
+ while !pipe.eof?
42
+ file = pipe.gets.chomp
43
+ begin
44
+ pipe.write "[Worker #{c} Starting: #{file}\n"
45
+ start = Time.now
46
+
47
+ @result = Test::Unit::TestResult.new
48
+ @result.add_listener(Test::Unit::TestResult::FAULT) do |value|
49
+ # $stderr.write "\n"
50
+ $stderr.write value
51
+ $stderr.write "\n\n"
52
+ $stderr.flush
53
+ end
54
+ klasses = Multitest.find_classes_in_file(file)
55
+ klasses.each{|k| k.suite.run(@result){|status, name| ;}}
56
+
57
+ #puts @result
58
+
59
+ #unless @result[:failures].empty?
60
+ # puts @result[:failures].inspect
61
+ #end
62
+
63
+ #unless @result.errors.empty?
64
+ # puts @result.errors.inspect
65
+ #end
66
+
67
+
68
+ finish = Time.now
69
+ pipe.write "[Worker #{c}] Completed: #{file} (#{finish-start})\n"
70
+ rescue => ex
71
+ pipe.write "[Worker #{c}] Failed: #{file} - #{ex.to_s}\n"
72
+ end
73
+ end
74
+ $stderr.write @result.to_s
75
+ $stderr.write "\n"
76
+ $stderr.flush
77
+ pipe.close
78
+ end
79
+ end
80
+
81
+ total_files = @files.size
82
+ @threads = []
83
+ @pipes.each do |_p|
84
+ @threads << Thread.new(_p) do |p|
85
+ Signal.trap("TERM") { exit }
86
+ p.identify_as_parent
87
+ # boot message
88
+ p.gets
89
+ while !@files.empty?
90
+ # puts "#{total_files - @files.size}/#{total_files}"
91
+ # send a file
92
+ p.write("#{@files.pop}\n")
93
+ # print the start message
94
+ msg = p.gets
95
+ # $stdout.write msg; $stdout.flush
96
+ # wait for the complete message
97
+ msg = p.gets
98
+ # print complete message
99
+ if msg =~ /Completed/
100
+ # $stdout.write msg; $stdout.flush
101
+ else
102
+ $stderr.write msg; $stderr.flush
103
+ end
104
+ end
105
+ p.close
106
+ end
107
+ end
108
+
109
+ Signal.trap("TERM") do
110
+ puts "Exiting"
111
+ @children.each{|c| Process.kill("TERM",c)}
112
+ @threads.each{|t| Thread.kill(t)}
113
+ end
114
+
115
+ @threads.each{|t| t.join}
116
+ @children.each{|c| Process.wait(c)}
117
+ end
118
+
119
+ def self.find_classes_in_file(f)
120
+ code = ""
121
+ File.open(f) {|buffer| code = buffer.read}
122
+ matches = code.scan /class\s+([\S]+)/
123
+ klasses = matches.collect{|c| eval(c.first) }
124
+ return klasses.select{|k| k.respond_to? 'suite'}
125
+ end
126
+
127
+ end
@@ -0,0 +1,52 @@
1
+ class PipeDream
2
+ def initialize
3
+ @child_read, @parent_write = IO.pipe
4
+ @parent_read, @child_write = IO.pipe
5
+ end
6
+
7
+ def gets
8
+ @reader.gets
9
+ end
10
+
11
+ def write(str)
12
+ @writer.write(str)
13
+ @writer.flush
14
+ str
15
+ end
16
+
17
+ def eof?
18
+ @reader.eof?
19
+ end
20
+
21
+ def identify_as_child
22
+ @parent_write.close
23
+ @parent_read.close
24
+ @reader = @child_read
25
+ @writer = @child_write
26
+ end
27
+
28
+ def identify_as_parent
29
+ @child_write.close
30
+ @child_read.close
31
+ @reader = @parent_read
32
+ @writer = @parent_write
33
+ end
34
+
35
+ def close
36
+ done_reading
37
+ done_writing
38
+ end
39
+
40
+ def done_writing
41
+ @writer.close unless @writer.closed?
42
+ end
43
+
44
+ def done_reading
45
+ @reader.close unless @reader.closed?
46
+ end
47
+
48
+ private
49
+ def force_identification
50
+ return "Must identify as child or parent" if @reader.nil? or @writer.nil?
51
+ end
52
+ end
@@ -0,0 +1,31 @@
1
+ module Multitest::Tasks
2
+ desc "Test all your code in parallel"
3
+ task :multitest => ['multitest:units', 'multitest:functionals', 'multitest:integration']
4
+
5
+ namespace :multitest do
6
+ desc "Multi-core test:units"
7
+ task :units do
8
+ pattern = 'test/unit/**/*_test.rb'
9
+ files = Dir.glob(pattern)
10
+ $stderr.write "Running multitest:units\n"
11
+ Multitest.new(files).run
12
+ $stderr.write "Completed multitest:units\n\n"
13
+ end
14
+ desc "Multi-core test:functionals"
15
+ task :functionals do
16
+ pattern = 'test/functional/**/*_test.rb'
17
+ files = Dir.glob(pattern)
18
+ $stderr.write "Running multitest:functionals\n"
19
+ Multitest.new(files).run
20
+ $stderr.write "Completed multitest:functionals\n\n"
21
+ end
22
+ desc "Multi-core test:integration"
23
+ task :integration do
24
+ pattern = 'test/integration/**/*_test.rb'
25
+ files = Dir.glob(pattern)
26
+ $stderr.write "Running multitest:integration\n"
27
+ Multitest.new(files).run
28
+ $stderr.write "Completed multitest:integration\n\n"
29
+ end
30
+ end
31
+ end
data/lib/multitest.rb ADDED
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), 'multitest', 'multitest.rb')
2
+ #require File.join(File.dirname(__FILE__), 'multitest', 'tasks.rb')
data/multitest.gemspec ADDED
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{multitest}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nick Gauthier"]
12
+ s.date = %q{2009-09-24}
13
+ s.description = %q{Run your tests across multiple cores automatically.}
14
+ s.email = %q{nick@smartlogicsolutions.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/multitest.rb",
27
+ "lib/multitest.rb",
28
+ "lib/multitest/multitest.rb",
29
+ "lib/multitest/multitest.rb",
30
+ "lib/multitest/pipe_dream.rb",
31
+ "lib/multitest/pipe_dream.rb",
32
+ "lib/multitest/tasks.rb",
33
+ "lib/multitest/tasks.rb",
34
+ "multitest.gemspec",
35
+ "test/multitest_test.rb",
36
+ "test/test_helper.rb",
37
+ "test/tests/another_test.rb",
38
+ "test/tests/sample_test.rb"
39
+ ]
40
+ s.homepage = %q{http://github.com/ngauthier/multitest}
41
+ s.rdoc_options = ["--charset=UTF-8"]
42
+ s.require_paths = ["lib"]
43
+ s.rubygems_version = %q{1.3.5}
44
+ s.summary = %q{Run your tests in parallel}
45
+ s.test_files = [
46
+ "test/test_helper.rb",
47
+ "test/multitest_test.rb",
48
+ "test/tests/another_test.rb",
49
+ "test/tests/sample_test.rb"
50
+ ]
51
+
52
+ if s.respond_to? :specification_version then
53
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
57
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
58
+ else
59
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
63
+ end
64
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+ require 'multitest'
3
+
4
+ class MultitestTest < Test::Unit::TestCase
5
+ context "a Multitest" do
6
+ setup do
7
+ @mt = Multitest.new([
8
+ File.expand_path(File.join(File.dirname(__FILE__), 'tests', 'sample_test.rb')),
9
+ File.expand_path(File.join(File.dirname(__FILE__), 'tests', 'another_test.rb'))
10
+ ])
11
+ FileUtils.mkdir_p(File.join(File.dirname(__FILE__), '..', 'tmp'))
12
+ FileUtils.rm_rf(File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp', 'test.log')))
13
+ end
14
+
15
+ should "run three instances" do
16
+ @mt.run
17
+ assert File.exists?(File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp', 'test.log')))
18
+ File.open(File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp', 'test.log'))) do |f|
19
+ assert_equal %w(1 1 3).sort, f.read.split("\n").sort
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+
8
+ class Test::Unit::TestCase
9
+ end
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class AnotherTest < Test::Unit::TestCase
4
+ should "output some text to a file" do
5
+ File.open(File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'test.log')), 'a') do |f|
6
+ f.write "3\n"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class SampleTest < Test::Unit::TestCase
4
+ should "output some text to a file" do
5
+ File.open(File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'test.log')), 'a') do |f|
6
+ f.write "1\n"
7
+ end
8
+ end
9
+ end
10
+
11
+ class SampleTestEnhanced < SampleTest
12
+ # will inherite the test above
13
+ end
14
+
15
+ class SampleTestHelper
16
+ def help
17
+ # do something
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ngauthier-multitest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nick Gauthier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Run your tests across multiple cores automatically.
26
+ email: nick@smartlogicsolutions.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/multitest.rb
42
+ - lib/multitest/multitest.rb
43
+ - lib/multitest/pipe_dream.rb
44
+ - lib/multitest/tasks.rb
45
+ - multitest.gemspec
46
+ - test/multitest_test.rb
47
+ - test/test_helper.rb
48
+ - test/tests/another_test.rb
49
+ - test/tests/sample_test.rb
50
+ has_rdoc: false
51
+ homepage: http://github.com/ngauthier/multitest
52
+ licenses:
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Run your tests in parallel
77
+ test_files:
78
+ - test/test_helper.rb
79
+ - test/multitest_test.rb
80
+ - test/tests/another_test.rb
81
+ - test/tests/sample_test.rb