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
64 my $schema = DBICTest->init_schema(
67 '::DBI::Replicated' => {
68 balancer_type=>'::Random',
70 auto_validate_every=>100,
82 sub generate_replicant_connect_info {}
87 ## --------------------------------------------------------------------- ##
88 ## Subclass for when you are using SQLite for testing, this provides a fake
89 ## replication support.
90 ## --------------------------------------------------------------------- ##
92 package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
96 use base 'DBIx::Class::DBI::Replicated::TestReplication';
98 __PACKAGE__->mk_accessors( qw/master_path slave_paths/ );
100 ## Set the mastep path from DBICTest
103 my $class = shift @_;
104 my $self = $class->SUPER::new(@_);
106 $self->master_path( DBICTest->_sqlite_dbfilename );
108 "t/var/DBIxClass_slave1.db",
109 "t/var/DBIxClass_slave2.db",
115 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
116 ## $storage->connect_info to be used for connecting replicants.
118 sub generate_replicant_connect_info {
122 } @{$self->slave_paths};
124 return map { [$_,'','',{AutoCommit=>1}] } @dsn;
127 ## Do a 'good enough' replication by copying the master dbfile over each of
128 ## the slave dbfiles. If the master is SQLite we do this, otherwise we
129 ## just do a one second pause to let the slaves catch up.
133 foreach my $slave (@{$self->slave_paths}) {
134 copy($self->master_path, $slave);
138 ## Cleanup after ourselves. Unlink all gthe slave paths.
142 foreach my $slave (@{$self->slave_paths}) {
147 ## --------------------------------------------------------------------- ##
148 ## Subclass for when you are setting the databases via custom export vars
149 ## This is for when you have a replicating database setup that you are
150 ## going to test against. You'll need to define the correct $ENV and have
151 ## two slave databases to test against, as well as a replication system
152 ## that will replicate in less than 1 second.
153 ## --------------------------------------------------------------------- ##
155 package DBIx::Class::DBI::Replicated::TestReplication::Custom;
156 use base 'DBIx::Class::DBI::Replicated::TestReplication';
158 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
159 ## $storage->connect_info to be used for connecting replicants.
161 sub generate_replicant_connect_info {
163 [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
164 [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],
168 ## pause a bit to let the replication catch up
175 ## ----------------------------------------------------------------------------
176 ## Create an object and run some tests
177 ## ----------------------------------------------------------------------------
179 ## Thi first bunch of tests are basic, just make sure all the bits are behaving
181 my $replicated_class = DBICTest->has_custom_dsn ?
182 'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
183 'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
185 ok my $replicated = $replicated_class->new
186 => 'Created a replication object';
188 isa_ok $replicated->schema
189 => 'DBIx::Class::Schema';
191 isa_ok $replicated->schema->storage
192 => 'DBIx::Class::Storage::DBI::Replicated';
194 ok $replicated->schema->storage->meta
195 => 'has a meta object';
197 isa_ok $replicated->schema->storage->master
198 => 'DBIx::Class::Storage::DBI';
200 isa_ok $replicated->schema->storage->pool
201 => 'DBIx::Class::Storage::DBI::Replicated::Pool';
203 does_ok $replicated->schema->storage->balancer
204 => 'DBIx::Class::Storage::DBI::Replicated::Balancer';
206 ok my @replicant_connects = $replicated->generate_replicant_connect_info
207 => 'got replication connect information';
209 ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
210 => 'Created some storages suitable for replicants';
212 isa_ok $replicated->schema->storage->balancer->current_replicant
213 => 'DBIx::Class::Storage::DBI';
215 ok $replicated->schema->storage->pool->has_replicants
216 => 'does have replicants';
218 is $replicated->schema->storage->pool->num_replicants => 2
219 => 'has two replicants';
221 does_ok $replicated_storages[0]
222 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
224 does_ok $replicated_storages[1]
225 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
227 my @replicant_names = keys %{$replicated->schema->storage->replicants};
229 does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
230 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
232 does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
233 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
235 ## Add some info to the database
239 ->populate('Artist', [
240 [ qw/artistid name/ ],
241 [ 4, "Ozric Tentacles"],
244 ## Make sure all the slaves have the table definitions
246 $replicated->replicate;
247 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
248 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
250 ## Make sure we can read the data.
252 ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
256 => 'DBICTest::Artist';
258 is $artist1->name, 'Ozric Tentacles'
259 => 'Found expected name for first result';
261 ## Add some new rows that only the master will have This is because
262 ## we overload any type of write operation so that is must hit the master
267 ->populate('Artist', [
268 [ qw/artistid name/ ],
269 [ 5, "Doom's Children"],
270 [ 6, "Dead On Arrival"],
274 ## Make sure all the slaves have the table definitions
275 $replicated->replicate;
277 ## Should find some data now
279 ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
283 => 'DBICTest::Artist';
285 is $artist2->name, "Doom's Children"
286 => 'Found expected name for first result';
288 ## What happens when we disconnect all the replicants?
290 is $replicated->schema->storage->pool->connected_replicants => 2
291 => "both replicants are connected";
293 $replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
294 $replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
296 is $replicated->schema->storage->pool->connected_replicants => 0
297 => "both replicants are now disconnected";
299 ## All these should pass, since the database should automatically reconnect
301 ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
302 => 'Still finding stuff.';
305 => 'DBICTest::Artist';
307 is $artist3->name, "Dead On Arrival"
308 => 'Found expected name for first result';
310 is $replicated->schema->storage->pool->connected_replicants => 1
311 => "One replicant reconnected to handle the job";
313 ## What happens when we try to select something that doesn't exist?
315 ok ! $replicated->schema->resultset('Artist')->find(666)
316 => 'Correctly failed to find something.';
318 ## test the reliable option
322 $replicated->schema->storage->set_reliable_storage;
324 ok $replicated->schema->resultset('Artist')->find(2)
325 => 'Read from master 1';
327 ok $replicated->schema->resultset('Artist')->find(5)
328 => 'Read from master 2';
330 $replicated->schema->storage->set_balanced_storage;
332 ok $replicated->schema->resultset('Artist')->find(3)
333 => 'Read from replicant';
336 ## Make sure when reliable goes out of scope, we are using replicants again
338 ok $replicated->schema->resultset('Artist')->find(1)
339 => 'back to replicant 1.';
341 ok $replicated->schema->resultset('Artist')->find(2)
342 => 'back to replicant 2.';
344 ## set all the replicants to inactive, and make sure the balancer falls back to
347 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
348 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
350 ok $replicated->schema->resultset('Artist')->find(2)
351 => 'Fallback to master';
353 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
354 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
356 ok $replicated->schema->resultset('Artist')->find(2)
357 => 'Returned to replicates';
359 ## Getting slave status tests
362 ## We skip this tests unless you have a custom replicants, since the default
363 ## sqlite based replication tests don't support these functions.
365 skip 'Cannot Test Replicant Status on Non Replicating Database', 9
366 unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
368 $replicated->replicate; ## Give the slaves a chance to catchup.
370 ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating
371 => 'Replicants are replicating';
373 is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0
374 => 'Replicant is zero seconds behind master';
376 ## Test the validate replicants
378 $replicated->schema->storage->pool->validate_replicants;
380 is $replicated->schema->storage->pool->active_replicants, 2
381 => 'Still have 2 replicants after validation';
383 ## Force the replicants to fail the validate test by required their lag to
384 ## be negative (ie ahead of the master!)
386 $replicated->schema->storage->pool->maximum_lag(-10);
387 $replicated->schema->storage->pool->validate_replicants;
389 is $replicated->schema->storage->pool->active_replicants, 0
390 => 'No way a replicant be be ahead of the master';
392 ## Let's be fair to the replicants again. Let them lag up to 5
394 $replicated->schema->storage->pool->maximum_lag(5);
395 $replicated->schema->storage->pool->validate_replicants;
397 is $replicated->schema->storage->pool->active_replicants, 2
398 => 'Both replicants in good standing again';
400 ## Check auto validate
402 is $replicated->schema->storage->balancer->auto_validate_every, 100
403 => "Got the expected value for auto validate";
405 ## This will make sure we auto validatge everytime
406 $replicated->schema->storage->balancer->auto_validate_every(0);
408 ## set all the replicants to inactive, and make sure the balancer falls back to
411 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
412 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
414 ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
416 is $replicated->schema->storage->pool->active_replicants => 0
417 => "both replicants turned off";
419 ok $replicated->schema->resultset('Artist')->find(5)
420 => 'replicant reactivated';
422 is $replicated->schema->storage->pool->active_replicants => 2
423 => "both replicants reactivated";
426 ## Test the reliably callback
428 ok my $reliably = sub {
430 ok $replicated->schema->resultset('Artist')->find(5)
431 => 'replicant reactivated';
433 } => 'created coderef properly';
435 $replicated->schema->storage->execute_reliably($reliably);
437 ## Try something with an error
439 ok my $unreliably = sub {
441 ok $replicated->schema->resultset('ArtistXX')->find(5)
442 => 'replicant reactivated';
444 } => 'created coderef properly';
446 throws_ok {$replicated->schema->storage->execute_reliably($unreliably)}
447 qr/Can't find source for ArtistXX/
448 => 'Bad coderef throws proper error';
450 ## Make sure replication came back
452 ok $replicated->schema->resultset('Artist')->find(3)
453 => 'replicant reactivated';
455 ## make sure transactions are set to execute_reliably
457 ok my $transaction = sub {
463 ->populate('Artist', [
464 [ qw/artistid name/ ],
465 [ $id, "Children of the Grave"],
468 ok my $result = $replicated->schema->resultset('Artist')->find($id)
469 => 'Found expected artist';
471 ok my $more = $replicated->schema->resultset('Artist')->find(1)
472 => 'Found expected artist again';
474 return ($result, $more);
476 } => 'Created a coderef properly';
478 ## Test the transaction with multi return
480 ok my @return = $replicated->schema->txn_do($transaction, 666)
481 => 'did transaction';
483 is $return[0]->id, 666
484 => 'first returned value is correct';
487 => 'second returned value is correct';
490 ## Test that asking for single return works
492 ok my $return = $replicated->schema->txn_do($transaction, 777)
493 => 'did transaction';
496 => 'first returned value is correct';
499 ## Test transaction returning a single value
502 ok my $result = $replicated->schema->txn_do(sub {
503 ok my $more = $replicated->schema->resultset('Artist')->find(1)
504 => 'found inside a transaction';
506 }) => 'successfully processed transaction';
509 => 'Got expected single result from transaction';
512 ## Make sure replication came back
514 ok $replicated->schema->resultset('Artist')->find(1)
515 => 'replicant reactivated';
517 ## Test Discard changes
520 ok my $artist = $replicated->schema->resultset('Artist')->find(2)
521 => 'got an artist to test discard changes';
523 ok $artist->discard_changes
524 => 'properly discard changes';
527 ## Test some edge cases, like trying to do a transaction inside a transaction, etc
530 ok my $result = $replicated->schema->txn_do(sub {
531 return $replicated->schema->txn_do(sub {
532 ok my $more = $replicated->schema->resultset('Artist')->find(1)
533 => 'found inside a transaction inside a transaction';
536 }) => 'successfully processed transaction';
539 => 'Got expected single result from transaction';
543 ok my $result = $replicated->schema->txn_do(sub {
544 return $replicated->schema->storage->execute_reliably(sub {
545 return $replicated->schema->txn_do(sub {
546 return $replicated->schema->storage->execute_reliably(sub {
547 ok my $more = $replicated->schema->resultset('Artist')->find(1)
548 => 'found inside crazy deep transactions and execute_reliably';
553 }) => 'successfully processed transaction';
556 => 'Got expected single result from transaction';
559 ## Test the force_pool resultset attribute.
562 ok my $artist_rs = $replicated->schema->resultset('Artist')
563 => 'got artist resultset';
565 ## Turn on Forced Pool Storage
566 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>'master'})
567 => 'Created a resultset using force_pool storage';
569 ok my $artist = $reliable_artist_rs->find(2)
570 => 'got an artist result via force_pool storage';
573 ## Delete the old database files
574 $replicated->cleanup;