+use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_replicated';
+
use strict;
use warnings;
-use lib qw(t/lib);
+
use Test::More;
-use Test::Exception;
+use DBIx::Class::_Util 'modver_gt_or_eq_and_lt';
+use lib qw(t/lib);
use DBICTest;
+
+BEGIN {
+ 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";
-BEGIN {
- eval { require Test::Moose; Test::Moose->import() };
- plan skip_all => "Need Test::Moose to run this test" if $@;
- require DBIx::Class;
+my $var_dir = quotemeta ( File::Spec->catdir(qw/t var/) );
+
+## 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);
+ };
- plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('replicated')
- unless DBIx::Class::Optional::Dependencies->req_ok_for ('replicated');
+ __PACKAGE__->meta->make_immutable;
+
+ 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';
-use Moose();
-use MooseX::Types();
-diag "Using Moose version $Moose::VERSION and MooseX::Types version $MooseX::Types::VERSION";
=head1 HOW TO USE
## 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 {
balancer_type=>'::Random',
balancer_args=>{
auto_validate_every=>100,
- master_read_weight => 1
+ master_read_weight => 1
},
}
},
storage_type=> '::DBI::Replicated',
balancer_type=>'::Random',
balancer_args=> {
- auto_validate_every=>100,
- master_read_weight => 1
+ auto_validate_every=>100,
+ master_read_weight => 1
+ },
+ pool_args=>{
+ maximum_lag=>1,
},
deploy_args=>{
add_drop_table => 1,
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.
## --------------------------------------------------------------------- ##
}
}
- ## 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;
## Silence warning about not supporting the is_replicating method if using the
## sqlite dbs.
$replicated->schema->storage->debugobj->silence(1)
- if first { m{^t/} } @replicant_names;
+ if first { $_ =~ /$var_dir/ } @replicant_names;
isa_ok $replicated->schema->storage->balancer->current_replicant
=> 'DBIx::Class::Storage::DBI';
## Silence warning about not supporting the is_replicating method if using the
## sqlite dbs.
$replicated->schema->storage->debugobj->silence(1)
- if first { m{^t/} } @replicant_names;
+ if first { $_ =~ /$var_dir/ } @replicant_names;
$replicated->schema->storage->pool->validate_replicants;
"got last query from a master: $debug{dsn}";
like $fallback_warning, qr/falling back to master/
- => 'emits falling back to master warning';
+ => 'emits falling back to master debug';
$replicated->schema->storage->debugfh($oldfh);
}
## Silence warning about not supporting the is_replicating method if using the
## sqlite dbs.
$replicated->schema->storage->debugobj->silence(1)
- if first { m{^t/} } @replicant_names;
+ if first { $_ =~ /$var_dir/ } @replicant_names;
$replicated->schema->storage->pool->validate_replicants;
$replicated->schema->storage->debugobj->silence(0);
-ok $replicated->schema->resultset('Artist')->find(2)
- => 'Returned to replicates';
+{
+ ## catch the fallback to master warning
+ open my $debugfh, '>', \my $return_warning;
+ my $oldfh = $replicated->schema->storage->debugfh;
+ $replicated->schema->storage->debugfh($debugfh);
-is $debug{storage_type}, 'REPLICANT',
- "got last query from a replicant: $debug{dsn}";
+ ok $replicated->schema->resultset('Artist')->find(2)
+ => 'Return to replicants';
+
+ is $debug{storage_type}, 'REPLICANT',
+ "got last query from a replicant: $debug{dsn}";
+
+ like $return_warning, qr/Moved back to slave/
+ => 'emits returning to slave debug';
+
+ $replicated->schema->storage->debugfh($oldfh);
+}
## Getting slave status tests
->schema
->populate('Artist', [
[ qw/artistid name/ ],
- [ $id, "Children of the Grave"],
+ [ $id, "Children of the Grave $id"],
]);
ok my $result = $replicated->schema->resultset('Artist')->find($id)
is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
}
+
## Delete the old database files
$replicated->cleanup;