Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / storage / replicated.t
index 3f813aa..59bf7e5 100644 (file)
@@ -1,33 +1,51 @@
+BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
+use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_replicated';
+
 use strict;
 use warnings;
 
 use Test::More;
+use DBIx::Class::_Util 'modver_gt_or_eq_and_lt';
+
+use DBICTest;
 
 BEGIN {
-    require DBIx::Class;
-    plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_replicated')
-      unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_replicated');
+  plan skip_all => "A trial version of Moose detected known to break replication - skipping test known to fail" if (
+    DBICTest::RunMode->is_smoker
+      and
+    modver_gt_or_eq_and_lt( 'Moose', '1.99', '1.9903' )
+  )
 }
 
 use Test::Moose;
 use Test::Exception;
-use List::Util 'first';
 use Scalar::Util 'reftype';
-use File::Spec;
-use IO::Handle;
 use Moose();
 use MooseX::Types();
 note "Using Moose version $Moose::VERSION and MooseX::Types version $MooseX::Types::VERSION";
 
-use lib qw(t/lib);
-use DBICTest;
+my $var_dir_re = qr{ t [\/\\] var [\/\\] }x;
+
+## Add a connect_info option to test option merging.
+use DBIx::Class::Storage::DBI::Replicated;
+{
+    package DBIx::Class::Storage::DBI::Replicated;
+
+    use Moose;
+
+    __PACKAGE__->meta->make_mutable;
+
+    around connect_info => sub {
+      my ($next, $self, $info) = @_;
+      $info->[3]{master_option} = 1;
+      $self->$next($info);
+    };
+
+    __PACKAGE__->meta->make_immutable;
 
-my $var_dir = quotemeta ( File::Spec->catdir(qw/t var/) );
+    no Moose;
+}
 
-use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
-use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer';
-use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant';
-use_ok 'DBIx::Class::Storage::DBI::Replicated';
 
 
 =head1 HOW TO USE
@@ -72,15 +90,8 @@ TESTSCHEMACLASSES: {
     ## Get the Schema and set the replication storage type
 
     sub init_schema {
-        # current SQLT SQLite producer does not handle DROP TABLE IF EXISTS, trap warnings here
-        local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /no such table.+DROP TABLE/s };
-
-        my ($class, $schema_method) = @_;
-
-        my $method = "get_schema_$schema_method";
-        my $schema = $class->$method;
-
-        return $schema;
+        #my ($class, $schema_getter) = @_;
+        shift->${\ ( 'get_schema_' . shift ) };
     }
 
     sub get_schema_by_storage_type {
@@ -124,27 +135,6 @@ TESTSCHEMACLASSES: {
     sub cleanup {}
 
     ## --------------------------------------------------------------------- ##
-    ## Add a connect_info option to test option merging.
-    ## --------------------------------------------------------------------- ##
-    {
-    package DBIx::Class::Storage::DBI::Replicated;
-
-    use Moose;
-
-    __PACKAGE__->meta->make_mutable;
-
-    around connect_info => sub {
-      my ($next, $self, $info) = @_;
-      $info->[3]{master_option} = 1;
-      $self->$next($info);
-    };
-
-    __PACKAGE__->meta->make_immutable;
-
-    no Moose;
-    }
-
-    ## --------------------------------------------------------------------- ##
     ## Subclass for when you are using SQLite for testing, this provides a fake
     ## replication support.
     ## --------------------------------------------------------------------- ##
@@ -165,8 +155,8 @@ TESTSCHEMACLASSES: {
 
         $self->master_path( DBICTest->_sqlite_dbfilename );
         $self->slave_paths([
-            File::Spec->catfile(qw/t var DBIxClass_slave1.db/),
-            File::Spec->catfile(qw/t var DBIxClass_slave2.db/),
+            't/var/DBIxClass_slave1.db',
+            't/var/DBIxClass_slave2.db',
         ]);
 
         return $self;
@@ -209,10 +199,11 @@ TESTSCHEMACLASSES: {
         }
     }
 
-    ## Cleanup after ourselves.  Unlink all gthe slave paths.
+    ## Cleanup after ourselves. Unlink all the slave paths.
 
     sub cleanup {
         my $self = shift @_;
+        $_->disconnect for values %{ $self->schema->storage->replicants };
         foreach my $slave (@{$self->slave_paths}) {
             if(-e $slave) {
                 unlink $slave;
@@ -383,7 +374,7 @@ ok @replicant_names, "found replicant names @replicant_names";
 ## Silence warning about not supporting the is_replicating method if using the
 ## sqlite dbs.
 $replicated->schema->storage->debugobj->silence(1)
-  if first { $_ =~ /$var_dir/ } @replicant_names;
+  if grep { $_ =~ $var_dir_re } @replicant_names;
 
 isa_ok $replicated->schema->storage->balancer->current_replicant
     => 'DBIx::Class::Storage::DBI';
@@ -431,7 +422,7 @@ $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
 ## Silence warning about not supporting the is_replicating method if using the
 ## sqlite dbs.
 $replicated->schema->storage->debugobj->silence(1)
-  if first { $_ =~ /$var_dir/ } @replicant_names;
+  if grep { $_ =~ $var_dir_re } @replicant_names;
 
 $replicated->schema->storage->pool->validate_replicants;
 
@@ -614,7 +605,7 @@ $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
 ## Silence warning about not supporting the is_replicating method if using the
 ## sqlite dbs.
 $replicated->schema->storage->debugobj->silence(1)
-  if first { $_ =~ /$var_dir/ } @replicant_names;
+  if grep { $_ =~ $var_dir_re } @replicant_names;
 
 $replicated->schema->storage->pool->validate_replicants;
 
@@ -718,9 +709,19 @@ ok my $reliably = sub {
     is $debug{storage_type}, 'MASTER',
         "got last query from a master: $debug{dsn}";
 
+    $_[1] = 9;
+
 } => 'created coderef properly';
 
-$replicated->schema->storage->execute_reliably($reliably);
+my @list_to_mangle = (1, 2, 3);
+
+$replicated->schema->storage->execute_reliably($reliably, @list_to_mangle);
+
+is_deeply
+  \@list_to_mangle,
+  [ 1, 9, 3],
+  'Aliasing of values passed to execute_reliably works'
+;
 
 ## Try something with an error
 
@@ -735,6 +736,12 @@ throws_ok {$replicated->schema->storage->execute_reliably($unreliably)}
     qr/Can't find source for ArtistXX/
     => 'Bad coderef throws proper error';
 
+throws_ok {
+  $replicated->schema->storage->execute_reliably(sub{
+    die bless [], 'SomeExceptionThing';
+  });
+} 'SomeExceptionThing', "Blessed exception kept intact";
+
 ## Make sure replication came back
 
 ok $replicated->schema->resultset('Artist')->find(3)
@@ -917,6 +924,7 @@ is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{d
 
     is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
 }
+
 ## Delete the old database files
 $replicated->cleanup;