9 eval "use Moose; use Test::Moose";
11 ? ( skip_all => 'needs Moose for testing' )
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
64 my $schema = DBICTest->init_schema(
66 '::DBI::Replicated' => {
67 balancer_type=>'::Random',
69 auto_validate_every=>100,
81 sub generate_replicant_connect_info {}
86 ## --------------------------------------------------------------------- ##
87 ## Subclass for when you are using SQLite for testing, this provides a fake
88 ## replication support.
89 ## --------------------------------------------------------------------- ##
91 package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
95 use base 'DBIx::Class::DBI::Replicated::TestReplication';
97 __PACKAGE__->mk_accessors( qw/master_path slave_paths/ );
99 ## Set the mastep path from DBICTest
102 my $class = shift @_;
103 my $self = $class->SUPER::new(@_);
105 $self->master_path( DBICTest->_sqlite_dbfilename );
107 "t/var/DBIxClass_slave1.db",
108 "t/var/DBIxClass_slave2.db",
114 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
115 ## $storage->connect_info to be used for connecting replicants.
117 sub generate_replicant_connect_info {
121 } @{$self->slave_paths};
123 return map { [$_,'','',{AutoCommit=>1}] } @dsn;
126 ## Do a 'good enough' replication by copying the master dbfile over each of
127 ## the slave dbfiles. If the master is SQLite we do this, otherwise we
128 ## just do a one second pause to let the slaves catch up.
132 foreach my $slave (@{$self->slave_paths}) {
133 copy($self->master_path, $slave);
137 ## Cleanup after ourselves. Unlink all gthe slave paths.
141 foreach my $slave (@{$self->slave_paths}) {
146 ## --------------------------------------------------------------------- ##
147 ## Subclass for when you are setting the databases via custom export vars
148 ## This is for when you have a replicating database setup that you are
149 ## going to test against. You'll need to define the correct $ENV and have
150 ## two slave databases to test against, as well as a replication system
151 ## that will replicate in less than 1 second.
152 ## --------------------------------------------------------------------- ##
154 package DBIx::Class::DBI::Replicated::TestReplication::Custom;
155 use base 'DBIx::Class::DBI::Replicated::TestReplication';
157 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
158 ## $storage->connect_info to be used for connecting replicants.
160 sub generate_replicant_connect_info {
162 [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
163 [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],
167 ## pause a bit to let the replication catch up
174 ## ----------------------------------------------------------------------------
175 ## Create an object and run some tests
176 ## ----------------------------------------------------------------------------
178 ## Thi first bunch of tests are basic, just make sure all the bits are behaving
180 my $replicated_class = DBICTest->has_custom_dsn ?
181 'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
182 'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
184 ok my $replicated = $replicated_class->new
185 => 'Created a replication object';
187 isa_ok $replicated->schema
188 => 'DBIx::Class::Schema';
190 isa_ok $replicated->schema->storage
191 => 'DBIx::Class::Storage::DBI::Replicated';
193 ok $replicated->schema->storage->meta
194 => 'has a meta object';
196 isa_ok $replicated->schema->storage->master
197 => 'DBIx::Class::Storage::DBI';
199 isa_ok $replicated->schema->storage->pool
200 => 'DBIx::Class::Storage::DBI::Replicated::Pool';
202 does_ok $replicated->schema->storage->balancer
203 => 'DBIx::Class::Storage::DBI::Replicated::Balancer';
205 ok my @replicant_connects = $replicated->generate_replicant_connect_info
206 => 'got replication connect information';
208 ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
209 => 'Created some storages suitable for replicants';
211 isa_ok $replicated->schema->storage->balancer->current_replicant
212 => 'DBIx::Class::Storage::DBI';
214 ok $replicated->schema->storage->pool->has_replicants
215 => 'does have replicants';
217 is $replicated->schema->storage->pool->num_replicants => 2
218 => 'has two replicants';
220 does_ok $replicated_storages[0]
221 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
223 does_ok $replicated_storages[1]
224 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
226 my @replicant_names = keys %{$replicated->schema->storage->replicants};
228 does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
229 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
231 does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
232 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
234 ## Add some info to the database
238 ->populate('Artist', [
239 [ qw/artistid name/ ],
240 [ 4, "Ozric Tentacles"],
243 ## Make sure all the slaves have the table definitions
245 $replicated->replicate;
246 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
247 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
249 ## Make sure we can read the data.
251 ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
255 => 'DBICTest::Artist';
257 is $artist1->name, 'Ozric Tentacles'
258 => 'Found expected name for first result';
260 ## Add some new rows that only the master will have This is because
261 ## we overload any type of write operation so that is must hit the master
266 ->populate('Artist', [
267 [ qw/artistid name/ ],
268 [ 5, "Doom's Children"],
269 [ 6, "Dead On Arrival"],
274 ## We can't do this test if we have a custom replicants, since we assume
275 ## if there are custom one that you are trying to test a real replicating
276 ## system. See docs above for more.
278 skip 'Cannot test inconsistent replication since you have a real replication system', 1
279 if DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
281 ## Alright, the database 'cluster' is not in a consistent state. When we do
282 ## a read now we expect bad news
283 is $replicated->schema->resultset('Artist')->find(5), undef
284 => 'read after disconnect fails because it uses a replicant which we have neglected to "replicate" yet';
287 ## Make sure all the slaves have the table definitions
288 $replicated->replicate;
290 ## Should find some data now
292 ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
296 => 'DBICTest::Artist';
298 is $artist2->name, "Doom's Children"
299 => 'Found expected name for first result';
301 ## What happens when we disconnect all the replicants?
303 is $replicated->schema->storage->pool->connected_replicants => 2
304 => "both replicants are connected";
306 $replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
307 $replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
309 is $replicated->schema->storage->pool->connected_replicants => 0
310 => "both replicants are now disconnected";
312 ## All these should pass, since the database should automatically reconnect
314 ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
315 => 'Still finding stuff.';
318 => 'DBICTest::Artist';
320 is $artist3->name, "Dead On Arrival"
321 => 'Found expected name for first result';
323 is $replicated->schema->storage->pool->connected_replicants => 1
324 => "One replicant reconnected to handle the job";
326 ## What happens when we try to select something that doesn't exist?
328 ok ! $replicated->schema->resultset('Artist')->find(666)
329 => 'Correctly failed to find something.';
331 ## test the reliable option
335 $replicated->schema->storage->set_reliable_storage;
337 ok $replicated->schema->resultset('Artist')->find(2)
338 => 'Read from master 1';
340 ok $replicated->schema->resultset('Artist')->find(5)
341 => 'Read from master 2';
343 $replicated->schema->storage->set_balanced_storage;
345 ok $replicated->schema->resultset('Artist')->find(3)
346 => 'Read from replicant';
349 ## Make sure when reliable goes out of scope, we are using replicants again
351 ok $replicated->schema->resultset('Artist')->find(1)
352 => 'back to replicant 1.';
354 ok $replicated->schema->resultset('Artist')->find(2)
355 => 'back to replicant 2.';
357 ## set all the replicants to inactive, and make sure the balancer falls back to
360 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
361 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
363 ok $replicated->schema->resultset('Artist')->find(2)
364 => 'Fallback to master';
366 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
367 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
369 ok $replicated->schema->resultset('Artist')->find(2)
370 => 'Returned to replicates';
372 ## Getting slave status tests
375 ## We skip this tests unless you have a custom replicants, since the default
376 ## sqlite based replication tests don't support these functions.
378 skip 'Cannot Test Replicant Status on Non Replicating Database', 9
379 unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
381 $replicated->replicate; ## Give the slaves a chance to catchup.
383 ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating
384 => 'Replicants are replicating';
386 is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0
387 => 'Replicant is zero seconds behind master';
389 ## Test the validate replicants
391 $replicated->schema->storage->pool->validate_replicants;
393 is $replicated->schema->storage->pool->active_replicants, 2
394 => 'Still have 2 replicants after validation';
396 ## Force the replicants to fail the validate test by required their lag to
397 ## be negative (ie ahead of the master!)
399 $replicated->schema->storage->pool->maximum_lag(-10);
400 $replicated->schema->storage->pool->validate_replicants;
402 is $replicated->schema->storage->pool->active_replicants, 0
403 => 'No way a replicant be be ahead of the master';
405 ## Let's be fair to the replicants again. Let them lag up to 5
407 $replicated->schema->storage->pool->maximum_lag(5);
408 $replicated->schema->storage->pool->validate_replicants;
410 is $replicated->schema->storage->pool->active_replicants, 2
411 => 'Both replicants in good standing again';
413 ## Check auto validate
415 is $replicated->schema->storage->balancer->auto_validate_every, 100
416 => "Got the expected value for auto validate";
418 ## This will make sure we auto validatge everytime
419 $replicated->schema->storage->balancer->auto_validate_every(0);
421 ## set all the replicants to inactive, and make sure the balancer falls back to
424 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
425 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
427 ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
429 is $replicated->schema->storage->pool->active_replicants => 0
430 => "both replicants turned off";
432 ok $replicated->schema->resultset('Artist')->find(5)
433 => 'replicant reactivated';
435 is $replicated->schema->storage->pool->active_replicants => 2
436 => "both replicants reactivated";
439 ## Test the reliably callback
441 ok my $reliably = sub {
443 ok $replicated->schema->resultset('Artist')->find(5)
444 => 'replicant reactivated';
446 } => 'created coderef properly';
448 $replicated->schema->storage->execute_reliably($reliably);
450 ## Try something with an error
452 ok my $unreliably = sub {
454 ok $replicated->schema->resultset('ArtistXX')->find(5)
455 => 'replicant reactivated';
457 } => 'created coderef properly';
459 throws_ok {$replicated->schema->storage->execute_reliably($unreliably)}
460 qr/Can't find source for ArtistXX/
461 => 'Bad coderef throws proper error';
463 ## Make sure replication came back
465 ok $replicated->schema->resultset('Artist')->find(3)
466 => 'replicant reactivated';
468 ## make sure transactions are set to execute_reliably
470 ok my $transaction = sub {
476 ->populate('Artist', [
477 [ qw/artistid name/ ],
478 [ $id, "Children of the Grave"],
481 ok my $result = $replicated->schema->resultset('Artist')->find($id)
482 => 'Found expected artist';
484 ok my $more = $replicated->schema->resultset('Artist')->find(1)
485 => 'Found expected artist again';
487 return ($result, $more);
489 } => 'Created a coderef properly';
491 ## Test the transaction with multi return
493 ok my @return = $replicated->schema->txn_do($transaction, 666)
494 => 'did transaction';
496 is $return[0]->id, 666
497 => 'first returned value is correct';
500 => 'second returned value is correct';
503 ## Test that asking for single return works
505 ok my $return = $replicated->schema->txn_do($transaction, 777)
506 => 'did transaction';
509 => 'first returned value is correct';
512 ## Test transaction returning a single value
515 ok my $result = $replicated->schema->txn_do(sub {
516 ok my $more = $replicated->schema->resultset('Artist')->find(1)
517 => 'found inside a transaction';
519 }) => 'successfully processed transaction';
522 => 'Got expected single result from transaction';
525 ## Make sure replication came back
527 ok $replicated->schema->resultset('Artist')->find(1)
528 => 'replicant reactivated';
530 ## Test Discard changes
533 ok my $artist = $replicated->schema->resultset('Artist')->find(2)
534 => 'got an artist to test discard changes';
536 ok $artist->discard_changes
537 => 'properly discard changes';
540 ## Test some edge cases, like trying to do a transaction inside a transaction, etc
543 ok my $result = $replicated->schema->txn_do(sub {
544 return $replicated->schema->txn_do(sub {
545 ok my $more = $replicated->schema->resultset('Artist')->find(1)
546 => 'found inside a transaction inside a transaction';
549 }) => 'successfully processed transaction';
552 => 'Got expected single result from transaction';
556 ok my $result = $replicated->schema->txn_do(sub {
557 return $replicated->schema->storage->execute_reliably(sub {
558 return $replicated->schema->txn_do(sub {
559 return $replicated->schema->storage->execute_reliably(sub {
560 ok my $more = $replicated->schema->resultset('Artist')->find(1)
561 => 'found inside crazy deep transactions and execute_reliably';
566 }) => 'successfully processed transaction';
569 => 'Got expected single result from transaction';
572 ## Test the force_pool resultset attribute.
575 ok my $artist_rs = $replicated->schema->resultset('Artist')
576 => 'got artist resultset';
578 ## Turn on Forced Pool Storage
579 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>'master'})
580 => 'Created a resultset using force_pool storage';
582 ok my $artist = $reliable_artist_rs->find(2)
583 => 'got an artist result via force_pool storage';
586 ## Delete the old database files
587 $replicated->cleanup;