・ Web系SE募集 (技術開発部開発系エンジニア・Java/Tomcat 420万円~750万円)
   ・ 社内SE募集 (業務系システムの開発・社内イントラ)

2008年10月アーカイブ

インストールした mod_perl2 の動作確認を Apache::Test で行う。

※mod_perl2のインストールは次の手順で行う。

※ Apache::Test は、mod_perl2をインストールする前に、install Bundle::Apache2 した時にインストールされます。


ファイル構成
$ tree ModPerlTest-0.01
ModPerlTest-0.01
|-- Changes
|-- MANIFEST
|-- META.yml
|-- Makefile.PL
|-- README
|-- lib
|   |-- ModPerlTest
|   |   |-- ResponseCleanup.pm
|   |   `-- ResponseHandler.pm
|   `-- ModPerlTest.pm
`-- t
    |-- 00_compile.t
    |-- 01_resp1.t
    |-- 01_resp2.t
    |-- 02_respClean.t
    |-- TEST
    |-- TEST.PL
    |-- conf
    |   `-- extra.conf.in
    `-- response
        `-- ModPerlTest
            `-- ResponseTestHandler.pm

6 directories, 16 files

設定ファイル extra.conf.in

LoadModule apreq_module    /usr/local/apache2/modules/mod_apreq2.so

<location responsehandler="">
        SetHandler perl-script
        PerlResponseHandler ModPerlTest::ResponseHandler
</location> 

<location responsecleanup="">
        SetHandler perl-script
        PerlResponseHandler ModPerlTest::ResponseCleanup
</location>  

ModPerlTest::ResponseHandler

パラメータを受けて返す レスポンスハンドラ

package ModPerlTest::ResponseHandler;

use Apache2::RequestUtil ();
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Const -compile => ':common';
use Apache2::Request;

use strict;
use 5.008_001;
our $VERSION = '0.01';

sub handler {
        my $r = shift;
        my $query  = Apache2::Request->new($r);
        my $param = $query->param if( $query->param );
        $param ||= {};
        my $input = $param->{input} || '';
        $r->content_type('text/html');
        $r->puts(__PACKAGE__ ."\t". $input);
        return Apache2::Const::OK;
}

1;
__END__

ModPerlTest::ResponseHandler にリクエストを送るためのテストコード

01_resp1.t

use strict;
use Apache::Test qw(:withtestmore);
use Apache::TestUtil;
use Apache::TestRequest 'GET_BODY';
use Test::More ( tests => 1 );
use URI;

my $input = 'ABC';
my $url = URI->new("/responseHandler?input=$input");
my $data = GET_BODY $url;
is ($data, "ModPerlTest::ResponseHandler\t$input");

1;

01_resp2.t

use strict;
use Apache::Test qw(:withtestmore);
use Apache::TestUtil;
use Apache::TestRequest 'GET_BODY';
use Test::More ( tests => 1 );
use URI;

my $url = URI->new("/responseHandler?input=$$");
my $data = GET_BODY $url;
is ($data, "ModPerlTest::ResponseHandler\t$$");

1;

cleanup_register を使用したレスポンスハンドラ

ModPerlTest::ResponseCleanup

package ModPerlTest::ResponseCleanup;

use Apache2::RequestUtil ();
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Const -compile => ':common';

use strict;
use 5.008_001;
our $VERSION = '0.01';

sub handler {
        my $r = shift;

        $r->content_type('text/html');
        $r->puts($$);
        $r->pool->cleanup_register(\&cleanup, time);
        return Apache2::Const::OK;
}

use Fcntl qw(:DEFAULT :flock);

sub cleanup{
        my $t = shift;
        my $file = "/tmp/mod_perl_test.$$.txt";
        sysopen(FH, $file, O_WRONLY | O_CREAT) or die "can't open $file: $!";
        print FH join("\t"=> $t, time, $$);
        close FH;
        return Apache2::Const::OK;
}

1;
__END__

ModPerlTest::ResponseCleanup にリクエストを送るためのテストコード

02_respClean.t

use strict;
use Apache::Test qw(:withtestmore);
use Apache::TestUtil;
use Apache::TestRequest 'GET_BODY';
use Test::More ( tests => 1 );
use URI;

my $url = URI->new("/responseCleanup");
my $pid = GET_BODY $url;
my $count=0;
my $fpid;
my ($t1, $t2);
use Fcntl qw(:DEFAULT :flock);
my $file = "/tmp/mod_perl_test.$pid.txt";
while(1){
        last if $count > 10;
        $count++;
        $fpid ||= '';
        sysopen(FH, $file, O_RDONLY) or  next;
        ($t1, $t2, $fpid) = split /\t/,(shift @{[]});
	close FH;
	last;
}
is($pid , $fpid, "($pid , $fpid)");
unlink $file;

1;

レスポンスハンドラ の中に書くテストコード

ModPerlTest::ResponseTestHandler

package ModPerlTest::ResponseTestHandler;

use Apache::Test;
use Apache2::RequestUtil ();
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Const -compile => ':common';
use Apache2::Request;

use strict;
use 5.008_001;
our $VERSION = '0.01';

sub handler {
        my $r = shift;
        plan $r, tests => 1;
        ok ref $r;
        return Apache2::Const::OK;
}

1;

テスト結果

$ perl Makefile.PL
[   info] generating script t/TEST
Checking if your kit is complete...
Looks good
Warning: prerequisite UNIVERSAL::require 0 not found.
Writing Makefile for ModPerlTest


$ make test
cp lib/ModPerlTest/ResponseHandler.pm blib/lib/ModPerlTest/ResponseHandler.pm
cp lib/ModPerlTest/ResponseCleanup.pm blib/lib/ModPerlTest/ResponseCleanup.pm
cp lib/ModPerlTest.pm blib/lib/ModPerlTest.pm
/usr/local/bin/perl -Iblib/arch -Iblib/lib \
        t/TEST  -clean
setting ulimit to allow core files
ulimit -c unlimited; /usr/local/bin/perl /home/suzu/work/ModPerlTest-0.01/t/TEST -clean
APACHE_TEST_GROUP= APACHE_TEST_HTTPD= APACHE_TEST_PORT= APACHE_TEST_USER= APACHE_TEST_APXS= \
        /usr/local/bin/perl -Iblib/arch -Iblib/lib \
        t/TEST  -bugreport -verbose=0
setting ulimit to allow core files
ulimit -c unlimited; /usr/local/bin/perl /home/suzu/work/ModPerlTest-0.01/t/TEST -bugreport -verbose=0
/usr/local/apache2/bin/httpd  -d /home/suzu/work/ModPerlTest-0.01/t -f /home/suzu/work/ModPerlTest-0.01/t/conf/httpd.conf -D APACHE2
using Apache/2.2.9 (prefork MPM)
waiting 60 seconds for server to start: 00:00[Sat Oct 04 00:43:51 2008] [warn] module apreq_module is already loaded, skipping
waiting 60 seconds for server to start: ok (waited 1 secs)
server localhost.localdomain:8529 started
t/00_compile.........................ok
t/01_resp1...........................ok
t/01_resp2...........................ok
t/02_respClean.......................ok
t/modperltest/responsetesthandler....ok
All tests successful.
Files=5, Tests=6,  4 wallclock secs ( 0.09 usr  0.01 sys +  2.52 cusr  0.95 csys =  3.57 CPU)
Result: PASS
server localhost.localdomain:8529 shutdown
$
$ cat ./t/logs/access_log
127.0.0.1 - - [04/Oct/2008:00:43:53 +0900] "GET /index.html HTTP/1.0" 200 795
127.0.0.1 - - [04/Oct/2008:00:43:54 +0900] "GET /responseHandler?input=ABC HTTP/1.0" 200 32
127.0.0.1 - - [04/Oct/2008:00:43:55 +0900] "GET /responseHandler?input=3790 HTTP/1.0" 200 33
127.0.0.1 - - [04/Oct/2008:00:43:55 +0900] "GET /responseCleanup HTTP/1.0" 200 4
127.0.0.1 - - [04/Oct/2008:00:43:56 +0900] "GET /ModPerlTest__ResponseTestHandler HTTP/1.0" 200 217
$ cat ./t/logs/error_log
[Sat Oct 04 00:43:51 2008] [info] Init: Seeding PRNG with 0 bytes of entropy
[Sat Oct 04 00:43:51 2008] [info] Init: Generating temporary RSA private keys (512/1024 bits)
[Sat Oct 04 00:43:51 2008] [info] Init: Generating temporary DH parameters (512/1024 bits)
[Sat Oct 04 00:43:51 2008] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Sat Oct 04 00:43:51 2008] [info] Init: Initializing (virtual) servers for SSL
[Sat Oct 04 00:43:51 2008] [info] mod_ssl/2.2.9 compiled against Server: Apache/2.2.9, Library: OpenSSL/0.9.8g
[Sat Oct 04 00:43:51 2008] [warn] module apreq_module is already loaded, skipping
[Sat Oct 04 00:43:51 2008] [info] Init: Seeding PRNG with 0 bytes of entropy
[Sat Oct 04 00:43:51 2008] [info] Init: Generating temporary RSA private keys (512/1024 bits)
[Sat Oct 04 00:43:52 2008] [info] Init: Generating temporary DH parameters (512/1024 bits)
[Sat Oct 04 00:43:52 2008] [info] Init: Initializing (virtual) servers for SSL
[Sat Oct 04 00:43:52 2008] [info] mod_ssl/2.2.9 compiled against Server: Apache/2.2.9, Library: OpenSSL/0.9.8g
[Sat Oct 04 00:43:52 2008] [notice] Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.8g mod_apreq2-20051231/2.6.0 mod_perl/2.0.4 Perl/v5.10.0 configured -- resuming normal operations
[Sat Oct 04 00:43:52 2008] [info] Server built: Sep 20 2008 19:27:27
[Sat Oct 04 00:43:52 2008] [debug] prefork.c(1001): AcceptMutex: sysvsem (default: sysvsem)
[Sat Oct 04 00:43:57 2008] [info] removed PID file /home/suzu/work/ModPerlTest-0.01/t/logs/httpd.pid (pid=3778)
[Sat Oct 04 00:43:57 2008] [notice] caught SIGTERM, shutting down

今回使用したファイル。

ModPerlTest-0.01.tar.gz


このアーカイブについて

このページには、2008年10月に書かれたブログ記事が新しい順に公開されています。

前のアーカイブは2008年9月です。

次のアーカイブは2008年11月です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。