7 use List::Util 'first';
8 use Scalar::Util 'reftype';
13 eval "use DBIx::Class::Storage::DBI::Replicated; use Test::Moose";
15 ? ( skip_all => "Deps not installed: $@" )
19 use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
20 use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer';
21 use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant';
22 use_ok 'DBIx::Class::Storage::DBI::Replicated';
26 diag "Using Moose version $Moose::VERSION and MooseX::Types version $MooseX::Types::VERSION";
30 This is a test of the replicated storage system. This will work in one of
31 two ways, either it was try to fake replication with a couple of SQLite DBs
32 and creative use of copy, or if you define a couple of %ENV vars correctly
33 will try to test those. If you do that, it will assume the setup is properly
34 replicating. Your results may vary, but I have demonstrated this to work with
35 mysql native replication.
40 ## ----------------------------------------------------------------------------
41 ## Build a class to hold all our required testing data and methods.
42 ## ----------------------------------------------------------------------------
46 ## --------------------------------------------------------------------- ##
47 ## Create an object to contain your replicated stuff.
48 ## --------------------------------------------------------------------- ##
50 package DBIx::Class::DBI::Replicated::TestReplication;
53 use base qw/Class::Accessor::Fast/;
55 __PACKAGE__->mk_accessors( qw/schema/ );
57 ## Initialize the object
60 my ($class, $schema_method) = (shift, shift);
61 my $self = $class->SUPER::new(@_);
63 $self->schema( $self->init_schema($schema_method) );
67 ## Get the Schema and set the replication storage type
70 # current SQLT SQLite producer does not handle DROP TABLE IF EXISTS, trap warnings here
71 local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /no such table.+DROP TABLE/ };
73 my ($class, $schema_method) = @_;
75 my $method = "get_schema_$schema_method";
76 my $schema = $class->$method;
81 sub get_schema_by_storage_type {
82 DBICTest->init_schema(
85 '::DBI::Replicated' => {
86 balancer_type=>'::Random',
88 auto_validate_every=>100,
89 master_read_weight => 1
99 sub get_schema_by_connect_info {
100 DBICTest->init_schema(
101 sqlite_use_file => 1,
102 storage_type=> '::DBI::Replicated',
103 balancer_type=>'::Random',
105 auto_validate_every=>100,
106 master_read_weight => 1
114 sub generate_replicant_connect_info {}
118 ## --------------------------------------------------------------------- ##
119 ## Add a connect_info option to test option merging.
120 ## --------------------------------------------------------------------- ##
122 package DBIx::Class::Storage::DBI::Replicated;
126 __PACKAGE__->meta->make_mutable;
128 around connect_info => sub {
129 my ($next, $self, $info) = @_;
130 $info->[3]{master_option} = 1;
134 __PACKAGE__->meta->make_immutable;
139 ## --------------------------------------------------------------------- ##
140 ## Subclass for when you are using SQLite for testing, this provides a fake
141 ## replication support.
142 ## --------------------------------------------------------------------- ##
144 package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
148 use base 'DBIx::Class::DBI::Replicated::TestReplication';
150 __PACKAGE__->mk_accessors(qw/master_path slave_paths/);
152 ## Set the master path from DBICTest
155 my $class = shift @_;
156 my $self = $class->SUPER::new(@_);
158 $self->master_path( DBICTest->_sqlite_dbfilename );
160 File::Spec->catfile(qw/t var DBIxClass_slave1.db/),
161 File::Spec->catfile(qw/t var DBIxClass_slave2.db/),
167 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
168 ## $storage->connect_info to be used for connecting replicants.
170 sub generate_replicant_connect_info {
174 } @{$self->slave_paths};
176 my @connect_infos = map { [$_,'','',{AutoCommit=>1}] } @dsn;
178 ## Make sure nothing is left over from a failed test
182 my $c = $connect_infos[0];
183 $connect_infos[0] = {
193 ## Do a 'good enough' replication by copying the master dbfile over each of
194 ## the slave dbfiles. If the master is SQLite we do this, otherwise we
195 ## just do a one second pause to let the slaves catch up.
199 foreach my $slave (@{$self->slave_paths}) {
200 copy($self->master_path, $slave);
204 ## Cleanup after ourselves. Unlink all gthe slave paths.
208 foreach my $slave (@{$self->slave_paths}) {
215 ## --------------------------------------------------------------------- ##
216 ## Subclass for when you are setting the databases via custom export vars
217 ## This is for when you have a replicating database setup that you are
218 ## going to test against. You'll need to define the correct $ENV and have
219 ## two slave databases to test against, as well as a replication system
220 ## that will replicate in less than 1 second.
221 ## --------------------------------------------------------------------- ##
223 package DBIx::Class::DBI::Replicated::TestReplication::Custom;
224 use base 'DBIx::Class::DBI::Replicated::TestReplication';
226 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
227 ## $storage->connect_info to be used for connecting replicants.
229 sub generate_replicant_connect_info {
231 [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
232 [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],
236 ## pause a bit to let the replication catch up
243 ## ----------------------------------------------------------------------------
244 ## Create an object and run some tests
245 ## ----------------------------------------------------------------------------
247 ## Thi first bunch of tests are basic, just make sure all the bits are behaving
249 my $replicated_class = DBICTest->has_custom_dsn ?
250 'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
251 'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
255 for my $method (qw/by_connect_info by_storage_type/) {
256 ok $replicated = $replicated_class->new($method)
257 => "Created a replication object $method";
259 isa_ok $replicated->schema
260 => 'DBIx::Class::Schema';
262 isa_ok $replicated->schema->storage
263 => 'DBIx::Class::Storage::DBI::Replicated';
265 isa_ok $replicated->schema->storage->balancer
266 => 'DBIx::Class::Storage::DBI::Replicated::Balancer::Random'
267 => 'configured balancer_type';
270 ok $replicated->schema->storage->meta
271 => 'has a meta object';
273 isa_ok $replicated->schema->storage->master
274 => 'DBIx::Class::Storage::DBI';
276 isa_ok $replicated->schema->storage->pool
277 => 'DBIx::Class::Storage::DBI::Replicated::Pool';
279 does_ok $replicated->schema->storage->balancer
280 => 'DBIx::Class::Storage::DBI::Replicated::Balancer';
282 ok my @replicant_connects = $replicated->generate_replicant_connect_info
283 => 'got replication connect information';
285 ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
286 => 'Created some storages suitable for replicants';
289 $replicated->schema->storage->debug(1);
290 $replicated->schema->storage->debugcb(sub {
291 my ($op, $info) = @_;
292 ##warn "\n$op, $info\n";
296 dsn => ($info=~m/\[(.+)\]/)[0],
297 storage_type => $info=~m/REPLICANT/ ? 'REPLICANT' : 'MASTER',
301 ok my @all_storages = $replicated->schema->storage->all_storages
304 is scalar @all_storages,
306 => 'correct number of ->all_storages';
308 is ((grep $_->isa('DBIx::Class::Storage::DBI'), @all_storages),
310 => '->all_storages are correct type');
312 my @all_storage_opts =
313 grep { (reftype($_)||'') eq 'HASH' }
314 map @{ $_->_connect_info }, @all_storages;
316 is ((grep $_->{master_option}, @all_storage_opts),
318 => 'connect_info was merged from master to replicants');
320 my @replicant_names = keys %{ $replicated->schema->storage->replicants };
322 ok @replicant_names, "found replicant names @replicant_names";
324 ## Silence warning about not supporting the is_replicating method if using the
326 $replicated->schema->storage->debugobj->silence(1)
327 if first { m{^t/} } @replicant_names;
329 isa_ok $replicated->schema->storage->balancer->current_replicant
330 => 'DBIx::Class::Storage::DBI';
332 $replicated->schema->storage->debugobj->silence(0);
334 ok $replicated->schema->storage->pool->has_replicants
335 => 'does have replicants';
337 is $replicated->schema->storage->pool->num_replicants => 2
338 => 'has two replicants';
340 does_ok $replicated_storages[0]
341 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
343 does_ok $replicated_storages[1]
344 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
346 does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
347 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
349 does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
350 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
352 ## Add some info to the database
356 ->populate('Artist', [
357 [ qw/artistid name/ ],
358 [ 4, "Ozric Tentacles"],
361 is $debug{storage_type}, 'MASTER',
362 "got last query from a master: $debug{dsn}";
364 like $debug{info}, qr/INSERT/, 'Last was an insert';
366 ## Make sure all the slaves have the table definitions
368 $replicated->replicate;
369 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
370 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
372 ## Silence warning about not supporting the is_replicating method if using the
374 $replicated->schema->storage->debugobj->silence(1)
375 if first { m{^t/} } @replicant_names;
377 $replicated->schema->storage->pool->validate_replicants;
379 $replicated->schema->storage->debugobj->silence(0);
381 ## Make sure we can read the data.
383 ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
386 ## We removed testing here since master read weight is on, so we can't tell in
387 ## advance what storage to expect. We turn master read weight off a bit lower
388 ## is $debug{storage_type}, 'REPLICANT'
389 ## => "got last query from a replicant: $debug{dsn}, $debug{info}";
392 => 'DBICTest::Artist';
394 is $artist1->name, 'Ozric Tentacles'
395 => 'Found expected name for first result';
397 ## Check that master_read_weight is honored
399 no warnings qw/once redefine/;
402 *DBIx::Class::Storage::DBI::Replicated::Balancer::Random::_random_number =
405 $replicated->schema->storage->balancer->increment_storage;
407 is $replicated->schema->storage->balancer->current_replicant,
408 $replicated->schema->storage->master
409 => 'master_read_weight is honored';
411 ## turn it off for the duration of the test
412 $replicated->schema->storage->balancer->master_read_weight(0);
413 $replicated->schema->storage->balancer->increment_storage;
416 ## Add some new rows that only the master will have This is because
417 ## we overload any type of write operation so that is must hit the master
422 ->populate('Artist', [
423 [ qw/artistid name/ ],
424 [ 5, "Doom's Children"],
425 [ 6, "Dead On Arrival"],
429 is $debug{storage_type}, 'MASTER',
430 "got last query from a master: $debug{dsn}";
432 like $debug{info}, qr/INSERT/, 'Last was an insert';
434 ## Make sure all the slaves have the table definitions
435 $replicated->replicate;
437 ## Should find some data now
439 ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
442 is $debug{storage_type}, 'REPLICANT'
443 => "got last query from a replicant: $debug{dsn}";
446 => 'DBICTest::Artist';
448 is $artist2->name, "Doom's Children"
449 => 'Found expected name for first result';
451 ## What happens when we disconnect all the replicants?
453 is $replicated->schema->storage->pool->connected_replicants => 2
454 => "both replicants are connected";
456 $replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
457 $replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
459 is $replicated->schema->storage->pool->connected_replicants => 0
460 => "both replicants are now disconnected";
462 ## All these should pass, since the database should automatically reconnect
464 ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
465 => 'Still finding stuff.';
467 is $debug{storage_type}, 'REPLICANT'
468 => "got last query from a replicant: $debug{dsn}";
471 => 'DBICTest::Artist';
473 is $artist3->name, "Dead On Arrival"
474 => 'Found expected name for first result';
476 is $replicated->schema->storage->pool->connected_replicants => 1
477 => "At Least One replicant reconnected to handle the job";
479 ## What happens when we try to select something that doesn't exist?
481 ok ! $replicated->schema->resultset('Artist')->find(666)
482 => 'Correctly failed to find something.';
484 is $debug{storage_type}, 'REPLICANT'
485 => "got last query from a replicant: $debug{dsn}";
487 ## test the reliable option
491 $replicated->schema->storage->set_reliable_storage;
493 ok $replicated->schema->resultset('Artist')->find(2)
494 => 'Read from master 1';
496 is $debug{storage_type}, 'MASTER',
497 "got last query from a master: $debug{dsn}";
499 ok $replicated->schema->resultset('Artist')->find(5)
500 => 'Read from master 2';
502 is $debug{storage_type}, 'MASTER',
503 "got last query from a master: $debug{dsn}";
505 $replicated->schema->storage->set_balanced_storage;
507 ok $replicated->schema->resultset('Artist')->find(3)
508 => 'Read from replicant';
510 is $debug{storage_type}, 'REPLICANT',
511 "got last query from a replicant: $debug{dsn}";
514 ## Make sure when reliable goes out of scope, we are using replicants again
516 ok $replicated->schema->resultset('Artist')->find(1)
517 => 'back to replicant 1.';
519 is $debug{storage_type}, 'REPLICANT',
520 "got last query from a replicant: $debug{dsn}";
522 ok $replicated->schema->resultset('Artist')->find(2)
523 => 'back to replicant 2.';
525 is $debug{storage_type}, 'REPLICANT',
526 "got last query from a replicant: $debug{dsn}";
528 ## set all the replicants to inactive, and make sure the balancer falls back to
531 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
532 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
535 ## catch the fallback to master warning
536 open my $debugfh, '>', \my $fallback_warning;
537 my $oldfh = $replicated->schema->storage->debugfh;
538 $replicated->schema->storage->debugfh($debugfh);
540 ok $replicated->schema->resultset('Artist')->find(2)
541 => 'Fallback to master';
543 is $debug{storage_type}, 'MASTER',
544 "got last query from a master: $debug{dsn}";
546 like $fallback_warning, qr/falling back to master/
547 => 'emits falling back to master warning';
549 $replicated->schema->storage->debugfh($oldfh);
552 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
553 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
555 ## Silence warning about not supporting the is_replicating method if using the
557 $replicated->schema->storage->debugobj->silence(1)
558 if first { m{^t/} } @replicant_names;
560 $replicated->schema->storage->pool->validate_replicants;
562 $replicated->schema->storage->debugobj->silence(0);
564 ok $replicated->schema->resultset('Artist')->find(2)
565 => 'Returned to replicates';
567 is $debug{storage_type}, 'REPLICANT',
568 "got last query from a replicant: $debug{dsn}";
570 ## Getting slave status tests
573 ## We skip this tests unless you have a custom replicants, since the default
574 ## sqlite based replication tests don't support these functions.
576 skip 'Cannot Test Replicant Status on Non Replicating Database', 10
577 unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
579 $replicated->replicate; ## Give the slaves a chance to catchup.
581 ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating
582 => 'Replicants are replicating';
584 is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0
585 => 'Replicant is zero seconds behind master';
587 ## Test the validate replicants
589 $replicated->schema->storage->pool->validate_replicants;
591 is $replicated->schema->storage->pool->active_replicants, 2
592 => 'Still have 2 replicants after validation';
594 ## Force the replicants to fail the validate test by required their lag to
595 ## be negative (ie ahead of the master!)
597 $replicated->schema->storage->pool->maximum_lag(-10);
598 $replicated->schema->storage->pool->validate_replicants;
600 is $replicated->schema->storage->pool->active_replicants, 0
601 => 'No way a replicant be be ahead of the master';
603 ## Let's be fair to the replicants again. Let them lag up to 5
605 $replicated->schema->storage->pool->maximum_lag(5);
606 $replicated->schema->storage->pool->validate_replicants;
608 is $replicated->schema->storage->pool->active_replicants, 2
609 => 'Both replicants in good standing again';
611 ## Check auto validate
613 is $replicated->schema->storage->balancer->auto_validate_every, 100
614 => "Got the expected value for auto validate";
616 ## This will make sure we auto validatge everytime
617 $replicated->schema->storage->balancer->auto_validate_every(0);
619 ## set all the replicants to inactive, and make sure the balancer falls back to
622 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
623 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
625 ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
627 is $replicated->schema->storage->pool->active_replicants => 0
628 => "both replicants turned off";
630 ok $replicated->schema->resultset('Artist')->find(5)
631 => 'replicant reactivated';
633 is $debug{storage_type}, 'REPLICANT',
634 "got last query from a replicant: $debug{dsn}";
636 is $replicated->schema->storage->pool->active_replicants => 2
637 => "both replicants reactivated";
640 ## Test the reliably callback
642 ok my $reliably = sub {
644 ok $replicated->schema->resultset('Artist')->find(5)
645 => 'replicant reactivated';
647 is $debug{storage_type}, 'MASTER',
648 "got last query from a master: $debug{dsn}";
650 } => 'created coderef properly';
652 $replicated->schema->storage->execute_reliably($reliably);
654 ## Try something with an error
656 ok my $unreliably = sub {
658 ok $replicated->schema->resultset('ArtistXX')->find(5)
659 => 'replicant reactivated';
661 } => 'created coderef properly';
663 throws_ok {$replicated->schema->storage->execute_reliably($unreliably)}
664 qr/Can't find source for ArtistXX/
665 => 'Bad coderef throws proper error';
667 ## Make sure replication came back
669 ok $replicated->schema->resultset('Artist')->find(3)
670 => 'replicant reactivated';
672 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
674 ## make sure transactions are set to execute_reliably
676 ok my $transaction = sub {
682 ->populate('Artist', [
683 [ qw/artistid name/ ],
684 [ $id, "Children of the Grave"],
687 ok my $result = $replicated->schema->resultset('Artist')->find($id)
688 => "Found expected artist for $id";
690 is $debug{storage_type}, 'MASTER',
691 "got last query from a master: $debug{dsn}";
693 ok my $more = $replicated->schema->resultset('Artist')->find(1)
694 => 'Found expected artist again for 1';
696 is $debug{storage_type}, 'MASTER',
697 "got last query from a master: $debug{dsn}";
699 return ($result, $more);
701 } => 'Created a coderef properly';
703 ## Test the transaction with multi return
705 ok my @return = $replicated->schema->txn_do($transaction, 666)
706 => 'did transaction';
708 is $return[0]->id, 666
709 => 'first returned value is correct';
711 is $debug{storage_type}, 'MASTER',
712 "got last query from a master: $debug{dsn}";
715 => 'second returned value is correct';
717 is $debug{storage_type}, 'MASTER',
718 "got last query from a master: $debug{dsn}";
722 ## Test that asking for single return works
724 ok my @return = $replicated->schema->txn_do($transaction, 777)
725 => 'did transaction';
727 is $return[0]->id, 777
728 => 'first returned value is correct';
731 => 'second returned value is correct';
734 ## Test transaction returning a single value
737 ok my $result = $replicated->schema->txn_do(sub {
738 ok my $more = $replicated->schema->resultset('Artist')->find(1)
739 => 'found inside a transaction';
740 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
742 }) => 'successfully processed transaction';
745 => 'Got expected single result from transaction';
748 ## Make sure replication came back
750 ok $replicated->schema->resultset('Artist')->find(1)
751 => 'replicant reactivated';
753 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
755 ## Test Discard changes
758 ok my $artist = $replicated->schema->resultset('Artist')->find(2)
759 => 'got an artist to test discard changes';
761 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
763 ok $artist->get_from_storage({force_pool=>'master'})
764 => 'properly called get_from_storage against master (manual attrs)';
766 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
768 ok $artist->discard_changes({force_pool=>'master'})
769 => 'properly alled discard_changes against master (manual attrs)';
771 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
773 ok $artist->discard_changes()
774 => 'properly alled discard_changes against master (default attrs)';
776 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"
779 ## Test some edge cases, like trying to do a transaction inside a transaction, etc
782 ok my $result = $replicated->schema->txn_do(sub {
783 return $replicated->schema->txn_do(sub {
784 ok my $more = $replicated->schema->resultset('Artist')->find(1)
785 => 'found inside a transaction inside a transaction';
786 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
789 }) => 'successfully processed transaction';
792 => 'Got expected single result from transaction';
796 ok my $result = $replicated->schema->txn_do(sub {
797 return $replicated->schema->storage->execute_reliably(sub {
798 return $replicated->schema->txn_do(sub {
799 return $replicated->schema->storage->execute_reliably(sub {
800 ok my $more = $replicated->schema->resultset('Artist')->find(1)
801 => 'found inside crazy deep transactions and execute_reliably';
802 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
807 }) => 'successfully processed transaction';
810 => 'Got expected single result from transaction';
813 ## Test the force_pool resultset attribute.
816 ok my $artist_rs = $replicated->schema->resultset('Artist')
817 => 'got artist resultset';
819 ## Turn on Forced Pool Storage
820 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>'master'})
821 => 'Created a resultset using force_pool storage';
823 ok my $artist = $reliable_artist_rs->find(2)
824 => 'got an artist result via force_pool storage';
826 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
829 ## Test the force_pool resultset attribute part two.
832 ok my $artist_rs = $replicated->schema->resultset('Artist')
833 => 'got artist resultset';
835 ## Turn on Forced Pool Storage
836 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>$replicant_names[0]})
837 => 'Created a resultset using force_pool storage';
839 ok my $artist = $reliable_artist_rs->find(2)
840 => 'got an artist result via force_pool storage';
842 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
844 ## Delete the old database files
845 $replicated->cleanup;