1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Only *configure* the queue in setup; DB creation must come later

setup gets called from the initializer, so it happens more than once in
a test run. Trying to drop the database again after the first process is
connected is.. ineffective. And entirely pointless.

Instead, defer creating the database to start_workers -- which only
happens once, right before we start doing anything real.
This commit is contained in:
Matthew Draper 2015-04-20 09:10:08 +09:30
parent 41ae432f49
commit 8b09b45458
2 changed files with 25 additions and 21 deletions

View file

@ -2,6 +2,15 @@ module QueJobsManager
def setup def setup
require 'sequel' require 'sequel'
ActiveJob::Base.queue_adapter = :que ActiveJob::Base.queue_adapter = :que
Que.mode = :off
Que.worker_count = 1
end
def clear_jobs
Que.clear!
end
def start_workers
que_url = ENV['QUE_DATABASE_URL'] || 'postgres:///active_jobs_que_int_test' que_url = ENV['QUE_DATABASE_URL'] || 'postgres:///active_jobs_que_int_test'
uri = URI.parse(que_url) uri = URI.parse(que_url)
user = uri.user||ENV['USER'] user = uri.user||ENV['USER']
@ -11,24 +20,17 @@ module QueJobsManager
%x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -c 'create database "#{db}"' -U #{user} -t template1} %x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -c 'create database "#{db}"' -U #{user} -t template1}
Que.connection = Sequel.connect(que_url) Que.connection = Sequel.connect(que_url)
Que.migrate! Que.migrate!
Que.mode = :off
Que.worker_count = 1
rescue Sequel::DatabaseConnectionError
puts "Cannot run integration tests for que. To be able to run integration tests for que you need to install and start postgresql.\n"
exit
end
def clear_jobs
Que.clear!
end
def start_workers
@thread = Thread.new do @thread = Thread.new do
loop do loop do
Que::Job.work("integration_tests") Que::Job.work("integration_tests")
sleep 0.5 sleep 0.5
end end
end end
rescue Sequel::DatabaseConnectionError
puts "Cannot run integration tests for que. To be able to run integration tests for que you need to install and start postgresql.\n"
exit
end end
def stop_workers def stop_workers

View file

@ -3,17 +3,7 @@ module QueueClassicJobsManager
ENV['QC_DATABASE_URL'] ||= 'postgres:///active_jobs_qc_int_test' ENV['QC_DATABASE_URL'] ||= 'postgres:///active_jobs_qc_int_test'
ENV['QC_RAILS_DATABASE'] = 'false' ENV['QC_RAILS_DATABASE'] = 'false'
ENV['QC_LISTEN_TIME'] = "0.5" ENV['QC_LISTEN_TIME'] = "0.5"
uri = URI.parse(ENV['QC_DATABASE_URL'])
user = uri.user||ENV['USER']
pass = uri.password
db = uri.path[1..-1]
%x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -c 'drop database if exists "#{db}"' -U #{user} -t template1}
%x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -c 'create database "#{db}"' -U #{user} -t template1}
ActiveJob::Base.queue_adapter = :queue_classic ActiveJob::Base.queue_adapter = :queue_classic
QC::Setup.create
rescue PG::ConnectionBad
puts "Cannot run integration tests for queue_classic. To be able to run integration tests for queue_classic you need to install and start postgresql.\n"
exit
end end
def clear_jobs def clear_jobs
@ -21,12 +11,24 @@ module QueueClassicJobsManager
end end
def start_workers def start_workers
uri = URI.parse(ENV['QC_DATABASE_URL'])
user = uri.user||ENV['USER']
pass = uri.password
db = uri.path[1..-1]
%x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -c 'drop database if exists "#{db}"' -U #{user} -t template1}
%x{#{"PGPASSWORD=\"#{pass}\"" if pass} psql -c 'create database "#{db}"' -U #{user} -t template1}
QC::Setup.create
QC.default_conn_adapter.disconnect QC.default_conn_adapter.disconnect
QC.default_conn_adapter = nil QC.default_conn_adapter = nil
@pid = fork do @pid = fork do
worker = QC::Worker.new(q_name: 'integration_tests') worker = QC::Worker.new(q_name: 'integration_tests')
worker.start worker.start
end end
rescue PG::ConnectionBad
puts "Cannot run integration tests for queue_classic. To be able to run integration tests for queue_classic you need to install and start postgresql.\n"
exit
end end
def stop_workers def stop_workers