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