9 eval "use DBIx::Class::Storage::DBI::Replicated; use Test::Moose";
11 ? ( skip_all => "Deps not installed: $@" )
15 use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
16 use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer';
17 use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant';
18 use_ok 'DBIx::Class::Storage::DBI::Replicated';
22 This is a test of the replicated storage system. This will work in one of
23 two ways, either it was try to fake replication with a couple of SQLite DBs
24 and creative use of copy, or if you define a couple of %ENV vars correctly
25 will try to test those. If you do that, it will assume the setup is properly
26 replicating. Your results may vary, but I have demonstrated this to work with
27 mysql native replication.
32 ## ----------------------------------------------------------------------------
33 ## Build a class to hold all our required testing data and methods.
34 ## ----------------------------------------------------------------------------
38 ## --------------------------------------------------------------------- ##
39 ## Create an object to contain your replicated stuff.
40 ## --------------------------------------------------------------------- ##
42 package DBIx::Class::DBI::Replicated::TestReplication;
45 use base qw/Class::Accessor::Fast/;
47 __PACKAGE__->mk_accessors( qw/schema/ );
49 ## Initialize the object
53 my $self = $class->SUPER::new(@_);
55 $self->schema( $self->init_schema );
59 ## Get the Schema and set the replication storage type
62 # current SQLT SQLite producer does not handle DROP TABLE IF EXISTS, trap warnings here
63 local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /no such table.+DROP TABLE/ };
67 my $schema = DBICTest->init_schema(
70 '::DBI::Replicated' => {
71 balancer_type=>'::Random',
73 auto_validate_every=>100,
85 sub generate_replicant_connect_info {}
90 ## --------------------------------------------------------------------- ##
91 ## Subclass for when you are using SQLite for testing, this provides a fake
92 ## replication support.
93 ## --------------------------------------------------------------------- ##
95 package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
99 use base 'DBIx::Class::DBI::Replicated::TestReplication';
101 __PACKAGE__->mk_accessors( qw/master_path slave_paths/ );
103 ## Set the mastep path from DBICTest
106 my $class = shift @_;
107 my $self = $class->SUPER::new(@_);
109 $self->master_path( DBICTest->_sqlite_dbfilename );
111 "t/var/DBIxClass_slave1.db",
112 "t/var/DBIxClass_slave2.db",
118 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
119 ## $storage->connect_info to be used for connecting replicants.
121 sub generate_replicant_connect_info {
125 } @{$self->slave_paths};
127 return map { [$_,'','',{AutoCommit=>1}] } @dsn;
130 ## Do a 'good enough' replication by copying the master dbfile over each of
131 ## the slave dbfiles. If the master is SQLite we do this, otherwise we
132 ## just do a one second pause to let the slaves catch up.
136 foreach my $slave (@{$self->slave_paths}) {
137 copy($self->master_path, $slave);
141 ## Cleanup after ourselves. Unlink all gthe slave paths.
145 foreach my $slave (@{$self->slave_paths}) {
150 ## --------------------------------------------------------------------- ##
151 ## Subclass for when you are setting the databases via custom export vars
152 ## This is for when you have a replicating database setup that you are
153 ## going to test against. You'll need to define the correct $ENV and have
154 ## two slave databases to test against, as well as a replication system
155 ## that will replicate in less than 1 second.
156 ## --------------------------------------------------------------------- ##
158 package DBIx::Class::DBI::Replicated::TestReplication::Custom;
159 use base 'DBIx::Class::DBI::Replicated::TestReplication';
161 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
162 ## $storage->connect_info to be used for connecting replicants.
164 sub generate_replicant_connect_info {
166 [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
167 [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],
171 ## pause a bit to let the replication catch up
178 ## ----------------------------------------------------------------------------
179 ## Create an object and run some tests
180 ## ----------------------------------------------------------------------------
182 ## Thi first bunch of tests are basic, just make sure all the bits are behaving
184 my $replicated_class = DBICTest->has_custom_dsn ?
185 'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
186 'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
188 ok my $replicated = $replicated_class->new
189 => 'Created a replication object';
191 isa_ok $replicated->schema
192 => 'DBIx::Class::Schema';
194 isa_ok $replicated->schema->storage
195 => 'DBIx::Class::Storage::DBI::Replicated';
197 ok $replicated->schema->storage->meta
198 => 'has a meta object';
200 isa_ok $replicated->schema->storage->master
201 => 'DBIx::Class::Storage::DBI';
203 isa_ok $replicated->schema->storage->pool
204 => 'DBIx::Class::Storage::DBI::Replicated::Pool';
206 does_ok $replicated->schema->storage->balancer
207 => 'DBIx::Class::Storage::DBI::Replicated::Balancer';
209 ok my @replicant_connects = $replicated->generate_replicant_connect_info
210 => 'got replication connect information';
212 ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
213 => 'Created some storages suitable for replicants';
215 isa_ok $replicated->schema->storage->balancer->current_replicant
216 => 'DBIx::Class::Storage::DBI';
218 ok $replicated->schema->storage->pool->has_replicants
219 => 'does have replicants';
221 is $replicated->schema->storage->pool->num_replicants => 2
222 => 'has two replicants';
224 does_ok $replicated_storages[0]
225 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
227 does_ok $replicated_storages[1]
228 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
230 my @replicant_names = keys %{$replicated->schema->storage->replicants};
232 does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
233 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
235 does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
236 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
238 ## Add some info to the database
242 ->populate('Artist', [
243 [ qw/artistid name/ ],
244 [ 4, "Ozric Tentacles"],
247 ## Make sure all the slaves have the table definitions
249 $replicated->replicate;
250 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
251 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
252 $replicated->schema->storage->pool->validate_replicants;
254 ## Make sure we can read the data.
256 ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
260 => 'DBICTest::Artist';
262 is $artist1->name, 'Ozric Tentacles'
263 => 'Found expected name for first result';
265 ## Add some new rows that only the master will have This is because
266 ## we overload any type of write operation so that is must hit the master
271 ->populate('Artist', [
272 [ qw/artistid name/ ],
273 [ 5, "Doom's Children"],
274 [ 6, "Dead On Arrival"],
278 ## Make sure all the slaves have the table definitions
279 $replicated->replicate;
281 ## Should find some data now
283 ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
287 => 'DBICTest::Artist';
289 is $artist2->name, "Doom's Children"
290 => 'Found expected name for first result';
292 ## What happens when we disconnect all the replicants?
294 is $replicated->schema->storage->pool->connected_replicants => 2
295 => "both replicants are connected";
297 $replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
298 $replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
300 is $replicated->schema->storage->pool->connected_replicants => 0
301 => "both replicants are now disconnected";
303 ## All these should pass, since the database should automatically reconnect
305 ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
306 => 'Still finding stuff.';
309 => 'DBICTest::Artist';
311 is $artist3->name, "Dead On Arrival"
312 => 'Found expected name for first result';
314 is $replicated->schema->storage->pool->connected_replicants => 1
315 => "At Least One replicant reconnected to handle the job";
317 ## What happens when we try to select something that doesn't exist?
319 ok ! $replicated->schema->resultset('Artist')->find(666)
320 => 'Correctly failed to find something.';
322 ## test the reliable option
326 $replicated->schema->storage->set_reliable_storage;
328 ok $replicated->schema->resultset('Artist')->find(2)
329 => 'Read from master 1';
331 ok $replicated->schema->resultset('Artist')->find(5)
332 => 'Read from master 2';
334 $replicated->schema->storage->set_balanced_storage;
336 ok $replicated->schema->resultset('Artist')->find(3)
337 => 'Read from replicant';
340 ## Make sure when reliable goes out of scope, we are using replicants again
342 ok $replicated->schema->resultset('Artist')->find(1)
343 => 'back to replicant 1.';
345 ok $replicated->schema->resultset('Artist')->find(2)
346 => 'back to replicant 2.';
348 ## set all the replicants to inactive, and make sure the balancer falls back to
351 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
352 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
354 ok $replicated->schema->resultset('Artist')->find(2)
355 => 'Fallback to master';
357 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
358 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
359 $replicated->schema->storage->pool->validate_replicants;
361 ok $replicated->schema->resultset('Artist')->find(2)
362 => 'Returned to replicates';
364 ## Getting slave status tests
367 ## We skip this tests unless you have a custom replicants, since the default
368 ## sqlite based replication tests don't support these functions.
370 skip 'Cannot Test Replicant Status on Non Replicating Database', 9
371 unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
373 $replicated->replicate; ## Give the slaves a chance to catchup.
375 ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating
376 => 'Replicants are replicating';
378 is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0
379 => 'Replicant is zero seconds behind master';
381 ## Test the validate replicants
383 $replicated->schema->storage->pool->validate_replicants;
385 is $replicated->schema->storage->pool->active_replicants, 2
386 => 'Still have 2 replicants after validation';
388 ## Force the replicants to fail the validate test by required their lag to
389 ## be negative (ie ahead of the master!)
391 $replicated->schema->storage->pool->maximum_lag(-10);
392 $replicated->schema->storage->pool->validate_replicants;
394 is $replicated->schema->storage->pool->active_replicants, 0
395 => 'No way a replicant be be ahead of the master';
397 ## Let's be fair to the replicants again. Let them lag up to 5
399 $replicated->schema->storage->pool->maximum_lag(5);
400 $replicated->schema->storage->pool->validate_replicants;
402 is $replicated->schema->storage->pool->active_replicants, 2
403 => 'Both replicants in good standing again';
405 ## Check auto validate
407 is $replicated->schema->storage->balancer->auto_validate_every, 100
408 => "Got the expected value for auto validate";
410 ## This will make sure we auto validatge everytime
411 $replicated->schema->storage->balancer->auto_validate_every(0);
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 ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
421 is $replicated->schema->storage->pool->active_replicants => 0
422 => "both replicants turned off";
424 ok $replicated->schema->resultset('Artist')->find(5)
425 => 'replicant reactivated';
427 is $replicated->schema->storage->pool->active_replicants => 2
428 => "both replicants reactivated";
431 ## Test the reliably callback
433 ok my $reliably = sub {
435 ok $replicated->schema->resultset('Artist')->find(5)
436 => 'replicant reactivated';
438 } => 'created coderef properly';
440 $replicated->schema->storage->execute_reliably($reliably);
442 ## Try something with an error
444 ok my $unreliably = sub {
446 ok $replicated->schema->resultset('ArtistXX')->find(5)
447 => 'replicant reactivated';
449 } => 'created coderef properly';
451 throws_ok {$replicated->schema->storage->execute_reliably($unreliably)}
452 qr/Can't find source for ArtistXX/
453 => 'Bad coderef throws proper error';
455 ## Make sure replication came back
457 ok $replicated->schema->resultset('Artist')->find(3)
458 => 'replicant reactivated';
460 ## make sure transactions are set to execute_reliably
462 ok my $transaction = sub {
468 ->populate('Artist', [
469 [ qw/artistid name/ ],
470 [ $id, "Children of the Grave"],
473 ok my $result = $replicated->schema->resultset('Artist')->find($id)
474 => 'Found expected artist';
476 ok my $more = $replicated->schema->resultset('Artist')->find(1)
477 => 'Found expected artist again';
479 return ($result, $more);
481 } => 'Created a coderef properly';
483 ## Test the transaction with multi return
485 ok my @return = $replicated->schema->txn_do($transaction, 666)
486 => 'did transaction';
488 is $return[0]->id, 666
489 => 'first returned value is correct';
492 => 'second returned value is correct';
495 ## Test that asking for single return works
497 ok my $return = $replicated->schema->txn_do($transaction, 777)
498 => 'did transaction';
501 => 'first returned value is correct';
504 ## Test transaction returning a single value
507 ok my $result = $replicated->schema->txn_do(sub {
508 ok my $more = $replicated->schema->resultset('Artist')->find(1)
509 => 'found inside a transaction';
511 }) => 'successfully processed transaction';
514 => 'Got expected single result from transaction';
517 ## Make sure replication came back
519 ok $replicated->schema->resultset('Artist')->find(1)
520 => 'replicant reactivated';
522 ## Test Discard changes
525 ok my $artist = $replicated->schema->resultset('Artist')->find(2)
526 => 'got an artist to test discard changes';
528 ok $artist->discard_changes
529 => 'properly discard changes';
532 ## Test some edge cases, like trying to do a transaction inside a transaction, etc
535 ok my $result = $replicated->schema->txn_do(sub {
536 return $replicated->schema->txn_do(sub {
537 ok my $more = $replicated->schema->resultset('Artist')->find(1)
538 => 'found inside a transaction inside a transaction';
541 }) => 'successfully processed transaction';
544 => 'Got expected single result from transaction';
548 ok my $result = $replicated->schema->txn_do(sub {
549 return $replicated->schema->storage->execute_reliably(sub {
550 return $replicated->schema->txn_do(sub {
551 return $replicated->schema->storage->execute_reliably(sub {
552 ok my $more = $replicated->schema->resultset('Artist')->find(1)
553 => 'found inside crazy deep transactions and execute_reliably';
558 }) => 'successfully processed transaction';
561 => 'Got expected single result from transaction';
564 ## Test the force_pool resultset attribute.
567 ok my $artist_rs = $replicated->schema->resultset('Artist')
568 => 'got artist resultset';
570 ## Turn on Forced Pool Storage
571 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>'master'})
572 => 'Created a resultset using force_pool storage';
574 ok my $artist = $reliable_artist_rs->find(2)
575 => 'got an artist result via force_pool storage';
578 ## Delete the old database files
579 $replicated->cleanup;
581 use Data::Dump qw/dump/;
582 #warn dump $replicated->schema->storage->read_handler;