[Bast-commits] r6190 - in DBIx-Class/0.08/branches/storage-tweaks: lib/DBIx/Class/Storage/DBI/Replicated/Balancer t

caelum at dev.catalyst.perl.org caelum at dev.catalyst.perl.org
Sat May 9 10:50:25 GMT 2009


Author: caelum
Date: 2009-05-09 10:50:25 +0000 (Sat, 09 May 2009)
New Revision: 6190

Modified:
   DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm
   DBIx-Class/0.08/branches/storage-tweaks/t/93storage_replication.t
Log:
::DBI::Replicated - fix fallback to master, test for the warning, other cleanups

Modified: DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm
===================================================================
--- DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm	2009-05-09 03:31:15 UTC (rev 6189)
+++ DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm	2009-05-09 10:50:25 UTC (rev 6190)
@@ -55,21 +55,20 @@
   my $self = shift @_;
 
   my @replicants = $self->pool->active_replicants;
+
+  if (not @replicants) {
+    # will fall back to master anyway
+    return;
+  }
+
   my $master     = $self->master;
 
-  my $rnd = $self->random_number(@replicants + $self->master_read_weight);
+  my $rnd = $self->_random_number(@replicants + $self->master_read_weight);
 
   return $rnd >= @replicants ? $master : $replicants[int $rnd];
 }
 
-=head2 random_number
-
-Returns a random number from 0 to x, not including x. Uses perl's
-L<perlfunc/rand> by default.
-
-=cut
-
-sub random_number {
+sub _random_number {
   rand($_[1])
 }
 

Modified: DBIx-Class/0.08/branches/storage-tweaks/t/93storage_replication.t
===================================================================
--- DBIx-Class/0.08/branches/storage-tweaks/t/93storage_replication.t	2009-05-09 03:31:15 UTC (rev 6189)
+++ DBIx-Class/0.08/branches/storage-tweaks/t/93storage_replication.t	2009-05-09 10:50:25 UTC (rev 6190)
@@ -6,12 +6,13 @@
 use DBICTest;
 use List::Util 'first';
 use Scalar::Util 'reftype';
+use IO::Handle;
 
 BEGIN {
     eval "use DBIx::Class::Storage::DBI::Replicated; use Test::Moose";
     plan $@
         ? ( skip_all => "Deps not installed: $@" )
-        : ( tests => 89 );
+        : ( tests => 90 );
 }
 
 use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
@@ -366,7 +367,7 @@
     local $SIG{__WARN__} = sub {};
 
     local
-    *DBIx::Class::Storage::DBI::Replicated::Balancer::Random::random_number =
+    *DBIx::Class::Storage::DBI::Replicated::Balancer::Random::_random_number =
 	sub { 999 };
 
     $replicated->schema->storage->balancer->increment_storage;
@@ -469,14 +470,21 @@
 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
 
-## Silence warning about falling back to master.
-$replicated->schema->storage->debugobj->silence(1);
- 
-ok $replicated->schema->resultset('Artist')->find(2)
-    => 'Fallback to master';
+{
+    ## catch the fallback to master warning
+    open my $debugfh, '>', \my $fallback_warning;
+    my $oldfh = $replicated->schema->storage->debugfh;
+    $replicated->schema->storage->debugfh($debugfh);
 
-$replicated->schema->storage->debugobj->silence(0);
+    ok $replicated->schema->resultset('Artist')->find(2)
+	=> 'Fallback to master';
 
+    like $fallback_warning, qr/falling back to master/
+	=> 'emits falling back to master warning';
+
+    $replicated->schema->storage->debugfh($oldfh);
+}
+
 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
 




More information about the Bast-commits mailing list