Merge 'trunk' into 'replication_dedux'
[dbsrgits/DBIx-Class.git] / t / 93storage_replication.t
index 16f911e..2d1e58b 100644 (file)
@@ -8,7 +8,7 @@ BEGIN {
     eval "use Moose";
     plan $@
         ? ( skip_all => 'needs Moose for testing' )
-        : ( tests => 34 );
+        : ( tests => 40 );
 }
 
 use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
@@ -47,7 +47,13 @@ TESTSCHEMACLASSES: {
     
     sub init_schema {
         my $class = shift @_;
-        my $schema = DBICTest->init_schema(storage_type=>'::DBI::Replicated');
+        my $schema = DBICTest->init_schema(
+            storage_type=>[
+               '::DBI::Replicated' => {
+                       balancer_type=>'::Random',
+               }],
+            );
+
         return $schema;
     }
     
@@ -181,7 +187,7 @@ ok my @replicant_connects = $replicated->generate_replicant_connect_info
 ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
     => 'Created some storages suitable for replicants';
     
-isa_ok $replicated->schema->storage->current_replicant
+isa_ok $replicated->schema->storage->balancer->current_replicant
     => 'DBIx::Class::Storage::DBI';
     
 ok $replicated->schema->storage->pool->has_replicants
@@ -288,7 +294,42 @@ is $replicated->schema->storage->pool->connected_replicants => 1
 
 ok ! $replicated->schema->resultset('Artist')->find(666)
     => 'Correctly failed to find something.';
+    
+## test the reliable option
+
+TESTRELIABLE: {
+       
+       $replicated->schema->storage->set_reliable_storage;
+       
+       ok $replicated->schema->resultset('Artist')->find(2)
+           => 'Read from master 1';
+       
+       ok $replicated->schema->resultset('Artist')->find(5)
+           => 'Read from master 2';
+           
+    $replicated->schema->storage->set_balanced_storage;            
+           
+       ok $replicated->schema->resultset('Artist')->find(3)
+        => 'Read from replicant';
+}
 
+## Make sure when reliable goes out of scope, we are using replicants again
+
+ok $replicated->schema->resultset('Artist')->find(1)
+    => 'back to replicant 1.';
+    
+ok $replicated->schema->resultset('Artist')->find(2)
+    => 'back to replicant 2.';
+
+## set all the replicants to inactive, and make sure the balancer falls back to
+## the master.
+
+$replicated->schema->storage->replicants->{"t/var/DBIxClass_slave1.db"}->active(0);
+$replicated->schema->storage->replicants->{"t/var/DBIxClass_slave2.db"}->active(0);
+    
+ok $replicated->schema->resultset('Artist')->find(2)
+    => 'Fallback to master'; 
+       
 ## Delete the old database files
 $replicated->cleanup;