Sometimes there is a need to dynamically allocate ports for non-vhosts (.e.g. if you spawn a non-Apache server used in testing that binds to some port). Since Apache-Test already provides a feature that allows more than one test suite run on the same machine at the same time by figuring out what are the available ports to use, (using t/TEST -port select), I thought we could tap into that feature and allocate the next available port, which can be used as either:

SetVar MyPortFoo @NextAvailablePort@

or via <Perl> sections for modperl users:

<Perl>
  my $port = @NextAvailablePort@;
  # now do something with it
</Perl>

What do you think?

The patch is trivial (could easily add a test)

Index: t/conf/extra.conf.in
===================================================================
--- t/conf/extra.conf.in        (revision 328716)
+++ t/conf/extra.conf.in        (working copy)
@@ -5,6 +5,8 @@
   Redirect /redirect http://@ServerName@/redirected/
 </IfModule>

+SetVar MyFeaturePort @NextAvailablePort@
+
 <IfModule mod_perl.c>

   <Location /TestMore__testpm>
Index: lib/Apache/TestConfig.pm
===================================================================
--- lib/Apache/TestConfig.pm    (revision 328716)
+++ lib/Apache/TestConfig.pm    (working copy)
@@ -1118,9 +1118,15 @@

     s[@(\w+)@]
      [ my $key = lc $1;
-      exists $self->{vars}->{$key}
-      ? $self->{vars}->{$key}
-      : die "invalid token: [EMAIL PROTECTED]@ $file\n";
+       if ($key eq 'nextavailableport') {
+           $self->server->select_next_port;
+       }
+       elsif (exists $self->{vars}->{$key}) {
+           $self->{vars}->{$key};
+       }
+       else {
+           die "invalid token: [EMAIL PROTECTED]@ $file\n";
+       }
      ]ge;
 }

--
_____________________________________________________________
Stas Bekman mailto:[EMAIL PROTECTED]  http://stason.org/
MailChannels: Assured Messaging(TM) http://mailchannels.com/
The "Practical mod_perl" book       http://modperlbook.org/
http://perl.apache.org/ http://perl.org/ http://logilune.com/

Reply via email to