From da62b49f0bb528aef461b141bc524707c79e9508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=BB=A5=E8=BE=BE?= <> Date: Fri, 25 Oct 2019 13:56:20 +0800 Subject: [PATCH] test protocol --- example/DescribeGroups.php | 6 +- test/Protocol/CommitOffsetTest.php | 191 +++++++++++++++++++++++ test/Protocol/DescribeGroupsTest.php | 57 +++++++ test/Protocol/FetchOffsetTest.php | 111 +++++++++++++ test/Protocol/GroupCoordinatorTest.php | 57 +++++++ test/Protocol/JoinGroupTest.php | 198 ++++++++++++++++++++++++ test/Protocol/LeaveGroupTest.php | 71 +++++++++ test/Protocol/ListGroupTest.php | 44 ++++++ test/Protocol/OffsetTest.php | 120 ++++++++++++++ test/Protocol/SaslHandShakeTest.php | 62 ++++++++ test/Protocol/SyncGroupTest.php | 206 +++++++++++++++++++++++++ 11 files changed, 1120 insertions(+), 3 deletions(-) create mode 100644 test/Protocol/CommitOffsetTest.php create mode 100644 test/Protocol/DescribeGroupsTest.php create mode 100644 test/Protocol/FetchOffsetTest.php create mode 100644 test/Protocol/GroupCoordinatorTest.php create mode 100644 test/Protocol/JoinGroupTest.php create mode 100644 test/Protocol/LeaveGroupTest.php create mode 100644 test/Protocol/ListGroupTest.php create mode 100644 test/Protocol/OffsetTest.php create mode 100644 test/Protocol/SaslHandShakeTest.php create mode 100644 test/Protocol/SyncGroupTest.php diff --git a/example/DescribeGroups.php b/example/DescribeGroups.php index 73d1360..1ec58a3 100644 --- a/example/DescribeGroups.php +++ b/example/DescribeGroups.php @@ -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(); diff --git a/test/Protocol/CommitOffsetTest.php b/test/Protocol/CommitOffsetTest.php new file mode 100644 index 0000000..4c776ba --- /dev/null +++ b/test/Protocol/CommitOffsetTest.php @@ -0,0 +1,191 @@ +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)); + } +} diff --git a/test/Protocol/DescribeGroupsTest.php b/test/Protocol/DescribeGroupsTest.php new file mode 100644 index 0000000..5710e01 --- /dev/null +++ b/test/Protocol/DescribeGroupsTest.php @@ -0,0 +1,57 @@ +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)); + } +} diff --git a/test/Protocol/FetchOffsetTest.php b/test/Protocol/FetchOffsetTest.php new file mode 100644 index 0000000..2954085 --- /dev/null +++ b/test/Protocol/FetchOffsetTest.php @@ -0,0 +1,111 @@ +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)); + } +} diff --git a/test/Protocol/GroupCoordinatorTest.php b/test/Protocol/GroupCoordinatorTest.php new file mode 100644 index 0000000..ffe4be3 --- /dev/null +++ b/test/Protocol/GroupCoordinatorTest.php @@ -0,0 +1,57 @@ +group = new GroupCoordinator('0.9.0.1'); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncode(): void + { + $data = ['group_id' => 'test']; + + $test = $this->group->encode($data); + self::assertSame('00000020000a00000000000a00104561737973776f6f6c652d6b61666b61000474657374', bin2hex($test)); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoGroupId(): void + { + $this->group->encode(); + } + + /** + * @throws \EasySwoole\Kafka\Exception\Exception + */ + public function testDecode(): void + { + $data = '000000000003000b31302e31332e342e313539000023e8'; + $expected = '{"errorCode":0,"nodeId":3,"coordinatorHost":"10.13.4.159","coordinatorPort":9192}'; + + $test = $this->group->decode(hex2bin($data)); + + self::assertJsonStringEqualsJsonString($expected, json_encode($test)); + } +} diff --git a/test/Protocol/JoinGroupTest.php b/test/Protocol/JoinGroupTest.php new file mode 100644 index 0000000..620efb2 --- /dev/null +++ b/test/Protocol/JoinGroupTest.php @@ -0,0 +1,198 @@ +group = new JoinGroup('0.9.0.1'); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncode(): void + { + $data = [ + 'group_id' => 'test', + 'session_timeout' => 6000, + 'member_id' => '', + 'data' => [ + [ + 'protocol_name' => 'group', + 'version' => 0, + 'subscription' => ['test'], + ], + ], + ]; + + $expected = '0000004f000b00000000000b00104561737973776f6f6c652d6b61666b610004746573740000177000000008636f6e73756d657200000001000567726f75700000001000000000000100047465737400000000'; + $test9 = $this->group->encode($data); + + self::assertSame($expected, bin2hex($test9)); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoGroupId(): void + { + $this->group->encode(); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoSessionTimeout(): void + { + $data = ['group_id' => 'test']; + + $this->group->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoMemberId(): void + { + $data = [ + 'group_id' => 'test', + 'session_timeout' => 6000, + ]; + + $test = $this->group->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoData(): void + { + $data = [ + 'group_id' => 'test', + 'session_timeout' => 6000, + 'member_id' => '', + ]; + + $this->group->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeHasProtocolType(): void + { + $data = [ + 'group_id' => 'test', + 'session_timeout' => 6000, + 'rebalance_timeout' => 6000, + 'member_id' => '', + 'protocol_type' => 'testtype', + 'data' => [ + [ + 'protocol_name' => 'group', + 'version' => 0, + 'subscription' => ['test'], + 'user_data' => '', + ], + ], + ]; + + $expected = '0000004f000b00000000000b00104561737973776f6f6c652d6b61666b610004746573740000177000000008746573747479706500000001000567726f75700000001000000000000100047465737400000000'; + $test = $this->group->encode($data); + + self::assertSame($expected, bin2hex($test)); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoProtocolName(): void + { + $data = [ + 'group_id' => 'test', + 'session_timeout' => 6000, + 'rebalance_timeout' => 6000, + 'member_id' => '', + 'data' => [ + [], + ], + ]; + + $this->group->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoVersion(): void + { + $data = [ + 'group_id' => 'test', + 'session_timeout' => 6000, + 'rebalance_timeout' => 6000, + 'member_id' => '', + 'data' => [ + ['protocol_name' => 'group'], + ], + ]; + + $test = $this->group->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoSubscription(): void + { + $data = [ + 'group_id' => 'test', + 'session_timeout' => 6000, + 'rebalance_timeout' => 6000, + 'member_id' => '', + 'data' => [ + [ + 'protocol_name' => 'group', + 'version' => 0, + ], + ], + ]; + + $this->group->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\Exception + */ + public function testDecode(): void + { + $data = '000000000001000567726f7570002e6b61666b612d7068702d31313433353333332d663663342d346663622d616532642d396134393335613934663366002e6b61666b612d7068702d31313433353333332d663663342d346663622d616532642d39613439333561393466336600000001002e6b61666b612d7068702d31313433353333332d663663342d346663622d616532642d3961343933356139346633660000001000000000000100047465737400000000'; + $expected = '{"errorCode":0,"generationId":1,"groupProtocol":"group","leaderId":"kafka-php-11435333-f6c4-4fcb-ae2d-9a4935a94f3f","memberId":"kafka-php-11435333-f6c4-4fcb-ae2d-9a4935a94f3f","members":[{"memberId":"kafka-php-11435333-f6c4-4fcb-ae2d-9a4935a94f3f","memberMeta":{"version":0,"topics":["test"],"userData":""}}]}'; + + $test = $this->group->decode(hex2bin($data)); + + self::assertJsonStringEqualsJsonString($expected, json_encode($test)); + } +} diff --git a/test/Protocol/LeaveGroupTest.php b/test/Protocol/LeaveGroupTest.php new file mode 100644 index 0000000..310d9f6 --- /dev/null +++ b/test/Protocol/LeaveGroupTest.php @@ -0,0 +1,71 @@ +leave = new LeaveGroup('0.9.0.1'); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncode(): void + { + $data = [ + 'group_id' => 'test', + 'member_id' => 'Easyswoole-kafka-eb19c0ea-4b3e-4ed0-bada-c873951c8eea', + ]; + + $expected = '00000057000d00000000000d00104561737973776f6f6c652d6b61666b6100047465737400354561737973776f6f6c652d6b61666b612d65623139633065612d346233652d346564302d626164612d633837333935316338656561'; + $test = $this->leave->encode($data); + + self::assertSame($expected, bin2hex($test)); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoGroupId(): void + { + $this->leave->encode(); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoMemberId(): void + { + $data = ['group_id' => 'test']; + + $this->leave->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\Exception + */ + public function testDecode(): void + { + $test = $this->leave->decode(hex2bin('0000')); + $expected = '{"errorCode":0}'; + + self::assertJsonStringEqualsJsonString($expected, json_encode($test)); + } +} diff --git a/test/Protocol/ListGroupTest.php b/test/Protocol/ListGroupTest.php new file mode 100644 index 0000000..4c40921 --- /dev/null +++ b/test/Protocol/ListGroupTest.php @@ -0,0 +1,44 @@ +list = new ListGroup('0.9.0.1'); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + */ + public function testEncode(): void + { + $test = $this->list->encode(); + + self::assertSame('0000001a001000000000001000104561737973776f6f6c652d6b61666b61', bin2hex($test)); + } + + /** + * @throws \EasySwoole\Kafka\Exception\Exception + */ + public function testDecode(): void + { + $test = $this->list->decode(hex2bin('0000000000010004746573740008636f6e73756d6572')); + $expected = '{"errorCode":0,"groups":[{"groupId":"test","protocolType":"consumer"}]}'; + + self::assertJsonStringEqualsJsonString($expected, json_encode($test)); + } +} diff --git a/test/Protocol/OffsetTest.php b/test/Protocol/OffsetTest.php new file mode 100644 index 0000000..8670e66 --- /dev/null +++ b/test/Protocol/OffsetTest.php @@ -0,0 +1,120 @@ +offset = new Offset('0.9.0.1'); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncode(): void + { + $data = [ + 'data' => [ + [ + 'topic_name' => 'test', + 'partitions' => [ + [ + 'partition_id' => 0, + 'offset' => 100, + ], + ], + ], + ], + ]; + + $expected = '0000003c000200000000000200104561737973776f6f6c652d6b61666b61ffffffff000000010004746573740000000100000000ffffffffffffffff000186a0'; + + self::assertSame($expected, bin2hex($this->offset->encode($data))); + } + + /** + * @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 testEncodeNoTopicName(): void + { + $data = [ + 'data' => [ + [], + ], + ]; + + $this->offset->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoPartitions(): void + { + $data = [ + 'data' => [ + ['topic_name' => 'test'], + ], + ]; + + $this->offset->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoPartitionId(): void + { + $data = [ + 'data' => [ + [ + 'topic_name' => 'test', + 'partitions' => [ + [], + ], + ], + ], + ]; + + $this->offset->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\Exception + */ + public function testDecode(): void + { + $data = '000000010004746573740000000100000000000000000001000000000000002a'; + $expected = '[{"topicName":"test","partitions":[{"partition":0,"errorCode":0,"timestamp":0,"offsets":[42]}]}]'; + + $test = $this->offset->decode(hex2bin($data)); + + self::assertJsonStringEqualsJsonString($expected, json_encode($test)); + } +} diff --git a/test/Protocol/SaslHandShakeTest.php b/test/Protocol/SaslHandShakeTest.php new file mode 100644 index 0000000..effe20f --- /dev/null +++ b/test/Protocol/SaslHandShakeTest.php @@ -0,0 +1,62 @@ +sasl = new SaslHandShake('0.10.0.0'); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncode(): void + { + $test = $this->sasl->encode(['PLAIN']); + + self::assertSame('00000021001100000000001100104561737973776f6f6c652d6b61666b610005504c41494e', bin2hex($test)); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoMechanismGiven(): void + { + $this->sasl->encode(); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeInvalidMechanism(): void + { + $this->sasl->encode(['NOTALLOW']); + } + + /** + * @throws \EasySwoole\Kafka\Exception\Exception + */ + public function testDecode(): void + { + $data = '0022000000010006475353415049'; + $expected = '{"mechanisms":["GSSAPI"],"errorCode":34}'; + self::assertJsonStringEqualsJsonString($expected, json_encode($this->sasl->decode(hex2bin($data)))); + } +} diff --git a/test/Protocol/SyncGroupTest.php b/test/Protocol/SyncGroupTest.php new file mode 100644 index 0000000..18da89a --- /dev/null +++ b/test/Protocol/SyncGroupTest.php @@ -0,0 +1,206 @@ +sync = new SyncGroup('0.9.0.1'); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncode(): void + { + $data = json_decode( + '{"group_id":"test","generation_id":1,"member_id":"Easyswoole-kafka-bd5d5bb2-2a1f-43d4-b831-b1510d81ac5c","data":[{"version":0,"member_id":"Easyswoole-kafka-bd5d5bb2-2a1f-43d4-b831-b1510d81ac5c","assignments":[{"topic_name":"test","partitions":[0]}]}]}', + true + ); + + $expected = '000000b2000e00000000000e00104561737973776f6f6c652d6b61666b610004746573740000000100354561737973776f6f6c652d6b61666b612d62643564356262322d326131662d343364342d623833312d6231353130643831616335630000000100354561737973776f6f6c652d6b61666b612d62643564356262322d326131662d343364342d623833312d62313531306438316163356300000018000000000001000474657374000000010000000000000000'; + + self::assertSame($expected, bin2hex($this->sync->encode($data))); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoGroupId(): void + { + $this->sync->encode(); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoGenerationId(): void + { + $data = ['group_id' => 'test']; + + $this->sync->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoMemberId(): void + { + $data = [ + 'group_id' => 'test', + 'generation_id' => '1', + ]; + + $this->sync->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoData(): void + { + $data = [ + 'group_id' => 'test', + 'generation_id' => '1', + 'member_id' => 'Easyswoole-kafka-bd5d5bb2-2a1f-43d4-b831-b1510d81ac5c', + ]; + + $this->sync->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoVersion(): void + { + $data = [ + 'group_id' => 'test', + 'generation_id' => '1', + 'member_id' => 'Easyswoole-kafka-bd5d5bb2-2a1f-43d4-b831-b1510d81ac5c', + 'data' => [ + [], + ], + ]; + + $this->sync->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoDataMemberId(): void + { + $data = [ + 'group_id' => 'test', + 'generation_id' => '1', + 'member_id' => 'Easyswoole-kafka-bd5d5bb2-2a1f-43d4-b831-b1510d81ac5c', + 'data' => [ + ['version' => 0], + ], + ]; + + $this->sync->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoDataAssignments(): void + { + $data = [ + 'group_id' => 'test', + 'generation_id' => '1', + 'member_id' => 'Easyswoole-kafka-bd5d5bb2-2a1f-43d4-b831-b1510d81ac5c', + 'data' => [ + [ + 'version' => 0 , + 'member_id' => 'Easyswoole-kafka-bd5d5bb2-2a1f-43d4-b831-b1510d81ac5c', + ], + ], + ]; + + $this->sync->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoTopicName(): void + { + $data = [ + 'group_id' => 'test', + 'generation_id' => '1', + 'member_id' => 'Easyswoole-kafka-bd5d5bb2-2a1f-43d4-b831-b1510d81ac5c', + 'data' => [ + [ + 'version' => 0 , + 'member_id' => 'Easyswoole-kafka-bd5d5bb2-2a1f-43d4-b831-b1510d81ac5c', + 'assignments' => [ + [], + ], + ], + ], + ]; + + $this->sync->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\NotSupported + * @throws \EasySwoole\Kafka\Exception\Protocol + */ + public function testEncodeNoPartitions(): void + { + $data = [ + 'group_id' => 'test', + 'generation_id' => '1', + 'member_id' => 'Easyswoole-kafka-bd5d5bb2-2a1f-43d4-b831-b1510d81ac5c', + 'data' => [ + [ + 'version' => 0 , + 'member_id' => 'Easyswoole-kafka-bd5d5bb2-2a1f-43d4-b831-b1510d81ac5c', + 'assignments' => [ + ['topic_name' => 'test'], + ], + ], + ], + ]; + + $this->sync->encode($data); + } + + /** + * @throws \EasySwoole\Kafka\Exception\Exception + */ + public function testDecode(): void + { + $data = '000000000018000000000001000474657374000000010000000000000000'; + $expected = '{"errorCode":0,"partitionAssignments":[{"topicName":"test","partitions":[0]}],"version":0,"userData":""}'; + + self::assertJsonStringEqualsJsonString($expected, json_encode($this->sync->decode(hex2bin($data)))); + self::assertJsonStringEqualsJsonString('{"errorCode":0}', json_encode($this->sync->decode(hex2bin('000000000000')))); + } +}