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_test_attempt(i, test, scripts, attempt) ⇒ 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.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/test-runner/executor.rb', line 23 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.
114 115 116 |
# File 'lib/test-runner/executor.rb', line 114 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.
114 115 116 |
# File 'lib/test-runner/executor.rb', line 114 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.
114 115 116 |
# File 'lib/test-runner/executor.rb', line 114 def workers @workers end |
Instance Method Details
#fill_queue ⇒ Object (protected)
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/test-runner/executor.rb', line 116 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)
355 356 357 |
# File 'lib/test-runner/executor.rb', line 355 def log(msg = '') mutex.synchronize { puts "[#{Time.now}] #{msg}" } end |
#run ⇒ Array<TestResult>
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 110 |
# File 'lib/test-runner/executor.rb', line 36 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)
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/test-runner/executor.rb', line 195 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], recreate_disks: opts[:recreate_disks] ) 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']] if result_hash['type'] == '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 elsif result_hash['type'] == 'example' status = if result_hash['success'] if result_hash['pending'] 'pending' elsif result_hash['skip'] 'skipped' else 'succeeded' end elsif result_hash['pending'] 'unexpectedly succeeded' else 'failed' end log("#{prefix} Example [#{result_hash['progress']}/#{result_hash['total']}] '#{result_hash['example']}' #{status} in #{result_hash['elapsed_time'].round(2)} seconds") 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_test_attempt(i, test, scripts, attempt) ⇒ Object (protected)
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/test-runner/executor.rb', line 162 def run_test_attempt(i, test, scripts, attempt) prefix = "[#{i + 1}/#{@test_count}]" script_list = scripts.map { |v| "##{v.name}" }.join(', ') if attempt > 0 log("#{prefix} Retrying test '#{test.path}' (#{script_list}) (attempt #{attempt + 1}/#{test.attempts})") else log("#{prefix} Running test '#{test.path}' (#{script_list})") end 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 result end |
#run_worker(_w_i) ⇒ Object (protected)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/test-runner/executor.rb', line 139 def run_worker(_w_i) loop do return if stop_work? begin i, test, scripts = queue.pop(true) rescue ThreadError return end result = nil test.attempts.times do |attempt| result = run_test_attempt(i, test, scripts, attempt) break if result.expected_result? || stop_work? sleep(5) end mutex.synchronize { results << result } end end |
#start_worker(i) ⇒ Object (protected)
131 132 133 |
# File 'lib/test-runner/executor.rb', line 131 def start_worker(i) workers << Thread.new { run_worker(i) } end |
#state_dir ⇒ Object (protected)
351 352 353 |
# File 'lib/test-runner/executor.rb', line 351 def state_dir opts[:state_dir] end |
#stop_work! ⇒ Object (protected)
335 336 337 |
# File 'lib/test-runner/executor.rb', line 335 def stop_work! @stop_work = true end |
#stop_work? ⇒ Boolean (protected)
339 340 341 |
# File 'lib/test-runner/executor.rb', line 339 def stop_work? @stop_work end |
#test_sock_dir ⇒ Object (protected)
347 348 349 |
# File 'lib/test-runner/executor.rb', line 347 def test_sock_dir File.join(state_dir, 'socks') end |
#test_state_dir(test) ⇒ Object (protected)
343 344 345 |
# File 'lib/test-runner/executor.rb', line 343 def test_state_dir(test) File.join(state_dir, "os-test-#{test.name}") end |
#wait_for_workers ⇒ Object (protected)
135 136 137 |
# File 'lib/test-runner/executor.rb', line 135 def wait_for_workers workers.each(&:join) end |