Port ::Replicated from Moose to Moo+Type::Tiny
[dbsrgits/DBIx-Class.git] / t / storage / replicated.t
CommitLineData
cb551b07 1use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_replicated';
2
e4dc89b3 3use strict;
4use warnings;
68de9438 5
e4dc89b3 6use Test::More;
cb551b07 7use DBIx::Class::_Util 'modver_gt_or_eq_and_lt';
4bea1fe7 8use lib qw(t/lib);
9use DBICTest;
1e1cc55e 10use Class::Inspector;
c4d3fae2 11use Test::Exception;
9901aad7 12use List::Util 'first';
b2e4d522 13use Scalar::Util 'reftype';
3da4f736 14use File::Spec;
1e1cc55e 15use Moo::Role ();
8f7986d6 16
7672675a 17my $var_dir = quotemeta ( File::Spec->catdir(qw/t var/) );
18
6287275c 19## Add a connect_info option to test option merging.
617474ab 20use DBIx::Class::Storage::DBI::Replicated;
6287275c 21{
22 package DBIx::Class::Storage::DBI::Replicated;
23
1e1cc55e 24 use Moo;
6287275c 25
26 around connect_info => sub {
27 my ($next, $self, $info) = @_;
28 $info->[3]{master_option} = 1;
29 $self->$next($info);
30 };
31
6287275c 32}
33
1e1cc55e 34sub does_ok($$;$) {
35 my ($thing, $role, $desc) = @_;
36 $desc ||= 'The object';
37 local $Test::Builder::Level = $Test::Builder::Level + 1;
38 ok Moo::Role::does_role($thing, $role), "$desc does $role";
39}
6a151f58 40
89cf6a70 41=head1 HOW TO USE
42
43 This is a test of the replicated storage system. This will work in one of
44 two ways, either it was try to fake replication with a couple of SQLite DBs
45 and creative use of copy, or if you define a couple of %ENV vars correctly
46 will try to test those. If you do that, it will assume the setup is properly
47 replicating. Your results may vary, but I have demonstrated this to work with
48 mysql native replication.
d59cf2f2 49
89cf6a70 50=cut
51
52
0f83441a 53## ----------------------------------------------------------------------------
54## Build a class to hold all our required testing data and methods.
55## ----------------------------------------------------------------------------
56
857d66ca 57TESTSCHEMACLASSES: {
2bf79155 58
857d66ca 59 ## --------------------------------------------------------------------- ##
60 ## Create an object to contain your replicated stuff.
61 ## --------------------------------------------------------------------- ##
d59cf2f2 62
2bf79155 63 package DBIx::Class::DBI::Replicated::TestReplication;
d59cf2f2 64
2bf79155 65 use DBICTest;
66 use base qw/Class::Accessor::Fast/;
d59cf2f2 67
857d66ca 68 __PACKAGE__->mk_accessors( qw/schema/ );
2bf79155 69
70 ## Initialize the object
d59cf2f2 71
72 sub new {
73 my ($class, $schema_method) = (shift, shift);
74 my $self = $class->SUPER::new(@_);
75
76 $self->schema( $self->init_schema($schema_method) );
77 return $self;
78 }
79
26ab719a 80 ## Get the Schema and set the replication storage type
d59cf2f2 81
2bf79155 82 sub init_schema {
7536c92b 83 #my ($class, $schema_getter) = @_;
84 shift->${\ ( 'get_schema_' . shift ) };
2bf79155 85 }
dcdf7b2c 86
87 sub get_schema_by_storage_type {
88 DBICTest->init_schema(
89 sqlite_use_file => 1,
90 storage_type=>{
91 '::DBI::Replicated' => {
92 balancer_type=>'::Random',
93 balancer_args=>{
94 auto_validate_every=>100,
64ae1667 95 master_read_weight => 1
dcdf7b2c 96 },
97 }
98 },
99 deploy_args=>{
100 add_drop_table => 1,
101 },
102 );
103 }
104
105 sub get_schema_by_connect_info {
106 DBICTest->init_schema(
107 sqlite_use_file => 1,
108 storage_type=> '::DBI::Replicated',
109 balancer_type=>'::Random',
110 balancer_args=> {
64ae1667 111 auto_validate_every=>100,
112 master_read_weight => 1
113 },
114 pool_args=>{
115 maximum_lag=>1,
dcdf7b2c 116 },
117 deploy_args=>{
118 add_drop_table => 1,
119 },
120 );
121 }
122
857d66ca 123 sub generate_replicant_connect_info {}
124 sub replicate {}
125 sub cleanup {}
126
b2e4d522 127 ## --------------------------------------------------------------------- ##
857d66ca 128 ## Subclass for when you are using SQLite for testing, this provides a fake
129 ## replication support.
130 ## --------------------------------------------------------------------- ##
d59cf2f2 131
857d66ca 132 package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
133
134 use DBICTest;
d59cf2f2 135 use File::Copy;
857d66ca 136 use base 'DBIx::Class::DBI::Replicated::TestReplication';
d59cf2f2 137
3da4f736 138 __PACKAGE__->mk_accessors(qw/master_path slave_paths/);
d59cf2f2 139
3da4f736 140 ## Set the master path from DBICTest
d59cf2f2 141
142 sub new {
143 my $class = shift @_;
144 my $self = $class->SUPER::new(@_);
145
146 $self->master_path( DBICTest->_sqlite_dbfilename );
147 $self->slave_paths([
148 File::Spec->catfile(qw/t var DBIxClass_slave1.db/),
149 File::Spec->catfile(qw/t var DBIxClass_slave2.db/),
150 ]);
151
152 return $self;
153 }
154
26ab719a 155 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
156 ## $storage->connect_info to be used for connecting replicants.
d59cf2f2 157
26ab719a 158 sub generate_replicant_connect_info {
857d66ca 159 my $self = shift @_;
26ab719a 160 my @dsn = map {
161 "dbi:SQLite:${_}";
162 } @{$self->slave_paths};
d59cf2f2 163
9901aad7 164 my @connect_infos = map { [$_,'','',{AutoCommit=>1}] } @dsn;
165
d59cf2f2 166 ## Make sure nothing is left over from a failed test
167 $self->cleanup;
3da4f736 168
d59cf2f2 169 ## try a hashref too
9901aad7 170 my $c = $connect_infos[0];
171 $connect_infos[0] = {
172 dsn => $c->[0],
173 user => $c->[1],
174 password => $c->[2],
175 %{ $c->[3] }
176 };
177
178 @connect_infos
26ab719a 179 }
9901aad7 180
26ab719a 181 ## Do a 'good enough' replication by copying the master dbfile over each of
50336325 182 ## the slave dbfiles. If the master is SQLite we do this, otherwise we
183 ## just do a one second pause to let the slaves catch up.
d59cf2f2 184
26ab719a 185 sub replicate {
186 my $self = shift @_;
187 foreach my $slave (@{$self->slave_paths}) {
188 copy($self->master_path, $slave);
189 }
190 }
d59cf2f2 191
9e75be92 192 ## Cleanup after ourselves. Unlink all the slave paths.
d59cf2f2 193
26ab719a 194 sub cleanup {
195 my $self = shift @_;
e1ff35c4 196 $_->disconnect for values %{ $self->schema->storage->replicants };
26ab719a 197 foreach my $slave (@{$self->slave_paths}) {
d59cf2f2 198 if(-e $slave) {
199 unlink $slave;
200 }
201 }
26ab719a 202 }
d59cf2f2 203
857d66ca 204 ## --------------------------------------------------------------------- ##
205 ## Subclass for when you are setting the databases via custom export vars
206 ## This is for when you have a replicating database setup that you are
207 ## going to test against. You'll need to define the correct $ENV and have
208 ## two slave databases to test against, as well as a replication system
209 ## that will replicate in less than 1 second.
210 ## --------------------------------------------------------------------- ##
d59cf2f2 211
212 package DBIx::Class::DBI::Replicated::TestReplication::Custom;
857d66ca 213 use base 'DBIx::Class::DBI::Replicated::TestReplication';
d59cf2f2 214
857d66ca 215 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
216 ## $storage->connect_info to be used for connecting replicants.
d59cf2f2 217
218 sub generate_replicant_connect_info {
857d66ca 219 return (
220 [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
d59cf2f2 221 [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],
857d66ca 222 );
223 }
d59cf2f2 224
225 ## pause a bit to let the replication catch up
226
857d66ca 227 sub replicate {
d59cf2f2 228 sleep 1;
229 }
2bf79155 230}
231
232## ----------------------------------------------------------------------------
233## Create an object and run some tests
234## ----------------------------------------------------------------------------
235
26ab719a 236## Thi first bunch of tests are basic, just make sure all the bits are behaving
2bf79155 237
857d66ca 238my $replicated_class = DBICTest->has_custom_dsn ?
239 'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
240 'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
241
dcdf7b2c 242my $replicated;
243
244for my $method (qw/by_connect_info by_storage_type/) {
a3df35f8 245 undef $replicated;
dcdf7b2c 246 ok $replicated = $replicated_class->new($method)
247 => "Created a replication object $method";
d59cf2f2 248
dcdf7b2c 249 isa_ok $replicated->schema
250 => 'DBIx::Class::Schema';
d59cf2f2 251
dcdf7b2c 252 isa_ok $replicated->schema->storage
253 => 'DBIx::Class::Storage::DBI::Replicated';
254
255 isa_ok $replicated->schema->storage->balancer
256 => 'DBIx::Class::Storage::DBI::Replicated::Balancer::Random'
257 => 'configured balancer_type';
258}
26ab719a 259
f29315ed 260### check that all Storage::DBI methods are handled by ::Replicated
261{
1e1cc55e 262 my @storage_dbi_methods = @{Class::Inspector
263 ->methods('DBIx::Class::Storage::DBI')};
f29315ed 264
1e1cc55e 265 my @replicated_methods = @{Class::Inspector
266 ->methods('DBIx::Class::Storage::DBI::Replicated')};
f29315ed 267
b75235fe 268# remove constants and OTHER_CRAP
f29315ed 269 @storage_dbi_methods = grep !/^[A-Z_]+\z/, @storage_dbi_methods;
270
271# remove CAG accessors
272 @storage_dbi_methods = grep !/_accessor\z/, @storage_dbi_methods;
273
274# remove DBIx::Class (the root parent, with CAG and stuff) methods
1e1cc55e 275 my @root_methods = @{Class::Inspector->methods('DBIx::Class')};
f29315ed 276 my %count;
b75235fe 277 $count{$_}++ for (@storage_dbi_methods, @root_methods);
f29315ed 278
279 @storage_dbi_methods = grep $count{$_} != 2, @storage_dbi_methods;
280
281# make hashes
282 my %storage_dbi_methods;
283 @storage_dbi_methods{@storage_dbi_methods} = ();
284 my %replicated_methods;
285 @replicated_methods{@replicated_methods} = ();
286
287# remove ::Replicated-specific methods
288 for my $method (@replicated_methods) {
289 delete $replicated_methods{$method}
290 unless exists $storage_dbi_methods{$method};
291 }
e4c5abce 292 @replicated_methods = keys %replicated_methods;
f29315ed 293
294# check that what's left is implemented
295 %count = ();
296 $count{$_}++ for (@storage_dbi_methods, @replicated_methods);
297
298 if ((grep $count{$_} == 2, @storage_dbi_methods) == @storage_dbi_methods) {
299 pass 'all DBIx::Class::Storage::DBI methods implemented';
300 }
301 else {
302 my @unimplemented = grep $count{$_} == 1, @storage_dbi_methods;
303
304 fail 'the following DBIx::Class::Storage::DBI methods are unimplemented: '
305 . "@unimplemented";
306 }
307}
308
26ab719a 309isa_ok $replicated->schema->storage->master
310 => 'DBIx::Class::Storage::DBI';
d59cf2f2 311
26ab719a 312isa_ok $replicated->schema->storage->pool
313 => 'DBIx::Class::Storage::DBI::Replicated::Pool';
d59cf2f2 314
17b05c13 315does_ok $replicated->schema->storage->balancer
d59cf2f2 316 => 'DBIx::Class::Storage::DBI::Replicated::Balancer';
26ab719a 317
318ok my @replicant_connects = $replicated->generate_replicant_connect_info
319 => 'got replication connect information';
320
955a6df6 321ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
26ab719a 322 => 'Created some storages suitable for replicants';
6412a592 323
071bbccb 324our %debug;
325$replicated->schema->storage->debug(1);
326$replicated->schema->storage->debugcb(sub {
d59cf2f2 327 my ($op, $info) = @_;
328 ##warn "\n$op, $info\n";
329 %debug = (
330 op => $op,
331 info => $info,
332 dsn => ($info=~m/\[(.+)\]/)[0],
333 storage_type => $info=~m/REPLICANT/ ? 'REPLICANT' : 'MASTER',
334 );
071bbccb 335});
336
6412a592 337ok my @all_storages = $replicated->schema->storage->all_storages
338 => '->all_storages';
339
dcdf7b2c 340is scalar @all_storages,
341 3
6412a592 342 => 'correct number of ->all_storages';
343
dcdf7b2c 344is ((grep $_->isa('DBIx::Class::Storage::DBI'), @all_storages),
345 3
6412a592 346 => '->all_storages are correct type');
b2e4d522 347
dcdf7b2c 348my @all_storage_opts =
349 grep { (reftype($_)||'') eq 'HASH' }
350 map @{ $_->_connect_info }, @all_storages;
351
352is ((grep $_->{master_option}, @all_storage_opts),
353 3
b2e4d522 354 => 'connect_info was merged from master to replicants');
d59cf2f2 355
9901aad7 356my @replicant_names = keys %{ $replicated->schema->storage->replicants };
357
bd5da369 358ok @replicant_names, "found replicant names @replicant_names";
359
9901aad7 360## Silence warning about not supporting the is_replicating method if using the
361## sqlite dbs.
362$replicated->schema->storage->debugobj->silence(1)
7672675a 363 if first { $_ =~ /$var_dir/ } @replicant_names;
d59cf2f2 364
cb6ec758 365isa_ok $replicated->schema->storage->balancer->current_replicant
d59cf2f2 366 => 'DBIx::Class::Storage::DBI';
9901aad7 367
368$replicated->schema->storage->debugobj->silence(0);
369
26ab719a 370ok $replicated->schema->storage->pool->has_replicants
d59cf2f2 371 => 'does have replicants';
26ab719a 372
17b05c13 373is $replicated->schema->storage->pool->num_replicants => 2
26ab719a 374 => 'has two replicants';
d59cf2f2 375
de5dc9ef 376does_ok $replicated_storages[0]
26ab719a 377 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
378
de5dc9ef 379does_ok $replicated_storages[1]
26ab719a 380 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
d59cf2f2 381
de5dc9ef 382does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
26ab719a 383 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
384
de5dc9ef 385does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
d59cf2f2 386 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
26ab719a 387
388## Add some info to the database
389
390$replicated
391 ->schema
392 ->populate('Artist', [
393 [ qw/artistid name/ ],
394 [ 4, "Ozric Tentacles"],
395 ]);
071bbccb 396
d59cf2f2 397 is $debug{storage_type}, 'MASTER',
398 "got last query from a master: $debug{dsn}";
399
400 like $debug{info}, qr/INSERT/, 'Last was an insert';
401
26ab719a 402## Make sure all the slaves have the table definitions
403
404$replicated->replicate;
5c1d82d2 405$replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
406$replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
9901aad7 407
408## Silence warning about not supporting the is_replicating method if using the
409## sqlite dbs.
410$replicated->schema->storage->debugobj->silence(1)
7672675a 411 if first { $_ =~ /$var_dir/ } @replicant_names;
d59cf2f2 412
f15afa13 413$replicated->schema->storage->pool->validate_replicants;
26ab719a 414
9901aad7 415$replicated->schema->storage->debugobj->silence(0);
416
26ab719a 417## Make sure we can read the data.
418
419ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
420 => 'Created Result';
421
071bbccb 422## We removed testing here since master read weight is on, so we can't tell in
423## advance what storage to expect. We turn master read weight off a bit lower
d59cf2f2 424## is $debug{storage_type}, 'REPLICANT'
425## => "got last query from a replicant: $debug{dsn}, $debug{info}";
071bbccb 426
26ab719a 427isa_ok $artist1
428 => 'DBICTest::Artist';
d59cf2f2 429
26ab719a 430is $artist1->name, 'Ozric Tentacles'
431 => 'Found expected name for first result';
432
ee356d00 433## Check that master_read_weight is honored
434{
87974600 435 no warnings qw/once redefine/;
ee356d00 436
437 local
f404f53c 438 *DBIx::Class::Storage::DBI::Replicated::Balancer::Random::_random_number =
d59cf2f2 439 sub { 999 };
ee356d00 440
441 $replicated->schema->storage->balancer->increment_storage;
442
443 is $replicated->schema->storage->balancer->current_replicant,
444 $replicated->schema->storage->master
445 => 'master_read_weight is honored';
446
447 ## turn it off for the duration of the test
448 $replicated->schema->storage->balancer->master_read_weight(0);
449 $replicated->schema->storage->balancer->increment_storage;
450}
451
26ab719a 452## Add some new rows that only the master will have This is because
453## we overload any type of write operation so that is must hit the master
454## database.
455
456$replicated
457 ->schema
458 ->populate('Artist', [
459 [ qw/artistid name/ ],
460 [ 5, "Doom's Children"],
461 [ 6, "Dead On Arrival"],
462 [ 7, "Watergate"],
463 ]);
464
d59cf2f2 465 is $debug{storage_type}, 'MASTER',
466 "got last query from a master: $debug{dsn}";
467
468 like $debug{info}, qr/INSERT/, 'Last was an insert';
071bbccb 469
26ab719a 470## Make sure all the slaves have the table definitions
471$replicated->replicate;
472
473## Should find some data now
474
475ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
476 => 'Sync succeed';
071bbccb 477
d59cf2f2 478is $debug{storage_type}, 'REPLICANT'
479 => "got last query from a replicant: $debug{dsn}";
480
26ab719a 481isa_ok $artist2
482 => 'DBICTest::Artist';
d59cf2f2 483
26ab719a 484is $artist2->name, "Doom's Children"
485 => 'Found expected name for first result';
486
487## What happens when we disconnect all the replicants?
488
50336325 489is $replicated->schema->storage->pool->connected_replicants => 2
490 => "both replicants are connected";
d59cf2f2 491
89cf6a70 492$replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
493$replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
26ab719a 494
50336325 495is $replicated->schema->storage->pool->connected_replicants => 0
496 => "both replicants are now disconnected";
497
498## All these should pass, since the database should automatically reconnect
499
26ab719a 500ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
501 => 'Still finding stuff.';
071bbccb 502
d59cf2f2 503is $debug{storage_type}, 'REPLICANT'
504 => "got last query from a replicant: $debug{dsn}";
505
26ab719a 506isa_ok $artist3
507 => 'DBICTest::Artist';
d59cf2f2 508
26ab719a 509is $artist3->name, "Dead On Arrival"
510 => 'Found expected name for first result';
2bf79155 511
50336325 512is $replicated->schema->storage->pool->connected_replicants => 1
13b9e828 513 => "At Least One replicant reconnected to handle the job";
d59cf2f2 514
6f6fb437 515## What happens when we try to select something that doesn't exist?
516
517ok ! $replicated->schema->resultset('Artist')->find(666)
518 => 'Correctly failed to find something.';
071bbccb 519
d59cf2f2 520is $debug{storage_type}, 'REPLICANT'
521 => "got last query from a replicant: $debug{dsn}";
522
cb6ec758 523## test the reliable option
524
525TESTRELIABLE: {
d59cf2f2 526
527 $replicated->schema->storage->set_reliable_storage;
528
529 ok $replicated->schema->resultset('Artist')->find(2)
530 => 'Read from master 1';
531
532 is $debug{storage_type}, 'MASTER',
533 "got last query from a master: $debug{dsn}";
534
535 ok $replicated->schema->resultset('Artist')->find(5)
536 => 'Read from master 2';
537
538 is $debug{storage_type}, 'MASTER',
539 "got last query from a master: $debug{dsn}";
540
541 $replicated->schema->storage->set_balanced_storage;
542
543 ok $replicated->schema->resultset('Artist')->find(3)
9c748388 544 => 'Read from replicant';
071bbccb 545
d59cf2f2 546 is $debug{storage_type}, 'REPLICANT',
547 "got last query from a replicant: $debug{dsn}";
cb6ec758 548}
549
9c748388 550## Make sure when reliable goes out of scope, we are using replicants again
cb6ec758 551
552ok $replicated->schema->resultset('Artist')->find(1)
553 => 'back to replicant 1.';
071bbccb 554
d59cf2f2 555 is $debug{storage_type}, 'REPLICANT',
556 "got last query from a replicant: $debug{dsn}";
557
cb6ec758 558ok $replicated->schema->resultset('Artist')->find(2)
559 => 'back to replicant 2.';
2156bbdd 560
d59cf2f2 561 is $debug{storage_type}, 'REPLICANT',
562 "got last query from a replicant: $debug{dsn}";
071bbccb 563
106d5f3b 564## set all the replicants to inactive, and make sure the balancer falls back to
565## the master.
566
89cf6a70 567$replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
568$replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
9901aad7 569
f404f53c 570{
571 ## catch the fallback to master warning
572 open my $debugfh, '>', \my $fallback_warning;
573 my $oldfh = $replicated->schema->storage->debugfh;
574 $replicated->schema->storage->debugfh($debugfh);
de5dc9ef 575
f404f53c 576 ok $replicated->schema->resultset('Artist')->find(2)
d59cf2f2 577 => 'Fallback to master';
071bbccb 578
d59cf2f2 579 is $debug{storage_type}, 'MASTER',
580 "got last query from a master: $debug{dsn}";
f404f53c 581
582 like $fallback_warning, qr/falling back to master/
9450f312 583 => 'emits falling back to master debug';
f404f53c 584
585 $replicated->schema->storage->debugfh($oldfh);
586}
9901aad7 587
de5dc9ef 588$replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
589$replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
9901aad7 590
591## Silence warning about not supporting the is_replicating method if using the
592## sqlite dbs.
593$replicated->schema->storage->debugobj->silence(1)
7672675a 594 if first { $_ =~ /$var_dir/ } @replicant_names;
d59cf2f2 595
f15afa13 596$replicated->schema->storage->pool->validate_replicants;
de5dc9ef 597
9901aad7 598$replicated->schema->storage->debugobj->silence(0);
599
9450f312 600{
601 ## catch the fallback to master warning
602 open my $debugfh, '>', \my $return_warning;
603 my $oldfh = $replicated->schema->storage->debugfh;
604 $replicated->schema->storage->debugfh($debugfh);
605
606 ok $replicated->schema->resultset('Artist')->find(2)
607 => 'Return to replicants';
608
609 is $debug{storage_type}, 'REPLICANT',
610 "got last query from a replicant: $debug{dsn}";
071bbccb 611
9450f312 612 like $return_warning, qr/Moved back to slave/
613 => 'emits returning to slave debug';
614
615 $replicated->schema->storage->debugfh($oldfh);
616}
d59cf2f2 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/ ],
84f7e8a1 732 [ $id, "Children of the Grave $id"],
d59cf2f2 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}
9e75be92 897
0f83441a 898## Delete the old database files
50336325 899$replicated->cleanup;
ee356d00 900
6988233d 901done_testing;
902
ee356d00 903# vim: sw=4 sts=4 :