From 4470ba26b332cb92d347af00cdb26c716b1a6953 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Mon, 2 Feb 2015 14:36:46 -0500 Subject: [PATCH] 3021: Use `passenger stop` to shut down passenger. Restore database fixtures after running DatabaseCleaner-powered tests. --- sdk/python/tests/run_test_server.py | 21 ++++++++++++++++----- services/api/test/websocket_runner.rb | 10 ++++++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/sdk/python/tests/run_test_server.py b/sdk/python/tests/run_test_server.py index 534c767b87..ddc2799008 100644 --- a/sdk/python/tests/run_test_server.py +++ b/sdk/python/tests/run_test_server.py @@ -61,21 +61,32 @@ def find_server_pid(PID_PATH, wait=10): return server_pid -def kill_server_pid(pidfile, wait=10): +def kill_server_pid(pidfile, wait=10, passenger=False): # Must re-import modules in order to work during atexit import os import signal + import subprocess import time try: + if passenger: + # First try to shut down nicely + restore_cwd = os.getcwd() + os.chdir(os.path.join(SERVICES_SRC_DIR, 'api')) + subprocess.call([ + 'bundle', 'exec', 'passenger', 'stop', '--pid-file', pidfile]) + os.chdir(restore_cwd) now = time.time() timeout = now + wait with open(pidfile, 'r') as f: server_pid = int(f.read()) while now <= timeout: - os.kill(server_pid, signal.SIGTERM) - os.getpgid(server_pid) # throw OSError if no such pid - now = time.time() + if not passenger or timeout - now < wait / 2: + # Half timeout has elapsed. Start sending SIGTERM + os.kill(server_pid, signal.SIGTERM) + # Raise OSError if process has disappeared + os.getpgid(server_pid) time.sleep(0.1) + now = time.time() except IOError: pass except OSError: @@ -143,7 +154,7 @@ def run(leave_running_atexit=False): env=env) if not leave_running_atexit: - atexit.register(kill_server_pid, pid_file) + atexit.register(kill_server_pid, pidfile, passenger=True) match = re.search(r'Accessible via: https://(.*?)/', start_msg) if not match: diff --git a/services/api/test/websocket_runner.rb b/services/api/test/websocket_runner.rb index df72e246a6..65af8ce2bd 100644 --- a/services/api/test/websocket_runner.rb +++ b/services/api/test/websocket_runner.rb @@ -7,7 +7,7 @@ class WebsocketTestRunner < MiniTest::Unit def _system(*cmd) Bundler.with_clean_env do if not system({'ARVADOS_WEBSOCKETS' => 'ws-only', 'RAILS_ENV' => 'test'}, *cmd) - raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}" + raise RuntimeError, "Command failed with exit status #{$?}: #{cmd.inspect}" end end end @@ -34,7 +34,13 @@ class WebsocketTestRunner < MiniTest::Unit begin super(args) ensure - Process.kill('TERM', server_pid) + Dir.chdir($ARV_API_SERVER_DIR) do + _system('passenger', 'stop', '-p3002') + end + # DatabaseCleaner leaves the database empty. Prefer to leave it full. + dc = DatabaseController.new + dc.define_singleton_method :render do |*args| end + dc.reset end end end -- 2.30.2