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