Skip to content

Commit

Permalink
test protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
林以达 committed Oct 25, 2019
1 parent 4fc52c2 commit da62b49
Show file tree
Hide file tree
Showing 11 changed files with 1,120 additions and 3 deletions.
6 changes: 3 additions & 3 deletions example/DescribeGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

go(function () {
$config = new ConsumerConfig();
$config->setMetadataBrokerList('127.0.0.1:9092');
$config->setBrokerVersion('0.8.2');
$config->setMetadataBrokerList('127.0.0.1:9093');
$config->setBrokerVersion('0.10.2');
$config->setGroupId('test');

EasySwoole\Kafka\Broker::getInstance()->setGroupBrokerId('127.0.0.1:9092');
EasySwoole\Kafka\Broker::getInstance()->setGroupBrokerId('127.0.0.1:9093');

$group = new Group();
$result = $group->describeGroups();
Expand Down
191 changes: 191 additions & 0 deletions test/Protocol/CommitOffsetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php
declare(strict_types=1);

namespace EasySwoole\test\Protocol;

use EasySwoole\Kafka\Protocol\CommitOffset;
use PHPUnit\Framework\TestCase;
use function bin2hex;
use function hex2bin;
use function json_encode;

final class CommitOffsetTest extends TestCase
{
/**
* @var CommitOffset
*/
private $commit;

public function setUp(): void
{
$this->commit = new CommitOffset('0.9.0.1');
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncode(): void
{
$data = [
'group_id' => 'test',
'generation_id' => 2,
'member_id' => 'Easyswoole-kafka-c7e3d40a-57d8-4220-9523-cebfce9a0685',
'retention_time' => 36000,
'data' => [
[
'topic_name' => 'test',
'partitions' => [
[
'partition' => 0,
'offset' => 45,
'metadata' => '',
],
],
],
],
];

$expected = '0000007f000800020000000800104561737973776f6f6c652d6b61666b610004746573740000000200354561737973776f6f6c652d6b61666b612d63376533643430612d353764382d343232302d393532332d6365626663653961303638350000000000008ca0000000010004746573740000000100000000000000000000002d0000';
$test = $this->commit->encode($data);

self::assertSame($expected, bin2hex($test));
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncodeDefault(): void
{
$data = [
'group_id' => 'test',
'data' => [
[
'topic_name' => 'test',
'partitions' => [
[
'partition' => 0,
'offset' => 45,
],
],
],
],
];

$expected = '0000004a000800020000000800104561737973776f6f6c652d6b61666b61000474657374ffffffff0000ffffffffffffffff000000010004746573740000000100000000000000000000002d0000';
$test = $this->commit->encode($data);

self::assertSame($expected, bin2hex($test));
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncodeNoData(): void
{
$data = ['group_id' => 'test'];

$this->commit->encode($data);
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncodeNoGroupId(): void
{
$data = [
'data' => [],
];

$this->commit->encode($data);
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncodeNoTopicName(): void
{
$data = [
'group_id' => 'test',
'data' => [
[],
],
];

$this->commit->encode($data);
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncodeNoPartitions(): void
{
$data = [
'group_id' => 'test',
'data' => [
['topic_name' => 'test'],
],
];

$this->commit->encode($data);
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncodeNoPartition(): void
{
$data = [
'group_id' => 'test',
'data' => [
[
'topic_name' => 'test',
'partitions' => [
[],
],
],
],
];

$this->commit->encode($data);
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncodeNoOffset(): void
{
$data = [
'group_id' => 'test',
'data' => [
[
'topic_name' => 'test',
'partitions' => [
['partition' => 0],
],
],
],
];

$this->commit->encode($data);
}

/**
* @throws \EasySwoole\Kafka\Exception\Exception
*/
public function testDecode(): void
{
$data = '0000000100047465737400000001000000000000';
$expected = '[{"topicName":"test","partitions":[{"partition":0,"errorCode":0}]}]';

$test = $this->commit->decode(hex2bin($data));
self::assertJsonStringEqualsJsonString($expected, json_encode($test));
}
}
57 changes: 57 additions & 0 deletions test/Protocol/DescribeGroupsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

namespace EasySwoole\test\Protocol;

use EasySwoole\Kafka\Protocol\DescribeGroups;
use PHPUnit\Framework\TestCase;
use function bin2hex;
use function hex2bin;
use function json_encode;

final class DescribeGroupsTest extends TestCase
{
/**
* @var DescribeGroups
*/
private $describe;

public function setUp(): void
{
$this->describe = new DescribeGroups('0.9.0.1');
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
*/
public function testEncode(): void
{
$data = ['test'];

$test = $this->describe->encode($data);
self::assertSame('00000024000f00000000000f00104561737973776f6f6c652d6b61666b6100000001000474657374', bin2hex($test));
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
*/
public function testEncodeEmptyArray(): void
{
$data = [];

$test = $this->describe->encode($data);
self::assertSame('0000001e000f00000000000f00104561737973776f6f6c652d6b61666b6100000000', bin2hex($test));
}

/**
* @throws \EasySwoole\Kafka\Exception\Exception
*/
public function testDecode(): void
{
$data = '0000000100000004746573740004446561640000000000000000';
$expected = '[{"errorCode":0,"groupId":"test","state":"Dead","protocolType":"","protocol":"","members":[]}]';

$test = $this->describe->decode(hex2bin($data));
self::assertJsonStringEqualsJsonString($expected, json_encode($test));
}
}
111 changes: 111 additions & 0 deletions test/Protocol/FetchOffsetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);

namespace EasySwoole\test\Protocol;

use EasySwoole\Kafka\Protocol\FetchOffset;
use PHPUnit\Framework\TestCase;
use function bin2hex;
use function hex2bin;
use function json_encode;

final class FetchOffsetTest extends TestCase
{
/**
* @var FetchOffset
*/
private $offset;

public function setUp(): void
{
$this->offset = new FetchOffset('0.9.0.1');
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncode(): void
{
$data = [
'group_id' => 'test',
'data' => [
[
'topic_name' => 'test',
'partitions' => [0],
],
],
];

$expected = '00000032000900010000000900104561737973776f6f6c652d6b61666b61000474657374000000010004746573740000000100000000';
$test = $this->offset->encode($data);

self::assertSame($expected, bin2hex($test));
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncodeNoData(): void
{
$this->offset->encode();
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncodeNoGroupId(): void
{
$data = [
'data' => [],
];

$this->offset->encode($data);
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncodeNoTopicName(): void
{
$data = [
'group_id' => 'test',
'data' => [
[],
],
];

$this->offset->encode($data);
}

/**
* @throws \EasySwoole\Kafka\Exception\NotSupported
* @throws \EasySwoole\Kafka\Exception\Protocol
*/
public function testEncodeNoPartitions(): void
{
$data = [
'group_id' => 'test',
'data' => [
['topic_name' => 'test'],
],
];

$this->offset->encode($data);
}

/**
* @throws \EasySwoole\Kafka\Exception\Exception
*/
public function testDecode(): void
{
$data = '000000010004746573740000000100000000ffffffffffffffff00000000';
$expected = '[{"topicName":"test","partitions":[{"partition":0,"errorCode":0,"metadata":"","offset":-1}]}]';

$test = $this->offset->decode(hex2bin($data));
self::assertJsonStringEqualsJsonString($expected, json_encode($test));
}
}
Loading

0 comments on commit da62b49

Please sign in to comment.