Skip to content

Commit

Permalink
Add a method to locks to change the expiry time.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nehren committed Sep 17, 2009
1 parent 78ea67f commit 9ae4eb4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
27 changes: 27 additions & 0 deletions lib/DBIx/Locker/Lock.pm
Expand Up @@ -82,6 +82,33 @@ sub unlock {
die('error releasing lock') unless $rows == 1;
}

=method update_expiry
This method updates the expiration time for the given lock. It accepts a Unix
epoch time as an integer.
=cut

sub update_expiry {
my ($self, $new_expiry) = @_;

die "new expiry must be a Unix epoch time" unless $new_expiry =~ /\A\d+\Z/;

my $dbh = $self->locker->dbh;
my $table = $self->locker->table;

my $rows = $dbh->do(
"UPDATE $table SET expires = ? WHERE id = ?",
undef,
$new_expiry,
$self->lock_id,
);

die('error updating expiry time') unless $rows == 1;

$self->{expires} = $new_expiry;
}

sub DESTROY {
my ($self) = @_;
local $@;
Expand Down
19 changes: 18 additions & 1 deletion t/basic.t
@@ -1,7 +1,7 @@
use strict;
use warnings;

use Test::More tests => 8;
use Test::More tests => 12;

use DBI;
use DBIx::Locker;
Expand Down Expand Up @@ -37,6 +37,10 @@ my $guid;
my $id = $lock->lock_id;
like($id, qr/\A\d+\z/, "we got a numeric lock id");

my $expiry = $lock->expires;
like($expiry, qr/\A\d+\z/, "expiry is an integer");
cmp_ok($expiry, '>', time, "expiry is in the future");

$guid = $lock->guid;

eval { $locker->lock('Zombie Soup'); };
Expand All @@ -54,3 +58,16 @@ my $guid;
isa_ok($lock_2, 'DBIx::Locker::Lock', 'third lock');
isnt($lock->lock_id, $lock_2->lock_id, 'two locks, two distinct id values');
}

{
my $lock = $locker->lock('Zombie Time Machine');
my $original_expiry = $lock->expires;
my $new_expiry = time + 1000;
$lock->update_expiry($new_expiry);
is($lock->expires, $new_expiry, "lock expiry updated correctly in object");
my $dbh = $locker->dbh;
my $sth = $dbh->prepare('SELECT expires FROM locks WHERE id = ?');
$sth->execute($lock->lock_id);
my ($new_expires) = $sth->fetchrow_array;
is($new_expires, $new_expiry, "lock expiry updated correctly in DB");
}

0 comments on commit 9ae4eb4

Please sign in to comment.