Class: Multitest

Inherits:
Object
  • Object
show all
Defined in:
lib/multitest/multitest.rb

Overview

The ability to test multiple files across multiple cores simultaneously

Defined Under Namespace

Modules: Tasks

Constant Summary collapse

@@cores =
2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, cores = Multitest.cores) ⇒ Multitest

Takes an options hash.

:files => files to test
:cores => number of cores to use


19
20
21
22
23
24
25
26
27
28
# File 'lib/multitest/multitest.rb', line 19

def initialize(files, cores = Multitest.cores)
  @files = files
  @cores = cores
  @children = []
  @threads = []
  @pipes = []
  
  Test::Unit.run = true
  @files.each{|f| load f}
end

Class Method Details

.coresObject



10
11
12
# File 'lib/multitest/multitest.rb', line 10

def self.cores
  @@cores
end

.cores=(c) ⇒ Object



13
14
15
# File 'lib/multitest/multitest.rb', line 13

def self.cores=(c)
  @@cores = c
end

.find_classes_in_file(f) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/multitest/multitest.rb', line 108

def self.find_classes_in_file(f)
  code = ""
  File.open(f) {|buffer| code = buffer.read}
  matches = code.scan(/class\s+([\S]+)/)
  klasses = matches.collect do |c|
    begin
      if c.first.respond_to? :constantize
        c.first.constantize
      else
        eval(c.first)
      end
    rescue NameError
      # $stderr.write "Could not load [#{c.first}] from [#{f}]\n"
      nil
    rescue SyntaxError
      # $stderr.write "Could not load [#{c.first}] from [#{f}]\n"
      nil
    end
  end
  return klasses.select{|k| k.respond_to? 'suite'}
end

Instance Method Details

#runObject

Run the tests that have been setup



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/multitest/multitest.rb', line 31

def run
  return if @files.empty?
  $stderr.write @files.inspect+"\n"; $stderr.flush
  @cores.times do |c|
    @pipes << PipeDream.new
    @children << SafeFork.fork do
      Signal.trap("TERM") { exit }
      Signal.trap("HUP") { exit }
      pipe = @pipes.last
      pipe.identify_as_child
      pipe.write("[Worker #{c}] Booted\n")
      @result = Test::Unit::TestResult.new
      @result.add_listener(Test::Unit::TestResult::FAULT) do |value|
        $stderr.write value
        $stderr.write "\n\n"
        $stderr.flush
      end
      while !pipe.eof?
        file = pipe.gets.chomp
        begin
          pipe.write "[Worker #{c} Starting: #{file}\n"
          start = Time.now

          klasses = Multitest.find_classes_in_file(file)
          klasses.each{|k| k.suite.run(@result){|status, name| ;}}
          
          finish = Time.now
          pipe.write "[Worker #{c}] Completed: #{file} (#{finish-start})\n"
        rescue => ex
          pipe.write "[Worker #{c}] Failed: #{file} - #{ex.to_s}\n"
        end
      end
      $stderr.write @result.to_s
      $stderr.write "\n"
      $stderr.flush
      pipe.close
    end
  end

  total_files = @files.size
  @threads = []
  @pipes.each do |_p|
    @threads << Thread.new(_p) do |p|
      Signal.trap("TERM") { exit }
      p.identify_as_parent
      # boot message
      p.gets
      while !@files.empty? 
        # puts "#{total_files - @files.size}/#{total_files}"
        # send a file
        p.write("#{@files.pop}\n")
        # print the start message
        msg = p.gets
        # $stdout.write msg; $stdout.flush
        # wait for the complete message
        msg = p.gets
        # print complete message
        if msg =~ /Completed/
          # $stdout.write msg; $stdout.flush
        else
          $stderr.write msg; $stderr.flush
        end
      end
      p.close
    end
  end

  Signal.trap("TERM") do
    puts "Exiting"
    @children.each{|c| Process.kill("TERM",c)}
    @threads.each{|t| Thread.kill(t)}
  end

  @threads.each{|t| t.join}
  @children.each{|c| Process.wait(c)}
end