7 use List::Util 'first';
8 use Scalar::Util 'reftype';
11 eval "use DBIx::Class::Storage::DBI::Replicated; use Test::Moose";
13 ? ( skip_all => "Deps not installed: $@" )
17 use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
18 use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer';
19 use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant';
20 use_ok 'DBIx::Class::Storage::DBI::Replicated';
24 This is a test of the replicated storage system. This will work in one of
25 two ways, either it was try to fake replication with a couple of SQLite DBs
26 and creative use of copy, or if you define a couple of %ENV vars correctly
27 will try to test those. If you do that, it will assume the setup is properly
28 replicating. Your results may vary, but I have demonstrated this to work with
29 mysql native replication.
34 ## ----------------------------------------------------------------------------
35 ## Build a class to hold all our required testing data and methods.
36 ## ----------------------------------------------------------------------------
40 ## --------------------------------------------------------------------- ##
41 ## Create an object to contain your replicated stuff.
42 ## --------------------------------------------------------------------- ##
44 package DBIx::Class::DBI::Replicated::TestReplication;
47 use base qw/Class::Accessor::Fast/;
49 __PACKAGE__->mk_accessors( qw/schema/ );
51 ## Initialize the object
55 my $self = $class->SUPER::new(@_);
57 $self->schema( $self->init_schema );
61 ## Get the Schema and set the replication storage type
64 # current SQLT SQLite producer does not handle DROP TABLE IF EXISTS, trap warnings here
65 local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /no such table.+DROP TABLE/ };
69 my $schema = DBICTest->init_schema(
72 '::DBI::Replicated' => {
73 balancer_type=>'::Random',
75 auto_validate_every=>100,
87 sub generate_replicant_connect_info {}
91 ## --------------------------------------------------------------------- ##
92 ## Add a connect_info option to test option merging.
93 ## --------------------------------------------------------------------- ##
95 package DBIx::Class::Storage::DBI::Replicated;
99 __PACKAGE__->meta->make_mutable;
101 around connect_info => sub {
102 my ($next, $self, $info) = @_;
103 $info->[3]{master_option} = 1;
107 __PACKAGE__->meta->make_immutable;
112 ## --------------------------------------------------------------------- ##
113 ## Subclass for when you are using SQLite for testing, this provides a fake
114 ## replication support.
115 ## --------------------------------------------------------------------- ##
117 package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
121 use base 'DBIx::Class::DBI::Replicated::TestReplication';
123 __PACKAGE__->mk_accessors( qw/master_path slave_paths/ );
125 ## Set the mastep path from DBICTest
128 my $class = shift @_;
129 my $self = $class->SUPER::new(@_);
131 $self->master_path( DBICTest->_sqlite_dbfilename );
133 "t/var/DBIxClass_slave1.db",
134 "t/var/DBIxClass_slave2.db",
140 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
141 ## $storage->connect_info to be used for connecting replicants.
143 sub generate_replicant_connect_info {
147 } @{$self->slave_paths};
149 my @connect_infos = map { [$_,'','',{AutoCommit=>1}] } @dsn;
152 my $c = $connect_infos[0];
153 $connect_infos[0] = {
163 ## Do a 'good enough' replication by copying the master dbfile over each of
164 ## the slave dbfiles. If the master is SQLite we do this, otherwise we
165 ## just do a one second pause to let the slaves catch up.
169 foreach my $slave (@{$self->slave_paths}) {
170 copy($self->master_path, $slave);
174 ## Cleanup after ourselves. Unlink all gthe slave paths.
178 foreach my $slave (@{$self->slave_paths}) {
183 ## --------------------------------------------------------------------- ##
184 ## Subclass for when you are setting the databases via custom export vars
185 ## This is for when you have a replicating database setup that you are
186 ## going to test against. You'll need to define the correct $ENV and have
187 ## two slave databases to test against, as well as a replication system
188 ## that will replicate in less than 1 second.
189 ## --------------------------------------------------------------------- ##
191 package DBIx::Class::DBI::Replicated::TestReplication::Custom;
192 use base 'DBIx::Class::DBI::Replicated::TestReplication';
194 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
195 ## $storage->connect_info to be used for connecting replicants.
197 sub generate_replicant_connect_info {
199 [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
200 [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],
204 ## pause a bit to let the replication catch up
211 ## ----------------------------------------------------------------------------
212 ## Create an object and run some tests
213 ## ----------------------------------------------------------------------------
215 ## Thi first bunch of tests are basic, just make sure all the bits are behaving
217 my $replicated_class = DBICTest->has_custom_dsn ?
218 'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
219 'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
221 ok my $replicated = $replicated_class->new
222 => 'Created a replication object';
224 isa_ok $replicated->schema
225 => 'DBIx::Class::Schema';
227 isa_ok $replicated->schema->storage
228 => 'DBIx::Class::Storage::DBI::Replicated';
230 ok $replicated->schema->storage->meta
231 => 'has a meta object';
233 isa_ok $replicated->schema->storage->master
234 => 'DBIx::Class::Storage::DBI';
236 isa_ok $replicated->schema->storage->pool
237 => 'DBIx::Class::Storage::DBI::Replicated::Pool';
239 does_ok $replicated->schema->storage->balancer
240 => 'DBIx::Class::Storage::DBI::Replicated::Balancer';
242 ok my @replicant_connects = $replicated->generate_replicant_connect_info
243 => 'got replication connect information';
245 ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
246 => 'Created some storages suitable for replicants';
248 ok my @all_storages = $replicated->schema->storage->all_storages
251 is scalar @all_storages
253 => 'correct number of ->all_storages';
255 is ((grep $_->isa('DBIx::Class::Storage::DBI'), @all_storages)
257 => '->all_storages are correct type');
259 is ((grep $_->{master_option},
260 grep { (reftype($_)||'') eq 'HASH' }
261 map @{ $_->_connect_info }, @all_storages)
263 => 'connect_info was merged from master to replicants');
265 my @replicant_names = keys %{ $replicated->schema->storage->replicants };
267 ## Silence warning about not supporting the is_replicating method if using the
269 $replicated->schema->storage->debugobj->silence(1)
270 if first { m{^t/} } @replicant_names;
272 isa_ok $replicated->schema->storage->balancer->current_replicant
273 => 'DBIx::Class::Storage::DBI';
275 $replicated->schema->storage->debugobj->silence(0);
277 ok $replicated->schema->storage->pool->has_replicants
278 => 'does have replicants';
280 is $replicated->schema->storage->pool->num_replicants => 2
281 => 'has two replicants';
283 does_ok $replicated_storages[0]
284 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
286 does_ok $replicated_storages[1]
287 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
289 does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
290 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
292 does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
293 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
295 ## Add some info to the database
299 ->populate('Artist', [
300 [ qw/artistid name/ ],
301 [ 4, "Ozric Tentacles"],
304 ## Make sure all the slaves have the table definitions
306 $replicated->replicate;
307 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
308 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
310 ## Silence warning about not supporting the is_replicating method if using the
312 $replicated->schema->storage->debugobj->silence(1)
313 if first { m{^t/} } @replicant_names;
315 $replicated->schema->storage->pool->validate_replicants;
317 $replicated->schema->storage->debugobj->silence(0);
319 ## Make sure we can read the data.
321 ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
325 => 'DBICTest::Artist';
327 is $artist1->name, 'Ozric Tentacles'
328 => 'Found expected name for first result';
330 ## Add some new rows that only the master will have This is because
331 ## we overload any type of write operation so that is must hit the master
336 ->populate('Artist', [
337 [ qw/artistid name/ ],
338 [ 5, "Doom's Children"],
339 [ 6, "Dead On Arrival"],
343 ## Make sure all the slaves have the table definitions
344 $replicated->replicate;
346 ## Should find some data now
348 ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
352 => 'DBICTest::Artist';
354 is $artist2->name, "Doom's Children"
355 => 'Found expected name for first result';
357 ## What happens when we disconnect all the replicants?
359 is $replicated->schema->storage->pool->connected_replicants => 2
360 => "both replicants are connected";
362 $replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
363 $replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
365 is $replicated->schema->storage->pool->connected_replicants => 0
366 => "both replicants are now disconnected";
368 ## All these should pass, since the database should automatically reconnect
370 ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
371 => 'Still finding stuff.';
374 => 'DBICTest::Artist';
376 is $artist3->name, "Dead On Arrival"
377 => 'Found expected name for first result';
379 is $replicated->schema->storage->pool->connected_replicants => 1
380 => "At Least One replicant reconnected to handle the job";
382 ## What happens when we try to select something that doesn't exist?
384 ok ! $replicated->schema->resultset('Artist')->find(666)
385 => 'Correctly failed to find something.';
387 ## test the reliable option
391 $replicated->schema->storage->set_reliable_storage;
393 ok $replicated->schema->resultset('Artist')->find(2)
394 => 'Read from master 1';
396 ok $replicated->schema->resultset('Artist')->find(5)
397 => 'Read from master 2';
399 $replicated->schema->storage->set_balanced_storage;
401 ok $replicated->schema->resultset('Artist')->find(3)
402 => 'Read from replicant';
405 ## Make sure when reliable goes out of scope, we are using replicants again
407 ok $replicated->schema->resultset('Artist')->find(1)
408 => 'back to replicant 1.';
410 ok $replicated->schema->resultset('Artist')->find(2)
411 => 'back to replicant 2.';
413 ## set all the replicants to inactive, and make sure the balancer falls back to
416 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
417 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
419 ## Silence warning about falling back to master.
420 $replicated->schema->storage->debugobj->silence(1);
422 ok $replicated->schema->resultset('Artist')->find(2)
423 => 'Fallback to master';
425 $replicated->schema->storage->debugobj->silence(0);
427 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
428 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
430 ## Silence warning about not supporting the is_replicating method if using the
432 $replicated->schema->storage->debugobj->silence(1)
433 if first { m{^t/} } @replicant_names;
435 $replicated->schema->storage->pool->validate_replicants;
437 $replicated->schema->storage->debugobj->silence(0);
439 ok $replicated->schema->resultset('Artist')->find(2)
440 => 'Returned to replicates';
442 ## Getting slave status tests
445 ## We skip this tests unless you have a custom replicants, since the default
446 ## sqlite based replication tests don't support these functions.
448 skip 'Cannot Test Replicant Status on Non Replicating Database', 9
449 unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
451 $replicated->replicate; ## Give the slaves a chance to catchup.
453 ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating
454 => 'Replicants are replicating';
456 is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0
457 => 'Replicant is zero seconds behind master';
459 ## Test the validate replicants
461 $replicated->schema->storage->pool->validate_replicants;
463 is $replicated->schema->storage->pool->active_replicants, 2
464 => 'Still have 2 replicants after validation';
466 ## Force the replicants to fail the validate test by required their lag to
467 ## be negative (ie ahead of the master!)
469 $replicated->schema->storage->pool->maximum_lag(-10);
470 $replicated->schema->storage->pool->validate_replicants;
472 is $replicated->schema->storage->pool->active_replicants, 0
473 => 'No way a replicant be be ahead of the master';
475 ## Let's be fair to the replicants again. Let them lag up to 5
477 $replicated->schema->storage->pool->maximum_lag(5);
478 $replicated->schema->storage->pool->validate_replicants;
480 is $replicated->schema->storage->pool->active_replicants, 2
481 => 'Both replicants in good standing again';
483 ## Check auto validate
485 is $replicated->schema->storage->balancer->auto_validate_every, 100
486 => "Got the expected value for auto validate";
488 ## This will make sure we auto validatge everytime
489 $replicated->schema->storage->balancer->auto_validate_every(0);
491 ## set all the replicants to inactive, and make sure the balancer falls back to
494 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
495 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
497 ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
499 is $replicated->schema->storage->pool->active_replicants => 0
500 => "both replicants turned off";
502 ok $replicated->schema->resultset('Artist')->find(5)
503 => 'replicant reactivated';
505 is $replicated->schema->storage->pool->active_replicants => 2
506 => "both replicants reactivated";
509 ## Test the reliably callback
511 ok my $reliably = sub {
513 ok $replicated->schema->resultset('Artist')->find(5)
514 => 'replicant reactivated';
516 } => 'created coderef properly';
518 $replicated->schema->storage->execute_reliably($reliably);
520 ## Try something with an error
522 ok my $unreliably = sub {
524 ok $replicated->schema->resultset('ArtistXX')->find(5)
525 => 'replicant reactivated';
527 } => 'created coderef properly';
529 throws_ok {$replicated->schema->storage->execute_reliably($unreliably)}
530 qr/Can't find source for ArtistXX/
531 => 'Bad coderef throws proper error';
533 ## Make sure replication came back
535 ok $replicated->schema->resultset('Artist')->find(3)
536 => 'replicant reactivated';
538 ## make sure transactions are set to execute_reliably
540 ok my $transaction = sub {
546 ->populate('Artist', [
547 [ qw/artistid name/ ],
548 [ $id, "Children of the Grave"],
551 ok my $result = $replicated->schema->resultset('Artist')->find($id)
552 => 'Found expected artist';
554 ok my $more = $replicated->schema->resultset('Artist')->find(1)
555 => 'Found expected artist again';
557 return ($result, $more);
559 } => 'Created a coderef properly';
561 ## Test the transaction with multi return
563 ok my @return = $replicated->schema->txn_do($transaction, 666)
564 => 'did transaction';
566 is $return[0]->id, 666
567 => 'first returned value is correct';
570 => 'second returned value is correct';
573 ## Test that asking for single return works
575 ok my $return = $replicated->schema->txn_do($transaction, 777)
576 => 'did transaction';
579 => 'first returned value is correct';
582 ## Test transaction returning a single value
585 ok my $result = $replicated->schema->txn_do(sub {
586 ok my $more = $replicated->schema->resultset('Artist')->find(1)
587 => 'found inside a transaction';
589 }) => 'successfully processed transaction';
592 => 'Got expected single result from transaction';
595 ## Make sure replication came back
597 ok $replicated->schema->resultset('Artist')->find(1)
598 => 'replicant reactivated';
600 ## Test Discard changes
603 ok my $artist = $replicated->schema->resultset('Artist')->find(2)
604 => 'got an artist to test discard changes';
606 ok $artist->discard_changes
607 => 'properly discard changes';
610 ## Test some edge cases, like trying to do a transaction inside a transaction, etc
613 ok my $result = $replicated->schema->txn_do(sub {
614 return $replicated->schema->txn_do(sub {
615 ok my $more = $replicated->schema->resultset('Artist')->find(1)
616 => 'found inside a transaction inside a transaction';
619 }) => 'successfully processed transaction';
622 => 'Got expected single result from transaction';
626 ok my $result = $replicated->schema->txn_do(sub {
627 return $replicated->schema->storage->execute_reliably(sub {
628 return $replicated->schema->txn_do(sub {
629 return $replicated->schema->storage->execute_reliably(sub {
630 ok my $more = $replicated->schema->resultset('Artist')->find(1)
631 => 'found inside crazy deep transactions and execute_reliably';
636 }) => 'successfully processed transaction';
639 => 'Got expected single result from transaction';
642 ## Test the force_pool resultset attribute.
645 ok my $artist_rs = $replicated->schema->resultset('Artist')
646 => 'got artist resultset';
648 ## Turn on Forced Pool Storage
649 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>'master'})
650 => 'Created a resultset using force_pool storage';
652 ok my $artist = $reliable_artist_rs->find(2)
653 => 'got an artist result via force_pool storage';
656 ## Delete the old database files
657 $replicated->cleanup;