Skip to content

Commit

Permalink
Added getChunk() and finishing up test suite.
Browse files Browse the repository at this point in the history
  • Loading branch information
boenrobot committed Oct 14, 2011
1 parent c1d9c15 commit 5c8ec9a
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/PEAR2/Net/Transmitter/Stream.php
Expand Up @@ -142,8 +142,8 @@ public function setBuffer($size, $direction = self::DIRECTION_ALL)
case self::DIRECTION_RECEIVE: case self::DIRECTION_RECEIVE:
return stream_set_read_buffer($this->stream, $size) === 0; return stream_set_read_buffer($this->stream, $size) === 0;
case self::DIRECTION_ALL: case self::DIRECTION_ALL:
return stream_set_write_buffer($this->stream, $size) === 0 return stream_set_read_buffer($this->stream, $size) === 0
&& stream_set_read_buffer($this->stream, $size) === 0; && stream_set_write_buffer($this->stream, $size) === 0;
} }
return false; return false;
} }
Expand Down Expand Up @@ -179,6 +179,27 @@ public function setChunk($size, $direction = self::DIRECTION_ALL)
} }
return false; 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. * Sends a string or stream over the wrapped stream.
Expand Down Expand Up @@ -207,7 +228,7 @@ public function send($contents)
} }
} }
fseek($contents, -$bytes, SEEK_CUR); fseek($contents, -$bytes, SEEK_CUR);
}else { } else {
$contents = (string) $contents; $contents = (string) $contents;
$bytesToSend = (double) sprintf('%u', strlen($contents)); $bytesToSend = (double) sprintf('%u', strlen($contents));
while ($bytes < $bytesToSend) { while ($bytes < $bytesToSend) {
Expand Down
97 changes: 97 additions & 0 deletions tests/ClientTest.php
Expand Up @@ -165,4 +165,101 @@ public function testClientSendingIncompleteDataStream()
$this->assertEquals(3, $e->getCode(), 'Improper exception code.'); $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()
);
}
} }
90 changes: 90 additions & 0 deletions tests/ServerTest.php
Expand Up @@ -170,4 +170,94 @@ public function testClientSendingIncompleteDataStream()
$this->assertEquals('888', $this->conn->receive(3)); $this->assertEquals('888', $this->conn->receive(3));
$this->conn->close(); $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()
);
}
} }

0 comments on commit 5c8ec9a

Please sign in to comment.