Class: Ace::TestRunner::Suite::ProcessMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/test_runner/suite/process_monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_parallel = 10, package_timeout: nil, termination_grace_period: 1.0, clock: nil) ⇒ ProcessMonitor

Returns a new instance of ProcessMonitor.



13
14
15
16
17
18
19
20
21
# File 'lib/ace/test_runner/suite/process_monitor.rb', line 13

def initialize(max_parallel = 10, package_timeout: nil, termination_grace_period: 1.0, clock: nil)
  @max_parallel = max_parallel
  @package_timeout = package_timeout
  @termination_grace_period = termination_grace_period
  @clock = clock || -> { Time.now }
  @processes = {}
  @queue = []
  @completed = []
end

Instance Attribute Details

#max_parallelObject (readonly)

Returns the value of attribute max_parallel.



11
12
13
# File 'lib/ace/test_runner/suite/process_monitor.rb', line 11

def max_parallel
  @max_parallel
end

#processesObject (readonly)

Returns the value of attribute processes.



11
12
13
# File 'lib/ace/test_runner/suite/process_monitor.rb', line 11

def processes
  @processes
end

Instance Method Details

#check_processesObject



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ace/test_runner/suite/process_monitor.rb', line 70

def check_processes
  @processes.each do |name, process_info|
    package = process_info[:package]
    thread = process_info[:thread]
    callback = process_info[:callback]

    stdout_chunk = drain_stream(process_info[:stdout], process_info[:output])
    stderr_chunk = drain_stream(process_info[:stderr], process_info[:stderr_output])

    parse_progress(process_info, stdout_chunk) if stdout_chunk && !stdout_chunk.empty?

    if callback && ((stdout_chunk && !stdout_chunk.empty?) || (stderr_chunk && !stderr_chunk.empty?))
      elapsed = now - process_info[:start_time]
      callback.call(package, {
        status: :running,
        progress: process_info[:tests_run],
        total: process_info[:test_count],
        dots: process_info[:dots],
        elapsed: elapsed
      }, stdout_chunk)
    end

    enforce_timeout(process_info)

    # Check if process completed
    unless thread.alive?
      elapsed = now - process_info[:start_time]
      exit_status = thread.value.exitstatus

      collect_remaining_output(process_info)
      results = build_results(process_info, elapsed, exit_status)
      close_streams(process_info)

      # Final callback
      if callback
        # Use results[:success] from summary.json if available, otherwise check exit code
        # This ensures the package status matches what ace-test actually reported
        success_status = (!results[:success].nil?) ? results[:success] : (exit_status == 0)

        callback.call(package, {
          status: :completed,
          completed: true,
          success: success_status,
          exit_code: exit_status,
          elapsed: elapsed,
          timed_out: process_info[:timeout_triggered],
          interrupted: process_info[:terminated_by] == :interrupt,
          results: results
        }, process_info[:output])
      end

      # Remove from active processes
      @completed << name
    end
  end

  # Remove completed processes
  @completed.each { |name| @processes.delete(name) }
  @completed.clear

  # Start queued processes if we have capacity
  while @processes.size < @max_parallel && !@queue.empty?
    queued = @queue.shift
    start_package(queued[:package], queued[:options], &queued[:callback])
  end
end

#running?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/ace/test_runner/suite/process_monitor.rb', line 137

def running?
  !@processes.empty? || !@queue.empty?
end

#start_package(package, test_options, &callback) ⇒ Object



23
24
25
26
27
28
29
30
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
# File 'lib/ace/test_runner/suite/process_monitor.rb', line 23

def start_package(package, test_options, &callback)
  # Queue the package if we're at max capacity
  if @processes.size >= @max_parallel
    @queue << {package: package, options: test_options, callback: callback}
    callback.call(package, {status: :waiting}, nil) if callback
    return
  end

  # Build command
  cmd = build_command(package, test_options)

  # Start the process
  # Strip assignment context vars to prevent tests from resolving to wrong assignments
  env = ENV.to_h.merge({
    "ACE_ASSIGN_ID" => nil,
    "ACE_ASSIGN_FORK_ROOT" => nil
  })
  start_time = now
  stdin, stdout, stderr, thread = Open3.popen3(env, *cmd, chdir: package["path"], pgroup: true)

  @processes[package["name"]] = {
    package: package,
    thread: thread,
    stdout: stdout,
    stderr: stderr,
    stdin: stdin,
    start_time: start_time,
    callback: callback,
    output: +"",
    stderr_output: +"",
    report_root: test_options["report_dir"],
    test_count: 0,
    tests_run: 0,
    dots: +"",
    timeout: @package_timeout,
    pid: thread.pid,
    pgid: thread.pid,
    terminating: false,
    timeout_triggered: false,
    terminated_by: nil,
    terminate_at: nil
  }

  # Initial callback
  callback.call(package, {status: :running, start_time: start_time}, nil) if callback
end

#stop_all(reason: :interrupt) ⇒ Object



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
# File 'lib/ace/test_runner/suite/process_monitor.rb', line 141

def stop_all(reason: :interrupt)
  @queue.clear

  @processes.each_value do |process_info|
    terminate_process_group(process_info, signal: "TERM", reason: reason)
  end

  deadline = now + @termination_grace_period
  while @processes.values.any? { |info| info[:thread].alive? } && now < deadline
    sleep 0.05
  end

  @processes.each_value do |process_info|
    next unless process_info[:thread].alive?

    terminate_process_group(process_info, signal: "KILL", reason: reason)
  end

  @processes.each_value do |process_info|
    begin
      Timeout.timeout(0.5) { process_info[:thread].value if process_info[:thread].alive? }
    rescue Timeout::Error, StandardError
      nil
    end

    close_streams(process_info)
  end

  @processes.clear
  @completed.clear
end

#wait_allObject



173
174
175
176
177
178
# File 'lib/ace/test_runner/suite/process_monitor.rb', line 173

def wait_all
  while running?
    check_processes
    sleep 0.1
  end
end