Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing issue where a server running in the background cannot be stopped #33

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/chef_zero/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# limitations under the License.
#

require 'net/http'
require 'openssl'
require 'puma'
require 'rubygems'
Expand Down Expand Up @@ -125,12 +126,21 @@ def start_background(wait = 5)
end

def running?
!!server.running
begin
uri = URI(@url)
if uri.host == '0.0.0.0' then uri.host = '127.0.0.1' end
Net::HTTP.get_response(uri)
rescue Errno::ECONNREFUSED
false
else
true
end
end

def stop(wait = 5)
if @thread
@thread.join(wait)
server.stop(false)
raise unless @thread.join(wait)
else
server.stop(true)
end
Expand Down
21 changes: 21 additions & 0 deletions spec/server_stops.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'bundler'
require 'bundler/setup'
require 'rspec'

require 'chef_zero/server'

describe ChefZero::Server do
describe '#stop' do
context 'when the server is started in the background' do
let(:server) do
server = ChefZero::Server.new()
server.start_background
server
end
it 'stops' do
server.stop
expect(server.running?).to eq false
end
end
end
end