This is an automated email from the ASF dual-hosted git repository.
dinglei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-cpp.git
The following commit(s) were added to refs/heads/master by this push:
new 1a7f8b2 fix(unittest): open some test cases (#218)
1a7f8b2 is described below
commit 1a7f8b2013552cd4a5013402d33e14c4fd360852
Author: dinglei <libya_003@163.com>
AuthorDate: Tue Jan 7 17:22:22 2020 +0800
fix(unittest): open some test cases (#218)
* fix(unittest): open test case for message.
* fix(unittest): open test case for message decode.
* fix(unittest): open test case for client remoting processor
* fix(unittest): open test case for client manager and move path for common
---
src/message/MQDecoder.cpp | 23 +++++++++++++---------
...ringIdMakerTest.cpp => MQClientManagerTest.cpp} | 21 ++++++++++++++------
test/src/{ => common}/UrlTest.cpp | 0
test/src/{ => message}/BatchMessageTest.cpp | 0
test/src/message/MQDecoderTest.cpp | 17 ++++++++--------
test/src/message/MQMessageTest.cpp | 12 ++++++-----
test/src/{ => message}/StringIdMakerTest.cpp | 0
test/src/transport/ClientRemotingProcessorTest.cpp | 5 ++++-
8 files changed, 49 insertions(+), 29 deletions(-)
diff --git a/src/message/MQDecoder.cpp b/src/message/MQDecoder.cpp
index 9e60c4d..1ba8a95 100644
--- a/src/message/MQDecoder.cpp
+++ b/src/message/MQDecoder.cpp
@@ -39,12 +39,12 @@ int MQDecoder::MessageStoreTimestampPostion = 56;
//<!***************************************************************************
string MQDecoder::createMessageId(sockaddr addr, int64 offset) {
- struct sockaddr_in* sa = (struct sockaddr_in*)&addr;
+ int host, port;
+ socketAddress2IPPort(addr, host, port);
MemoryOutputStream outputmen(MSG_ID_LENGTH);
- outputmen.writeIntBigEndian(sa->sin_addr.s_addr);
- outputmen.writeRepeatedByte(0, 2);
- outputmen.write(&(sa->sin_port), 2);
+ outputmen.writeIntBigEndian(host);
+ outputmen.writeIntBigEndian(port);
outputmen.writeInt64BigEndian(offset);
const char* bytes = static_cast<const char*>(outputmen.getData());
@@ -56,20 +56,25 @@ string MQDecoder::createMessageId(sockaddr addr, int64 offset) {
MQMessageId MQDecoder::decodeMessageId(const string& msgId) {
string ipStr = msgId.substr(0, 8);
string portStr = msgId.substr(8, 8);
- string offsetStr = msgId.substr(16);
+ string offsetStr = msgId.substr(16, 16);
+
+ int num = strspn(offsetStr.c_str(), "F");
+ offsetStr = offsetStr.substr(num, 16 - num);
char* end;
int ipInt = strtoul(ipStr.c_str(), &end, 16);
int portInt = strtoul(portStr.c_str(), &end, 16);
-
- int64 offset = UtilAll::hexstr2ull(offsetStr.c_str());
+ uint64 offset = strtoul(offsetStr.c_str(), &end, 16);
+ // int64 offset = UtilAll::hexstr2ull(offsetStr.c_str());
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(portInt);
sa.sin_addr.s_addr = htonl(ipInt);
+ sockaddr addr;
+ memcpy(&addr, &sa, sizeof(sockaddr));
- MQMessageId id(*((sockaddr*)&sa), offset);
+ MQMessageId id(addr, offset);
return id;
}
@@ -242,4 +247,4 @@ void MQDecoder::string2messageProperties(const string& propertiesString,
map<str
}
}
}
-} //<!end namespace;
+} // namespace rocketmq
diff --git a/test/src/StringIdMakerTest.cpp b/test/src/MQClientManagerTest.cpp
similarity index 61%
copy from test/src/StringIdMakerTest.cpp
copy to test/src/MQClientManagerTest.cpp
index ebe5897..830be46 100644
--- a/test/src/StringIdMakerTest.cpp
+++ b/test/src/MQClientManagerTest.cpp
@@ -20,20 +20,29 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
-#include "StringIdMaker.h"
+#include "MQClientManager.h"
using namespace std;
using namespace rocketmq;
+using rocketmq::MQClientFactory;
+using rocketmq::MQClientManager;
using ::testing::InitGoogleMock;
using ::testing::InitGoogleTest;
using testing::Return;
-TEST(StringIdMakerTest, get_unique_id) {
- string unique_id = StringIdMaker::getInstance().createUniqID();
- cout << "unique_id: " << unique_id << endl;
- EXPECT_EQ(unique_id.size(), 32);
-}
+TEST(MQClientManagerTest, getClientFactory) {
+ string clientId = "testClientId";
+ string unitName = "central";
+ MQClientFactory* factory = MQClientManager::getInstance()->getMQClientFactory(clientId,
1, 1000, 3000, unitName);
+ MQClientFactory* factory2 = MQClientManager::getInstance()->getMQClientFactory(clientId,
1, 1000, 3000, unitName);
+ EXPECT_EQ(factory, factory2);
+ MQClientManager::getInstance()->removeClientFactory(clientId);
+}
+TEST(MQClientManagerTest, removeClientFactory) {
+ string clientId = "testClientId";
+ MQClientManager::getInstance()->removeClientFactory(clientId);
+}
int main(int argc, char* argv[]) {
InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
diff --git a/test/src/UrlTest.cpp b/test/src/common/UrlTest.cpp
similarity index 100%
rename from test/src/UrlTest.cpp
rename to test/src/common/UrlTest.cpp
diff --git a/test/src/BatchMessageTest.cpp b/test/src/message/BatchMessageTest.cpp
similarity index 100%
rename from test/src/BatchMessageTest.cpp
rename to test/src/message/BatchMessageTest.cpp
diff --git a/test/src/message/MQDecoderTest.cpp b/test/src/message/MQDecoderTest.cpp
index 748d89c..a92ba10 100644
--- a/test/src/message/MQDecoderTest.cpp
+++ b/test/src/message/MQDecoderTest.cpp
@@ -52,17 +52,18 @@ using rocketmq::SendMessageRequestHeader;
using rocketmq::UtilAll;
// TODO
-TEST(decoders, messageId) {
+TEST(decoder, messageId) {
int host;
int port;
+ int64 offset = 1234567890;
string msgIdStr =
- MQDecoder::createMessageId(rocketmq::IPPort2socketAddress(inet_addr("127.0.0.1"), 10091),
(int64)1024);
+ MQDecoder::createMessageId(rocketmq::IPPort2socketAddress(ntohl(inet_addr("127.0.0.1")),
10091), offset);
MQMessageId msgId = MQDecoder::decodeMessageId(msgIdStr);
- EXPECT_EQ(msgId.getOffset(), 1024);
+ EXPECT_EQ(msgId.getOffset(), offset);
rocketmq::socketAddress2IPPort(msgId.getAddress(), host, port);
- EXPECT_EQ(host, inet_addr("127.0.0.1"));
+ EXPECT_EQ(host, ntohl(inet_addr("127.0.0.1")));
EXPECT_EQ(port, 10091);
}
@@ -99,16 +100,16 @@ TEST(decoder, decoder) {
memoryOut->writeInt64BigEndian((int64)4096);
mext.setBornTimestamp(4096);
// 10 BORNHOST 56= 48+8
- memoryOut->writeIntBigEndian(inet_addr("127.0.0.1"));
+ memoryOut->writeIntBigEndian(ntohl(inet_addr("127.0.0.1")));
memoryOut->writeIntBigEndian(10091);
- mext.setBornHost(rocketmq::IPPort2socketAddress(inet_addr("127.0.0.1"), 10091));
+ mext.setBornHost(rocketmq::IPPort2socketAddress(ntohl(inet_addr("127.0.0.1")), 10091));
// 11 STORETIMESTAMP 64 =56+8
memoryOut->writeInt64BigEndian((int64)4096);
mext.setStoreTimestamp(4096);
// 12 STOREHOST 72 = 64+8
- memoryOut->writeIntBigEndian(inet_addr("127.0.0.2"));
+ memoryOut->writeIntBigEndian(ntohl(inet_addr("127.0.0.2")));
memoryOut->writeIntBigEndian(10092);
- mext.setStoreHost(rocketmq::IPPort2socketAddress(inet_addr("127.0.0.2"), 10092));
+ mext.setStoreHost(rocketmq::IPPort2socketAddress(ntohl(inet_addr("127.0.0.2")), 10092));
// 13 RECONSUMETIMES 76 = 72+4
mext.setReconsumeTimes(111111);
memoryOut->writeIntBigEndian(mext.getReconsumeTimes());
diff --git a/test/src/message/MQMessageTest.cpp b/test/src/message/MQMessageTest.cpp
index 6c0165a..100e73e 100644
--- a/test/src/message/MQMessageTest.cpp
+++ b/test/src/message/MQMessageTest.cpp
@@ -78,16 +78,18 @@ TEST(message, info) {
EXPECT_EQ(message.getTopic(), "");
message.setTopic("testTopic");
EXPECT_EQ(message.getTopic(), "testTopic");
- char* topic = "testTopic";
- message.setTopic(topic, 5);
+ string topic = "testTopic";
+ const char* ctopic = topic.c_str();
+ message.setTopic(ctopic, 5);
EXPECT_EQ(message.getTopic(), "testT");
EXPECT_EQ(message.getBody(), "");
message.setBody("testBody");
EXPECT_EQ(message.getBody(), "testBody");
- char* body = "testBody";
- message.setBody(body, 5);
+ string body = "testBody";
+ const char* b = body.c_str();
+ message.setBody(b, 5);
EXPECT_EQ(message.getBody(), "testB");
string tags(message.getTags());
@@ -146,7 +148,7 @@ TEST(message, properties) {
int main(int argc, char* argv[]) {
InitGoogleMock(&argc, argv);
- testing::GTEST_FLAG(filter) = "message.info";
+ // testing::GTEST_FLAG(filter) = "message.info";
int itestts = RUN_ALL_TESTS();
return itestts;
}
diff --git a/test/src/StringIdMakerTest.cpp b/test/src/message/StringIdMakerTest.cpp
similarity index 100%
rename from test/src/StringIdMakerTest.cpp
rename to test/src/message/StringIdMakerTest.cpp
diff --git a/test/src/transport/ClientRemotingProcessorTest.cpp b/test/src/transport/ClientRemotingProcessorTest.cpp
index 5c906eb..3664d89 100644
--- a/test/src/transport/ClientRemotingProcessorTest.cpp
+++ b/test/src/transport/ClientRemotingProcessorTest.cpp
@@ -164,8 +164,9 @@ TEST(clientRemotingProcessor, resetOffset) {
delete request;
}
-TEST(clientRemotingProcessorS, getConsumerRunningInfo) {
+TEST(clientRemotingProcessor, getConsumerRunningInfoFailed) {
MockMQClientFactory* factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a");
+ Mock::AllowLeak(factory);
ConsumerRunningInfo* info = new ConsumerRunningInfo();
EXPECT_CALL(*factory, consumerRunningInfo(_)).Times(2).WillOnce(Return(info)).WillOnce(Return(info));
EXPECT_CALL(*factory, getSessionCredentialFromConsumer(_, _))
@@ -174,6 +175,8 @@ TEST(clientRemotingProcessorS, getConsumerRunningInfo) {
GetConsumerRunningInfoRequestHeader* header = new GetConsumerRunningInfoRequestHeader();
header->setConsumerGroup("testGroup");
+ header->setClientId("testClientId");
+ header->setJstackEnable(false);
RemotingCommand* request = new RemotingCommand(14, header);
|