Bind server sockets to any free port

at 2010-02-01 in UnittestsEditorialAnnouncements by friebe (0 comments)

At the company I work for, we let the XP Framework's unittest run on various different machines, including Windows 2008 server and 32- as well as 64-bit Debian Linux boxes, with PHP versions ranging from 5.2.0 - 5.3.1 (lots of permutation, yes).

Hudson

On some of the newer machines we have configured Hudson to start multiple test runners at the same time. This lead to problems with the integration tests (for the FTP API, for example) where we actually fork off a standalone server in the background: The port in use was hardcoded. While this is perfectly OK normally, with mutiples suites executing simultaneously and trying to bind the same port, we were running into problems.

To workaround this, we changed the server instances to bind any free port - this can be accomplished by passing 0 to the peer.ServerSocket's constructor parameter port. Finally, the forked process needs to communicate the port that was chosen (by the operating system) back to the unittest.

Here's the basics:

  // In server instance
$s= new ServerSocket('127.0.0.1', 0);
$s->create();
$s->bind();
Console::writeLine
('Bound to ', $s->port);

// In unittest
$p= Runtime::getInstance()->newInstance(...);
sscanf
($p->out->readLine(), 'Bound to %d', $port);

This made a small patch to the peer.ServerSocket class necessary which has been committed to SVN head and will be available in the upcoming release, 5.7.8.



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases
5.8-SERIES
Unicode
Language
5.9-SERIES

Related

Find related articles by a search for «Bind».