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