add test to display unhandled methods
[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{
271 my $storage_dbi_meta = Class::MOP::Class->initialize('DBIx::Class::Storage::DBI');
272 my $replicated_meta = DBIx::Class::Storage::DBI::Replicated->meta;
273
274 my @storage_dbi_methods = sort $storage_dbi_meta->get_all_method_names;
275 my @replicated_methods = sort $replicated_meta->get_all_method_names;
276
277# remove constants
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
284 my @cag_methods = Class::MOP::Class->initialize('DBIx::Class')
285 ->get_all_method_names;
286 my %count;
287 $count{$_}++ for (@storage_dbi_methods, @cag_methods);
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 }
302
303# check that what's left is implemented
304 %count = ();
305 $count{$_}++ for (@storage_dbi_methods, @replicated_methods);
306
307 if ((grep $count{$_} == 2, @storage_dbi_methods) == @storage_dbi_methods) {
308 pass 'all DBIx::Class::Storage::DBI methods implemented';
309 }
310 else {
311 my @unimplemented = grep $count{$_} == 1, @storage_dbi_methods;
312
313 fail 'the following DBIx::Class::Storage::DBI methods are unimplemented: '
314 . "@unimplemented";
315 }
316}
317
26ab719a 318ok $replicated->schema->storage->meta
319 => 'has a meta object';
d59cf2f2 320
26ab719a 321isa_ok $replicated->schema->storage->master
322 => 'DBIx::Class::Storage::DBI';
d59cf2f2 323
26ab719a 324isa_ok $replicated->schema->storage->pool
325 => 'DBIx::Class::Storage::DBI::Replicated::Pool';
d59cf2f2 326
17b05c13 327does_ok $replicated->schema->storage->balancer
d59cf2f2 328 => 'DBIx::Class::Storage::DBI::Replicated::Balancer';
26ab719a 329
330ok my @replicant_connects = $replicated->generate_replicant_connect_info
331 => 'got replication connect information';
332
955a6df6 333ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
26ab719a 334 => 'Created some storages suitable for replicants';
6412a592 335
071bbccb 336our %debug;
337$replicated->schema->storage->debug(1);
338$replicated->schema->storage->debugcb(sub {
d59cf2f2 339 my ($op, $info) = @_;
340 ##warn "\n$op, $info\n";
341 %debug = (
342 op => $op,
343 info => $info,
344 dsn => ($info=~m/\[(.+)\]/)[0],
345 storage_type => $info=~m/REPLICANT/ ? 'REPLICANT' : 'MASTER',
346 );
071bbccb 347});
348
6412a592 349ok my @all_storages = $replicated->schema->storage->all_storages
350 => '->all_storages';
351
dcdf7b2c 352is scalar @all_storages,
353 3
6412a592 354 => 'correct number of ->all_storages';
355
dcdf7b2c 356is ((grep $_->isa('DBIx::Class::Storage::DBI'), @all_storages),
357 3
6412a592 358 => '->all_storages are correct type');
b2e4d522 359
dcdf7b2c 360my @all_storage_opts =
361 grep { (reftype($_)||'') eq 'HASH' }
362 map @{ $_->_connect_info }, @all_storages;
363
364is ((grep $_->{master_option}, @all_storage_opts),
365 3
b2e4d522 366 => 'connect_info was merged from master to replicants');
d59cf2f2 367
9901aad7 368my @replicant_names = keys %{ $replicated->schema->storage->replicants };
369
bd5da369 370ok @replicant_names, "found replicant names @replicant_names";
371
9901aad7 372## Silence warning about not supporting the is_replicating method if using the
373## sqlite dbs.
374$replicated->schema->storage->debugobj->silence(1)
375 if first { m{^t/} } @replicant_names;
d59cf2f2 376
cb6ec758 377isa_ok $replicated->schema->storage->balancer->current_replicant
d59cf2f2 378 => 'DBIx::Class::Storage::DBI';
9901aad7 379
380$replicated->schema->storage->debugobj->silence(0);
381
26ab719a 382ok $replicated->schema->storage->pool->has_replicants
d59cf2f2 383 => 'does have replicants';
26ab719a 384
17b05c13 385is $replicated->schema->storage->pool->num_replicants => 2
26ab719a 386 => 'has two replicants';
d59cf2f2 387
de5dc9ef 388does_ok $replicated_storages[0]
26ab719a 389 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
390
de5dc9ef 391does_ok $replicated_storages[1]
26ab719a 392 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
d59cf2f2 393
de5dc9ef 394does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
26ab719a 395 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
396
de5dc9ef 397does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
d59cf2f2 398 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
26ab719a 399
400## Add some info to the database
401
402$replicated
403 ->schema
404 ->populate('Artist', [
405 [ qw/artistid name/ ],
406 [ 4, "Ozric Tentacles"],
407 ]);
071bbccb 408
d59cf2f2 409 is $debug{storage_type}, 'MASTER',
410 "got last query from a master: $debug{dsn}";
411
412 like $debug{info}, qr/INSERT/, 'Last was an insert';
413
26ab719a 414## Make sure all the slaves have the table definitions
415
416$replicated->replicate;
5c1d82d2 417$replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
418$replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
9901aad7 419
420## Silence warning about not supporting the is_replicating method if using the
421## sqlite dbs.
422$replicated->schema->storage->debugobj->silence(1)
423 if first { m{^t/} } @replicant_names;
d59cf2f2 424
f15afa13 425$replicated->schema->storage->pool->validate_replicants;
26ab719a 426
9901aad7 427$replicated->schema->storage->debugobj->silence(0);
428
26ab719a 429## Make sure we can read the data.
430
431ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
432 => 'Created Result';
433
071bbccb 434## We removed testing here since master read weight is on, so we can't tell in
435## advance what storage to expect. We turn master read weight off a bit lower
d59cf2f2 436## is $debug{storage_type}, 'REPLICANT'
437## => "got last query from a replicant: $debug{dsn}, $debug{info}";
071bbccb 438
26ab719a 439isa_ok $artist1
440 => 'DBICTest::Artist';
d59cf2f2 441
26ab719a 442is $artist1->name, 'Ozric Tentacles'
443 => 'Found expected name for first result';
444
ee356d00 445## Check that master_read_weight is honored
446{
87974600 447 no warnings qw/once redefine/;
ee356d00 448
449 local
f404f53c 450 *DBIx::Class::Storage::DBI::Replicated::Balancer::Random::_random_number =
d59cf2f2 451 sub { 999 };
ee356d00 452
453 $replicated->schema->storage->balancer->increment_storage;
454
455 is $replicated->schema->storage->balancer->current_replicant,
456 $replicated->schema->storage->master
457 => 'master_read_weight is honored';
458
459 ## turn it off for the duration of the test
460 $replicated->schema->storage->balancer->master_read_weight(0);
461 $replicated->schema->storage->balancer->increment_storage;
462}
463
26ab719a 464## Add some new rows that only the master will have This is because
465## we overload any type of write operation so that is must hit the master
466## database.
467
468$replicated
469 ->schema
470 ->populate('Artist', [
471 [ qw/artistid name/ ],
472 [ 5, "Doom's Children"],
473 [ 6, "Dead On Arrival"],
474 [ 7, "Watergate"],
475 ]);
476
d59cf2f2 477 is $debug{storage_type}, 'MASTER',
478 "got last query from a master: $debug{dsn}";
479
480 like $debug{info}, qr/INSERT/, 'Last was an insert';
071bbccb 481
26ab719a 482## Make sure all the slaves have the table definitions
483$replicated->replicate;
484
485## Should find some data now
486
487ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
488 => 'Sync succeed';
071bbccb 489
d59cf2f2 490is $debug{storage_type}, 'REPLICANT'
491 => "got last query from a replicant: $debug{dsn}";
492
26ab719a 493isa_ok $artist2
494 => 'DBICTest::Artist';
d59cf2f2 495
26ab719a 496is $artist2->name, "Doom's Children"
497 => 'Found expected name for first result';
498
499## What happens when we disconnect all the replicants?
500
50336325 501is $replicated->schema->storage->pool->connected_replicants => 2
502 => "both replicants are connected";
d59cf2f2 503
89cf6a70 504$replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
505$replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
26ab719a 506
50336325 507is $replicated->schema->storage->pool->connected_replicants => 0
508 => "both replicants are now disconnected";
509
510## All these should pass, since the database should automatically reconnect
511
26ab719a 512ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
513 => 'Still finding stuff.';
071bbccb 514
d59cf2f2 515is $debug{storage_type}, 'REPLICANT'
516 => "got last query from a replicant: $debug{dsn}";
517
26ab719a 518isa_ok $artist3
519 => 'DBICTest::Artist';
d59cf2f2 520
26ab719a 521is $artist3->name, "Dead On Arrival"
522 => 'Found expected name for first result';
2bf79155 523
50336325 524is $replicated->schema->storage->pool->connected_replicants => 1
13b9e828 525 => "At Least One replicant reconnected to handle the job";
d59cf2f2 526
6f6fb437 527## What happens when we try to select something that doesn't exist?
528
529ok ! $replicated->schema->resultset('Artist')->find(666)
530 => 'Correctly failed to find something.';
071bbccb 531
d59cf2f2 532is $debug{storage_type}, 'REPLICANT'
533 => "got last query from a replicant: $debug{dsn}";
534
cb6ec758 535## test the reliable option
536
537TESTRELIABLE: {
d59cf2f2 538
539 $replicated->schema->storage->set_reliable_storage;
540
541 ok $replicated->schema->resultset('Artist')->find(2)
542 => 'Read from master 1';
543
544 is $debug{storage_type}, 'MASTER',
545 "got last query from a master: $debug{dsn}";
546
547 ok $replicated->schema->resultset('Artist')->find(5)
548 => 'Read from master 2';
549
550 is $debug{storage_type}, 'MASTER',
551 "got last query from a master: $debug{dsn}";
552
553 $replicated->schema->storage->set_balanced_storage;
554
555 ok $replicated->schema->resultset('Artist')->find(3)
9c748388 556 => 'Read from replicant';
071bbccb 557
d59cf2f2 558 is $debug{storage_type}, 'REPLICANT',
559 "got last query from a replicant: $debug{dsn}";
cb6ec758 560}
561
9c748388 562## Make sure when reliable goes out of scope, we are using replicants again
cb6ec758 563
564ok $replicated->schema->resultset('Artist')->find(1)
565 => 'back to replicant 1.';
071bbccb 566
d59cf2f2 567 is $debug{storage_type}, 'REPLICANT',
568 "got last query from a replicant: $debug{dsn}";
569
cb6ec758 570ok $replicated->schema->resultset('Artist')->find(2)
571 => 'back to replicant 2.';
2156bbdd 572
d59cf2f2 573 is $debug{storage_type}, 'REPLICANT',
574 "got last query from a replicant: $debug{dsn}";
071bbccb 575
106d5f3b 576## set all the replicants to inactive, and make sure the balancer falls back to
577## the master.
578
89cf6a70 579$replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
580$replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
9901aad7 581
f404f53c 582{
583 ## catch the fallback to master warning
584 open my $debugfh, '>', \my $fallback_warning;
585 my $oldfh = $replicated->schema->storage->debugfh;
586 $replicated->schema->storage->debugfh($debugfh);
de5dc9ef 587
f404f53c 588 ok $replicated->schema->resultset('Artist')->find(2)
d59cf2f2 589 => 'Fallback to master';
071bbccb 590
d59cf2f2 591 is $debug{storage_type}, 'MASTER',
592 "got last query from a master: $debug{dsn}";
f404f53c 593
594 like $fallback_warning, qr/falling back to master/
d59cf2f2 595 => 'emits falling back to master warning';
f404f53c 596
597 $replicated->schema->storage->debugfh($oldfh);
598}
9901aad7 599
de5dc9ef 600$replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
601$replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
9901aad7 602
603## Silence warning about not supporting the is_replicating method if using the
604## sqlite dbs.
605$replicated->schema->storage->debugobj->silence(1)
606 if first { m{^t/} } @replicant_names;
d59cf2f2 607
f15afa13 608$replicated->schema->storage->pool->validate_replicants;
de5dc9ef 609
9901aad7 610$replicated->schema->storage->debugobj->silence(0);
611
de5dc9ef 612ok $replicated->schema->resultset('Artist')->find(2)
613 => 'Returned to replicates';
071bbccb 614
d59cf2f2 615is $debug{storage_type}, 'REPLICANT',
616 "got last query from a replicant: $debug{dsn}";
617
de5dc9ef 618## Getting slave status tests
619
f797e89e 620SKIP: {
621 ## We skip this tests unless you have a custom replicants, since the default
622 ## sqlite based replication tests don't support these functions.
d59cf2f2 623
624 skip 'Cannot Test Replicant Status on Non Replicating Database', 10
f797e89e 625 unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
626
627 $replicated->replicate; ## Give the slaves a chance to catchup.
628
d59cf2f2 629 ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating
630 => 'Replicants are replicating';
631
632 is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0
633 => 'Replicant is zero seconds behind master';
634
635 ## Test the validate replicants
636
637 $replicated->schema->storage->pool->validate_replicants;
638
639 is $replicated->schema->storage->pool->active_replicants, 2
640 => 'Still have 2 replicants after validation';
641
642 ## Force the replicants to fail the validate test by required their lag to
643 ## be negative (ie ahead of the master!)
644
7edf5f1c 645 $replicated->schema->storage->pool->maximum_lag(-10);
646 $replicated->schema->storage->pool->validate_replicants;
d59cf2f2 647
7edf5f1c 648 is $replicated->schema->storage->pool->active_replicants, 0
649 => 'No way a replicant be be ahead of the master';
d59cf2f2 650
7edf5f1c 651 ## Let's be fair to the replicants again. Let them lag up to 5
d59cf2f2 652
7edf5f1c 653 $replicated->schema->storage->pool->maximum_lag(5);
654 $replicated->schema->storage->pool->validate_replicants;
d59cf2f2 655
7edf5f1c 656 is $replicated->schema->storage->pool->active_replicants, 2
d59cf2f2 657 => 'Both replicants in good standing again';
658
659 ## Check auto validate
660
661 is $replicated->schema->storage->balancer->auto_validate_every, 100
662 => "Got the expected value for auto validate";
663
664 ## This will make sure we auto validatge everytime
665 $replicated->schema->storage->balancer->auto_validate_every(0);
666
667 ## set all the replicants to inactive, and make sure the balancer falls back to
668 ## the master.
669
670 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
671 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
672
673 ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
674
675 is $replicated->schema->storage->pool->active_replicants => 0
676 => "both replicants turned off";
677
678 ok $replicated->schema->resultset('Artist')->find(5)
679 => 'replicant reactivated';
680
681 is $debug{storage_type}, 'REPLICANT',
682 "got last query from a replicant: $debug{dsn}";
683
684 is $replicated->schema->storage->pool->active_replicants => 2
685 => "both replicants reactivated";
f797e89e 686}
687
c4d3fae2 688## Test the reliably callback
689
690ok my $reliably = sub {
d59cf2f2 691
c4d3fae2 692 ok $replicated->schema->resultset('Artist')->find(5)
071bbccb 693 => 'replicant reactivated';
694
d59cf2f2 695 is $debug{storage_type}, 'MASTER',
696 "got last query from a master: $debug{dsn}";
697
c4d3fae2 698} => 'created coderef properly';
699
700$replicated->schema->storage->execute_reliably($reliably);
701
702## Try something with an error
703
704ok my $unreliably = sub {
d59cf2f2 705
c4d3fae2 706 ok $replicated->schema->resultset('ArtistXX')->find(5)
d59cf2f2 707 => 'replicant reactivated';
708
c4d3fae2 709} => 'created coderef properly';
710
d59cf2f2 711throws_ok {$replicated->schema->storage->execute_reliably($unreliably)}
ed213e85 712 qr/Can't find source for ArtistXX/
c4d3fae2 713 => 'Bad coderef throws proper error';
d59cf2f2 714
ed213e85 715## Make sure replication came back
716
717ok $replicated->schema->resultset('Artist')->find(3)
718 => 'replicant reactivated';
071bbccb 719
720is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
d59cf2f2 721
c4d3fae2 722## make sure transactions are set to execute_reliably
723
724ok my $transaction = sub {
d59cf2f2 725
726 my $id = shift @_;
727
728 $replicated
729 ->schema
730 ->populate('Artist', [
731 [ qw/artistid name/ ],
732 [ $id, "Children of the Grave"],
733 ]);
734
64cdad22 735 ok my $result = $replicated->schema->resultset('Artist')->find($id)
bd5da369 736 => "Found expected artist for $id";
071bbccb 737
d59cf2f2 738 is $debug{storage_type}, 'MASTER',
739 "got last query from a master: $debug{dsn}";
740
64cdad22 741 ok my $more = $replicated->schema->resultset('Artist')->find(1)
bd5da369 742 => 'Found expected artist again for 1';
071bbccb 743
d59cf2f2 744 is $debug{storage_type}, 'MASTER',
745 "got last query from a master: $debug{dsn}";
746
ed213e85 747 return ($result, $more);
d59cf2f2 748
64cdad22 749} => 'Created a coderef properly';
c4d3fae2 750
ed213e85 751## Test the transaction with multi return
752{
d59cf2f2 753 ok my @return = $replicated->schema->txn_do($transaction, 666)
754 => 'did transaction';
755
756 is $return[0]->id, 666
757 => 'first returned value is correct';
071bbccb 758
d59cf2f2 759 is $debug{storage_type}, 'MASTER',
760 "got last query from a master: $debug{dsn}";
071bbccb 761
d59cf2f2 762 is $return[1]->id, 1
763 => 'second returned value is correct';
764
765 is $debug{storage_type}, 'MASTER',
766 "got last query from a master: $debug{dsn}";
071bbccb 767
ed213e85 768}
769
770## Test that asking for single return works
771{
d59cf2f2 772 ok my @return = $replicated->schema->txn_do($transaction, 777)
773 => 'did transaction';
774
775 is $return[0]->id, 777
776 => 'first returned value is correct';
777
778 is $return[1]->id, 1
779 => 'second returned value is correct';
ed213e85 780}
781
782## Test transaction returning a single value
783
784{
d59cf2f2 785 ok my $result = $replicated->schema->txn_do(sub {
786 ok my $more = $replicated->schema->resultset('Artist')->find(1)
787 => 'found inside a transaction';
788 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
789 return $more;
790 }) => 'successfully processed transaction';
791
792 is $result->id, 1
793 => 'Got expected single result from transaction';
ed213e85 794}
c4d3fae2 795
796## Make sure replication came back
797
ed213e85 798ok $replicated->schema->resultset('Artist')->find(1)
c4d3fae2 799 => 'replicant reactivated';
071bbccb 800
801is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
d59cf2f2 802
ed213e85 803## Test Discard changes
804
805{
d59cf2f2 806 ok my $artist = $replicated->schema->resultset('Artist')->find(2)
807 => 'got an artist to test discard changes';
071bbccb 808
d59cf2f2 809 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
071bbccb 810
d59cf2f2 811 ok $artist->get_from_storage({force_pool=>'master'})
812 => 'properly discard changes';
071bbccb 813
d59cf2f2 814 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
071bbccb 815
3a443bb0 816 ok $artist->discard_changes({force_pool=>'master'})
817 => 'properly called discard_changes against master (manual attrs)';
9c169362 818
3a443bb0 819 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
9c169362 820
3a443bb0 821 ok $artist->discard_changes()
822 => 'properly called discard_changes against master (default attrs)';
9c169362 823
3a443bb0 824 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
8287e9f8 825
3a443bb0 826 ok $artist->discard_changes({force_pool=>$replicant_names[0]})
827 => 'properly able to override the default attributes';
8287e9f8 828
3a443bb0 829 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}"
ed213e85 830}
64cdad22 831
832## Test some edge cases, like trying to do a transaction inside a transaction, etc
833
834{
835 ok my $result = $replicated->schema->txn_do(sub {
d59cf2f2 836 return $replicated->schema->txn_do(sub {
837 ok my $more = $replicated->schema->resultset('Artist')->find(1)
838 => 'found inside a transaction inside a transaction';
839 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
840 return $more;
841 });
64cdad22 842 }) => 'successfully processed transaction';
d59cf2f2 843
64cdad22 844 is $result->id, 1
d59cf2f2 845 => 'Got expected single result from transaction';
64cdad22 846}
847
848{
849 ok my $result = $replicated->schema->txn_do(sub {
d59cf2f2 850 return $replicated->schema->storage->execute_reliably(sub {
851 return $replicated->schema->txn_do(sub {
852 return $replicated->schema->storage->execute_reliably(sub {
853 ok my $more = $replicated->schema->resultset('Artist')->find(1)
854 => 'found inside crazy deep transactions and execute_reliably';
855 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
856 return $more;
857 });
858 });
859 });
64cdad22 860 }) => 'successfully processed transaction';
d59cf2f2 861
64cdad22 862 is $result->id, 1
d59cf2f2 863 => 'Got expected single result from transaction';
864}
2ce6e9a6 865
7e38d850 866## Test the force_pool resultset attribute.
bbafcf26 867
868{
d59cf2f2 869 ok my $artist_rs = $replicated->schema->resultset('Artist')
bbafcf26 870 => 'got artist resultset';
d59cf2f2 871
872 ## Turn on Forced Pool Storage
873 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>'master'})
7e38d850 874 => 'Created a resultset using force_pool storage';
d59cf2f2 875
876 ok my $artist = $reliable_artist_rs->find(2)
7e38d850 877 => 'got an artist result via force_pool storage';
071bbccb 878
d59cf2f2 879 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
bbafcf26 880}
881
bd5da369 882## Test the force_pool resultset attribute part two.
883
884{
d59cf2f2 885 ok my $artist_rs = $replicated->schema->resultset('Artist')
bd5da369 886 => 'got artist resultset';
d59cf2f2 887
888 ## Turn on Forced Pool Storage
889 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>$replicant_names[0]})
bd5da369 890 => 'Created a resultset using force_pool storage';
d59cf2f2 891
892 ok my $artist = $reliable_artist_rs->find(2)
bd5da369 893 => 'got an artist result via force_pool storage';
071bbccb 894
d59cf2f2 895 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
bd5da369 896}
0f83441a 897## Delete the old database files
50336325 898$replicated->cleanup;
ee356d00 899
6988233d 900done_testing;
901
ee356d00 902# vim: sw=4 sts=4 :