You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2019/03/27 16:52:27 UTC

[GitHub] [rocketmq-client-cpp] githublaohu commented on a change in pull request #110: unit-test-transport

githublaohu commented on a change in pull request #110: unit-test-transport
URL: https://github.com/apache/rocketmq-client-cpp/pull/110#discussion_r269665969
 
 

 ##########
 File path: test/src/transport/ClientRemotingProcessorTest.cpp
 ##########
 @@ -0,0 +1,235 @@
+/*
+ * 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 <memory>
+#include "map"
+#include "string.h"
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+#include "json/value.h"
+#include "json/writer.h"
+
+#include "ClientRPCHook.h"
+#include "ClientRemotingProcessor.h"
+#include "ConsumerRunningInfo.h"
+#include "MQClientFactory.h"
+#include "MQMessageQueue.h"
+#include "MQProtos.h"
+#include "RemotingCommand.h"
+#include "SessionCredentials.h"
+#include "UtilAll.h"
+#include "dataBlock.h"
+
+using std::map;
+using std::string;
+
+using ::testing::_;
+using ::testing::InitGoogleMock;
+using ::testing::InitGoogleTest;
+using testing::Mock;
+using testing::Return;
+using testing::SetArgReferee;
+
+using Json::FastWriter;
+using Json::Value;
+
+using rocketmq::ClientRemotingProcessor;
+using rocketmq::ClientRPCHook;
+using rocketmq::ConsumerRunningInfo;
+using rocketmq::GetConsumerRunningInfoRequestHeader;
+using rocketmq::MemoryBlock;
+using rocketmq::MQClientFactory;
+using rocketmq::MQMessageQueue;
+using rocketmq::MQRequestCode;
+using rocketmq::MQResponseCode;
+using rocketmq::NotifyConsumerIdsChangedRequestHeader;
+using rocketmq::RemotingCommand;
+using rocketmq::ResetOffsetBody;
+using rocketmq::ResetOffsetRequestHeader;
+using rocketmq::SessionCredentials;
+using rocketmq::UtilAll;
+
+class MockClientRemotingProcessor : public ClientRemotingProcessor {
+   public:
+    MockClientRemotingProcessor(MQClientFactory *factrory) : ClientRemotingProcessor(factrory) {}
+    MOCK_METHOD1(resetOffset, RemotingCommand *(RemotingCommand *request));
+    MOCK_METHOD1(getConsumerRunningInfo, RemotingCommand *(RemotingCommand *request));
+    MOCK_METHOD1(notifyConsumerIdsChanged, RemotingCommand *(RemotingCommand *request));
+};
+
+class MockMQClientFactory : public MQClientFactory {
+   public:
+    MockMQClientFactory(const string &clientID,
+                        int pullThreadNum,
+                        uint64_t tcpConnectTimeout,
+                        uint64_t tcpTransportTryLockTimeout,
+                        string unitName)
+        : MQClientFactory(clientID, pullThreadNum, tcpConnectTimeout, tcpTransportTryLockTimeout, unitName) {}
+
+    MOCK_METHOD3(resetOffset,
+                 void(const string &group, const string &topic, const map<MQMessageQueue, int64> &offsetTable));
+    MOCK_METHOD1(consumerRunningInfo, ConsumerRunningInfo *(const string &consumerGroup));
+    MOCK_METHOD2(getSessionCredentialFromConsumer,
+                 bool(const string &consumerGroup, SessionCredentials &sessionCredentials));
+    MOCK_METHOD1(doRebalanceByConsumerGroup, void(const string &consumerGroup));
+};
+
+TEST(clientRemotingProcessor, processRequest) {
+    MockMQClientFactory *factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a");
+    ClientRemotingProcessor clientRemotingProcessor(factory);
+
+    string addr = "127.0.0.1:9876";
+    RemotingCommand *command = new RemotingCommand();
+    RemotingCommand *pResponse = new RemotingCommand(13);
+
+    pResponse->setCode(MQRequestCode::RESET_CONSUMER_CLIENT_OFFSET);
+    command->setCode(MQRequestCode::RESET_CONSUMER_CLIENT_OFFSET);
+    EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr);
+    EXPECT_EQ(nullptr, clientRemotingProcessor.processRequest(addr, command));
+
+    NotifyConsumerIdsChangedRequestHeader *header = new NotifyConsumerIdsChangedRequestHeader();
+    header->setGroup("testGroup");
+    RemotingCommand *twoCommand = new RemotingCommand(MQRequestCode::NOTIFY_CONSUMER_IDS_CHANGED, header);
+
+    EXPECT_EQ(NULL, clientRemotingProcessor.processRequest(addr, twoCommand));
+
+    command->setCode(MQRequestCode::GET_CONSUMER_RUNNING_INFO);
+    // EXPECT_EQ(NULL , clientRemotingProcessor.processRequest(addr, command));
+
+    command->setCode(MQRequestCode::CHECK_TRANSACTION_STATE);
+    EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr);
+
+    command->setCode(MQRequestCode::GET_CONSUMER_STATUS_FROM_CLIENT);
+    EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr);
+
+    command->setCode(MQRequestCode::CONSUME_MESSAGE_DIRECTLY);
+    EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr);
+
+    command->setCode(1);
+    EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr);
+
+    delete command;
+    delete pResponse;
+}
+
+TEST(clientRemotingProcessor, resetOffset) {
+    MockMQClientFactory *factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a");
+    Mock::AllowLeak(factory);
+    ClientRemotingProcessor clientRemotingProcessor(factory);
+    Value root;
+    Value messageQueues;
+    Value messageQueue;
+    messageQueue["brokerName"] = "testBroker";
+    messageQueue["queueId"] = 4;
+    messageQueue["topic"] = "testTopic";
+    messageQueue["offset"] = 1024;
+
+    messageQueues.append(messageQueue);
+    root["offsetTable"] = messageQueues;
+
+    FastWriter wrtier;
+    string strData = wrtier.write(root);
+
+    ResetOffsetRequestHeader *header = new ResetOffsetRequestHeader();
+    RemotingCommand *request = new RemotingCommand(13, header);
+
+    EXPECT_CALL(*factory, resetOffset(_, _, _)).Times(1);
+    clientRemotingProcessor.resetOffset(request);
+
+    request->SetBody(strData.c_str(), strData.size() - 2);
+    clientRemotingProcessor.resetOffset(request);
+
+    request->SetBody(strData.c_str(), strData.size());
+    clientRemotingProcessor.resetOffset(request);
+
+    delete header;
+    delete request;
+}
+
+TEST(clientRemotingProcessorS, getConsumerRunningInfo) {
+    MockMQClientFactory *factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a");
 
 Review comment:
   Mock::AllowLeak(factory);   Be equal to deleted

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services