Request for comments about Testing sockets code
Hi every one. We have a couple of classes that we wrote beofore deciding to use TDD (Test Driven Development) using simpletest framework. So, this classes need to pass several tests before we continue. The more important classes are:
AmiProxy is intented to be daemonized later, for now is just a server that connects to Asterisk (VoIP server) and login in it. Then start listening for client connections, so, it works like a proxy between its clients ant Asterisk Server. I have to test several functionality :
- connect successfull to Asterisk
- Accept clients connections
- Relay clients connections..
- and may be more.
So, anyway, the point is, i have 2 main problems.
The simpler, does it exists in simpletest framework a Console reporter? i would like to use it instead the HtmlReporter()? this is just a dump question, because i will look into the code later, and if its not implemented i will came up with something.
The important question, or request for comments, is if is "correct" the approach im taking to test this thing. The code follows below, with a couple of comments:
Ok, here is how i think it will work.
* this class should test that the AmiProxy accomplish with its responsabilities
* AmiProxy Responsabilities:
* - Connect to Asterisk Manager through the socket localhost:5038
* - Wait for incoming connections of clients that wish to execute one of the
* following commands:
* When a client issue this command, is requesting to send
* the specified action to Asterisk. The action can, or cannot
* have an ActionID associated to it.
* This command asks the AmiProxy to send the specified event
* to all the clients suscribed (see below) to the event. In
* this way, any client can make aware of any event to all the
* the clients that are interested in the event.
* In this way, a client tells to AmiProxy that we wants to be
* aware of the specified event. So, when the event shows up,
* (either because of an Asterisk Event or other client Event)
* the event data will be sent to the client
* When a client request UnsuscribeEvent, then it wont be aware
* anymore of the specified event. No more event data for that
* event will be sent to that client.
* *Note about Events:
* events are launched either by Asterisk or other clients. Until
* now, we have no mechanism to avoid conflicts/collisions between
* Asterisk events and clients events, so its up to the developer of
* the client dont use events that are already taken by Asterisk
* - Send commands to Asterisk
* - Receive commands responses from Asterisk
* - Receive events from Asterisk
* - Relay events to the clients
final class AmiProxyTest extends UnitTestCase
private $ami_socket_uri = '';
private $ami_username = '';
private $ami_password = '';
private $assert_file = '/tmp/assert_AmiProxyTest';
public function SetAmiProxyData($UserName, $Password, $SocketUri)
$this->ami_username = $UserName;
$this->ami_password = $Password;
$this->ami_socket_uri = $SocketUri;
private function ReceivePong()
public function setUp()
public function testConnectingToManager()
$server_pid = pcntl_fork(); /* make a fork for the server application */
if ( $server_pid > 0 )
$client_pid = pcntl_fork(); /* make a fork for the client application */
if ( $client_pid > 0 )
$time_left = 3;
while ( $time_left > 0 )
if ( file_exists($this->assert_file) )
/* KILL the child process (client and server) */
$client = ApSignalHandler();
$client->OnActionResponse('ReceivePong'); /* Receive pong is the function to execute in case that a ping response is received*/
$client->HandleProxyData(); /* this function enters in an infinite while to receive proxy data */
$server = new AmiProxy($this->ami_username, $this->ami_password, $this->ami_socket_uri); /* this function enters in an infinite while, waiting for clients connections */
- The test start, when enter the method testConnectingToManager, removes the flag file (/tmp/etcetc..)
- then forks the proces so the parent continue, and the first child start the AmiProxy instance and waits for clients connections
- the parent then forks again, so the parent continue, and the child (by now the second child) starts a client and try to send a command (ping) to the proxy. If the proxy is connected, then it should send the command to asterisk, asterisk wil return a response, the response sent to the client, the client will then execute the method specified by OnActionResponse(), then the method writes to the flag file.
- the parent will be cheking if the file exists for a while, if the file does not show up, we can tell that something is wrong and kill both childs and assert to false.
Any comment will be greatly appreciated.