"Fossies" - the Fresh Open Source Software Archive

Member "Apache-Session-1.93/t/99semaphore.t" (15 Sep 2009, 2658 Bytes) of package /linux/www/Apache-Session-1.93.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 use strict;
    2 use Test::More;
    3 #use Test::Exception;
    4 use File::Temp qw[tempdir];
    5 #use Cwd qw[getcwd];
    6 use Config;
    7 
    8 BEGIN {
    9  plan skip_all => "semget not implemented" unless $Config{d_semget};
   10  #Darwin may not have semaphores, see
   11  #http://sysnet.ucsd.edu/~bellardo/darwin/sysvsem.html
   12  plan skip_all => "semctl not implemented" unless $Config{d_semctl};
   13  plan skip_all => "Can't tune this test. Help needed.";
   14  
   15  plan skip_all => "Optional modules (IPC::SysV, IPC::Semaphore) not installed"
   16   unless eval {
   17                require IPC::SysV;
   18                require IPC::Semaphore;
   19               };
   20  plan skip_all => "Cygserver is not running"
   21   if $^O eq 'cygwin' && (!exists $ENV{'CYGWIN'} || $ENV{'CYGWIN'} !~ /server/i);
   22 }
   23 
   24 plan tests => 33;
   25 
   26 my $package = 'Apache::Session::Lock::Semaphore';
   27 use_ok $package;
   28 
   29 #my $origdir = getcwd;
   30 #my $tempdir = tempdir( DIR => '.', CLEANUP => 1 );
   31 #chdir( $tempdir );
   32 
   33 use IPC::SysV qw(IPC_CREAT S_IRWXU SEM_UNDO);
   34 use IPC::Semaphore;
   35 diag("IPC::Semaphore version $IPC::Semaphore::VERSION");
   36 
   37 my $semkey = int(rand(2**15-1));
   38 
   39 my $session = {
   40     data => {_session_id => 'foo'},
   41     args => {SemaphoreKey => $semkey}    
   42 };
   43 
   44 my $number = 1;
   45 for my $iter (2,4,6,8) {
   46     $session->{args}->{NSems} = $iter;
   47     my $locker = $package->new($session);
   48     
   49     isa_ok $locker, $package;
   50 
   51     $locker->acquire_read_lock($session);
   52     my $semnum = $locker->{read_sem};
   53     ok(defined($semnum),'$locker->{read_sem} is defined');
   54 
   55     my $sem = IPC::Semaphore->new($semkey, $number++, S_IRWXU);
   56     diag("NSems: $iter, error: $!") unless defined($sem);
   57 
   58     isa_ok $sem, 'IPC::Semaphore';
   59 
   60     my @sems = $sem->getall;
   61 
   62     ok $sems[$semnum] == 1 && $sems[$semnum+$iter/2] == 0,
   63        'the semaphores seem right';
   64 
   65     $locker->acquire_write_lock($session);
   66 
   67     @sems = $sem->getall;
   68 
   69     ok $sems[$semnum] == 0 && $sems[$semnum+$iter/2] == 1,
   70        'semaphores seem right again';
   71 
   72     $locker->release_write_lock($session);
   73     
   74     @sems = $sem->getall;
   75 
   76     ok $sems[$semnum] == 0 && $sems[$semnum+$iter/2] == 0,
   77        'the semaphores seem right x3';
   78 
   79     $locker->acquire_write_lock($session);
   80     $locker->release_all_locks($session);
   81     
   82     @sems = $sem->getall;
   83 
   84     ok $sems[$semnum] == 0 && $sems[$semnum+$iter/2] == 0,
   85        'the semaphores seem right x4';
   86 
   87     $locker->acquire_read_lock($session);
   88     $locker->release_all_locks($session);
   89     
   90     @sems = $sem->getall;
   91 
   92     ok $sems[$semnum] == 0 && $sems[$semnum+$iter/2] == 0,
   93        'the semaphores seem right x5';
   94 
   95     $sem->remove;
   96 }
   97 
   98 #chdir( $origdir );