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