Permalink
Browse files

Progress with the test suite.

  • Loading branch information...
1 parent 99e5ea5 commit f9af7c446ba5d64cde037053f770c2c8a3dc59c3 @boenrobot boenrobot committed Oct 11, 2011
@@ -194,6 +194,17 @@ public function current()
}
/**
+ * Moves the pointer backwards by 1.
+ *
+ * @return bool TRUE if the new position is valid, FALSE otherwise.
+ */
+ public function prev()
+ {
+ --$this->position;
+ return $this->valid();
+ }
+
+ /**
* Moves the pointer to the last valid position.
*
* @return bool TRUE if the collection is not empty, FALSE otherwise.
@@ -24,8 +24,8 @@
* A stream transmitter.
*
* This is a convinience wrapper for stream functionality. Used to ensure data
- * integrity. Designed for sockets, but it has intentionally been made to accept
- * any stream.
+ * integrity. Designed for TCP sockets, but it has intentionally been made to
+ * accept any stream.
*
* @category Net
* @package PEAR2_Net_Transmitter
@@ -36,16 +36,15 @@
class Stream
{
/**
- * Used in {@link setBuffer()} to apply the setting to both sending and
- * receiving.
+ * Used to apply settings to both sending and receiving.
*/
- const DIRECTION_BOTH = '|||';
+ const DIRECTION_ALL = '|||';
/**
- * Used in {@link setBuffer()} to apply the setting only to sending.
+ * Used to apply settings only to sending.
*/
const DIRECTION_SEND = '<<<';
/**
- * Used in {@link setBuffer()} to apply the setting only to receiving.
+ * Used to apply settings only to receiving.
*/
const DIRECTION_RECEIVE = '>>>';
@@ -131,22 +130,22 @@ public function setTimeout($seconds, $microseconds = 0)
*
* @param int $size The desired size of the buffer, in bytes.
* @param string $direction The buffer of which direction to set. Valid
- * values are the DIRECTION_* constants. Any other value is treated as
- * {@link DIRECTION_BOTH}.
+ * values are the DIRECTION_* constants.
*
* @return bool TRUE on success, FALSE on failure.
*/
- public function setBuffer($size, $direction = self::DIRECTION_BOTH)
+ public function setBuffer($size, $direction = self::DIRECTION_ALL)
{
switch($direction) {
case self::DIRECTION_SEND:
return stream_set_write_buffer($this->stream, $size) === 0;
case self::DIRECTION_RECEIVE:
return stream_set_read_buffer($this->stream, $size) === 0;
- default:
+ case self::DIRECTION_ALL:
return stream_set_write_buffer($this->stream, $size) === 0
&& stream_set_read_buffer($this->stream, $size) === 0;
}
+ return false;
}
/**
@@ -158,12 +157,11 @@ public function setBuffer($size, $direction = self::DIRECTION_BOTH)
*
* @param int $size The desired size of the chunk, in bytes.
* @param string $direction The chunk of which direction to set. Valid
- * values are the DIRECTION_* constants. Any other value is treated as
- * {@link DIRECTION_BOTH}.
+ * values are the DIRECTION_* constants.
*
* @return bool TRUE on success, FALSE on failure.
*/
- public function setChunk($size, $direction = self::DIRECTION_BOTH)
+ public function setChunk($size, $direction = self::DIRECTION_ALL)
{
$size = (int) $size;
if ($size <= 0) {
@@ -174,11 +172,12 @@ public function setChunk($size, $direction = self::DIRECTION_BOTH)
case self::DIRECTION_RECEIVE:
$this->chunkSize[$direction] = $size;
return true;
- default:
+ case self::DIRECTION_ALL:
$this->chunkSize[self::DIRECTION_SEND]
= $this->chunkSize[self::DIRECTION_RECEIVE] = $size;
return true;
}
+ return false;
}
/**
@@ -69,7 +69,7 @@ public function __construct($server, $timeout = null)
$hostPortCombo = explode(':', $peername);
$this->peerIP = $hostPortCombo[0];
$this->peerPort = (int) $hostPortCombo[1];
- } catch (StreamException $e) {
+ } catch (Exception $e) {
throw $this->createException('Failed to initialize connection.', 9);
}
}
View
@@ -141,4 +141,28 @@ public function testServerReceivingIncompleteDataStream()
{
$this->assertEquals(1, $this->client->send('t'), 'Wrong amount sent.');
}
+
+ public function testClientSendingIncompleteData()
+ {
+ $size = 3/*m*/ * 1024/*k*/ * 1024/*b*/;
+ $contents = str_repeat('7', $size);
+ try {
+ $this->client->send($contents);
+ } catch(SocketException $e) {
+ $this->assertEquals(2, $e->getCode(), 'Improper exception code.');
+ }
+ }
+
+ public function testClientSendingIncompleteDataStream()
+ {
+ $size = 3/*m*/ * 1024/*k*/ * 1024/*b*/;
+ $stream = fopen('php://temp', 'r+b');
+ fwrite($stream, str_repeat('8', $size));
+ rewind($stream);
+ try {
+ $this->client->sendStream($stream);
+ } catch(SocketException $e) {
+ $this->assertEquals(3, $e->getCode(), 'Improper exception code.');
+ }
+ }
}
View
@@ -20,7 +20,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase
protected static $errstr;
/**
- * @var SocketServerConnectionTransmitter
+ * @var TcpServerConnection
*/
protected $conn;
@@ -158,4 +158,16 @@ public function testServerReceivingIncompleteDataStream()
$this->assertEquals(5, $e->getCode(), 'Improper exception code.');
}
}
+
+ public function testClientSendingIncompleteData()
+ {
+ $this->assertEquals('777', $this->conn->receive(3));
+ $this->conn->close();
+ }
+
+ public function testClientSendingIncompleteDataStream()
+ {
+ $this->assertEquals('888', $this->conn->receive(3));
+ $this->conn->close();
+ }
}
View
@@ -81,6 +81,11 @@ public function testFilterCollection()
$this->assertEquals(array(), $filters->current());
$this->assertEquals(4, $filters->getCurrentPosition());
+ $this->assertTrue($filters->prev());
+ $this->assertEquals('string.toupper', $filters->key());
+ $this->assertEquals(array(), $filters->current());
+ $this->assertEquals(3, $filters->getCurrentPosition());
+
$this->assertTrue($filters->rewind());
$filters->removeAt(2);
@@ -149,4 +154,21 @@ public function testInvalidServer()
$this->assertEquals(8, $e->getCode(), 'Improper exception code.');
}
}
+
+ public function testServerConnectionTimeout()
+ {
+ try {
+ new SC(
+ stream_socket_server(
+ 'tcp://' . LOCAL_HOSTNAME . ':' . LOCAL_PORT
+ )
+ ,
+ 2
+ );
+ $this->fail('Server creation had to fail.');
+ } catch(SocketException $e)
+ {
+ $this->assertEquals(9, $e->getCode(), 'Improper exception code.');
+ }
+ }
}

0 comments on commit f9af7c4

Please sign in to comment.