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