Class: DeepTest::Database::SetupListener

Inherits:
NullWorkerListener show all
Defined in:
lib/deep_test/database/setup_listener.rb

Overview

Skeleton Listener to help with setting up a separate database for each worker. Calls dump_schema, load_schema, create_database, and drop_database hooks provided by subclasses that implement database setup strategies for particular database flavors.

Direct Known Subclasses

MysqlSetupListener

Constant Summary collapse

DUMPED_SCHEMAS =
[]

Instance Method Summary collapse

Methods inherited from NullWorkerListener

#finished_work, #starting_work

Instance Method Details

#before_starting_workersObject

:nodoc:



16
17
18
# File 'lib/deep_test/database/setup_listener.rb', line 16

def before_starting_workers # :nodoc:
  dump_schema_once
end

#before_syncObject

:nodoc:



12
13
14
# File 'lib/deep_test/database/setup_listener.rb', line 12

def before_sync # :nodoc:
  dump_schema_once
end

#connect_to_databaseObject

Called on each worker after creating database and before loading schema to initialize connections



44
45
46
# File 'lib/deep_test/database/setup_listener.rb', line 44

def connect_to_database
  ActiveRecord::Base.establish_connection(worker_database_config)
end

#create_databaseObject

Called in each worker to create the database named by worker_database.



52
53
54
# File 'lib/deep_test/database/setup_listener.rb', line 52

def create_database
  raise "Subclass must implement"
end

#drop_databaseObject

Called in each worker to drop the database created by create_database. This method is called twice, once before create_database to ensure that no database exists and once at exit to clean as the worker process exits. This method must not fail if the database does not exist when it is called.



63
64
65
# File 'lib/deep_test/database/setup_listener.rb', line 63

def drop_database
  raise "Subclass must implement"
end

#dump_schemaObject

Called before any workers are spawned to dump the schema that will be used for testing. When running distributed, this method is called on the local machine providing the tests to run.

For distributed testing to work, the schema must be dumped in location accessible by all worker machines. The easiest way to accomplish this is to dump it to a location within the working copy.



76
77
78
# File 'lib/deep_test/database/setup_listener.rb', line 76

def dump_schema
  raise "Subclass must implement"
end

#dump_schema_onceObject

:nodoc:



20
21
22
23
24
# File 'lib/deep_test/database/setup_listener.rb', line 20

def dump_schema_once # :nodoc:
  schema_name = master_database_config[:database]
  dump_schema unless DUMPED_SCHEMAS.include?(schema_name)
  DUMPED_SCHEMAS << schema_name
end

#load_schemaObject

Called once in each worker as it is starting to load the schema dumped from dump_schema. Subclasses should load the schema definition into the worker_database



86
87
88
# File 'lib/deep_test/database/setup_listener.rb', line 86

def load_schema
  raise "Subclass must implement"
end

#master_database_configObject

ActiveRecord configuration for the master database, based on RAILS_ENV. If not running Rails, you’ll need to override this to provide the correct configuration.



104
105
106
# File 'lib/deep_test/database/setup_listener.rb', line 104

def master_database_config
  ActiveRecord::Base.configurations[RAILS_ENV].with_indifferent_access
end

#starting(worker) ⇒ Object

:nodoc:



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/deep_test/database/setup_listener.rb', line 26

def starting(worker) # :nodoc:
  @worker = worker

  at_exit do
    DeepTest.logger.debug("dropping database #{worker_database}")
    drop_database
  end

  drop_database
  create_database
  connect_to_database
  load_schema
end

#worker_databaseObject

Unique name for database on machine that worker is running on.



111
112
113
# File 'lib/deep_test/database/setup_listener.rb', line 111

def worker_database
  "deep_test_worker_#{@worker.number}_pid_#{Process.pid}" 
end

#worker_database_configObject

ActiveRecord configuration for the worker database. By default, the same as master_database_config, except that points to worker_database instead of the database named in the master config.



95
96
97
# File 'lib/deep_test/database/setup_listener.rb', line 95

def worker_database_config
  master_database_config.merge(:database => worker_database)
end