Skip to content

Commit

Permalink
save() method was broken when the replace() or add().
Browse files Browse the repository at this point in the history
  • Loading branch information
xaicron committed Jul 4, 2016
1 parent 5ff4611 commit a94f235
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/Data/WeightedRoundRobin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ our $DEFAULT_WEIGHT = 100;
our $BTREE_BORDER = 10;

use Scope::Guard qw(guard);
use Data::Clone qw(clone);

sub new {
my ($class, $list, $args) = @_;
Expand Down Expand Up @@ -176,7 +177,7 @@ sub next {

sub save {
my $self = shift;
my $orig_rrlist = $self->{rrlist};
my $orig_rrlist = clone $self->{rrlist};
guard { $self->set($orig_rrlist) };
}

Expand Down
59 changes: 50 additions & 9 deletions t/07_save.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,59 @@ use Test::More;

use Data::WeightedRoundRobin;

my $dwr = Data::WeightedRoundRobin->new([qw/foo bar/]);
subtest 'set' => sub {
my $dwr = Data::WeightedRoundRobin->new([qw/foo/]);

subtest 'remove foo' => sub {
my $guard = $dwr->save;
$dwr->remove('foo');
is $dwr->next, 'bar';
};
{
my $guard = $dwr->save;
$dwr->set([qw/hoge/]);
is $dwr->next, 'hoge';
};

subtest 'remove bar' => sub {
my $guard = $dwr->save;
$dwr->remove('bar');
is $dwr->next, 'foo';
};

subtest 'remove' => sub {
my $dwr = Data::WeightedRoundRobin->new([qw/foo bar/]);

{
my $guard = $dwr->save;
$dwr->remove('foo');
is $dwr->next, 'bar';
};
{
my $guard = $dwr->save;
$dwr->remove('bar');
is $dwr->next, 'foo';
};

like $dwr->next, qr/\A(?:foo|bar)\z/;
};

subtest 'add' => sub {
my $dwr = Data::WeightedRoundRobin->new;

{
my $guard = $dwr->save;
$dwr->add('bar');
is $dwr->next, 'bar';
};

ok !$dwr->next;
};

subtest 'replace' => sub {
my $dwr = Data::WeightedRoundRobin->new([
{ key => 'foo', value => 'bar' },
]);

{
my $guard = $dwr->save;
$dwr->replace({ key => 'foo', value => 'hoge' });
is $dwr->next, 'hoge';
};

is $dwr->next, 'bar';
};

done_testing;

0 comments on commit a94f235

Please sign in to comment.