8 eval "use Moose; use Test::Moose";
10 ? ( skip_all => 'needs Moose for testing' )
14 use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
15 use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer';
16 use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant';
17 use_ok 'DBIx::Class::Storage::DBI::Replicated';
21 This is a test of the replicated storage system. This will work in one of
22 two ways, either it was try to fake replication with a couple of SQLite DBs
23 and creative use of copy, or if you define a couple of %ENV vars correctly
24 will try to test those. If you do that, it will assume the setup is properly
25 replicating. Your results may vary, but I have demonstrated this to work with
26 mysql native replication.
31 ## ----------------------------------------------------------------------------
32 ## Build a class to hold all our required testing data and methods.
33 ## ----------------------------------------------------------------------------
37 ## --------------------------------------------------------------------- ##
38 ## Create an object to contain your replicated stuff.
39 ## --------------------------------------------------------------------- ##
41 package DBIx::Class::DBI::Replicated::TestReplication;
44 use base qw/Class::Accessor::Fast/;
46 __PACKAGE__->mk_accessors( qw/schema/ );
48 ## Initialize the object
52 my $self = $class->SUPER::new(@_);
54 $self->schema( $self->init_schema );
58 ## Get the Schema and set the replication storage type
63 my $schema = DBICTest->init_schema(
65 '::DBI::Replicated' => {
66 balancer_type=>'::Random',
68 auto_validate_every=>100,
80 sub generate_replicant_connect_info {}
85 ## --------------------------------------------------------------------- ##
86 ## Subclass for when you are using SQLite for testing, this provides a fake
87 ## replication support.
88 ## --------------------------------------------------------------------- ##
90 package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
94 use base 'DBIx::Class::DBI::Replicated::TestReplication';
96 __PACKAGE__->mk_accessors( qw/master_path slave_paths/ );
98 ## Set the mastep path from DBICTest
101 my $class = shift @_;
102 my $self = $class->SUPER::new(@_);
104 $self->master_path( DBICTest->_sqlite_dbfilename );
106 "t/var/DBIxClass_slave1.db",
107 "t/var/DBIxClass_slave2.db",
113 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
114 ## $storage->connect_info to be used for connecting replicants.
116 sub generate_replicant_connect_info {
120 } @{$self->slave_paths};
122 return map { [$_,'','',{AutoCommit=>1}] } @dsn;
125 ## Do a 'good enough' replication by copying the master dbfile over each of
126 ## the slave dbfiles. If the master is SQLite we do this, otherwise we
127 ## just do a one second pause to let the slaves catch up.
131 foreach my $slave (@{$self->slave_paths}) {
132 copy($self->master_path, $slave);
136 ## Cleanup after ourselves. Unlink all gthe slave paths.
140 foreach my $slave (@{$self->slave_paths}) {
145 ## --------------------------------------------------------------------- ##
146 ## Subclass for when you are setting the databases via custom export vars
147 ## This is for when you have a replicating database setup that you are
148 ## going to test against. You'll need to define the correct $ENV and have
149 ## two slave databases to test against, as well as a replication system
150 ## that will replicate in less than 1 second.
151 ## --------------------------------------------------------------------- ##
153 package DBIx::Class::DBI::Replicated::TestReplication::Custom;
154 use base 'DBIx::Class::DBI::Replicated::TestReplication';
156 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
157 ## $storage->connect_info to be used for connecting replicants.
159 sub generate_replicant_connect_info {
161 [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
162 [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],
166 ## pause a bit to let the replication catch up
173 ## ----------------------------------------------------------------------------
174 ## Create an object and run some tests
175 ## ----------------------------------------------------------------------------
177 ## Thi first bunch of tests are basic, just make sure all the bits are behaving
179 my $replicated_class = DBICTest->has_custom_dsn ?
180 'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
181 'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
183 ok my $replicated = $replicated_class->new
184 => 'Created a replication object';
186 isa_ok $replicated->schema
187 => 'DBIx::Class::Schema';
189 isa_ok $replicated->schema->storage
190 => 'DBIx::Class::Storage::DBI::Replicated';
192 ok $replicated->schema->storage->meta
193 => 'has a meta object';
195 isa_ok $replicated->schema->storage->master
196 => 'DBIx::Class::Storage::DBI';
198 isa_ok $replicated->schema->storage->pool
199 => 'DBIx::Class::Storage::DBI::Replicated::Pool';
201 does_ok $replicated->schema->storage->balancer
202 => 'DBIx::Class::Storage::DBI::Replicated::Balancer';
204 ok my @replicant_connects = $replicated->generate_replicant_connect_info
205 => 'got replication connect information';
207 ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
208 => 'Created some storages suitable for replicants';
210 isa_ok $replicated->schema->storage->balancer->current_replicant
211 => 'DBIx::Class::Storage::DBI';
213 ok $replicated->schema->storage->pool->has_replicants
214 => 'does have replicants';
216 is $replicated->schema->storage->pool->num_replicants => 2
217 => 'has two replicants';
219 does_ok $replicated_storages[0]
220 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
222 does_ok $replicated_storages[1]
223 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
225 my @replicant_names = keys %{$replicated->schema->storage->replicants};
227 does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
228 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
230 does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
231 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
233 ## Add some info to the database
237 ->populate('Artist', [
238 [ qw/artistid name/ ],
239 [ 4, "Ozric Tentacles"],
242 ## Make sure all the slaves have the table definitions
244 $replicated->replicate;
246 ## Make sure we can read the data.
248 ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
252 => 'DBICTest::Artist';
254 is $artist1->name, 'Ozric Tentacles'
255 => 'Found expected name for first result';
257 ## Add some new rows that only the master will have This is because
258 ## we overload any type of write operation so that is must hit the master
263 ->populate('Artist', [
264 [ qw/artistid name/ ],
265 [ 5, "Doom's Children"],
266 [ 6, "Dead On Arrival"],
271 ## We can't do this test if we have a custom replicants, since we assume
272 ## if there are custom one that you are trying to test a real replicating
273 ## system. See docs above for more.
275 skip 'Cannot test inconsistent replication since you have a real replication system', 1
276 if DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
278 ## Alright, the database 'cluster' is not in a consistent state. When we do
279 ## a read now we expect bad news
280 is $replicated->schema->resultset('Artist')->find(5), undef
281 => 'read after disconnect fails because it uses a replicant which we have neglected to "replicate" yet';
284 ## Make sure all the slaves have the table definitions
285 $replicated->replicate;
287 ## Should find some data now
289 ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
293 => 'DBICTest::Artist';
295 is $artist2->name, "Doom's Children"
296 => 'Found expected name for first result';
298 ## What happens when we disconnect all the replicants?
300 is $replicated->schema->storage->pool->connected_replicants => 2
301 => "both replicants are connected";
303 $replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
304 $replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
306 is $replicated->schema->storage->pool->connected_replicants => 0
307 => "both replicants are now disconnected";
309 ## All these should pass, since the database should automatically reconnect
311 ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
312 => 'Still finding stuff.';
315 => 'DBICTest::Artist';
317 is $artist3->name, "Dead On Arrival"
318 => 'Found expected name for first result';
320 is $replicated->schema->storage->pool->connected_replicants => 1
321 => "One replicant reconnected to handle the job";
323 ## What happens when we try to select something that doesn't exist?
325 ok ! $replicated->schema->resultset('Artist')->find(666)
326 => 'Correctly failed to find something.';
328 ## test the reliable option
332 $replicated->schema->storage->set_reliable_storage;
334 ok $replicated->schema->resultset('Artist')->find(2)
335 => 'Read from master 1';
337 ok $replicated->schema->resultset('Artist')->find(5)
338 => 'Read from master 2';
340 $replicated->schema->storage->set_balanced_storage;
342 ok $replicated->schema->resultset('Artist')->find(3)
343 => 'Read from replicant';
346 ## Make sure when reliable goes out of scope, we are using replicants again
348 ok $replicated->schema->resultset('Artist')->find(1)
349 => 'back to replicant 1.';
351 ok $replicated->schema->resultset('Artist')->find(2)
352 => 'back to replicant 2.';
354 ## set all the replicants to inactive, and make sure the balancer falls back to
357 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
358 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
360 ok $replicated->schema->resultset('Artist')->find(2)
361 => 'Fallback to master';
363 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
364 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
366 ok $replicated->schema->resultset('Artist')->find(2)
367 => 'Returned to replicates';
369 ## Getting slave status tests
372 ## We skip this tests unless you have a custom replicants, since the default
373 ## sqlite based replication tests don't support these functions.
375 skip 'Cannot Test Replicant Status on Non Replicating Database', 9
376 unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
378 $replicated->replicate; ## Give the slaves a chance to catchup.
380 ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating
381 => 'Replicants are replicating';
383 is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0
384 => 'Replicant is zero seconds behind master';
386 ## Test the validate replicants
388 $replicated->schema->storage->pool->validate_replicants;
390 is $replicated->schema->storage->pool->active_replicants, 2
391 => 'Still have 2 replicants after validation';
393 ## Force the replicants to fail the validate test by required their lag to
394 ## be negative (ie ahead of the master!)
396 $replicated->schema->storage->pool->maximum_lag(-10);
397 $replicated->schema->storage->pool->validate_replicants;
399 is $replicated->schema->storage->pool->active_replicants, 0
400 => 'No way a replicant be be ahead of the master';
402 ## Let's be fair to the replicants again. Let them lag up to 5
404 $replicated->schema->storage->pool->maximum_lag(5);
405 $replicated->schema->storage->pool->validate_replicants;
407 is $replicated->schema->storage->pool->active_replicants, 2
408 => 'Both replicants in good standing again';
410 ## Check auto validate
412 is $replicated->schema->storage->balancer->auto_validate_every, 100
413 => "Got the expected value for auto validate";
415 ## This will make sure we auto validatge everytime
416 $replicated->schema->storage->balancer->auto_validate_every(0);
418 ## set all the replicants to inactive, and make sure the balancer falls back to
421 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
422 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
424 ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
426 is $replicated->schema->storage->pool->active_replicants => 0
427 => "both replicants turned off";
429 ok $replicated->schema->resultset('Artist')->find(5)
430 => 'replicant reactivated';
432 is $replicated->schema->storage->pool->active_replicants => 2
433 => "both replicants reactivated";
436 ## Delete the old database files
437 $replicated->cleanup;