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