Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(protocol): delete string print in header decode and open test cases. #223

Merged
merged 4 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/protocol/CommandHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,11 @@ void PullMessageRequestHeader::Encode(Json::Value& outData) {
outData["topic"] = topic;
outData["queueId"] = queueId;
outData["queueOffset"] = UtilAll::to_string(queueOffset);
;
outData["maxMsgNums"] = maxMsgNums;
outData["sysFlag"] = sysFlag;
outData["commitOffset"] = UtilAll::to_string(commitOffset);
;
outData["subVersion"] = UtilAll::to_string(subVersion);
;
outData["suspendTimeoutMillis"] = UtilAll::to_string(suspendTimeoutMillis);
;
outData["subscription"] = subscription;
}

Expand Down Expand Up @@ -534,8 +530,8 @@ CommandHeader* ResetOffsetRequestHeader::Decode(Json::Value& ext) {
if (tempValue.isString()) {
h->isForce = UtilAll::to_bool(tempValue.asCString());
}
LOG_INFO("topic:%s, group:%s, timestamp:%lld, isForce:%d,isForce:%s", h->topic.c_str(), h->group.c_str(),
h->timestamp, h->isForce, tempValue.asCString());
LOG_INFO("topic:%s, group:%s, timestamp:%lld, isForce:%d", h->topic.c_str(), h->group.c_str(), h->timestamp,
h->isForce);
return h;
}

Expand Down
2 changes: 1 addition & 1 deletion test/src/message/MQMessageExtTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ TEST(messageExt, init) {
int main(int argc, char* argv[]) {
InitGoogleMock(&argc, argv);

testing::GTEST_FLAG(filter) = "messageExt.init";
testing::GTEST_FLAG(filter) = "messageExt.*";
int itestts = RUN_ALL_TESTS();
return itestts;
}
262 changes: 256 additions & 6 deletions test/src/protocol/CommandHeaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/

#include <memory>
#include <string>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
Expand All @@ -25,11 +24,7 @@
#include "json/writer.h"

#include "CommandHeader.h"
#include "MQClientException.h"
#include "MessageSysFlag.h"
#include "UtilAll.h"
#include "dataBlock.h"
#include "json/json.h"

using std::shared_ptr;

Expand All @@ -40,9 +35,11 @@ using testing::Return;
using Json::FastWriter;
using Json::Value;

using rocketmq::CheckTransactionStateRequestHeader;
using rocketmq::CommandHeader;
using rocketmq::ConsumerSendMsgBackRequestHeader;
using rocketmq::CreateTopicRequestHeader;
using rocketmq::EndTransactionRequestHeader;
using rocketmq::GetConsumerListByGroupRequestHeader;
using rocketmq::GetConsumerListByGroupResponseBody;
using rocketmq::GetConsumerListByGroupResponseHeader;
Expand All @@ -69,7 +66,260 @@ using rocketmq::UnregisterClientRequestHeader;
using rocketmq::UpdateConsumerOffsetRequestHeader;
using rocketmq::ViewMessageRequestHeader;

TEST(commandHeader, ConsumerSendMsgBackRequestHeader) {}
TEST(commandHeader, GetRouteInfoRequestHeader) {
GetRouteInfoRequestHeader header("testTopic");
map<string, string> requestMap;
header.SetDeclaredFieldOfCommandHeader(requestMap);
EXPECT_EQ(requestMap["topic"], "testTopic");

Value outData;
header.Encode(outData);
EXPECT_EQ(outData["topic"], "testTopic");
}

TEST(commandHeader, UnregisterClientRequestHeader) {
UnregisterClientRequestHeader header("testGroup", "testProducer", "testConsumer");
map<string, string> requestMap;
header.SetDeclaredFieldOfCommandHeader(requestMap);
EXPECT_EQ(requestMap["clientID"], "testGroup");
EXPECT_EQ(requestMap["producerGroup"], "testProducer");
EXPECT_EQ(requestMap["consumerGroup"], "testConsumer");

Value outData;
header.Encode(outData);
EXPECT_EQ(outData["clientID"], "testGroup");
EXPECT_EQ(outData["producerGroup"], "testProducer");
EXPECT_EQ(outData["consumerGroup"], "testConsumer");
}

TEST(commandHeader, CreateTopicRequestHeader) {
string topic = "testTopic";
string defaultTopic = "defaultTopic";
int readQueueNums = 4;
int writeQueueNums = 6;
int perm = 8;
string topicFilterType = "filterType";
CreateTopicRequestHeader header;
header.topic = topic;
header.defaultTopic = defaultTopic;
header.readQueueNums = readQueueNums;
header.writeQueueNums = writeQueueNums;
header.perm = perm;
header.topicFilterType = topicFilterType;
map<string, string> requestMap;
header.SetDeclaredFieldOfCommandHeader(requestMap);
EXPECT_EQ(requestMap["topic"], topic);
EXPECT_EQ(requestMap["defaultTopic"], defaultTopic);
EXPECT_EQ(requestMap["readQueueNums"], "4");
EXPECT_EQ(requestMap["writeQueueNums"], "6");
EXPECT_EQ(requestMap["perm"], "8");
EXPECT_EQ(requestMap["topicFilterType"], topicFilterType);

Value outData;
header.Encode(outData);
EXPECT_EQ(outData["topic"], topic);
EXPECT_EQ(outData["defaultTopic"], defaultTopic);
EXPECT_EQ(outData["readQueueNums"], readQueueNums);
EXPECT_EQ(outData["writeQueueNums"], writeQueueNums);
EXPECT_EQ(outData["perm"], perm);
EXPECT_EQ(outData["topicFilterType"], topicFilterType);
}

TEST(commandHeader, CheckTransactionStateRequestHeader) {
CheckTransactionStateRequestHeader header(2000, 1000, "ABC", "DEF", "GHI");
map<string, string> requestMap;
header.SetDeclaredFieldOfCommandHeader(requestMap);
EXPECT_EQ(requestMap["msgId"], "ABC");
EXPECT_EQ(requestMap["transactionId"], "DEF");
EXPECT_EQ(requestMap["offsetMsgId"], "GHI");
EXPECT_EQ(requestMap["commitLogOffset"], "1000");
EXPECT_EQ(requestMap["tranStateTableOffset"], "2000");

Value value;
value["msgId"] = "ABC";
value["transactionId"] = "DEF";
value["offsetMsgId"] = "GHI";
value["commitLogOffset"] = "1000";
value["tranStateTableOffset"] = "2000";
shared_ptr<CheckTransactionStateRequestHeader> headerDecode(
static_cast<CheckTransactionStateRequestHeader*>(CheckTransactionStateRequestHeader::Decode(value)));
EXPECT_EQ(headerDecode->m_msgId, "ABC");
EXPECT_EQ(headerDecode->m_commitLogOffset, 1000);
EXPECT_EQ(headerDecode->m_tranStateTableOffset, 2000);
EXPECT_EQ(headerDecode->toString(), header.toString());
}

TEST(commandHeader, EndTransactionRequestHeader) {
EndTransactionRequestHeader header("testProducer", 1000, 2000, 3000, true, "ABC", "DEF");
map<string, string> requestMap;
header.SetDeclaredFieldOfCommandHeader(requestMap);
EXPECT_EQ(requestMap["msgId"], "ABC");
EXPECT_EQ(requestMap["transactionId"], "DEF");
EXPECT_EQ(requestMap["producerGroup"], "testProducer");
EXPECT_EQ(requestMap["tranStateTableOffset"], "1000");
EXPECT_EQ(requestMap["commitLogOffset"], "2000");
EXPECT_EQ(requestMap["commitOrRollback"], "3000");
EXPECT_EQ(requestMap["fromTransactionCheck"], "1");

Value outData;
header.Encode(outData);
EXPECT_EQ(outData["msgId"], "ABC");
EXPECT_EQ(outData["transactionId"], "DEF");
EXPECT_EQ(outData["producerGroup"], "testProducer");
EXPECT_EQ(outData["tranStateTableOffset"], "1000");
EXPECT_EQ(outData["commitLogOffset"], "2000");
EXPECT_EQ(outData["commitOrRollback"], "3000");
EXPECT_EQ(outData["fromTransactionCheck"], "1");

EXPECT_NO_THROW(header.toString());
}

TEST(commandHeader, SendMessageRequestHeader) {
string producerGroup = "testProducer";
string topic = "testTopic";
string defaultTopic = "defaultTopic";
int defaultTopicQueueNums = 1;
int queueId = 2;
int sysFlag = 3;
int64 bornTimestamp = 4;
int flag = 5;
string properties = "testProperty";
int reconsumeTimes = 6;
bool unitMode = true;
bool batch = false;

SendMessageRequestHeader header;
header.producerGroup = producerGroup;
header.topic = topic;
header.defaultTopic = defaultTopic;
header.defaultTopicQueueNums = defaultTopicQueueNums;
header.queueId = queueId;
header.sysFlag = sysFlag;
header.bornTimestamp = bornTimestamp;
header.flag = flag;
header.properties = properties;
header.reconsumeTimes = reconsumeTimes;
header.unitMode = unitMode;
header.batch = batch;
header.setReconsumeTimes(reconsumeTimes);
EXPECT_EQ(header.getReconsumeTimes(), reconsumeTimes);
map<string, string> requestMap;
header.SetDeclaredFieldOfCommandHeader(requestMap);
EXPECT_EQ(requestMap["topic"], topic);
EXPECT_EQ(requestMap["producerGroup"], producerGroup);
EXPECT_EQ(requestMap["defaultTopic"], defaultTopic);
EXPECT_EQ(requestMap["defaultTopicQueueNums"], "1");
EXPECT_EQ(requestMap["queueId"], "2");
EXPECT_EQ(requestMap["sysFlag"], "3");
EXPECT_EQ(requestMap["bornTimestamp"], "4");
EXPECT_EQ(requestMap["flag"], "5");
EXPECT_EQ(requestMap["properties"], properties);
EXPECT_EQ(requestMap["reconsumeTimes"], "6");
EXPECT_EQ(requestMap["unitMode"], "1");
EXPECT_EQ(requestMap["batch"], "0");

Value outData;
header.Encode(outData);
EXPECT_EQ(outData["topic"], topic);
EXPECT_EQ(outData["producerGroup"], producerGroup);
EXPECT_EQ(outData["defaultTopic"], defaultTopic);
EXPECT_EQ(outData["defaultTopicQueueNums"], defaultTopicQueueNums);
EXPECT_EQ(outData["queueId"], queueId);
EXPECT_EQ(outData["sysFlag"], sysFlag);
EXPECT_EQ(outData["bornTimestamp"], "4");
EXPECT_EQ(outData["flag"], flag);
EXPECT_EQ(outData["properties"], properties);
EXPECT_EQ(outData["reconsumeTimes"], "6");
EXPECT_EQ(outData["unitMode"], "1");
EXPECT_EQ(outData["batch"], "0");
}

TEST(commandHeader, SendMessageResponseHeader) {
SendMessageResponseHeader header;
header.msgId = "ABCDEFG";
header.queueId = 1;
header.queueOffset = 2;
map<string, string> requestMap;
header.SetDeclaredFieldOfCommandHeader(requestMap);
EXPECT_EQ(requestMap["msgId"], "ABCDEFG");
EXPECT_EQ(requestMap["queueId"], "1");
EXPECT_EQ(requestMap["queueOffset"], "2");

Value value;
value["msgId"] = "EFGHIJK";
value["queueId"] = "3";
value["queueOffset"] = "4";
shared_ptr<SendMessageResponseHeader> headerDecode(
static_cast<SendMessageResponseHeader*>(SendMessageResponseHeader::Decode(value)));
EXPECT_EQ(headerDecode->msgId, "EFGHIJK");
EXPECT_EQ(headerDecode->queueId, 3);
EXPECT_EQ(headerDecode->queueOffset, 4);
}

TEST(commandHeader, PullMessageRequestHeader) {
PullMessageRequestHeader header;
header.consumerGroup = "testConsumer";
header.topic = "testTopic";
header.queueId = 1;
header.maxMsgNums = 2;
header.sysFlag = 3;
header.subscription = "testSub";
header.queueOffset = 4;
header.commitOffset = 5;
header.suspendTimeoutMillis = 6;
header.subVersion = 7;
map<string, string> requestMap;
header.SetDeclaredFieldOfCommandHeader(requestMap);
EXPECT_EQ(requestMap["consumerGroup"], "testConsumer");
EXPECT_EQ(requestMap["topic"], "testTopic");
EXPECT_EQ(requestMap["queueId"], "1");
EXPECT_EQ(requestMap["maxMsgNums"], "2");
EXPECT_EQ(requestMap["sysFlag"], "3");
EXPECT_EQ(requestMap["subscription"], "testSub");
EXPECT_EQ(requestMap["queueOffset"], "4");
EXPECT_EQ(requestMap["commitOffset"], "5");
EXPECT_EQ(requestMap["suspendTimeoutMillis"], "6");
EXPECT_EQ(requestMap["subVersion"], "7");

Value outData;
header.Encode(outData);
EXPECT_EQ(outData["consumerGroup"], "testConsumer");
EXPECT_EQ(outData["topic"], "testTopic");
EXPECT_EQ(outData["queueId"], 1);
EXPECT_EQ(outData["maxMsgNums"], 2);
EXPECT_EQ(outData["sysFlag"], 3);
EXPECT_EQ(outData["subscription"], "testSub");
EXPECT_EQ(outData["queueOffset"], "4");
EXPECT_EQ(outData["commitOffset"], "5");
EXPECT_EQ(outData["suspendTimeoutMillis"], "6");
EXPECT_EQ(outData["subVersion"], "7");
}

TEST(commandHeader, PullMessageResponseHeader) {
PullMessageResponseHeader header;
header.suggestWhichBrokerId = 100;
header.nextBeginOffset = 200;
header.minOffset = 3000;
header.maxOffset = 5000;
map<string, string> requestMap;
header.SetDeclaredFieldOfCommandHeader(requestMap);
EXPECT_EQ(requestMap["suggestWhichBrokerId"], "100");
EXPECT_EQ(requestMap["nextBeginOffset"], "200");
EXPECT_EQ(requestMap["minOffset"], "3000");
EXPECT_EQ(requestMap["maxOffset"], "5000");

Value value;
value["suggestWhichBrokerId"] = "5";
value["nextBeginOffset"] = "102400";
value["minOffset"] = "1";
value["maxOffset"] = "123456789";
shared_ptr<PullMessageResponseHeader> headerDecode(
static_cast<PullMessageResponseHeader*>(PullMessageResponseHeader::Decode(value)));
EXPECT_EQ(headerDecode->suggestWhichBrokerId, 5);
EXPECT_EQ(headerDecode->nextBeginOffset, 102400);
EXPECT_EQ(headerDecode->minOffset, 1);
EXPECT_EQ(headerDecode->maxOffset, 123456789);
}

TEST(commandHeader, GetConsumerListByGroupResponseBody) {
Value value;
Expand Down
Loading