Skip to content

Commit fe32777

Browse files
authored
test(unittest): add some test cases for default producer implement. (#242)
* fix(namespace): get name space error from endpoint in util * fix(utilall): modify util and add unit tests * feat(unittest): add test case for topic route publish info * feat(ci): modify travis ci file temply * feat(unittest): add test case for topic route publish info * feat(ci): open apt update again * feat(unittest): add test case for default mq producer. * feat(unittest): add test case for default mq producer. * feat(unittest): add test case for default mq producer.
1 parent 1e4ac9c commit fe32777

14 files changed

+538
-22
lines changed

src/MQClientAPIImpl.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
namespace rocketmq {
2929
//<!************************************************************************
30+
MQClientAPIImpl::MQClientAPIImpl(const string& mqClientId) : m_firstFetchNameSrv(true), m_mqClientId(mqClientId) {}
3031
MQClientAPIImpl::MQClientAPIImpl(const string& mqClientId,
3132
ClientRemotingProcessor* clientRemotingProcessor,
3233
int pullThreadNum,

src/MQClientAPIImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace rocketmq {
4242
//<!************************************************************************
4343
class MQClientAPIImpl {
4444
public:
45+
MQClientAPIImpl(const string& mqClientId);
4546
MQClientAPIImpl(const string& mqClientId,
4647
ClientRemotingProcessor* clientRemotingProcessor,
4748
int pullThreadNum,

src/MQClientFactory.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
namespace rocketmq {
3232
//<!***************************************************************************
33+
MQClientFactory::MQClientFactory(const string& clientID) : m_bFetchNSService(true) {
34+
m_clientId = clientID;
35+
}
3336
MQClientFactory::MQClientFactory(const string& clientID,
3437
int pullThreadNum,
3538
uint64_t tcpConnectTimeout,
@@ -716,7 +719,7 @@ void MQClientFactory::checkTransactionState(const std::string& addr,
716719
}
717720
}
718721

719-
MQClientAPIImpl* MQClientFactory::getMQClientAPIImpl() const {
722+
MQClientAPIImpl* MQClientFactory::getMQClientAPIImpl() {
720723
return m_pClientAPIImpl.get();
721724
}
722725

src/MQClientFactory.h

+12-11
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ class MQClientFactory {
4646
uint64_t tcpConnectTimeout,
4747
uint64_t tcpTransportTryLockTimeout,
4848
string unitName);
49+
MQClientFactory(const string& clientID);
4950
virtual ~MQClientFactory();
5051

51-
void start();
52-
void shutdown();
53-
bool registerProducer(MQProducer* pProducer);
54-
void unregisterProducer(MQProducer* pProducer);
55-
bool registerConsumer(MQConsumer* pConsumer);
56-
void unregisterConsumer(MQConsumer* pConsumer);
52+
virtual void start();
53+
virtual void shutdown();
54+
virtual bool registerProducer(MQProducer* pProducer);
55+
virtual void unregisterProducer(MQProducer* pProducer);
56+
virtual bool registerConsumer(MQConsumer* pConsumer);
57+
virtual void unregisterConsumer(MQConsumer* pConsumer);
5758

5859
void createTopic(const string& key,
5960
const string& newTopic,
@@ -76,7 +77,7 @@ class MQClientFactory {
7677
void checkTransactionState(const std::string& addr,
7778
const MQMessageExt& message,
7879
const CheckTransactionStateRequestHeader& checkRequestHeader);
79-
MQClientAPIImpl* getMQClientAPIImpl() const;
80+
virtual MQClientAPIImpl* getMQClientAPIImpl();
8081
MQProducer* selectProducer(const string& group);
8182
MQConsumer* selectConsumer(const string& group);
8283

@@ -88,10 +89,10 @@ class MQClientFactory {
8889

8990
FindBrokerResult* findBrokerAddressInAdmin(const string& brokerName);
9091

91-
string findBrokerAddressInPublish(const string& brokerName);
92+
virtual string findBrokerAddressInPublish(const string& brokerName);
9293

93-
boost::shared_ptr<TopicPublishInfo> tryToFindTopicPublishInfo(const string& topic,
94-
const SessionCredentials& session_credentials);
94+
virtual boost::shared_ptr<TopicPublishInfo> tryToFindTopicPublishInfo(const string& topic,
95+
const SessionCredentials& session_credentials);
9596

9697
void fetchSubscribeMessageQueues(const string& topic,
9798
vector<MQMessageQueue>& mqs,
@@ -102,7 +103,7 @@ class MQClientFactory {
102103
bool isDefault = false);
103104
void rebalanceImmediately();
104105
void doRebalanceByConsumerGroup(const string& consumerGroup);
105-
void sendHeartbeatToAllBroker();
106+
virtual void sendHeartbeatToAllBroker();
106107

107108
void cleanOfflineBrokers();
108109

src/common/DefaultMQClient.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ void DefaultMQClient::shutdown() {
157157
MQClientFactory* DefaultMQClient::getFactory() const {
158158
return m_clientFactory;
159159
}
160+
void DefaultMQClient::setFactory(MQClientFactory* factory) {
161+
m_clientFactory = factory;
162+
}
160163

161164
bool DefaultMQClient::isServiceStateOk() {
162165
return m_serviceState == RUNNING;

src/common/NameSpaceUtil.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ string NameSpaceUtil::getNameSpaceFromNsURL(string nameServerAddr) {
4141
LOG_DEBUG("Try to get Name Space from nameServerAddr [%s]", nameServerAddr.c_str());
4242
string nsAddr = formatNameServerURL(nameServerAddr);
4343
string nameSpace;
44-
auto index = nameServerAddr.find(NAMESPACE_PREFIX);
44+
auto index = nsAddr.find(NAMESPACE_PREFIX);
4545
if (index != string::npos) {
46-
auto indexDot = nameServerAddr.find('.');
47-
if (indexDot != string::npos) {
48-
nameSpace = nameServerAddr.substr(index, indexDot);
46+
auto indexDot = nsAddr.find('.');
47+
if (indexDot != string::npos && indexDot > index) {
48+
nameSpace = nsAddr.substr(index, indexDot - index);
4949
LOG_INFO("Get Name Space [%s] from nameServerAddr [%s]", nameSpace.c_str(), nameServerAddr.c_str());
5050
return nameSpace;
5151
}
@@ -83,7 +83,7 @@ string NameSpaceUtil::withNameSpace(string source, string ns) {
8383
}
8484

8585
bool NameSpaceUtil::hasNameSpace(string source, string ns) {
86-
if (source.length() >= ns.length() && source.find(ns) != string::npos) {
86+
if (!ns.empty() && source.length() >= ns.length() && source.find(ns) != string::npos) {
8787
return true;
8888
}
8989
return false;

src/common/UtilAll.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ int UtilAll::Split(vector<string>& ret_, const string& strIn, const string& sep)
176176
return ret_.size();
177177
}
178178

179-
int32_t UtilAll::StringToInt32(const std::string& str, int32_t& out) {
179+
bool UtilAll::StringToInt32(const std::string& str, int32_t& out) {
180180
out = 0;
181181
if (str.empty()) {
182182
return false;
@@ -196,7 +196,7 @@ int32_t UtilAll::StringToInt32(const std::string& str, int32_t& out) {
196196
return true;
197197
}
198198

199-
int64_t UtilAll::StringToInt64(const std::string& str, int64_t& val) {
199+
bool UtilAll::StringToInt64(const std::string& str, int64_t& val) {
200200
char* endptr = NULL;
201201
errno = 0; /* To distinguish success/failure after call */
202202
val = strtoll(str.c_str(), &endptr, 10);

src/common/UtilAll.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ class UtilAll {
112112
static int Split(vector<string>& ret_, const string& strIn, const char sep);
113113
static int Split(vector<string>& ret_, const string& strIn, const string& sep);
114114

115-
static int32_t StringToInt32(const std::string& str, int32_t& out);
116-
static int64_t StringToInt64(const std::string& str, int64_t& val);
115+
static bool StringToInt32(const std::string& str, int32_t& out);
116+
static bool StringToInt64(const std::string& str, int64_t& val);
117117

118118
static string getLocalHostName();
119119
static string getLocalAddress();

src/include/DefaultMQClient.h

+2
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ class DefaultMQClient {
167167
const std::string& input_onsChannel);
168168
const SessionCredentials& getSessionCredentials() const;
169169

170+
virtual void setFactory(MQClientFactory*);
171+
170172
protected:
171173
virtual void start();
172174
virtual void shutdown();

src/producer/TopicPublishInfo.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
#include <boost/date_time/posix_time/posix_time.hpp>
2525
#include <boost/scoped_ptr.hpp>
2626
#include <boost/thread/thread.hpp>
27+
#include <map>
28+
#include <string>
29+
#include <vector>
2730
#include "Logging.h"
2831
#include "MQMessageQueue.h"
32+
#include "UtilAll.h"
2933

3034
namespace rocketmq {
3135
//<!************************************************************************/
@@ -257,6 +261,6 @@ class TopicPublishInfo {
257261
};
258262

259263
//<!***************************************************************************
260-
} //<!end namespace;
264+
} // namespace rocketmq
261265

262266
#endif

test/src/common/NameSpaceUtilTest.cpp

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <string>
19+
20+
#include "gmock/gmock.h"
21+
#include "gtest/gtest.h"
22+
23+
#include "NameSpaceUtil.h"
24+
25+
using std::string;
26+
27+
using ::testing::InitGoogleMock;
28+
using ::testing::InitGoogleTest;
29+
using testing::Return;
30+
31+
using rocketmq::NameSpaceUtil;
32+
33+
TEST(NameSpaceUtil, isEndPointURL) {
34+
const string url = "http://rocketmq.nameserver.com";
35+
EXPECT_TRUE(NameSpaceUtil::isEndPointURL(url));
36+
EXPECT_FALSE(NameSpaceUtil::isEndPointURL("rocketmq.nameserver.com"));
37+
EXPECT_FALSE(NameSpaceUtil::isEndPointURL("127.0.0.1"));
38+
}
39+
TEST(NameSpaceUtil, formatNameServerURL) {
40+
string url = "http://rocketmq.nameserver.com";
41+
string urlFormatted = "rocketmq.nameserver.com";
42+
EXPECT_EQ(NameSpaceUtil::formatNameServerURL(url), urlFormatted);
43+
EXPECT_EQ(NameSpaceUtil::formatNameServerURL(urlFormatted), urlFormatted);
44+
}
45+
TEST(NameSpaceUtil, getNameSpaceFromNsURL) {
46+
string url = "http://MQ_INST_UNITTEST.rocketmq.nameserver.com";
47+
string url2 = "MQ_INST_UNITTEST.rocketmq.nameserver.com";
48+
string noInstUrl = "http://rocketmq.nameserver.com";
49+
string inst = "MQ_INST_UNITTEST";
50+
EXPECT_EQ(NameSpaceUtil::getNameSpaceFromNsURL(url), inst);
51+
EXPECT_EQ(NameSpaceUtil::getNameSpaceFromNsURL(url2), inst);
52+
EXPECT_EQ(NameSpaceUtil::getNameSpaceFromNsURL(noInstUrl), "");
53+
}
54+
TEST(NameSpaceUtil, checkNameSpaceExistInNsURL) {
55+
string url = "http://MQ_INST_UNITTEST.rocketmq.nameserver.com";
56+
string url2 = "MQ_INST_UNITTEST.rocketmq.nameserver.com";
57+
string noInstUrl = "http://rocketmq.nameserver.com";
58+
EXPECT_TRUE(NameSpaceUtil::checkNameSpaceExistInNsURL(url));
59+
EXPECT_FALSE(NameSpaceUtil::checkNameSpaceExistInNsURL(url2));
60+
EXPECT_FALSE(NameSpaceUtil::checkNameSpaceExistInNsURL(noInstUrl));
61+
}
62+
TEST(NameSpaceUtil, checkNameSpaceExistInNameServer) {
63+
string url = "http://MQ_INST_UNITTEST.rocketmq.nameserver.com";
64+
string url2 = "MQ_INST_UNITTEST.rocketmq.nameserver.com";
65+
string noInstUrl = "rocketmq.nameserver.com";
66+
string nsIP = "127.0.0.1";
67+
EXPECT_TRUE(NameSpaceUtil::checkNameSpaceExistInNameServer(url));
68+
EXPECT_TRUE(NameSpaceUtil::checkNameSpaceExistInNameServer(url2));
69+
EXPECT_FALSE(NameSpaceUtil::checkNameSpaceExistInNameServer(noInstUrl));
70+
EXPECT_FALSE(NameSpaceUtil::checkNameSpaceExistInNameServer(nsIP));
71+
}
72+
TEST(NameSpaceUtil, withNameSpace) {
73+
string source = "testTopic";
74+
string ns = "MQ_INST_UNITTEST";
75+
string nsSource = "MQ_INST_UNITTEST%testTopic";
76+
EXPECT_EQ(NameSpaceUtil::withNameSpace(source, ns), nsSource);
77+
EXPECT_EQ(NameSpaceUtil::withNameSpace(source, ""), source);
78+
}
79+
TEST(NameSpaceUtil, hasNameSpace) {
80+
string source = "testTopic";
81+
string ns = "MQ_INST_UNITTEST";
82+
string nsSource = "MQ_INST_UNITTEST%testTopic";
83+
EXPECT_TRUE(NameSpaceUtil::hasNameSpace(nsSource, ns));
84+
EXPECT_FALSE(NameSpaceUtil::hasNameSpace(source, ns));
85+
EXPECT_FALSE(NameSpaceUtil::hasNameSpace(source, ""));
86+
}
87+
int main(int argc, char* argv[]) {
88+
InitGoogleMock(&argc, argv);
89+
testing::GTEST_FLAG(throw_on_failure) = true;
90+
testing::GTEST_FLAG(filter) = "NameSpaceUtil.*";
91+
int itestts = RUN_ALL_TESTS();
92+
return itestts;
93+
}

test/src/common/UtilAllTest.cpp

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <string>
19+
20+
#include "gmock/gmock.h"
21+
#include "gtest/gtest.h"
22+
23+
#include "UtilAll.h"
24+
25+
using std::string;
26+
27+
using ::testing::InitGoogleMock;
28+
using ::testing::InitGoogleTest;
29+
using testing::Return;
30+
31+
using rocketmq::UtilAll;
32+
33+
TEST(UtilAll, startsWith_retry) {
34+
string source = "testTopic";
35+
string retrySource = "%RETRY%testTopic";
36+
string noRetrySource = "%DLQ%testTopic";
37+
EXPECT_TRUE(UtilAll::startsWith_retry(retrySource));
38+
EXPECT_FALSE(UtilAll::startsWith_retry(source));
39+
EXPECT_FALSE(UtilAll::startsWith_retry(noRetrySource));
40+
}
41+
TEST(UtilAll, getRetryTopic) {
42+
string source = "testTopic";
43+
string retrySource = "%RETRY%testTopic";
44+
EXPECT_EQ(UtilAll::getRetryTopic(source), retrySource);
45+
}
46+
TEST(UtilAll, Trim) {
47+
string source = "testTopic";
48+
string preSource = " testTopic";
49+
string surSource = "testTopic ";
50+
string allSource = " testTopic ";
51+
UtilAll::Trim(preSource);
52+
UtilAll::Trim(surSource);
53+
UtilAll::Trim(allSource);
54+
EXPECT_EQ(preSource, source);
55+
EXPECT_EQ(surSource, source);
56+
EXPECT_EQ(allSource, source);
57+
}
58+
TEST(UtilAll, hexstr2ull) {
59+
const char* a = "1";
60+
const char* b = "FF";
61+
const char* c = "1a";
62+
const char* d = "101";
63+
EXPECT_EQ(UtilAll::hexstr2ull(a), 1);
64+
EXPECT_EQ(UtilAll::hexstr2ull(b), 255);
65+
EXPECT_EQ(UtilAll::hexstr2ull(c), 26);
66+
EXPECT_EQ(UtilAll::hexstr2ull(d), 257);
67+
}
68+
TEST(UtilAll, SplitURL) {
69+
string source = "127.0.0.1";
70+
string source1 = "127.0.0.1:0";
71+
string source2 = "127.0.0.1:9876";
72+
string addr;
73+
string addr1;
74+
string addr2;
75+
short port;
76+
EXPECT_FALSE(UtilAll::SplitURL(source, addr, port));
77+
EXPECT_FALSE(UtilAll::SplitURL(source1, addr1, port));
78+
EXPECT_TRUE(UtilAll::SplitURL(source2, addr2, port));
79+
EXPECT_EQ(addr2, "127.0.0.1");
80+
EXPECT_EQ(port, 9876);
81+
}
82+
TEST(UtilAll, SplitOne) {
83+
string source = "127.0.0.1:9876";
84+
vector<string> ret;
85+
EXPECT_EQ(UtilAll::Split(ret, source, '.'), 4);
86+
EXPECT_EQ(ret[0], "127");
87+
}
88+
TEST(UtilAll, SplitStr) {
89+
string source = "11AA222AA3333AA44444AA5";
90+
vector<string> ret;
91+
EXPECT_EQ(UtilAll::Split(ret, source, "AA"), 5);
92+
EXPECT_EQ(ret[0], "11");
93+
}
94+
TEST(UtilAll, StringToInt32) {
95+
string source = "123";
96+
int value;
97+
EXPECT_TRUE(UtilAll::StringToInt32(source, value));
98+
EXPECT_EQ(123, value);
99+
EXPECT_FALSE(UtilAll::StringToInt32("123456789X123456789", value));
100+
EXPECT_FALSE(UtilAll::StringToInt32("-1234567890123456789", value));
101+
EXPECT_FALSE(UtilAll::StringToInt32("1234567890123456789", value));
102+
}
103+
TEST(UtilAll, StringToInt64) {
104+
string source = "123";
105+
int64_t value;
106+
EXPECT_TRUE(UtilAll::StringToInt64(source, value));
107+
EXPECT_EQ(123, value);
108+
EXPECT_FALSE(UtilAll::StringToInt64("XXXXXXXXXXX", value));
109+
EXPECT_FALSE(UtilAll::StringToInt64("123456789X123456789", value));
110+
EXPECT_EQ(123456789, value);
111+
EXPECT_FALSE(UtilAll::StringToInt64("-123456789012345678901234567890123456789012345678901234567890", value));
112+
EXPECT_FALSE(UtilAll::StringToInt64("123456789012345678901234567890123456789012345678901234567890", value));
113+
}
114+
int main(int argc, char* argv[]) {
115+
InitGoogleMock(&argc, argv);
116+
testing::GTEST_FLAG(throw_on_failure) = true;
117+
testing::GTEST_FLAG(filter) = "UtilAll.*";
118+
int itestts = RUN_ALL_TESTS();
119+
return itestts;
120+
}

0 commit comments

Comments
 (0)