Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added getChunk() and finishing up test suite.

  • Loading branch information...
commit 5c8ec9a0c079e44626d75e92a06c4b0a95b14534 1 parent c1d9c15
@boenrobot boenrobot authored
View
27 src/PEAR2/Net/Transmitter/Stream.php
@@ -142,8 +142,8 @@ public function setBuffer($size, $direction = self::DIRECTION_ALL)
case self::DIRECTION_RECEIVE:
return stream_set_read_buffer($this->stream, $size) === 0;
case self::DIRECTION_ALL:
- return stream_set_write_buffer($this->stream, $size) === 0
- && stream_set_read_buffer($this->stream, $size) === 0;
+ return stream_set_read_buffer($this->stream, $size) === 0
+ && stream_set_write_buffer($this->stream, $size) === 0;
}
return false;
}
@@ -179,6 +179,27 @@ public function setChunk($size, $direction = self::DIRECTION_ALL)
}
return false;
}
+
+ /**
+ * Gets the size of the chunk.
+ *
+ * @param string $direction The chunk of which direction to get. Valid
+ * values are the DIRECTION_* constants.
+ *
+ * @return int|array The chunk size in bytes, or an array of chunk sizes
+ * with the directions as keys. FALSE on invalid direction.
+ */
+ public function getChunk($direction = self::DIRECTION_ALL)
+ {
+ switch($direction) {
+ case self::DIRECTION_SEND:
+ case self::DIRECTION_RECEIVE:
+ return $this->chunkSize[$direction];
+ case self::DIRECTION_ALL:
+ return $this->chunkSize;
+ }
+ return false;
+ }
/**
* Sends a string or stream over the wrapped stream.
@@ -207,7 +228,7 @@ public function send($contents)
}
}
fseek($contents, -$bytes, SEEK_CUR);
- }else {
+ } else {
$contents = (string) $contents;
$bytesToSend = (double) sprintf('%u', strlen($contents));
while ($bytes < $bytesToSend) {
View
97 tests/ClientTest.php
@@ -165,4 +165,101 @@ public function testClientSendingIncompleteDataStream()
$this->assertEquals(3, $e->getCode(), 'Improper exception code.');
}
}
+
+ public function testClientTimingOut()
+ {
+ $this->assertEquals('999', $this->client->receive(3));
+ $this->client->setTimeout(2);
+ //$this->client->setChunk(1);
+ try {
+ $this->client->receive(30);
+ $this->fail('Second receiving had to fail.');
+ } catch(SocketException $e) {
+ $this->assertEquals(4, $e->getCode(), 'Improper exception code.');
+ }
+ }
+
+ public function testClientTimingOutStream()
+ {
+ $this->assertEquals('aaa', $this->client->receive(3));
+ $this->client->setTimeout(2);
+ //$this->client->setChunk(1);
+ try {
+ $this->client->receiveStream(30);
+ $this->fail('Second receiving had to fail.');
+ } catch(SocketException $e) {
+ $this->assertEquals(5, $e->getCode(), 'Improper exception code.');
+ }
+ }
+
+ public function testSetBuffer()
+ {
+ $this->assertFalse($this->client->setBuffer(0, 'unknown direction'));
+ $this->assertFalse($this->client->setBuffer(-1));
+ $this->assertTrue(
+ $this->client->setBuffer(99, Stream::DIRECTION_RECEIVE)
+ );
+ }
+
+ public function testSetChunk()
+ {
+ $defaultChunks = $this->client->getChunk();
+ $this->assertInternalType('array', $defaultChunks);
+
+ $this->assertFalse($this->client->getChunk('unknown direction'));
+ $this->assertFalse($this->client->setChunk(1, 'unknown direction'));
+
+ $this->assertFalse($this->client->setChunk(0));
+ $this->assertFalse($this->client->setChunk(0, Stream::DIRECTION_ALL));
+ $this->assertFalse($this->client->setChunk(0, Stream::DIRECTION_SEND));
+ $this->assertFalse(
+ $this->client->setChunk(0, Stream::DIRECTION_RECEIVE)
+ );
+
+ $this->assertTrue(
+ $this->client->setChunk(1, Stream::DIRECTION_RECEIVE)
+ );
+ $this->assertEquals(
+ 1, $this->client->getChunk(Stream::DIRECTION_RECEIVE)
+ );
+ $this->assertEquals(
+ $defaultChunks[Stream::DIRECTION_SEND],
+ $this->client->getChunk(Stream::DIRECTION_SEND)
+ );
+ $this->assertEquals(
+ array(
+ Stream::DIRECTION_RECEIVE => 1,
+ Stream::DIRECTION_SEND => $defaultChunks[Stream::DIRECTION_SEND]
+ ),
+ $this->client->getChunk()
+ );
+
+ $this->assertTrue(
+ $this->client->setChunk(1, Stream::DIRECTION_SEND)
+ );
+ $this->assertEquals(
+ 1, $this->client->getChunk(Stream::DIRECTION_SEND)
+ );
+ $this->assertEquals(
+ 1, $this->client->getChunk(Stream::DIRECTION_RECEIVE)
+ );
+ $this->assertEquals(
+ array(Stream::DIRECTION_RECEIVE => 1,Stream::DIRECTION_SEND => 1),
+ $this->client->getChunk()
+ );
+
+ $this->assertTrue(
+ $this->client->setChunk(2)
+ );
+ $this->assertEquals(
+ 2, $this->client->getChunk(Stream::DIRECTION_SEND)
+ );
+ $this->assertEquals(
+ 2, $this->client->getChunk(Stream::DIRECTION_RECEIVE)
+ );
+ $this->assertEquals(
+ array(Stream::DIRECTION_RECEIVE => 2,Stream::DIRECTION_SEND => 2),
+ $this->client->getChunk()
+ );
+ }
}
View
90 tests/ServerTest.php
@@ -170,4 +170,94 @@ public function testClientSendingIncompleteDataStream()
$this->assertEquals('888', $this->conn->receive(3));
$this->conn->close();
}
+
+ public function testClientTimingOut()
+ {
+ $this->conn->send('999');
+ sleep(1);
+ $this->conn->send('999');
+ sleep(3);
+ $this->conn->send('999');
+ }
+
+ public function testClientTimingOutStream()
+ {
+ $this->conn->send('aaa');
+ sleep(1);
+ $this->conn->send('aaa');
+ sleep(3);
+ $this->conn->send('aaa');
+ }
+
+
+ public function testSetBuffer()
+ {
+ $this->assertFalse($this->conn->setBuffer(0, 'unknown direction'));
+ $this->assertFalse($this->conn->setBuffer(-1));
+ $this->assertTrue(
+ $this->conn->setBuffer(99, Stream::DIRECTION_RECEIVE)
+ );
+ }
+
+ public function testSetChunk()
+ {
+ $defaultChunks = $this->conn->getChunk();
+ $this->assertInternalType('array', $defaultChunks);
+
+ $this->assertFalse($this->conn->getChunk('unknown direction'));
+ $this->assertFalse($this->conn->setChunk(1, 'unknown direction'));
+
+ $this->assertFalse($this->conn->setChunk(0));
+ $this->assertFalse($this->conn->setChunk(0, Stream::DIRECTION_ALL));
+ $this->assertFalse($this->conn->setChunk(0, Stream::DIRECTION_SEND));
+ $this->assertFalse(
+ $this->conn->setChunk(0, Stream::DIRECTION_RECEIVE)
+ );
+
+ $this->assertTrue(
+ $this->conn->setChunk(1, Stream::DIRECTION_RECEIVE)
+ );
+ $this->assertEquals(
+ 1, $this->conn->getChunk(Stream::DIRECTION_RECEIVE)
+ );
+ $this->assertEquals(
+ $defaultChunks[Stream::DIRECTION_SEND],
+ $this->conn->getChunk(Stream::DIRECTION_SEND)
+ );
+ $this->assertEquals(
+ array(
+ Stream::DIRECTION_RECEIVE => 1,
+ Stream::DIRECTION_SEND => $defaultChunks[Stream::DIRECTION_SEND]
+ ),
+ $this->conn->getChunk()
+ );
+
+ $this->assertTrue(
+ $this->conn->setChunk(1, Stream::DIRECTION_SEND)
+ );
+ $this->assertEquals(
+ 1, $this->conn->getChunk(Stream::DIRECTION_SEND)
+ );
+ $this->assertEquals(
+ 1, $this->conn->getChunk(Stream::DIRECTION_RECEIVE)
+ );
+ $this->assertEquals(
+ array(Stream::DIRECTION_RECEIVE => 1,Stream::DIRECTION_SEND => 1),
+ $this->conn->getChunk()
+ );
+
+ $this->assertTrue(
+ $this->conn->setChunk(2)
+ );
+ $this->assertEquals(
+ 2, $this->conn->getChunk(Stream::DIRECTION_SEND)
+ );
+ $this->assertEquals(
+ 2, $this->conn->getChunk(Stream::DIRECTION_RECEIVE)
+ );
+ $this->assertEquals(
+ array(Stream::DIRECTION_RECEIVE => 2,Stream::DIRECTION_SEND => 2),
+ $this->conn->getChunk()
+ );
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.