Merge pull request #8270 from chrisroberts/enhancement/process-stop

Add new methods to Subprocess
This commit is contained in:
Chris Roberts 2017-02-16 13:45:25 -08:00 committed by GitHub
commit 088829de9b
2 changed files with 81 additions and 1 deletions

View File

@ -35,6 +35,26 @@ module Vagrant
@logger = Log4r::Logger.new("vagrant::util::subprocess")
end
# @return [TrueClass, FalseClass] subprocess is currently running
def running?
!!(@process && @process.alive?)
end
# Stop the subprocess if running
#
# @return [TrueClass] FalseClass] true if process was running and stopped
def stop
if @process && @process.alive?
@process.stop
true
else
false
end
end
# Start the process
#
# @return [Result]
def execute
# Get the timeout, if we have one
timeout = @options[:timeout]
@ -62,7 +82,7 @@ module Vagrant
# Build the ChildProcess
@logger.info("Starting process: #{@command.inspect}")
process = ChildProcess.build(*@command)
@process = process = ChildProcess.build(*@command)
# Create the pipes so we can read the output in real time as
# we execute the command.

View File

@ -47,4 +47,64 @@ describe Vagrant::Util::Subprocess do
expect(result.stdout).to eq(data)
end
end
describe "#running?" do
context "when subprocess has not been started" do
it "should return false" do
sp = described_class.new("ls")
expect(sp.running?).to be_false
end
end
context "when subprocess has completed" do
it "should return false" do
sp = described_class.new("ls")
sp.execute
expect(sp.running?).to be_false
end
end
context "when subprocess is running" do
it "should return true" do
sp = described_class.new("sleep", "0.2")
thread = Thread.new{ sp.execute }
sleep(0.1)
expect(sp.running?).to be_true
thread.join
end
end
end
describe "#stop" do
context "when subprocess has not been started" do
it "should return false" do
sp = described_class.new("ls")
expect(sp.stop).to be_false
end
end
context "when subprocess has already completed" do
it "should return false" do
sp = described_class.new("ls")
sp.execute
expect(sp.stop).to be_false
end
end
context "when subprocess is running" do
let(:sp){ described_class.new("sleep", "1") }
it "should return true" do
thread = Thread.new{ sp.execute }
sleep(0.1)
expect(sp.stop).to be_true
thread.join
end
it "should stop the process" do
thread = Thread.new{ sp.execute }
sleep(0.1)
sp.stop
expect(sp.running?).to be_false
thread.join
end
end
end
end