Skip to content

Commit

Permalink
Merge pull request #12 from liuggio/test_fail
Browse files Browse the repository at this point in the history
fixed #11
  • Loading branch information
liuggio committed Nov 2, 2013
2 parents 5b98344 + d22d1e8 commit cb575a9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/Liuggio/StatsdClient/StatsdClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ private function throwException(\Exception $exception)
function doReduce($result, $item)
{
$oldLastItem = array_pop($result);
$sizeResult = strlen($oldLastItem);
$message = $item;
$totalSize = $sizeResult + strlen($message) + 1; //the comma is the 1
$sizeResult = strlen($oldLastItem);
$message = $item;
$totalSize = $sizeResult + strlen($message) + 1; //the newline is the 1

if (self::MAX_UDP_SIZE_STR < $totalSize) {
//going to build another one
array_push($result, $message);
array_push($result, $oldLastItem);
array_push($result, $message);
} else {
//going to modifying the existing
$separator = '';
Expand Down
2 changes: 1 addition & 1 deletion src/Liuggio/StatsdClient/StatsdClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Interface StatsdClientInterface
{
const MAX_UDP_SIZE_STR = 548;
const MAX_UDP_SIZE_STR = 512;

/*
* Send the metrics over UDP
Expand Down
4 changes: 1 addition & 3 deletions tests/Liuggio/StatsdClient/ReadmeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public function testFullUsageWithObject() {
$sender = $this->mockSender();
// $sender = new Sender();

// StatsdClient(SenderInterface $sender, $host = 'udp://localhost', $port = 8126, $reducePacket = true, $fail_silently = true)
$client = new StatsdClient($sender);
$factory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData');

Expand All @@ -36,8 +35,7 @@ public function testFullUsageArray() {
$sender = $this->mockSender();
// $sender = new Sender();

// StatsdClient(SenderInterface $sender, $host = 'localhost', $port = 8126, $protocol='udp', $reducePacket = true, $fail_silently = true)
$client = new StatsdClient($sender, $host = 'localhost', $port = 8126, 'udp', $reducePacket = true, $fail_silently = true);
$client = new StatsdClient($sender);

$data[] ="increment:1|c";
$data[] ="set:value|s";
Expand Down
80 changes: 70 additions & 10 deletions tests/Liuggio/StatsdClient/StatsdClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testSend($array, $assertion) {

public function testReduceCount()
{
$statd = $this->mockStatsdClientWithAssertionOnWrite(null);
$statsd = $this->mockStatsdClientWithAssertionOnWrite(null);

$entity0 = new StatsdData();
$entity0->setKey('key1');
Expand All @@ -139,13 +139,13 @@ public function testReduceCount()

$reducedMessage = array('key1:1|c' . PHP_EOL . 'key2:2|ms');

$this->assertEquals($statd->reduceCount($array0), $reducedMessage);
$this->assertEquals($statsd->reduceCount($array0), $reducedMessage);

}

public function testReduceWithString()
{
$statd = $this->mockStatsdClientWithAssertionOnWrite(null);
$statsd = $this->mockStatsdClientWithAssertionOnWrite(null);

$msg = 'A3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789:';
$msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c';
Expand All @@ -154,15 +154,15 @@ public function testReduceWithString()
$msg = 'B3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789:';
$msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c';
$array0[] = $msg;
$reduced = $statd->reduceCount($array0);
$reduced = $statsd->reduceCount($array0);
$combined = $array0[0] . PHP_EOL . $array0[1];
$this->assertEquals($combined, $reduced[0]);
}


public function testReduceWithMaxUdpPacketSplittedInTwoPacket()
public function testReduceWithMaxUdpPacketSplitInTwoPacket()
{
$statd = $this->mockStatsdClientWithAssertionOnWrite(null);
$statsd = $this->mockStatsdClientWithAssertionOnWrite(null);

$msg = 'A3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789'; //1
$msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 '; //2
Expand All @@ -175,12 +175,72 @@ public function testReduceWithMaxUdpPacketSplittedInTwoPacket()
$msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c';
$array0[] = $msg;

$reduced = $statd->reduceCount($array0);
$reduced = $statsd->reduceCount($array0);

$combined = $array0[0] . PHP_EOL . $array0[1];
$this->assertEquals($array0, $reduced);
}



public function testMultiplePacketsWithReducing()
{
$statsd = $this->mockStatsdClientWithAssertionOnWrite(null);

$msg = 'A3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789';
$array0[] = $msg;
$array0[] = $msg;
$array0[] = $msg;
$array0[] = $msg;
$array0[] = $msg;
$array0[] = $msg;
$array0[] = $msg;
$array0[] = $msg;

$this->assertEquals($array0[1], $reduced[0]);
$this->assertEquals($array0[0], $reduced[1]);
$total = count($array0) * strlen($msg);
$reducedPacketsAssertion = (int) ceil($total / StatsdClientInterface::MAX_UDP_SIZE_STR);

$reduced = $statsd->reduceCount($array0);

$this->assertEquals($reducedPacketsAssertion, count($reduced));
}

public function testPacketReducing()
{
$assertion = array();
$msg = '23456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789';

$data[] = 'A'.$msg;
$assertion[0] = 'A'.$msg;
$data[] = 'B'. $msg;
$assertion[0] .= PHP_EOL . 'B'.$msg;
$data[] = 'C'.$msg;
$assertion[0] .= PHP_EOL . 'C'.$msg;
$data[] = 'D'.$msg;
$assertion[0] .= PHP_EOL . 'D'.$msg;
$data[] = 'E'.$msg;
$assertion[0] .= PHP_EOL . 'E'.$msg;

$data[] = 'F'.$msg;
$assertion[1] = 'F'.$msg;
$data[] = 'G'.$msg;
$assertion[1] .= PHP_EOL . 'G'.$msg;
$data[] = 'H'.$msg;
$assertion[1] .= PHP_EOL . 'H'.$msg;
$data[] = 'I'.$msg;
$assertion[1] .= PHP_EOL . 'I'.$msg;
$data[] = 'L'.$msg;
$assertion[1] .= PHP_EOL . 'L'.$msg;

$data[] = 'M'.$msg;
$assertion[2] = 'M'.$msg;
$data[] = 'N'.$msg;
$assertion[2] .= PHP_EOL . 'N'.$msg;

$statsd = $this->mockStatsdClientWithAssertionOnWrite(null);

$reduced = $statsd->reduceCount($data);

$this->assertEquals($assertion, $reduced);
}

}

0 comments on commit cb575a9

Please sign in to comment.