Class: TestRunner::Executor
- Inherits:
-
Object
- Object
- TestRunner::Executor
- Defined in:
- lib/test-runner/executor.rb
Instance Attribute Summary collapse
-
#mutex ⇒ Object
readonly
protected
Returns the value of attribute mutex.
- #opts ⇒ Hash readonly
-
#queue ⇒ Object
readonly
protected
Returns the value of attribute queue.
- #results ⇒ Array<TestResult> readonly
- #test_scripts ⇒ Array<TestScript> readonly
-
#workers ⇒ Object
readonly
protected
Returns the value of attribute workers.
Instance Method Summary collapse
- #fill_queue ⇒ Object protected
-
#initialize(test_scripts, **opts) ⇒ Executor
constructor
A new instance of Executor.
- #log(msg = '') ⇒ Object protected
- #run ⇒ Array<TestResult>
- #run_test(test, scripts, prefix:) ⇒ Object protected
- #run_worker(_w_i) ⇒ Object protected
- #start_worker(i) ⇒ Object protected
- #state_dir ⇒ Object protected
- #stop_work! ⇒ Object protected
- #stop_work? ⇒ Boolean protected
- #test_sock_dir ⇒ Object protected
- #test_state_dir(test) ⇒ Object protected
- #wait_for_workers ⇒ Object protected
Constructor Details
#initialize(test_scripts, **opts) ⇒ Executor
Returns a new instance of Executor.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/test-runner/executor.rb', line 22 def initialize(test_scripts, **opts) @test_scripts = test_scripts @opts = opts @workers = [] @queue = Queue.new @results = [] @stop_work = false @mutex = Mutex.new fill_queue end |
Instance Attribute Details
#mutex ⇒ Object (readonly, protected)
Returns the value of attribute mutex.
113 114 115 |
# File 'lib/test-runner/executor.rb', line 113 def mutex @mutex end |
#opts ⇒ Hash (readonly)
10 11 12 |
# File 'lib/test-runner/executor.rb', line 10 def opts @opts end |
#queue ⇒ Object (readonly, protected)
Returns the value of attribute queue.
113 114 115 |
# File 'lib/test-runner/executor.rb', line 113 def queue @queue end |
#results ⇒ Array<TestResult> (readonly)
13 14 15 |
# File 'lib/test-runner/executor.rb', line 13 def results @results end |
#test_scripts ⇒ Array<TestScript> (readonly)
7 8 9 |
# File 'lib/test-runner/executor.rb', line 7 def test_scripts @test_scripts end |
#workers ⇒ Object (readonly, protected)
Returns the value of attribute workers.
113 114 115 |
# File 'lib/test-runner/executor.rb', line 113 def workers @workers end |
Instance Method Details
#fill_queue ⇒ Object (protected)
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/test-runner/executor.rb', line 115 def fill_queue tests = {} test_scripts.each do |ts| tests[ts.test] ||= [] tests[ts.test] << ts end tests.to_a.shuffle!.each_with_index do |(test, scripts), i| @queue << [i, test, scripts.shuffle!] end @test_count = tests.length end |
#log(msg = '') ⇒ Object (protected)
314 315 316 |
# File 'lib/test-runner/executor.rb', line 314 def log(msg = '') mutex.synchronize { puts "[#{Time.now}] #{msg}" } end |
#run ⇒ Array<TestResult>
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 107 108 109 |
# File 'lib/test-runner/executor.rb', line 35 def run log("Running #{test_scripts.length} scripts of #{@test_count} tests, #{opts[:jobs]} tests at a time") log("State directory is #{state_dir}") t1 = Time.now opts[:jobs].times do |i| start_worker(i) end wait_for_workers log("Run #{results.inject(0) { |acc, r| acc + r.script_results.length }} test scripts of #{@test_count} tests in #{(Time.now - t1).round(2)} seconds") expected_successful = results.select do |r| r.expected_to_succeed? && r.successful? end expected_failed = results.select do |r| r.expected_to_fail? && r.failed? end unexpected_failed = results.select do |r| r.expected_to_succeed? && r.failed? end unexpected_successful = results.select do |r| r.expected_to_fail? && r.successful? end if expected_successful.any? log("#{expected_successful.length} tests successful") end if expected_failed.any? log("#{expected_failed.length} tests failed as expected") end if unexpected_failed.any? log("#{unexpected_failed.length} tests should have succeeded, but failed") end if unexpected_successful.any? log("#{unexpected_successful.length} tests should have failed, but succeeded") end if unexpected_failed.any? log('Unexpectedly failed test scripts:') unexpected_failed.each do |test_result| test_result.script_results.each do |test_script_result| next if test_script_result.expected_result? log(" #{test_script_result.test_script.path}") end end puts end if unexpected_successful.any? log('Unexpectedly successful test scripts:') unexpected_successful.each do |test_result| test_result.script_results.each do |test_script_result| next if test_script_result.expected_result? log(" #{test_script_result.test_script.path}") end end puts end results end |
#run_test(test, scripts, prefix:) ⇒ Object (protected)
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/test-runner/executor.rb', line 174 def run_test(test, scripts, prefix:) t1 = Time.now dir = test_state_dir(test) r, w = IO.pipe # 4 ports for use with boot.qemu.networks.[i].socket.mcast.port mcast_ports = OsVm::PortReservation.get_ports(key: test.name, size: 4) pid = Process.fork do r.close FileUtils.mkdir_p(dir) out = File.open(File.join(dir, 'test-runner.log'), 'w') $stdout.reopen(out) $stderr.reopen(out) $stdin.close OsVm::PortReservation.reset_to_ports(mcast_ports) ev = TestRunner::TestEvaluator.new( test, scripts, state_dir: dir, sock_dir: test_sock_dir, default_timeout: opts[:default_timeout], destructive: opts[:destructive] ) ev.run do |result_hash| w.puts(result_hash.to_json) end end w.close script_results = [] begin r.each_line do |line| begin result_hash = JSON.parse(line) rescue JSON::ParserError warn "Unable to parse test script result json: #{line.inspect}" next end test_script = test.test_scripts[result_hash['script']] script_result = TestScriptResult.new( test_script, result_hash['success'], result_hash['elapsed_time'] ) script_results << script_result next if test_script.singleton? secs = script_result.elapsed_time.round(2) if script_result.expected_result? if script_result.successful? log("#{prefix} Script '#{test_script.path}' successful in #{secs} seconds") else log("#{prefix} Script '#{test_script.path}' failed as expected in #{secs} seconds") end else # unexpected result if script_result.successful? log("#{prefix} Script '#{test_script.path}' unexpectedly succeeded in #{secs} seconds") else log("#{prefix} Script '#{test_script.path}' failed after #{secs} seconds") end stop_work! if opts[:stop_on_failure] end end rescue EOFError # pass end Process.wait(pid) OsVm::PortReservation.release_ports(key: test.name) # Complement script results if some are missing scripts.each do |script| script_result = script_results.detect { |sr| sr.test_script == script } next if script_result script_results << TestScriptResult.new(script, false, -1) end result = TestResult.new( test, script_results, $?.exitstatus == 0, Time.now - t1, dir ) File.open(File.join(dir, 'test-result.txt'), 'w') do |f| str = if result.expected_result? if result.successful? 'expected_success' else 'expected_failure' end elsif result.successful? 'unexpected_success' else 'unexpected_failure' end f.puts(str) end result end |
#run_worker(_w_i) ⇒ Object (protected)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/test-runner/executor.rb', line 138 def run_worker(_w_i) loop do return if stop_work? begin i, test, scripts = queue.pop(true) rescue ThreadError return end prefix = "[#{i + 1}/#{@test_count}]" log("#{prefix} Running test '#{test.path}' (#{scripts.map { |v| "##{v.name}" }.join(', ')})") result = run_test(test, scripts, prefix:) secs = result.elapsed_time.round(2) if result.expected_result? if result.successful? log("#{prefix} Test '#{test.path}' successful in #{secs} seconds") else log("#{prefix} Test '#{test.path}' failed as expected in #{secs} seconds") end else # unexpected result if result.successful? log("#{prefix} Test '#{test.path}' unexpectedly succeeded in #{secs} seconds, see #{result.state_dir}") else log("#{prefix} Test '#{test.path}' failed after #{secs} seconds, see #{result.state_dir}") end stop_work! if opts[:stop_on_failure] end mutex.synchronize { results << result } end end |
#start_worker(i) ⇒ Object (protected)
130 131 132 |
# File 'lib/test-runner/executor.rb', line 130 def start_worker(i) workers << Thread.new { run_worker(i) } end |
#state_dir ⇒ Object (protected)
310 311 312 |
# File 'lib/test-runner/executor.rb', line 310 def state_dir opts[:state_dir] end |
#stop_work! ⇒ Object (protected)
294 295 296 |
# File 'lib/test-runner/executor.rb', line 294 def stop_work! @stop_work = true end |
#stop_work? ⇒ Boolean (protected)
298 299 300 |
# File 'lib/test-runner/executor.rb', line 298 def stop_work? @stop_work end |
#test_sock_dir ⇒ Object (protected)
306 307 308 |
# File 'lib/test-runner/executor.rb', line 306 def test_sock_dir File.join(state_dir, 'socks') end |
#test_state_dir(test) ⇒ Object (protected)
302 303 304 |
# File 'lib/test-runner/executor.rb', line 302 def test_state_dir(test) File.join(state_dir, "os-test-#{test.name}") end |
#wait_for_workers ⇒ Object (protected)
134 135 136 |
# File 'lib/test-runner/executor.rb', line 134 def wait_for_workers workers.each(&:join) end |