Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/Apache/Session/MongoDB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,58 @@ sub searchOnExpr {
return $class->_query( $args, { $selectField => qr/$value/i } );
}

sub deleteIfLowerThan {
my ( $class, $args, $rule ) = @_;
my $query;

if ( $rule->{or} ) {
$query = {
'$or' => [
map {
{ $_ => { '$lt' => $rule->{or}->{$_} } }
}
keys %{ $rule->{or} }
]
};
}
elsif ( $rule->{and} ) {
$query = {
'$and' => [
map {
{ $_ => { '$lt' => $rule->{or}->{$_} } }
}
keys %{ $rule->{or} }
]
};
}
if ( $rule->{not} ) {
$query = {
'$and' => [
$query,
map {
{ $_ => { '$ne' => $rule->{not}->{$_} } }
}
keys %{ $rule->{not} }
]
};
}
return 0 unless ($query);

my $col = $class->_col($args);
my $result = $col->delete_many($query);
if ( $result->acknowledged ) {
if (wantarray) {
return 1, $result->deleted_count;
}
else {
return 1;
}
}
else {
return 1;
}
}

sub _query {
my ( $class, $args, $filter, @fields ) = @_;
my $col = $class->_col($args);
Expand Down
87 changes: 86 additions & 1 deletion t/Apache-Session-MongoDB.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use strict;
use warnings;
use utf8;

use Test::More tests => 11;
use Test::More tests => 24;
BEGIN { use_ok('Apache::Session::MongoDB') }

#########################
Expand Down Expand Up @@ -54,6 +54,79 @@ SKIP: {
#binmode(STDERR, ":utf8");
#print STDERR $h2{utf8}."\n";

# Create a few sessions to test deleteIfLowerThan
my @delSessions;
push @delSessions,
newsession( $args, type => "persistent", ttl => 100 ),
newsession( $args, type => "persistent", ttl => 10 ),
newsession( $args, type => "temporary", ttl => 100 ),
newsession( $args, type => "temporary", ttl => 10 ),
newsession( $args, type => "temporary", ttl => 100, actttl => 10 ),
newsession( $args, type => "temporary", ttl => 10 );

is(
keys
%{ Apache::Session::MongoDB->searchOn( $args, "type", "persistent" )
},
2,
"Check correct number of permanent sessions"
);
is(
keys
%{ Apache::Session::MongoDB->searchOn( $args, "type", "temporary" ) },
4,
"check correct number of temp sessions"
);

my ( $status, $count ) = Apache::Session::MongoDB->deleteIfLowerThan(
$args,
{
not => { 'type' => 'persistent' },
or => {
ttl => 50,
actttl => 50,
}
}
);
is( $status, 1, "reported success" );
is( $count, 3, "3 sessions deleted" );

# Make sure success is correctly returned as a scalar when no job is done
$status = Apache::Session::MongoDB->deleteIfLowerThan(
$args,
{
not => { 'type' => 'persistent' },
or => {
ttl => 50,
actttl => 50,
}
}
);
is( $status, 1, "Status is OK" );

is(
keys
%{ Apache::Session::MongoDB->searchOn( $args, "type", "persistent" )
},
2,
"Check correct number of permanent sessions"
);
is(
keys
%{ Apache::Session::MongoDB->searchOn( $args, "type", "temporary" ) },
1,
"check correct number of temp sessions"
);

# Delete sessions
for (@delSessions) {
my %h;
eval {
tie( %h, 'Apache::Session::MongoDB', $_, $args );
tied(%h)->delete;
}
}

ok( ( tied(%h2)->delete or 1 ), 'Delete session' );

unless ( defined $ENV{MONGODB_USER} and defined $ENV{MONGODB_DB_NAME} ) {
Expand All @@ -67,3 +140,15 @@ SKIP: {
'Authentified object' );
ok( ( tied(%h)->delete or 1 ), 'Delete session' );
}

sub newsession {
my ( $args, %data ) = @_;
my %h;
ok( tie( %h, 'Apache::Session::MongoDB', undef, $args ), 'New object' );
for ( keys %data ) {
$h{$_} = $data{$_};
}
my $id = $h{_session_id};
untie(%h);
return $id;
}