forked from apache/rocketmq-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMQClientAPIImpTest.cpp
More file actions
197 lines (186 loc) · 8.49 KB
/
MQClientAPIImpTest.cpp
File metadata and controls
197 lines (186 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "CommunicationMode.h"
#include "MQClientAPIImpl.h"
#include "MQClientException.h"
using namespace std;
using namespace rocketmq;
using rocketmq::CommunicationMode;
using rocketmq::RemotingCommand;
using rocketmq::TcpRemotingClient;
using testing::_;
using ::testing::InitGoogleMock;
using ::testing::InitGoogleTest;
using testing::Mock;
using testing::Return;
class MockTcpRemotingClient : public TcpRemotingClient {
public:
MockTcpRemotingClient(int pullThreadNum, uint64_t tcpConnectTimeout, uint64_t tcpTransportTryLockTimeout)
: TcpRemotingClient(pullThreadNum, tcpConnectTimeout, tcpTransportTryLockTimeout) {}
MOCK_METHOD3(invokeSync, RemotingCommand*(const string&, RemotingCommand&, int));
};
class MockMQClientAPIImpl : public MQClientAPIImpl {
public:
MockMQClientAPIImpl(const string& mqClientId,
ClientRemotingProcessor* clientRemotingProcessor,
int pullThreadNum,
uint64_t tcpConnectTimeout,
uint64_t tcpTransportTryLockTimeout,
string unitName)
: MQClientAPIImpl(mqClientId,
clientRemotingProcessor,
pullThreadNum,
tcpConnectTimeout,
tcpTransportTryLockTimeout,
unitName) {
m_processor = clientRemotingProcessor;
}
ClientRemotingProcessor* m_processor;
void reInitRemoteClient(TcpRemotingClient* client) {
m_pRemotingClient.reset(client);
m_pRemotingClient->registerProcessor(CHECK_TRANSACTION_STATE, m_processor);
m_pRemotingClient->registerProcessor(RESET_CONSUMER_CLIENT_OFFSET, m_processor);
m_pRemotingClient->registerProcessor(GET_CONSUMER_STATUS_FROM_CLIENT, m_processor);
m_pRemotingClient->registerProcessor(GET_CONSUMER_RUNNING_INFO, m_processor);
m_pRemotingClient->registerProcessor(NOTIFY_CONSUMER_IDS_CHANGED, m_processor);
m_pRemotingClient->registerProcessor(CONSUME_MESSAGE_DIRECTLY, m_processor);
}
};
class MockMQClientAPIImplUtil {
public:
static MockMQClientAPIImplUtil* GetInstance() {
static MockMQClientAPIImplUtil instance;
return &instance;
}
MockMQClientAPIImpl* GetGtestMockClientAPIImpl() {
if (m_impl != nullptr) {
return m_impl;
}
string cid = "testClientId";
int ptN = 1;
uint64_t tct = 3000;
uint64_t ttt = 3000;
string un = "central";
SessionCredentials sc;
ClientRemotingProcessor* pp = new ClientRemotingProcessor(nullptr);
MockMQClientAPIImpl* impl = new MockMQClientAPIImpl(cid, pp, ptN, tct, ttt, un);
MockTcpRemotingClient* pClient = new MockTcpRemotingClient(ptN, tct, ttt);
impl->reInitRemoteClient(pClient);
m_impl = impl;
m_pClient = pClient;
return impl;
}
MockTcpRemotingClient* GetGtestMockRemotingClient() { return m_pClient; }
MockMQClientAPIImpl* m_impl = nullptr;
MockTcpRemotingClient* m_pClient = nullptr;
};
TEST(MQClientAPIImplTest, getMaxOffset) {
SessionCredentials sc;
MockMQClientAPIImpl* impl = MockMQClientAPIImplUtil::GetInstance()->GetGtestMockClientAPIImpl();
Mock::AllowLeak(impl);
MockTcpRemotingClient* pClient = MockMQClientAPIImplUtil::GetInstance()->GetGtestMockRemotingClient();
Mock::AllowLeak(pClient);
GetMaxOffsetResponseHeader* pHead = new GetMaxOffsetResponseHeader();
pHead->offset = 4096;
RemotingCommand* pCommandFailed = new RemotingCommand(SYSTEM_ERROR, nullptr);
RemotingCommand* pCommandSuccuss = new RemotingCommand(SUCCESS_VALUE, pHead);
EXPECT_CALL(*pClient, invokeSync(_, _, _))
.Times(3)
.WillOnce(Return(nullptr))
.WillOnce(Return(pCommandFailed))
.WillOnce(Return(pCommandSuccuss));
EXPECT_ANY_THROW(impl->getMaxOffset("127.0.0.0:10911", "testTopic", 0, 1000, sc));
EXPECT_ANY_THROW(impl->getMaxOffset("127.0.0.0:10911", "testTopic", 0, 1000, sc));
int64 offset = impl->getMaxOffset("127.0.0.0:10911", "testTopic", 0, 1000, sc);
EXPECT_EQ(4096, offset);
}
TEST(MQClientAPIImplTest, getMinOffset) {
SessionCredentials sc;
MockMQClientAPIImpl* impl = MockMQClientAPIImplUtil::GetInstance()->GetGtestMockClientAPIImpl();
Mock::AllowLeak(impl);
MockTcpRemotingClient* pClient = MockMQClientAPIImplUtil::GetInstance()->GetGtestMockRemotingClient();
Mock::AllowLeak(pClient);
GetMinOffsetResponseHeader* pHead = new GetMinOffsetResponseHeader();
pHead->offset = 2048;
RemotingCommand* pCommandFailed = new RemotingCommand(SYSTEM_ERROR, nullptr);
RemotingCommand* pCommandSuccuss = new RemotingCommand(SUCCESS_VALUE, pHead);
EXPECT_CALL(*pClient, invokeSync(_, _, _))
.Times(3)
.WillOnce(Return(nullptr))
.WillOnce(Return(pCommandFailed))
.WillOnce(Return(pCommandSuccuss));
EXPECT_ANY_THROW(impl->getMinOffset("127.0.0.0:10911", "testTopic", 0, 1000, sc));
EXPECT_ANY_THROW(impl->getMinOffset("127.0.0.0:10911", "testTopic", 0, 1000, sc));
int64 offset = impl->getMinOffset("127.0.0.0:10911", "testTopic", 0, 1000, sc);
EXPECT_EQ(2048, offset);
}
TEST(MQClientAPIImplTest, sendMessage) {
string cid = "testClientId";
SessionCredentials sc;
MockMQClientAPIImpl* impl = MockMQClientAPIImplUtil::GetInstance()->GetGtestMockClientAPIImpl();
Mock::AllowLeak(impl);
MockTcpRemotingClient* pClient = MockMQClientAPIImplUtil::GetInstance()->GetGtestMockRemotingClient();
Mock::AllowLeak(pClient);
SendMessageResponseHeader* pHead = new SendMessageResponseHeader();
pHead->msgId = "MessageID";
pHead->queueId = 1;
pHead->queueOffset = 409600;
RemotingCommand* pCommandSync = new RemotingCommand(SUCCESS_VALUE, pHead);
EXPECT_CALL(*pClient, invokeSync(_, _, _)).Times(1).WillOnce(Return(pCommandSync));
MQMessage message("testTopic", "Hello, RocketMQ");
string unique_msgId = "UniqMessageID";
message.setProperty(MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, unique_msgId);
SendMessageRequestHeader* requestHeader = new SendMessageRequestHeader();
requestHeader->producerGroup = cid;
requestHeader->topic = (message.getTopic());
requestHeader->defaultTopic = DEFAULT_TOPIC;
requestHeader->defaultTopicQueueNums = 4;
requestHeader->bornTimestamp = UtilAll::currentTimeMillis();
SendResult result =
impl->sendMessage("127.0.0.0:10911", "testBroker", message, requestHeader, 100, 1, ComMode_SYNC, nullptr, sc);
EXPECT_EQ(result.getSendStatus(), SEND_OK);
EXPECT_EQ(result.getMsgId(), unique_msgId);
EXPECT_EQ(result.getQueueOffset(), 409600);
EXPECT_EQ(result.getOffsetMsgId(), "MessageID");
EXPECT_EQ(result.getMessageQueue().getBrokerName(), "testBroker");
EXPECT_EQ(result.getMessageQueue().getTopic(), "testTopic");
}
TEST(MQClientAPIImplTest, consumerSendMessageBack) {
SessionCredentials sc;
MQMessageExt msg;
MockMQClientAPIImpl* impl = MockMQClientAPIImplUtil::GetInstance()->GetGtestMockClientAPIImpl();
Mock::AllowLeak(impl);
MockTcpRemotingClient* pClient = MockMQClientAPIImplUtil::GetInstance()->GetGtestMockRemotingClient();
Mock::AllowLeak(pClient);
RemotingCommand* pCommandFailed = new RemotingCommand(SYSTEM_ERROR, nullptr);
RemotingCommand* pCommandSuccuss = new RemotingCommand(SUCCESS_VALUE, nullptr);
EXPECT_CALL(*pClient, invokeSync(_, _, _))
.Times(3)
.WillOnce(Return(nullptr))
.WillOnce(Return(pCommandFailed))
.WillOnce(Return(pCommandSuccuss));
EXPECT_ANY_THROW(impl->consumerSendMessageBack("127.0.0.0:10911", msg, "testGroup", 0, 1000, 16, sc));
EXPECT_ANY_THROW(impl->consumerSendMessageBack("127.0.0.0:10911", msg, "testGroup", 0, 1000, 16, sc));
EXPECT_NO_THROW(impl->consumerSendMessageBack("127.0.0.0:10911", msg, "testGroup", 0, 1000, 16, sc));
}
int main(int argc, char* argv[]) {
InitGoogleMock(&argc, argv);
testing::GTEST_FLAG(filter) = "MQClientAPIImplTest.*";
return RUN_ALL_TESTS();
}