~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to lib/test/unit/parallel.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'test/unit'
 
2
 
 
3
module Test
 
4
  module Unit
 
5
    class Worker < Runner
 
6
      class << self
 
7
        undef autorun
 
8
      end
 
9
 
 
10
      alias orig_run_suite _run_suite
 
11
      undef _run_suite
 
12
      undef _run_suites
 
13
      undef run
 
14
 
 
15
      def increment_io(orig)
 
16
        *rest, io = 32.times.inject([orig.dup]){|ios, | ios << ios.last.dup }
 
17
        rest.each(&:close)
 
18
        io
 
19
      end
 
20
 
 
21
      def _run_suites(suites, type)
 
22
        suites.map do |suite|
 
23
          _run_suite(suite, type)
 
24
        end
 
25
      end
 
26
 
 
27
      def _run_suite(suite, type)
 
28
        r = report.dup
 
29
        orig_testout = MiniTest::Unit.output
 
30
        i,o = IO.pipe
 
31
 
 
32
        MiniTest::Unit.output = o
 
33
        orig_stdin, orig_stdout = $stdin, $stdout
 
34
 
 
35
        th = Thread.new do
 
36
          begin
 
37
            while buf = (self.verbose ? i.gets : i.read(5))
 
38
              @stdout.puts "p #{[buf].pack("m").gsub("\n","")}"
 
39
            end
 
40
          rescue IOError
 
41
          rescue Errno::EPIPE
 
42
          end
 
43
        end
 
44
 
 
45
        e, f, s = @errors, @failures, @skips
 
46
 
 
47
        begin
 
48
          result = orig_run_suite(suite, type)
 
49
        rescue Interrupt
 
50
          @need_exit = true
 
51
          result = [nil,nil]
 
52
        end
 
53
 
 
54
        MiniTest::Unit.output = orig_testout
 
55
        $stdin = orig_stdin
 
56
        $stdout = orig_stdout
 
57
 
 
58
        o.close
 
59
        begin
 
60
          th.join
 
61
        rescue IOError
 
62
          raise unless ["stream closed","closed stream"].include? $!.message
 
63
        end
 
64
        i.close
 
65
 
 
66
        result << (report - r)
 
67
        result << [@errors-e,@failures-f,@skips-s]
 
68
        result << ($: - @old_loadpath)
 
69
        result << suite.name
 
70
 
 
71
        begin
 
72
          @stdout.puts "done #{[Marshal.dump(result)].pack("m").gsub("\n","")}"
 
73
        rescue Errno::EPIPE; end
 
74
        return result
 
75
      ensure
 
76
        MiniTest::Unit.output = orig_stdout
 
77
        $stdin = orig_stdin
 
78
        $stdout = orig_stdout
 
79
        o.close if o && !o.closed?
 
80
        i.close if i && !i.closed?
 
81
      end
 
82
 
 
83
      def run(args = [])
 
84
        process_args args
 
85
        @@stop_auto_run = true
 
86
        @opts = @options.dup
 
87
        @need_exit = false
 
88
 
 
89
        @old_loadpath = []
 
90
        begin
 
91
          @stdout = increment_io(STDOUT)
 
92
          @stdin = increment_io(STDIN)
 
93
          @stdout.sync = true
 
94
          @stdout.puts "ready"
 
95
          while buf = @stdin.gets
 
96
            case buf.chomp
 
97
            when /^loadpath (.+?)$/
 
98
              @old_loadpath = $:.dup
 
99
              $:.push(*Marshal.load($1.unpack("m")[0].force_encoding("ASCII-8BIT"))).uniq!
 
100
            when /^run (.+?) (.+?)$/
 
101
              @stdout.puts "okay"
 
102
 
 
103
              @options = @opts.dup
 
104
              suites = MiniTest::Unit::TestCase.test_suites
 
105
 
 
106
              begin
 
107
                require $1
 
108
              rescue LoadError
 
109
                @stdout.puts "after #{[Marshal.dump([$1, $!])].pack("m").gsub("\n","")}"
 
110
                @stdout.puts "ready"
 
111
                next
 
112
              end
 
113
              _run_suites MiniTest::Unit::TestCase.test_suites-suites, $2.to_sym
 
114
 
 
115
              if @need_exit
 
116
                begin
 
117
                  @stdout.puts "bye"
 
118
                rescue Errno::EPIPE; end
 
119
                exit
 
120
              else
 
121
                @stdout.puts "ready"
 
122
              end
 
123
            when /^quit$/
 
124
              begin
 
125
                @stdout.puts "bye"
 
126
              rescue Errno::EPIPE; end
 
127
              exit
 
128
            end
 
129
          end
 
130
        rescue Errno::EPIPE
 
131
        rescue Exception => e
 
132
          begin
 
133
            @stdout.puts "bye #{[Marshal.dump(e)].pack("m").gsub("\n","")}"
 
134
          rescue Errno::EPIPE;end
 
135
          exit
 
136
        ensure
 
137
          @stdin.close
 
138
          @stdout.close
 
139
        end
 
140
      end
 
141
    end
 
142
  end
 
143
end
 
144
 
 
145
if $0 == __FILE__
 
146
  module Test
 
147
    module Unit
 
148
      class TestCase < MiniTest::Unit::TestCase
 
149
        def on_parallel_worker?
 
150
          true
 
151
        end
 
152
      end
 
153
    end
 
154
  end
 
155
 
 
156
  Test::Unit::Worker.new.run(ARGV)
 
157
end