7 use List::Util 'first';
10 eval "use DBIx::Class::Storage::DBI::Replicated; use Test::Moose";
12 ? ( skip_all => "Deps not installed: $@" )
16 use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
17 use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer';
18 use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant';
19 use_ok 'DBIx::Class::Storage::DBI::Replicated';
23 This is a test of the replicated storage system. This will work in one of
24 two ways, either it was try to fake replication with a couple of SQLite DBs
25 and creative use of copy, or if you define a couple of %ENV vars correctly
26 will try to test those. If you do that, it will assume the setup is properly
27 replicating. Your results may vary, but I have demonstrated this to work with
28 mysql native replication.
33 ## ----------------------------------------------------------------------------
34 ## Build a class to hold all our required testing data and methods.
35 ## ----------------------------------------------------------------------------
39 ## --------------------------------------------------------------------- ##
40 ## Create an object to contain your replicated stuff.
41 ## --------------------------------------------------------------------- ##
43 package DBIx::Class::DBI::Replicated::TestReplication;
46 use base qw/Class::Accessor::Fast/;
48 __PACKAGE__->mk_accessors( qw/schema/ );
50 ## Initialize the object
54 my $self = $class->SUPER::new(@_);
56 $self->schema( $self->init_schema );
60 ## Get the Schema and set the replication storage type
63 # current SQLT SQLite producer does not handle DROP TABLE IF EXISTS, trap warnings here
64 local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /no such table.+DROP TABLE/ };
68 my $schema = DBICTest->init_schema(
71 '::DBI::Replicated' => {
72 balancer_type=>'::Random',
74 auto_validate_every=>100,
86 sub generate_replicant_connect_info {}
91 ## --------------------------------------------------------------------- ##
92 ## Subclass for when you are using SQLite for testing, this provides a fake
93 ## replication support.
94 ## --------------------------------------------------------------------- ##
96 package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
100 use base 'DBIx::Class::DBI::Replicated::TestReplication';
102 __PACKAGE__->mk_accessors( qw/master_path slave_paths/ );
104 ## Set the mastep path from DBICTest
107 my $class = shift @_;
108 my $self = $class->SUPER::new(@_);
110 $self->master_path( DBICTest->_sqlite_dbfilename );
112 "t/var/DBIxClass_slave1.db",
113 "t/var/DBIxClass_slave2.db",
119 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
120 ## $storage->connect_info to be used for connecting replicants.
122 sub generate_replicant_connect_info {
126 } @{$self->slave_paths};
128 my @connect_infos = map { [$_,'','',{AutoCommit=>1}] } @dsn;
131 my $c = $connect_infos[0];
132 $connect_infos[0] = {
142 ## Do a 'good enough' replication by copying the master dbfile over each of
143 ## the slave dbfiles. If the master is SQLite we do this, otherwise we
144 ## just do a one second pause to let the slaves catch up.
148 foreach my $slave (@{$self->slave_paths}) {
149 copy($self->master_path, $slave);
153 ## Cleanup after ourselves. Unlink all gthe slave paths.
157 foreach my $slave (@{$self->slave_paths}) {
162 ## --------------------------------------------------------------------- ##
163 ## Subclass for when you are setting the databases via custom export vars
164 ## This is for when you have a replicating database setup that you are
165 ## going to test against. You'll need to define the correct $ENV and have
166 ## two slave databases to test against, as well as a replication system
167 ## that will replicate in less than 1 second.
168 ## --------------------------------------------------------------------- ##
170 package DBIx::Class::DBI::Replicated::TestReplication::Custom;
171 use base 'DBIx::Class::DBI::Replicated::TestReplication';
173 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
174 ## $storage->connect_info to be used for connecting replicants.
176 sub generate_replicant_connect_info {
178 [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
179 [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],
183 ## pause a bit to let the replication catch up
190 ## ----------------------------------------------------------------------------
191 ## Create an object and run some tests
192 ## ----------------------------------------------------------------------------
194 ## Thi first bunch of tests are basic, just make sure all the bits are behaving
196 my $replicated_class = DBICTest->has_custom_dsn ?
197 'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
198 'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
200 ok my $replicated = $replicated_class->new
201 => 'Created a replication object';
203 isa_ok $replicated->schema
204 => 'DBIx::Class::Schema';
206 isa_ok $replicated->schema->storage
207 => 'DBIx::Class::Storage::DBI::Replicated';
209 ok $replicated->schema->storage->meta
210 => 'has a meta object';
212 isa_ok $replicated->schema->storage->master
213 => 'DBIx::Class::Storage::DBI';
215 isa_ok $replicated->schema->storage->pool
216 => 'DBIx::Class::Storage::DBI::Replicated::Pool';
218 does_ok $replicated->schema->storage->balancer
219 => 'DBIx::Class::Storage::DBI::Replicated::Balancer';
221 ok my @replicant_connects = $replicated->generate_replicant_connect_info
222 => 'got replication connect information';
224 ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
225 => 'Created some storages suitable for replicants';
227 my @replicant_names = keys %{ $replicated->schema->storage->replicants };
229 ## Silence warning about not supporting the is_replicating method if using the
231 $replicated->schema->storage->debugobj->silence(1)
232 if first { m{^t/} } @replicant_names;
234 isa_ok $replicated->schema->storage->balancer->current_replicant
235 => 'DBIx::Class::Storage::DBI';
237 $replicated->schema->storage->debugobj->silence(0);
239 ok $replicated->schema->storage->pool->has_replicants
240 => 'does have replicants';
242 is $replicated->schema->storage->pool->num_replicants => 2
243 => 'has two replicants';
245 does_ok $replicated_storages[0]
246 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
248 does_ok $replicated_storages[1]
249 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
251 does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
252 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
254 does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
255 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
257 ## Add some info to the database
261 ->populate('Artist', [
262 [ qw/artistid name/ ],
263 [ 4, "Ozric Tentacles"],
266 ## Make sure all the slaves have the table definitions
268 $replicated->replicate;
269 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
270 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
272 ## Silence warning about not supporting the is_replicating method if using the
274 $replicated->schema->storage->debugobj->silence(1)
275 if first { m{^t/} } @replicant_names;
277 $replicated->schema->storage->pool->validate_replicants;
279 $replicated->schema->storage->debugobj->silence(0);
281 ## Make sure we can read the data.
283 ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
287 => 'DBICTest::Artist';
289 is $artist1->name, 'Ozric Tentacles'
290 => 'Found expected name for first result';
292 ## Add some new rows that only the master will have This is because
293 ## we overload any type of write operation so that is must hit the master
298 ->populate('Artist', [
299 [ qw/artistid name/ ],
300 [ 5, "Doom's Children"],
301 [ 6, "Dead On Arrival"],
305 ## Make sure all the slaves have the table definitions
306 $replicated->replicate;
308 ## Should find some data now
310 ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
314 => 'DBICTest::Artist';
316 is $artist2->name, "Doom's Children"
317 => 'Found expected name for first result';
319 ## What happens when we disconnect all the replicants?
321 is $replicated->schema->storage->pool->connected_replicants => 2
322 => "both replicants are connected";
324 $replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
325 $replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
327 is $replicated->schema->storage->pool->connected_replicants => 0
328 => "both replicants are now disconnected";
330 ## All these should pass, since the database should automatically reconnect
332 ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
333 => 'Still finding stuff.';
336 => 'DBICTest::Artist';
338 is $artist3->name, "Dead On Arrival"
339 => 'Found expected name for first result';
341 is $replicated->schema->storage->pool->connected_replicants => 1
342 => "At Least One replicant reconnected to handle the job";
344 ## What happens when we try to select something that doesn't exist?
346 ok ! $replicated->schema->resultset('Artist')->find(666)
347 => 'Correctly failed to find something.';
349 ## test the reliable option
353 $replicated->schema->storage->set_reliable_storage;
355 ok $replicated->schema->resultset('Artist')->find(2)
356 => 'Read from master 1';
358 ok $replicated->schema->resultset('Artist')->find(5)
359 => 'Read from master 2';
361 $replicated->schema->storage->set_balanced_storage;
363 ok $replicated->schema->resultset('Artist')->find(3)
364 => 'Read from replicant';
367 ## Make sure when reliable goes out of scope, we are using replicants again
369 ok $replicated->schema->resultset('Artist')->find(1)
370 => 'back to replicant 1.';
372 ok $replicated->schema->resultset('Artist')->find(2)
373 => 'back to replicant 2.';
375 ## set all the replicants to inactive, and make sure the balancer falls back to
378 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
379 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
381 ## Silence warning about falling back to master.
382 $replicated->schema->storage->debugobj->silence(1);
384 ok $replicated->schema->resultset('Artist')->find(2)
385 => 'Fallback to master';
387 $replicated->schema->storage->debugobj->silence(0);
389 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
390 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
392 ## Silence warning about not supporting the is_replicating method if using the
394 $replicated->schema->storage->debugobj->silence(1)
395 if first { m{^t/} } @replicant_names;
397 $replicated->schema->storage->pool->validate_replicants;
399 $replicated->schema->storage->debugobj->silence(0);
401 ok $replicated->schema->resultset('Artist')->find(2)
402 => 'Returned to replicates';
404 ## Getting slave status tests
407 ## We skip this tests unless you have a custom replicants, since the default
408 ## sqlite based replication tests don't support these functions.
410 skip 'Cannot Test Replicant Status on Non Replicating Database', 9
411 unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
413 $replicated->replicate; ## Give the slaves a chance to catchup.
415 ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating
416 => 'Replicants are replicating';
418 is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0
419 => 'Replicant is zero seconds behind master';
421 ## Test the validate replicants
423 $replicated->schema->storage->pool->validate_replicants;
425 is $replicated->schema->storage->pool->active_replicants, 2
426 => 'Still have 2 replicants after validation';
428 ## Force the replicants to fail the validate test by required their lag to
429 ## be negative (ie ahead of the master!)
431 $replicated->schema->storage->pool->maximum_lag(-10);
432 $replicated->schema->storage->pool->validate_replicants;
434 is $replicated->schema->storage->pool->active_replicants, 0
435 => 'No way a replicant be be ahead of the master';
437 ## Let's be fair to the replicants again. Let them lag up to 5
439 $replicated->schema->storage->pool->maximum_lag(5);
440 $replicated->schema->storage->pool->validate_replicants;
442 is $replicated->schema->storage->pool->active_replicants, 2
443 => 'Both replicants in good standing again';
445 ## Check auto validate
447 is $replicated->schema->storage->balancer->auto_validate_every, 100
448 => "Got the expected value for auto validate";
450 ## This will make sure we auto validatge everytime
451 $replicated->schema->storage->balancer->auto_validate_every(0);
453 ## set all the replicants to inactive, and make sure the balancer falls back to
456 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
457 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
459 ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
461 is $replicated->schema->storage->pool->active_replicants => 0
462 => "both replicants turned off";
464 ok $replicated->schema->resultset('Artist')->find(5)
465 => 'replicant reactivated';
467 is $replicated->schema->storage->pool->active_replicants => 2
468 => "both replicants reactivated";
471 ## Test the reliably callback
473 ok my $reliably = sub {
475 ok $replicated->schema->resultset('Artist')->find(5)
476 => 'replicant reactivated';
478 } => 'created coderef properly';
480 $replicated->schema->storage->execute_reliably($reliably);
482 ## Try something with an error
484 ok my $unreliably = sub {
486 ok $replicated->schema->resultset('ArtistXX')->find(5)
487 => 'replicant reactivated';
489 } => 'created coderef properly';
491 throws_ok {$replicated->schema->storage->execute_reliably($unreliably)}
492 qr/Can't find source for ArtistXX/
493 => 'Bad coderef throws proper error';
495 ## Make sure replication came back
497 ok $replicated->schema->resultset('Artist')->find(3)
498 => 'replicant reactivated';
500 ## make sure transactions are set to execute_reliably
502 ok my $transaction = sub {
508 ->populate('Artist', [
509 [ qw/artistid name/ ],
510 [ $id, "Children of the Grave"],
513 ok my $result = $replicated->schema->resultset('Artist')->find($id)
514 => 'Found expected artist';
516 ok my $more = $replicated->schema->resultset('Artist')->find(1)
517 => 'Found expected artist again';
519 return ($result, $more);
521 } => 'Created a coderef properly';
523 ## Test the transaction with multi return
525 ok my @return = $replicated->schema->txn_do($transaction, 666)
526 => 'did transaction';
528 is $return[0]->id, 666
529 => 'first returned value is correct';
532 => 'second returned value is correct';
535 ## Test that asking for single return works
537 ok my $return = $replicated->schema->txn_do($transaction, 777)
538 => 'did transaction';
541 => 'first returned value is correct';
544 ## Test transaction returning a single value
547 ok my $result = $replicated->schema->txn_do(sub {
548 ok my $more = $replicated->schema->resultset('Artist')->find(1)
549 => 'found inside a transaction';
551 }) => 'successfully processed transaction';
554 => 'Got expected single result from transaction';
557 ## Make sure replication came back
559 ok $replicated->schema->resultset('Artist')->find(1)
560 => 'replicant reactivated';
562 ## Test Discard changes
565 ok my $artist = $replicated->schema->resultset('Artist')->find(2)
566 => 'got an artist to test discard changes';
568 ok $artist->discard_changes
569 => 'properly discard changes';
572 ## Test some edge cases, like trying to do a transaction inside a transaction, etc
575 ok my $result = $replicated->schema->txn_do(sub {
576 return $replicated->schema->txn_do(sub {
577 ok my $more = $replicated->schema->resultset('Artist')->find(1)
578 => 'found inside a transaction inside a transaction';
581 }) => 'successfully processed transaction';
584 => 'Got expected single result from transaction';
588 ok my $result = $replicated->schema->txn_do(sub {
589 return $replicated->schema->storage->execute_reliably(sub {
590 return $replicated->schema->txn_do(sub {
591 return $replicated->schema->storage->execute_reliably(sub {
592 ok my $more = $replicated->schema->resultset('Artist')->find(1)
593 => 'found inside crazy deep transactions and execute_reliably';
598 }) => 'successfully processed transaction';
601 => 'Got expected single result from transaction';
604 ## Test the force_pool resultset attribute.
607 ok my $artist_rs = $replicated->schema->resultset('Artist')
608 => 'got artist resultset';
610 ## Turn on Forced Pool Storage
611 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>'master'})
612 => 'Created a resultset using force_pool storage';
614 ok my $artist = $reliable_artist_rs->find(2)
615 => 'got an artist result via force_pool storage';
618 ## Delete the old database files
619 $replicated->cleanup;