Aniki

 view release on metacpan or  search on metacpan

t/handler/weighted_round_robin.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;

use Test::More;
use Test::Requires qw(Data::WeightedRoundRobin);

use File::Spec;
use lib File::Spec->catfile('t', 'lib');

use Aniki::Handler::WeightedRoundRobin;
use List::Util qw/reduce/;
use List::MoreUtils qw/apply/;

srand 4649;

my @connect_info = (
    {
        value  => ['dbi:mysql:dbname=test;host=db1.localhost;port='.int(rand 65535), 'foo'.int(rand 65535), 'bar'.int(rand 65535), { PrintError => 0, RaiseError => 1 }],
        weight => 10000000,
    },
    {
        value  => ['dbi:mysql:dbname=test;host=db2.localhost;port='.int(rand 65535), 'foo'.int(rand 65535), 'bar'.int(rand 65535), { PrintError => 0, RaiseError => 1 }],
        weight => 10000000,
    },
    {
        value  => ['dbi:mysql:dbname=test;host=db3.localhost;port='.int(rand 65535), 'foo'.int(rand 65535), 'bar'.int(rand 65535), { PrintError => 0, RaiseError => 1 }],
        weight => 10000000,
    },
);

my $handler = Aniki::Handler::WeightedRoundRobin->new(connect_info => \@connect_info);
isa_ok $handler->handler, 'DBIx::Handler';
ok reduce { $a && $b } map { $handler->connect_info()->[0] eq $handler->connect_info()->[0] } 1..100;

my %seen;
for (1..100) {
    $seen{$handler->connect_info->[0]}++;
    $handler->disconnect();
}
ok eq_set(
    [keys %seen],
    [map { $_->{value}->[0] } @connect_info],
);

ok $handler->is_connect_error(q{DBI connect('dbname=test;host=127.0.0.1;port=34783','foo25622',...) failed: Can't connect to MySQL server on '127.0.0.1' (61)});

if (eval { require DBD::mysql; 1 }) {
    my $called = 0;
    no warnings qw/redefine once/;
    local *DBD::mysql::dr::connect = do {
        use warnings qw/redefine once/;
        my $orig = \&DBD::mysql::dr::connect;
        sub {
            $called++;
            goto $orig;
        };
    };
    use warnings qw/redefine once/;

    subtest 'retry connect' => sub {
        $called = 0;
        no warnings qw/redefine once/;



( run in 1.616 second using v1.01-cache-2.11-cpan-437f7b0c052 )