Skip to content
This repository has been archived by the owner on Apr 5, 2020. It is now read-only.

Commit

Permalink
- Use net.xp_framework.unittest.StartServer action
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 19, 2013
1 parent 203cf24 commit b7570ca
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 80 deletions.
Expand Up @@ -4,18 +4,33 @@
use peer\Socket;
use lang\Runtime;


/**
* TestCase
*
* @see xp://peer.Socket
*/
abstract class AbstractSocketTest extends TestCase {
protected static
$serverProcess = null,
$bindAddress = array(null, -1);

protected static $bindAddress= array(null, -1);
protected $fixture= null;

/**
* Callback for when server is connected
*
* @param string $bindAddress
*/
public static function connected($bindAddress) {
self::$bindAddress= explode(':', $bindAddress);
}

/**
* Callback for when server should be shut down
*/
public static function shutdown() {
$c= new Socket(self::$bindAddress[0], self::$bindAddress[1]);
$c->connect();
$c->write("HALT\n");
$c->close();
}

/**
* Creates a new client socket
Expand All @@ -28,79 +43,18 @@ protected abstract function newSocket($addr, $port);

/**
* Setup this test case
*
*/
public function setUp() {
$this->fixture= $this->newSocket(self::$bindAddress[0], self::$bindAddress[1]);
}

/**
* Tears down this test case
*
*/
public function tearDown() {
$this->fixture->isConnected() && $this->fixture->close();
}

/**
* Starts server in background
*
*/
#[@beforeClass]
public static function startServer() {

// Start server process
with ($rt= Runtime::getInstance()); {
self::$serverProcess= $rt->getExecutable()->newInstance(array_merge(
$rt->startupOptions()->asArguments(),
array($rt->bootstrapScript('class')),
array('net.xp_framework.unittest.peer.sockets.TestingServer')
));
}
self::$serverProcess->in->close();

// Check if startup succeeded
$status= self::$serverProcess->out->readLine();
if (2 != sscanf($status, '+ Service %[0-9.]:%d', self::$bindAddress[0], self::$bindAddress[1])) {
try {
self::shutdownServer();
} catch (\lang\IllegalStateException $e) {
$status.= $e->getMessage();
}
throw new \unittest\PrerequisitesNotMetError('Cannot start server: '.$status, null);
}
}

/**
* Shut down socket server
*
*/
#[@afterClass]
public static function shutdownServer() {

// Tell the server to shut down
try {
$c= new Socket(self::$bindAddress[0], self::$bindAddress[1]);
$c->connect();
$c->write("HALT\n");
$c->close();
} catch (\lang\Throwable $ignored) {
// Fall through, below should terminate the process anyway
}
$status= self::$serverProcess->out->readLine();
if (!strlen($status) || '+' != $status{0}) {
while ($l= self::$serverProcess->out->readLine()) {
$status.= $l;
}
while ($l= self::$serverProcess->err->readLine()) {
$status.= $l;
}
self::$serverProcess->close();
throw new \lang\IllegalStateException($status);
}
self::$serverProcess->close();
}


/**
* Test
*
Expand Down
@@ -1,27 +1,22 @@
<?php namespace net\xp_framework\unittest\peer\sockets;

use unittest\actions\ExtensionAvailable;
use unittest\actions\Actions;
use net\xp_framework\unittest\StartServer;
use peer\BSDSocket;


/**
* TestCase
*
* @ext sockets
* @see xp://peer.BSDSocket
*/
#[@action([
# new ExtensionAvailable('sockets'),
# new StartServer('net.xp_framework.unittest.peer.sockets.TestingServer', 'connected', 'shutdown')
#])]
class BSDSocketTest extends AbstractSocketTest {

/**
* Setup this test case
*
*/
public function setUp() {
if (!\lang\Runtime::getInstance()->extensionAvailable('sockets')) {
throw new \unittest\PrerequisitesNotMetError('Sockets extension not available', null, array('ext/sockets'));
}
parent::setUp();
}

/**
* Creates a new client socket
*
Expand Down
@@ -1,13 +1,14 @@
<?php namespace net\xp_framework\unittest\peer\sockets;

use net\xp_framework\unittest\StartServer;
use peer\Socket;


/**
* TestCase
*
* @see xp://peer.Socket
*/
#[@action(new StartServer('net.xp_framework.unittest.peer.sockets.TestingServer', 'connected', 'shutdown'))]
class SocketTest extends AbstractSocketTest {

/**
Expand Down

0 comments on commit b7570ca

Please sign in to comment.