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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 12 additions & 11 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 5 additions & 1 deletion
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

0 commit comments

Comments
 (0)