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