Class: DeepTest::Database::SetupListener
- Inherits:
-
NullListener
- Object
- NullListener
- DeepTest::Database::SetupListener
- Defined in:
- lib/deep_test/database/setup_listener.rb
Overview
Skeleton Listener to help with setting up a separate database for each agent. 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
Constant Summary collapse
- DUMPED_SCHEMAS =
[]
Instance Method Summary collapse
-
#agent_database ⇒ Object
Unique name for database on machine that agent is running on.
-
#agent_database_config ⇒ Object
ActiveRecord configuration for the agent database.
-
#before_starting_agents ⇒ Object
:nodoc:.
-
#before_sync ⇒ Object
:nodoc:.
-
#connect_to_database ⇒ Object
Called on each agent after creating database and before loading schema to initialize connections.
-
#create_database ⇒ Object
Called in each agent to create the database named by
agent_database. -
#drop_database ⇒ Object
Called in each agent to drop the database created by
create_database. -
#dump_schema ⇒ Object
Called before any agents are spawned to dump the schema that will be used for testing.
-
#dump_schema_once ⇒ Object
:nodoc:.
-
#load_schema ⇒ Object
Called once in each agent as it is starting to load the schema dumped from dump_schema.
-
#master_database_config ⇒ Object
ActiveRecord configuration for the master database, based on RAILS_ENV.
-
#starting(agent) ⇒ Object
:nodoc:.
Methods inherited from NullListener
#finished_work, #starting_work
Instance Method Details
#agent_database ⇒ Object
Unique name for database on machine that agent is running on.
111 112 113 |
# File 'lib/deep_test/database/setup_listener.rb', line 111 def agent_database "deep_test_agent_#{@agent.number}_pid_#{Process.pid}" end |
#agent_database_config ⇒ Object
ActiveRecord configuration for the agent database. By default, the same as master_database_config, except that points to agent_database instead of the database named in the master config.
95 96 97 |
# File 'lib/deep_test/database/setup_listener.rb', line 95 def agent_database_config master_database_config.merge(:database => agent_database) end |
#before_starting_agents ⇒ Object
:nodoc:
16 17 18 |
# File 'lib/deep_test/database/setup_listener.rb', line 16 def before_starting_agents # :nodoc: dump_schema_once end |
#before_sync ⇒ Object
:nodoc:
12 13 14 |
# File 'lib/deep_test/database/setup_listener.rb', line 12 def before_sync # :nodoc: dump_schema_once end |
#connect_to_database ⇒ Object
Called on each agent 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(agent_database_config) end |
#create_database ⇒ Object
Called in each agent to create the database named by agent_database.
52 53 54 |
# File 'lib/deep_test/database/setup_listener.rb', line 52 def create_database raise "Subclass must implement" end |
#drop_database ⇒ Object
Called in each agent 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 agent 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_schema ⇒ Object
Called before any agents 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 agent 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_once ⇒ Object
: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_schema ⇒ Object
Called once in each agent as it is starting to load the schema dumped from dump_schema. Subclasses should load the schema definition into the agent_database
86 87 88 |
# File 'lib/deep_test/database/setup_listener.rb', line 86 def load_schema raise "Subclass must implement" end |
#master_database_config ⇒ Object
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(agent) ⇒ 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(agent) # :nodoc: @agent = agent at_exit do DeepTest.logger.debug { "dropping database #{agent_database}" } drop_database end drop_database create_database connect_to_database load_schema end |