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