Skip to content

Instantly share code, notes, and snippets.

@lukemelia
Created June 9, 2010 20:48
Show Gist options
  • Save lukemelia/432144 to your computer and use it in GitHub Desktop.
Save lukemelia/432144 to your computer and use it in GitHub Desktop.
How we're using Hydra's listener and messaging architecture to have each runner spin up a database after it boots. See also: http://github.com/ngauthier/hydra/wikis/multiple-test-databases
require 'hydra'
require 'hydra/tasks'
Hydra::TestTask.new('hydra:specs') do |t|
require 'hydra_extensions'
t.verbose = !!ENV['VERBOSE']
t.listeners << HydraExtensions::Listener::MultipleDatabases.new
t.add_files 'spec/**/*_spec.rb'
end
Hydra::TestTask.new('hydra:features') do |t|
require 'hydra_extensions'
t.verbose = !!ENV['VERBOSE']
t.listeners << HydraExtensions::Listener::MultipleDatabases.new
t.autosort = false
t.add_files 'features/activity_feeds/*.feature'
end
require 'hydra_extensions/listener/multiple_databases'
require 'hydra_extensions/messages/master_messages'
require 'hydra_extensions/messages/worker_messages'
module HydraExtensions
module Messages
module Master
class CreateDatabases < Hydra::Message
def handle(worker)
worker.runners.each do |runner|
runner[:io].write(HydraExtensions::Messages::Worker::CreateDatabase.new)
end
end
end
class DropDatabases < Hydra::Message
def handle(worker)
worker.runners.each do |runner|
runner[:io].write(HydraExtensions::Messages::Worker::DropDatabase.new)
end
end
end
end
end
end
module HydraExtensions
module Listener
class MultipleDatabases < Hydra::Listener::Abstract
def worker_begin(worker)
worker[:io].write(HydraExtensions::Messages::Master::CreateDatabases.new)
end
# Fired before shutting down the worker
def worker_end(worker)
worker[:io].write(HydraExtensions::Messages::Master::DropDatabases.new) if worker[:io]
end
end
end
end
module HydraExtensions
module Messages
module Worker
# Message telling the runner to create database.
class CreateDatabase < Hydra::Message
def handle(runner)
ENV['TEST_ENV_NUMBER'] = "hydra_#{Process.pid}"
schema_file = Rails.root.join('db','development_structure.sql')
`/usr/local/mysql/bin/mysql -u root -f -e 'drop database if exists #{ENV['TEST_ENV_NUMBER']}'`
`/usr/local/mysql/bin/mysqladmin -u root create #{ENV['TEST_ENV_NUMBER']}`
`/usr/local/mysql/bin/mysql -u root #{ENV['TEST_ENV_NUMBER']} < #{schema_file}`
end
end
class DropDatabase < Hydra::Message
def handle(runner)
`/usr/local/mysql/bin/mysqladmin -u root -f drop #{ENV['TEST_ENV_NUMBER']}`
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment