Move find_co_root into DBICTest::Util
[dbsrgits/DBIx-Class-Historic.git] / t / storage / replicated.t
CommitLineData
cb551b07 1use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_replicated';
2
e4dc89b3 3use strict;
4use warnings;
68de9438 5
e4dc89b3 6use Test::More;
cb551b07 7use DBIx::Class::_Util 'modver_gt_or_eq_and_lt';
4bea1fe7 8use lib qw(t/lib);
9use DBICTest;
10
68de9438 11BEGIN {
cb551b07 12 plan skip_all => "A trial version of Moose detected known to break replication - skipping test known to fail" if (
13 DBICTest::RunMode->is_smoker
14 and
15 modver_gt_or_eq_and_lt( 'Moose', '1.99', '1.9903' )
16 )
e4ee5861 17}
18
68de9438 19use Test::Moose;
c4d3fae2 20use Test::Exception;
9901aad7 21use List::Util 'first';
b2e4d522 22use Scalar::Util 'reftype';
3da4f736 23use File::Spec;
68de9438 24use Moose();
25use MooseX::Types();
26note "Using Moose version $Moose::VERSION and MooseX::Types version $MooseX::Types::VERSION";
8f7986d6 27
7672675a 28my $var_dir = quotemeta ( File::Spec->catdir(qw/t var/) );
29
6287275c 30## Add a connect_info option to test option merging.
617474ab 31use DBIx::Class::Storage::DBI::Replicated;
6287275c 32{
33 package DBIx::Class::Storage::DBI::Replicated;
34
35 use Moose;
36
37 __PACKAGE__->meta->make_mutable;
38
39 around connect_info => sub {
40 my ($next, $self, $info) = @_;
41 $info->[3]{master_option} = 1;
42 $self->$next($info);
43 };
44
45 __PACKAGE__->meta->make_immutable;
46
47 no Moose;
48}
49
0f83441a 50
6a151f58 51
89cf6a70 52=head1 HOW TO USE
53
54 This is a test of the replicated storage system. This will work in one of
55 two ways, either it was try to fake replication with a couple of SQLite DBs
56 and creative use of copy, or if you define a couple of %ENV vars correctly
57 will try to test those. If you do that, it will assume the setup is properly
58 replicating. Your results may vary, but I have demonstrated this to work with
59 mysql native replication.
d59cf2f2 60
89cf6a70 61=cut
62
63
0f83441a 64## ----------------------------------------------------------------------------
65## Build a class to hold all our required testing data and methods.
66## ----------------------------------------------------------------------------
67
857d66ca 68TESTSCHEMACLASSES: {
2bf79155 69
857d66ca 70 ## --------------------------------------------------------------------- ##
71 ## Create an object to contain your replicated stuff.
72 ## --------------------------------------------------------------------- ##
d59cf2f2 73
2bf79155 74 package DBIx::Class::DBI::Replicated::TestReplication;
d59cf2f2 75
2bf79155 76 use DBICTest;
77 use base qw/Class::Accessor::Fast/;
d59cf2f2 78
857d66ca 79 __PACKAGE__->mk_accessors( qw/schema/ );
2bf79155 80
81 ## Initialize the object
d59cf2f2 82
83 sub new {
84 my ($class, $schema_method) = (shift, shift);
85 my $self = $class->SUPER::new(@_);
86
87 $self->schema( $self->init_schema($schema_method) );
88 return $self;
89 }
90
26ab719a 91 ## Get the Schema and set the replication storage type
d59cf2f2 92
2bf79155 93 sub init_schema {
7536c92b 94 #my ($class, $schema_getter) = @_;
95 shift->${\ ( 'get_schema_' . shift ) };
2bf79155 96 }
dcdf7b2c 97
98 sub get_schema_by_storage_type {
99 DBICTest->init_schema(
100 sqlite_use_file => 1,
101 storage_type=>{
102 '::DBI::Replicated' => {
103 balancer_type=>'::Random',
104 balancer_args=>{
105 auto_validate_every=>100,
64ae1667 106 master_read_weight => 1
dcdf7b2c 107 },
108 }
109 },
110 deploy_args=>{
111 add_drop_table => 1,
112 },
113 );
114 }
115
116 sub get_schema_by_connect_info {
117 DBICTest->init_schema(
118 sqlite_use_file => 1,
119 storage_type=> '::DBI::Replicated',
120 balancer_type=>'::Random',
121 balancer_args=> {
64ae1667 122 auto_validate_every=>100,
123 master_read_weight => 1
124 },
125 pool_args=>{
126 maximum_lag=>1,
dcdf7b2c 127 },
128 deploy_args=>{
129 add_drop_table => 1,
130 },
131 );
132 }
133
857d66ca 134 sub generate_replicant_connect_info {}
135 sub replicate {}
136 sub cleanup {}
137
b2e4d522 138 ## --------------------------------------------------------------------- ##
857d66ca 139 ## Subclass for when you are using SQLite for testing, this provides a fake
140 ## replication support.
141 ## --------------------------------------------------------------------- ##
d59cf2f2 142
857d66ca 143 package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
144
145 use DBICTest;
d59cf2f2 146 use File::Copy;
857d66ca 147 use base 'DBIx::Class::DBI::Replicated::TestReplication';
d59cf2f2 148
3da4f736 149 __PACKAGE__->mk_accessors(qw/master_path slave_paths/);
d59cf2f2 150
3da4f736 151 ## Set the master path from DBICTest
d59cf2f2 152
153 sub new {
154 my $class = shift @_;
155 my $self = $class->SUPER::new(@_);
156
157 $self->master_path( DBICTest->_sqlite_dbfilename );
158 $self->slave_paths([
159 File::Spec->catfile(qw/t var DBIxClass_slave1.db/),
160 File::Spec->catfile(qw/t var DBIxClass_slave2.db/),
161 ]);
162
163 return $self;
164 }
165
26ab719a 166 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
167 ## $storage->connect_info to be used for connecting replicants.
d59cf2f2 168
26ab719a 169 sub generate_replicant_connect_info {
857d66ca 170 my $self = shift @_;
26ab719a 171 my @dsn = map {
172 "dbi:SQLite:${_}";
173 } @{$self->slave_paths};
d59cf2f2 174
9901aad7 175 my @connect_infos = map { [$_,'','',{AutoCommit=>1}] } @dsn;
176
d59cf2f2 177 ## Make sure nothing is left over from a failed test
178 $self->cleanup;
3da4f736 179
d59cf2f2 180 ## try a hashref too
9901aad7 181 my $c = $connect_infos[0];
182 $connect_infos[0] = {
183 dsn => $c->[0],
184 user => $c->[1],
185 password => $c->[2],
186 %{ $c->[3] }
187 };
188
189 @connect_infos
26ab719a 190 }
9901aad7 191
26ab719a 192 ## Do a 'good enough' replication by copying the master dbfile over each of
50336325 193 ## the slave dbfiles. If the master is SQLite we do this, otherwise we
194 ## just do a one second pause to let the slaves catch up.
d59cf2f2 195
26ab719a 196 sub replicate {
197 my $self = shift @_;
198 foreach my $slave (@{$self->slave_paths}) {
199 copy($self->master_path, $slave);
200 }
201 }
d59cf2f2 202
9e75be92 203 ## Cleanup after ourselves. Unlink all the slave paths.
d59cf2f2 204
26ab719a 205 sub cleanup {
206 my $self = shift @_;
e1ff35c4 207 $_->disconnect for values %{ $self->schema->storage->replicants };
26ab719a 208 foreach my $slave (@{$self->slave_paths}) {
d59cf2f2 209 if(-e $slave) {
210 unlink $slave;
211 }
212 }
26ab719a 213 }
d59cf2f2 214
857d66ca 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 ## --------------------------------------------------------------------- ##
d59cf2f2 222
223 package DBIx::Class::DBI::Replicated::TestReplication::Custom;
857d66ca 224 use base 'DBIx::Class::DBI::Replicated::TestReplication';
d59cf2f2 225
857d66ca 226 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
227 ## $storage->connect_info to be used for connecting replicants.
d59cf2f2 228
229 sub generate_replicant_connect_info {
857d66ca 230 return (
231 [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
d59cf2f2 232 [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],
857d66ca 233 );
234 }
d59cf2f2 235
236 ## pause a bit to let the replication catch up
237
857d66ca 238 sub replicate {
d59cf2f2 239 sleep 1;
240 }
2bf79155 241}
242
243## ----------------------------------------------------------------------------
244## Create an object and run some tests
245## ----------------------------------------------------------------------------
246
26ab719a 247## Thi first bunch of tests are basic, just make sure all the bits are behaving
2bf79155 248
857d66ca 249my $replicated_class = DBICTest->has_custom_dsn ?
250 'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
251 'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
252
dcdf7b2c 253my $replicated;
254
255for my $method (qw/by_connect_info by_storage_type/) {
a3df35f8 256 undef $replicated;
dcdf7b2c 257 ok $replicated = $replicated_class->new($method)
258 => "Created a replication object $method";
d59cf2f2 259
dcdf7b2c 260 isa_ok $replicated->schema
261 => 'DBIx::Class::Schema';
d59cf2f2 262
dcdf7b2c 263 isa_ok $replicated->schema->storage
264 => 'DBIx::Class::Storage::DBI::Replicated';
265
266 isa_ok $replicated->schema->storage->balancer
267 => 'DBIx::Class::Storage::DBI::Replicated::Balancer::Random'
268 => 'configured balancer_type';
269}
26ab719a 270
f29315ed 271### check that all Storage::DBI methods are handled by ::Replicated
272{
b75235fe 273 my @storage_dbi_methods = Class::MOP::Class
274 ->initialize('DBIx::Class::Storage::DBI')->get_all_method_names;
f29315ed 275
b75235fe 276 my @replicated_methods = DBIx::Class::Storage::DBI::Replicated->meta
277 ->get_all_method_names;
f29315ed 278
b75235fe 279# remove constants and OTHER_CRAP
f29315ed 280 @storage_dbi_methods = grep !/^[A-Z_]+\z/, @storage_dbi_methods;
281
282# remove CAG accessors
283 @storage_dbi_methods = grep !/_accessor\z/, @storage_dbi_methods;
284
285# remove DBIx::Class (the root parent, with CAG and stuff) methods
b75235fe 286 my @root_methods = Class::MOP::Class->initialize('DBIx::Class')
f29315ed 287 ->get_all_method_names;
288 my %count;
b75235fe 289 $count{$_}++ for (@storage_dbi_methods, @root_methods);
f29315ed 290
291 @storage_dbi_methods = grep $count{$_} != 2, @storage_dbi_methods;
292
293# make hashes
294 my %storage_dbi_methods;
295 @storage_dbi_methods{@storage_dbi_methods} = ();
296 my %replicated_methods;
297 @replicated_methods{@replicated_methods} = ();
298
299# remove ::Replicated-specific methods
300 for my $method (@replicated_methods) {
301 delete $replicated_methods{$method}
302 unless exists $storage_dbi_methods{$method};
303 }
e4c5abce 304 @replicated_methods = keys %replicated_methods;
f29315ed 305
306# check that what's left is implemented
307 %count = ();
308 $count{$_}++ for (@storage_dbi_methods, @replicated_methods);
309
310 if ((grep $count{$_} == 2, @storage_dbi_methods) == @storage_dbi_methods) {
311 pass 'all DBIx::Class::Storage::DBI methods implemented';
312 }
313 else {
314 my @unimplemented = grep $count{$_} == 1, @storage_dbi_methods;
315
316 fail 'the following DBIx::Class::Storage::DBI methods are unimplemented: '
317 . "@unimplemented";
318 }
319}
320
26ab719a 321ok $replicated->schema->storage->meta
322 => 'has a meta object';
d59cf2f2 323
26ab719a 324isa_ok $replicated->schema->storage->master
325 => 'DBIx::Class::Storage::DBI';
d59cf2f2 326
26ab719a 327isa_ok $replicated->schema->storage->pool
328 => 'DBIx::Class::Storage::DBI::Replicated::Pool';
d59cf2f2 329
17b05c13 330does_ok $replicated->schema->storage->balancer
d59cf2f2 331 => 'DBIx::Class::Storage::DBI::Replicated::Balancer';
26ab719a 332
333ok my @replicant_connects = $replicated->generate_replicant_connect_info
334 => 'got replication connect information';
335
955a6df6 336ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
26ab719a 337 => 'Created some storages suitable for replicants';
6412a592 338
071bbccb 339our %debug;
340$replicated->schema->storage->debug(1);
341$replicated->schema->storage->debugcb(sub {
d59cf2f2 342 my ($op, $info) = @_;
343 ##warn "\n$op, $info\n";
344 %debug = (
345 op => $op,
346 info => $info,
347 dsn => ($info=~m/\[(.+)\]/)[0],
348 storage_type => $info=~m/REPLICANT/ ? 'REPLICANT' : 'MASTER',
349 );
071bbccb 350});
351
6412a592 352ok my @all_storages = $replicated->schema->storage->all_storages
353 => '->all_storages';
354
dcdf7b2c 355is scalar @all_storages,
356 3
6412a592 357 => 'correct number of ->all_storages';
358
dcdf7b2c 359is ((grep $_->isa('DBIx::Class::Storage::DBI'), @all_storages),
360 3
6412a592 361 => '->all_storages are correct type');
b2e4d522 362
dcdf7b2c 363my @all_storage_opts =
364 grep { (reftype($_)||'') eq 'HASH' }
365 map @{ $_->_connect_info }, @all_storages;
366
367is ((grep $_->{master_option}, @all_storage_opts),
368 3
b2e4d522 369 => 'connect_info was merged from master to replicants');
d59cf2f2 370
9901aad7 371my @replicant_names = keys %{ $replicated->schema->storage->replicants };
372
bd5da369 373ok @replicant_names, "found replicant names @replicant_names";
374
9901aad7 375## Silence warning about not supporting the is_replicating method if using the
376## sqlite dbs.
377$replicated->schema->storage->debugobj->silence(1)
7672675a 378 if first { $_ =~ /$var_dir/ } @replicant_names;
d59cf2f2 379
cb6ec758 380isa_ok $replicated->schema->storage->balancer->current_replicant
d59cf2f2 381 => 'DBIx::Class::Storage::DBI';
9901aad7 382
383$replicated->schema->storage->debugobj->silence(0);
384
26ab719a 385ok $replicated->schema->storage->pool->has_replicants
d59cf2f2 386 => 'does have replicants';
26ab719a 387
17b05c13 388is $replicated->schema->storage->pool->num_replicants => 2
26ab719a 389 => 'has two replicants';
d59cf2f2 390
de5dc9ef 391does_ok $replicated_storages[0]
26ab719a 392 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
393
de5dc9ef 394does_ok $replicated_storages[1]
26ab719a 395 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
d59cf2f2 396
de5dc9ef 397does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
26ab719a 398 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
399
de5dc9ef 400does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
d59cf2f2 401 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
26ab719a 402
403## Add some info to the database
404
405$replicated
406 ->schema
407 ->populate('Artist', [
408 [ qw/artistid name/ ],
409 [ 4, "Ozric Tentacles"],
410 ]);
071bbccb 411
d59cf2f2 412 is $debug{storage_type}, 'MASTER',
413 "got last query from a master: $debug{dsn}";
414
415 like $debug{info}, qr/INSERT/, 'Last was an insert';
416
26ab719a 417## Make sure all the slaves have the table definitions
418
419$replicated->replicate;
5c1d82d2 420$replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
421$replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
9901aad7 422
423## Silence warning about not supporting the is_replicating method if using the
424## sqlite dbs.
425$replicated->schema->storage->debugobj->silence(1)
7672675a 426 if first { $_ =~ /$var_dir/ } @replicant_names;
d59cf2f2 427
f15afa13 428$replicated->schema->storage->pool->validate_replicants;
26ab719a 429
9901aad7 430$replicated->schema->storage->debugobj->silence(0);
431
26ab719a 432## Make sure we can read the data.
433
434ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
435 => 'Created Result';
436
071bbccb 437## We removed testing here since master read weight is on, so we can't tell in
438## advance what storage to expect. We turn master read weight off a bit lower
d59cf2f2 439## is $debug{storage_type}, 'REPLICANT'
440## => "got last query from a replicant: $debug{dsn}, $debug{info}";
071bbccb 441
26ab719a 442isa_ok $artist1
443 => 'DBICTest::Artist';
d59cf2f2 444
26ab719a 445is $artist1->name, 'Ozric Tentacles'
446 => 'Found expected name for first result';
447
ee356d00 448## Check that master_read_weight is honored
449{
87974600 450 no warnings qw/once redefine/;
ee356d00 451
452 local
f404f53c 453 *DBIx::Class::Storage::DBI::Replicated::Balancer::Random::_random_number =
d59cf2f2 454 sub { 999 };
ee356d00 455
456 $replicated->schema->storage->balancer->increment_storage;
457
458 is $replicated->schema->storage->balancer->current_replicant,
459 $replicated->schema->storage->master
460 => 'master_read_weight is honored';
461
462 ## turn it off for the duration of the test
463 $replicated->schema->storage->balancer->master_read_weight(0);
464 $replicated->schema->storage->balancer->increment_storage;
465}
466
26ab719a 467## Add some new rows that only the master will have This is because
468## we overload any type of write operation so that is must hit the master
469## database.
470
471$replicated
472 ->schema
473 ->populate('Artist', [
474 [ qw/artistid name/ ],
475 [ 5, "Doom's Children"],
476 [ 6, "Dead On Arrival"],
477 [ 7, "Watergate"],
478 ]);
479
d59cf2f2 480 is $debug{storage_type}, 'MASTER',
481 "got last query from a master: $debug{dsn}";
482
483 like $debug{info}, qr/INSERT/, 'Last was an insert';
071bbccb 484
26ab719a 485## Make sure all the slaves have the table definitions
486$replicated->replicate;
487
488## Should find some data now
489
490ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
491 => 'Sync succeed';
071bbccb 492
d59cf2f2 493is $debug{storage_type}, 'REPLICANT'
494 => "got last query from a replicant: $debug{dsn}";
495
26ab719a 496isa_ok $artist2
497 => 'DBICTest::Artist';
d59cf2f2 498
26ab719a 499is $artist2->name, "Doom's Children"
500 => 'Found expected name for first result';
501
502## What happens when we disconnect all the replicants?
503
50336325 504is $replicated->schema->storage->pool->connected_replicants => 2
505 => "both replicants are connected";
d59cf2f2 506
89cf6a70 507$replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
508$replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
26ab719a 509
50336325 510is $replicated->schema->storage->pool->connected_replicants => 0
511 => "both replicants are now disconnected";
512
513## All these should pass, since the database should automatically reconnect
514
26ab719a 515ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
516 => 'Still finding stuff.';
071bbccb 517
d59cf2f2 518is $debug{storage_type}, 'REPLICANT'
519 => "got last query from a replicant: $debug{dsn}";
520
26ab719a 521isa_ok $artist3
522 => 'DBICTest::Artist';
d59cf2f2 523
26ab719a 524is $artist3->name, "Dead On Arrival"
525 => 'Found expected name for first result';
2bf79155 526
50336325 527is $replicated->schema->storage->pool->connected_replicants => 1
13b9e828 528 => "At Least One replicant reconnected to handle the job";
d59cf2f2 529
6f6fb437 530## What happens when we try to select something that doesn't exist?
531
532ok ! $replicated->schema->resultset('Artist')->find(666)
533 => 'Correctly failed to find something.';
071bbccb 534
d59cf2f2 535is $debug{storage_type}, 'REPLICANT'
536 => "got last query from a replicant: $debug{dsn}";
537
cb6ec758 538## test the reliable option
539
540TESTRELIABLE: {
d59cf2f2 541
542 $replicated->schema->storage->set_reliable_storage;
543
544 ok $replicated->schema->resultset('Artist')->find(2)
545 => 'Read from master 1';
546
547 is $debug{storage_type}, 'MASTER',
548 "got last query from a master: $debug{dsn}";
549
550 ok $replicated->schema->resultset('Artist')->find(5)
551 => 'Read from master 2';
552
553 is $debug{storage_type}, 'MASTER',
554 "got last query from a master: $debug{dsn}";
555
556 $replicated->schema->storage->set_balanced_storage;
557
558 ok $replicated->schema->resultset('Artist')->find(3)
9c748388 559 => 'Read from replicant';
071bbccb 560
d59cf2f2 561 is $debug{storage_type}, 'REPLICANT',
562 "got last query from a replicant: $debug{dsn}";
cb6ec758 563}
564
9c748388 565## Make sure when reliable goes out of scope, we are using replicants again
cb6ec758 566
567ok $replicated->schema->resultset('Artist')->find(1)
568 => 'back to replicant 1.';
071bbccb 569
d59cf2f2 570 is $debug{storage_type}, 'REPLICANT',
571 "got last query from a replicant: $debug{dsn}";
572
cb6ec758 573ok $replicated->schema->resultset('Artist')->find(2)
574 => 'back to replicant 2.';
2156bbdd 575
d59cf2f2 576 is $debug{storage_type}, 'REPLICANT',
577 "got last query from a replicant: $debug{dsn}";
071bbccb 578
106d5f3b 579## set all the replicants to inactive, and make sure the balancer falls back to
580## the master.
581
89cf6a70 582$replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
583$replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
9901aad7 584
f404f53c 585{
586 ## catch the fallback to master warning
587 open my $debugfh, '>', \my $fallback_warning;
588 my $oldfh = $replicated->schema->storage->debugfh;
589 $replicated->schema->storage->debugfh($debugfh);
de5dc9ef 590
f404f53c 591 ok $replicated->schema->resultset('Artist')->find(2)
d59cf2f2 592 => 'Fallback to master';
071bbccb 593
d59cf2f2 594 is $debug{storage_type}, 'MASTER',
595 "got last query from a master: $debug{dsn}";
f404f53c 596
597 like $fallback_warning, qr/falling back to master/
9450f312 598 => 'emits falling back to master debug';
f404f53c 599
600 $replicated->schema->storage->debugfh($oldfh);
601}
9901aad7 602
de5dc9ef 603$replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
604$replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
9901aad7 605
606## Silence warning about not supporting the is_replicating method if using the
607## sqlite dbs.
608$replicated->schema->storage->debugobj->silence(1)
7672675a 609 if first { $_ =~ /$var_dir/ } @replicant_names;
d59cf2f2 610
f15afa13 611$replicated->schema->storage->pool->validate_replicants;
de5dc9ef 612
9901aad7 613$replicated->schema->storage->debugobj->silence(0);
614
9450f312 615{
616 ## catch the fallback to master warning
617 open my $debugfh, '>', \my $return_warning;
618 my $oldfh = $replicated->schema->storage->debugfh;
619 $replicated->schema->storage->debugfh($debugfh);
620
621 ok $replicated->schema->resultset('Artist')->find(2)
622 => 'Return to replicants';
623
624 is $debug{storage_type}, 'REPLICANT',
625 "got last query from a replicant: $debug{dsn}";
071bbccb 626
9450f312 627 like $return_warning, qr/Moved back to slave/
628 => 'emits returning to slave debug';
629
630 $replicated->schema->storage->debugfh($oldfh);
631}
d59cf2f2 632
de5dc9ef 633## Getting slave status tests
634
f797e89e 635SKIP: {
636 ## We skip this tests unless you have a custom replicants, since the default
637 ## sqlite based replication tests don't support these functions.
d59cf2f2 638
639 skip 'Cannot Test Replicant Status on Non Replicating Database', 10
f797e89e 640 unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
641
642 $replicated->replicate; ## Give the slaves a chance to catchup.
643
d59cf2f2 644 ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating
645 => 'Replicants are replicating';
646
647 is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0
648 => 'Replicant is zero seconds behind master';
649
650 ## Test the validate replicants
651
652 $replicated->schema->storage->pool->validate_replicants;
653
654 is $replicated->schema->storage->pool->active_replicants, 2
655 => 'Still have 2 replicants after validation';
656
657 ## Force the replicants to fail the validate test by required their lag to
658 ## be negative (ie ahead of the master!)
659
7edf5f1c 660 $replicated->schema->storage->pool->maximum_lag(-10);
661 $replicated->schema->storage->pool->validate_replicants;
d59cf2f2 662
7edf5f1c 663 is $replicated->schema->storage->pool->active_replicants, 0
664 => 'No way a replicant be be ahead of the master';
d59cf2f2 665
7edf5f1c 666 ## Let's be fair to the replicants again. Let them lag up to 5
d59cf2f2 667
7edf5f1c 668 $replicated->schema->storage->pool->maximum_lag(5);
669 $replicated->schema->storage->pool->validate_replicants;
d59cf2f2 670
7edf5f1c 671 is $replicated->schema->storage->pool->active_replicants, 2
d59cf2f2 672 => 'Both replicants in good standing again';
673
674 ## Check auto validate
675
676 is $replicated->schema->storage->balancer->auto_validate_every, 100
677 => "Got the expected value for auto validate";
678
679 ## This will make sure we auto validatge everytime
680 $replicated->schema->storage->balancer->auto_validate_every(0);
681
682 ## set all the replicants to inactive, and make sure the balancer falls back to
683 ## the master.
684
685 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
686 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
687
688 ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
689
690 is $replicated->schema->storage->pool->active_replicants => 0
691 => "both replicants turned off";
692
693 ok $replicated->schema->resultset('Artist')->find(5)
694 => 'replicant reactivated';
695
696 is $debug{storage_type}, 'REPLICANT',
697 "got last query from a replicant: $debug{dsn}";
698
699 is $replicated->schema->storage->pool->active_replicants => 2
700 => "both replicants reactivated";
f797e89e 701}
702
c4d3fae2 703## Test the reliably callback
704
705ok my $reliably = sub {
d59cf2f2 706
c4d3fae2 707 ok $replicated->schema->resultset('Artist')->find(5)
071bbccb 708 => 'replicant reactivated';
709
d59cf2f2 710 is $debug{storage_type}, 'MASTER',
711 "got last query from a master: $debug{dsn}";
712
c4d3fae2 713} => 'created coderef properly';
714
715$replicated->schema->storage->execute_reliably($reliably);
716
717## Try something with an error
718
719ok my $unreliably = sub {
d59cf2f2 720
c4d3fae2 721 ok $replicated->schema->resultset('ArtistXX')->find(5)
d59cf2f2 722 => 'replicant reactivated';
723
c4d3fae2 724} => 'created coderef properly';
725
d59cf2f2 726throws_ok {$replicated->schema->storage->execute_reliably($unreliably)}
ed213e85 727 qr/Can't find source for ArtistXX/
c4d3fae2 728 => 'Bad coderef throws proper error';
d59cf2f2 729
ed213e85 730## Make sure replication came back
731
732ok $replicated->schema->resultset('Artist')->find(3)
733 => 'replicant reactivated';
071bbccb 734
735is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
d59cf2f2 736
c4d3fae2 737## make sure transactions are set to execute_reliably
738
739ok my $transaction = sub {
d59cf2f2 740
741 my $id = shift @_;
742
743 $replicated
744 ->schema
745 ->populate('Artist', [
746 [ qw/artistid name/ ],
84f7e8a1 747 [ $id, "Children of the Grave $id"],
d59cf2f2 748 ]);
749
64cdad22 750 ok my $result = $replicated->schema->resultset('Artist')->find($id)
bd5da369 751 => "Found expected artist for $id";
071bbccb 752
d59cf2f2 753 is $debug{storage_type}, 'MASTER',
754 "got last query from a master: $debug{dsn}";
755
64cdad22 756 ok my $more = $replicated->schema->resultset('Artist')->find(1)
bd5da369 757 => 'Found expected artist again for 1';
071bbccb 758
d59cf2f2 759 is $debug{storage_type}, 'MASTER',
760 "got last query from a master: $debug{dsn}";
761
ed213e85 762 return ($result, $more);
d59cf2f2 763
64cdad22 764} => 'Created a coderef properly';
c4d3fae2 765
ed213e85 766## Test the transaction with multi return
767{
d59cf2f2 768 ok my @return = $replicated->schema->txn_do($transaction, 666)
769 => 'did transaction';
770
771 is $return[0]->id, 666
772 => 'first returned value is correct';
071bbccb 773
d59cf2f2 774 is $debug{storage_type}, 'MASTER',
775 "got last query from a master: $debug{dsn}";
071bbccb 776
d59cf2f2 777 is $return[1]->id, 1
778 => 'second returned value is correct';
779
780 is $debug{storage_type}, 'MASTER',
781 "got last query from a master: $debug{dsn}";
071bbccb 782
ed213e85 783}
784
785## Test that asking for single return works
786{
d59cf2f2 787 ok my @return = $replicated->schema->txn_do($transaction, 777)
788 => 'did transaction';
789
790 is $return[0]->id, 777
791 => 'first returned value is correct';
792
793 is $return[1]->id, 1
794 => 'second returned value is correct';
ed213e85 795}
796
797## Test transaction returning a single value
798
799{
d59cf2f2 800 ok my $result = $replicated->schema->txn_do(sub {
801 ok my $more = $replicated->schema->resultset('Artist')->find(1)
802 => 'found inside a transaction';
803 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
804 return $more;
805 }) => 'successfully processed transaction';
806
807 is $result->id, 1
808 => 'Got expected single result from transaction';
ed213e85 809}
c4d3fae2 810
811## Make sure replication came back
812
ed213e85 813ok $replicated->schema->resultset('Artist')->find(1)
c4d3fae2 814 => 'replicant reactivated';
071bbccb 815
816is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
d59cf2f2 817
ed213e85 818## Test Discard changes
819
820{
d59cf2f2 821 ok my $artist = $replicated->schema->resultset('Artist')->find(2)
822 => 'got an artist to test discard changes';
071bbccb 823
d59cf2f2 824 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
071bbccb 825
d59cf2f2 826 ok $artist->get_from_storage({force_pool=>'master'})
827 => 'properly discard changes';
071bbccb 828
d59cf2f2 829 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
071bbccb 830
3a443bb0 831 ok $artist->discard_changes({force_pool=>'master'})
832 => 'properly called discard_changes against master (manual attrs)';
9c169362 833
3a443bb0 834 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
9c169362 835
3a443bb0 836 ok $artist->discard_changes()
837 => 'properly called discard_changes against master (default attrs)';
9c169362 838
3a443bb0 839 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
8287e9f8 840
3a443bb0 841 ok $artist->discard_changes({force_pool=>$replicant_names[0]})
842 => 'properly able to override the default attributes';
8287e9f8 843
3a443bb0 844 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}"
ed213e85 845}
64cdad22 846
847## Test some edge cases, like trying to do a transaction inside a transaction, etc
848
849{
850 ok my $result = $replicated->schema->txn_do(sub {
d59cf2f2 851 return $replicated->schema->txn_do(sub {
852 ok my $more = $replicated->schema->resultset('Artist')->find(1)
853 => 'found inside a transaction inside a transaction';
854 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
855 return $more;
856 });
64cdad22 857 }) => 'successfully processed transaction';
d59cf2f2 858
64cdad22 859 is $result->id, 1
d59cf2f2 860 => 'Got expected single result from transaction';
64cdad22 861}
862
863{
864 ok my $result = $replicated->schema->txn_do(sub {
d59cf2f2 865 return $replicated->schema->storage->execute_reliably(sub {
866 return $replicated->schema->txn_do(sub {
867 return $replicated->schema->storage->execute_reliably(sub {
868 ok my $more = $replicated->schema->resultset('Artist')->find(1)
869 => 'found inside crazy deep transactions and execute_reliably';
870 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
871 return $more;
872 });
873 });
874 });
64cdad22 875 }) => 'successfully processed transaction';
d59cf2f2 876
64cdad22 877 is $result->id, 1
d59cf2f2 878 => 'Got expected single result from transaction';
879}
2ce6e9a6 880
7e38d850 881## Test the force_pool resultset attribute.
bbafcf26 882
883{
d59cf2f2 884 ok my $artist_rs = $replicated->schema->resultset('Artist')
bbafcf26 885 => 'got artist resultset';
d59cf2f2 886
887 ## Turn on Forced Pool Storage
888 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>'master'})
7e38d850 889 => 'Created a resultset using force_pool storage';
d59cf2f2 890
891 ok my $artist = $reliable_artist_rs->find(2)
7e38d850 892 => 'got an artist result via force_pool storage';
071bbccb 893
d59cf2f2 894 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
bbafcf26 895}
896
bd5da369 897## Test the force_pool resultset attribute part two.
898
899{
d59cf2f2 900 ok my $artist_rs = $replicated->schema->resultset('Artist')
bd5da369 901 => 'got artist resultset';
d59cf2f2 902
903 ## Turn on Forced Pool Storage
904 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>$replicant_names[0]})
bd5da369 905 => 'Created a resultset using force_pool storage';
d59cf2f2 906
907 ok my $artist = $reliable_artist_rs->find(2)
bd5da369 908 => 'got an artist result via force_pool storage';
071bbccb 909
d59cf2f2 910 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
bd5da369 911}
9e75be92 912
0f83441a 913## Delete the old database files
50336325 914$replicated->cleanup;
ee356d00 915
6988233d 916done_testing;
917
ee356d00 918# vim: sw=4 sts=4 :