forked from easy-swoole/kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
林以达
committed
Oct 25, 2019
1 parent
4fc52c2
commit da62b49
Showing
11 changed files
with
1,120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.