Skip to content
Merged
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
48 changes: 20 additions & 28 deletions spec/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,32 +188,6 @@ def log_and_run( logpath, *cmd )

extend Loggable

### Check the current directory for directories that look like they're
### testing directories from previous tests, and tell any postgres instances
### running in them to shut down.
def stop_existing_postmasters
# tmp_test_0.22329534700318
pat = Pathname.getwd + 'tmp_test_*'
Pathname.glob( pat.to_s ).each do |testdir|
datadir = testdir + 'data'
pidfile = datadir + 'postmaster.pid'
if pidfile.exist? && pid = pidfile.read.chomp.to_i
trace "pidfile (%p) exists: %d" % [ pidfile, pid ]
begin
Process.kill( 0, pid )
rescue Errno::ESRCH
trace "No postmaster running for %s" % [ datadir ]
# Process isn't alive, so don't try to stop it
else
trace "Stopping lingering database at PID %d" % [ pid ]
run 'pg_ctl', '-D', datadir.to_s, '-m', 'fast', 'stop'
end
else
trace "No pidfile (%p)" % [ pidfile ]
end
end
end

class PostgresServer
include Loggable

Expand All @@ -238,6 +212,7 @@ def initialize(name, port: 23456, postgresql_conf: '')

begin
@pgdata.mkpath
stop_existing_cluster
setup_cluster(postgresql_conf)
start_cluster
rescue => err
Expand Down Expand Up @@ -317,6 +292,25 @@ def start_cluster
log_and_run @logfile, pg_bin_path('pg_ctl'), '-w', '-o', sopt, '-D', @pgdata.to_s, 'start'
end

def stop_existing_cluster
pidfile = @pgdata + 'postmaster.pid'

if pidfile.exist? && pid = pidfile.read.chomp.to_i
trace "pidfile (%p) exists: %d" % [ pidfile, pid ]
begin
Process.kill( 0, pid )
rescue Errno::ESRCH
trace "No postmaster running for %s" % [ @pgdata ]
# Process isn't alive, so don't try to stop it
else
trace "Stopping lingering database at PID %d" % [ pid ]
run pg_bin_path('pg_ctl'), '-D', @pgdata.to_s, '-m', 'fast', 'stop'
end
else
trace "No pidfile (%p)" % [ pidfile ]
end
end

def generate_ssl_certs(output_dir)
gen = CertGenerator.new(output_dir)

Expand Down Expand Up @@ -701,8 +695,6 @@ def set_etc_hosts(hostaddr, hostname)

### Automatically set up and tear down the database
config.before(:suite) do |*args|
PG::TestingHelpers.stop_existing_postmasters

ENV['PGHOST'] = 'localhost'
ENV['PGPORT'] ||= "23456"
port = ENV['PGPORT'].to_i
Expand Down
Loading