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