Class: TestRunner::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/test-runner/executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tests, **opts) ⇒ Executor

Returns a new instance of Executor.

Parameters:

  • tests (Array<Test>)
  • opts (Hash)

Options Hash (**opts):

  • :state_dir (String)
  • :jobs (Integer)
  • :default_timeout (Integer)
  • :stop_on_failure (Boolean)
  • :destructive (Boolean)


15
16
17
18
19
20
21
22
23
24
# File 'lib/test-runner/executor.rb', line 15

def initialize(tests, **opts)
  @tests = tests
  @opts = opts
  @workers = []
  @queue = Queue.new
  tests.each_with_index { |t, i| @queue << [i, t] }
  @results = []
  @stop_work = false
  @mutex = Mutex.new
end

Instance Attribute Details

#mutexObject (readonly, protected)

Returns the value of attribute mutex.



52
53
54
# File 'lib/test-runner/executor.rb', line 52

def mutex
  @mutex
end

#optsObject (readonly)

Returns the value of attribute opts.



6
7
8
# File 'lib/test-runner/executor.rb', line 6

def opts
  @opts
end

#queueObject (readonly, protected)

Returns the value of attribute queue.



52
53
54
# File 'lib/test-runner/executor.rb', line 52

def queue
  @queue
end

#resultsObject (readonly)

Returns the value of attribute results.



6
7
8
# File 'lib/test-runner/executor.rb', line 6

def results
  @results
end

#testsObject (readonly)

Returns the value of attribute tests.



6
7
8
# File 'lib/test-runner/executor.rb', line 6

def tests
  @tests
end

#workersObject (readonly, protected)

Returns the value of attribute workers.



52
53
54
# File 'lib/test-runner/executor.rb', line 52

def workers
  @workers
end

Instance Method Details

#log(msg) ⇒ Object (protected)



142
143
144
# File 'lib/test-runner/executor.rb', line 142

def log(msg)
  mutex.synchronize { puts "[#{Time.now}] #{msg}" }
end

#runArray<TestResult>

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/test-runner/executor.rb', line 27

def run
  log("Running #{tests.length} tests, #{opts[:jobs]} 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.length} tests in #{Time.now - t1} seconds")
  successful = results.select(&:successful?)
  failed = results.reject(&:successful?)
  log("#{successful.length} tests successful")
  log("#{failed.length} tests failed")

  if failed.any?
    log("Failed tests:\n#{failed.map { |r| "  #{r.test.path}" }.join("\n")}")
  end

  results
end

#run_test(test) ⇒ Object (protected)



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/test-runner/executor.rb', line 90

def run_test(test)
  t1 = Time.now
  dir = test_state_dir(test)

  pid = Process.fork do
    FileUtils.mkdir_p(dir)

    out = File.open(File.join(dir, 'test-runner.log'), 'w')
    STDOUT.reopen(out)
    STDERR.reopen(out)
    STDIN.close

    ev = TestRunner::TestEvaluator.new(
      test,
      state_dir: dir,
      sock_dir: test_sock_dir,
      default_timeout: opts[:default_timeout],
      destructive: opts[:destructive],
    )
    ev.run
  end

  Process.wait(pid)

  TestResult.new(
    test,
    $?.exitstatus == 0,
    Time.now - t1,
    dir
  )
end

#run_worker(w_i) ⇒ Object (protected)



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
# File 'lib/test-runner/executor.rb', line 62

def run_worker(w_i)
  loop do
    return if stop_work?

    begin
      i, t = queue.pop(true)
    rescue ThreadError
      return
    end

    prefix = "[#{i+1}/#{tests.length}]"
    log("#{prefix} Running test '#{t.path}'")
    result = run_test(t)

    if result.successful?
      log("#{prefix} Test '#{t.path}' successful in #{result.elapsed_time} seconds")
    else
      log(
        "#{prefix} Test '#{t.path}' failed after #{result.elapsed_time} "+
        "seconds, see #{result.state_dir}"
      )
      stop_work! if opts[:stop_on_failure]
    end

    mutex.synchronize { results << result }
  end
end

#start_worker(i) ⇒ Object (protected)



54
55
56
# File 'lib/test-runner/executor.rb', line 54

def start_worker(i)
  workers << Thread.new { run_worker(i) }
end

#state_dirObject (protected)



138
139
140
# File 'lib/test-runner/executor.rb', line 138

def state_dir
  opts[:state_dir]
end

#stop_work!Object (protected)



122
123
124
# File 'lib/test-runner/executor.rb', line 122

def stop_work!
  @stop_work = true
end

#stop_work?Boolean (protected)

Returns:

  • (Boolean)


126
127
128
# File 'lib/test-runner/executor.rb', line 126

def stop_work?
  @stop_work
end

#test_sock_dirObject (protected)



134
135
136
# File 'lib/test-runner/executor.rb', line 134

def test_sock_dir
  File.join(state_dir, 'socks')
end

#test_state_dir(test) ⇒ Object (protected)



130
131
132
# File 'lib/test-runner/executor.rb', line 130

def test_state_dir(test)
  File.join(state_dir, "os-test-#{test.name}")
end

#wait_for_workersObject (protected)



58
59
60
# File 'lib/test-runner/executor.rb', line 58

def wait_for_workers
  workers.each(&:join)
end