Class: TestServer::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/test_server/command_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(timeout:) ⇒ CommandRunner

Returns a new instance of CommandRunner.



14
15
16
# File 'lib/test_server/command_runner.rb', line 14

def initialize(timeout:)
  @timeout = timeout
end

Instance Method Details

#run(command) ⇒ Object



18
19
20
21
22
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
# File 'lib/test_server/command_runner.rb', line 18

def run(command)
  # stdout, stderr pipes
  rout, wout = IO.pipe
  rerr, werr = IO.pipe

  result = OpenStruct.new

  begin
    result.pid = Process.spawn(command, :out => wout, :err => werr)
    _, status = Timeout::timeout(timeout) { Process.wait2(result.pid) }

    result.status = status.exitstatus
  rescue Timeout::Error
    Process.kill 'KILL', result.pid
    result.status = 99
  end

  # close write ends so we could read them
  wout.close
  werr.close

  result.stdout = rout.readlines
  result.stderr = rerr.readlines

  # dispose the read ends of the pipes
  rout.close
  rerr.close

  result
end