Test detach option

This commit is contained in:
sophia 2020-06-30 16:34:44 -05:00
parent ed8d6b007e
commit ee90e11832
2 changed files with 40 additions and 24 deletions

View File

@ -160,6 +160,8 @@ module Vagrant
raise LaunchError.new(ex.message)
end
# If running with the detach option, no need to capture IO or
# ensure program exists.
if @options[:detach]
return
end

View File

@ -2,6 +2,9 @@ require File.expand_path("../../../base", __FILE__)
require "vagrant/util/subprocess"
describe Vagrant::Util::Subprocess do
let(:ls_test_commands) {[ described_class.new("ls"), described_class.new("ls", {:detach => true}) ]}
let(:sleep_test_commands) {[ described_class.new("sleep", "5"), described_class.new("sleep", "5", {:detach => true}) ]}
describe '#execute' do
before do
# ensure we have `cat` and `echo` in our PATH so that we can run these
@ -47,6 +50,11 @@ describe Vagrant::Util::Subprocess do
expect(result.stdout).to eq(data)
end
it 'does not wait for process if detach option specified' do
cat = described_class.new('cat', {:detach => true})
expect(cat.execute).to eq(nil)
end
context "running within AppImage" do
let(:appimage_ld_path) { nil }
let(:exec_path) { "/exec/path" }
@ -102,22 +110,22 @@ describe Vagrant::Util::Subprocess do
end
describe "#running?" do
context "when subprocess has not been started" do
it "should return false" do
sp = described_class.new("ls")
it "should return false when subprocess has not been started" do
ls_test_commands.each do |sp|
expect(sp.running?).to be(false)
end
end
context "when subprocess has completed" do
it "should return false" do
sp = described_class.new("ls")
it "should return false when subprocess has completed" do
ls_test_commands.each do |sp|
sp.execute
sleep(0.1)
expect(sp.running?).to be(false)
end
end
context "when subprocess is running" do
it "should return true" do
sp = described_class.new("sleep", "5")
it "should return true when subprocess is running" do
sleep_test_commands.each do |sp|
thread = Thread.new{ sp.execute }
sleep(0.3)
expect(sp.running?).to be(true)
@ -130,34 +138,40 @@ describe Vagrant::Util::Subprocess do
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)
ls_test_commands.each do |sp|
expect(sp.stop).to be(false)
end
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)
ls_test_commands.each do |sp|
sp.execute
sleep(0.1)
expect(sp.stop).to be(false)
end
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
sleep_test_commands.each do |sp|
thread = Thread.new{ sp.execute }
sleep(0.1)
expect(sp.stop).to be(true)
thread.join
end
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
sleep_test_commands.each do |sp|
thread = Thread.new{ sp.execute }
sleep(0.1)
sp.stop
expect(sp.running?).to be(false)
thread.join
end
end
end
end