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 066af4e unit-test-common-3 (#117)
066af4e is described below
commit 066af4e3b0754bc4b0b672712254757577ed7c41
Author: githublaohu <[email protected]>
AuthorDate: Thu Apr 4 15:09:25 2019 +0800
unit-test-common-3 (#117)
---
test/src/common/ClientRPCHookTest.cpp | 60 ++++++++++++
test/src/common/MemoryBlockTest.cpp | 175 ++++++++++++++++++++++++++++++++++
test/src/common/big_endianTest.cpp | 100 +++++++++++++++++++
3 files changed, 335 insertions(+)
diff --git a/test/src/common/ClientRPCHookTest.cpp
b/test/src/common/ClientRPCHookTest.cpp
new file mode 100644
index 0000000..eb6cd69
--- /dev/null
+++ b/test/src/common/ClientRPCHookTest.cpp
@@ -0,0 +1,60 @@
+/*
+ * 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 "ClientRPCHook.h"
+#include "CommandHeader.h"
+#include "RemotingCommand.h"
+#include "SessionCredentials.h"
+
+using ::testing::InitGoogleMock;
+using ::testing::InitGoogleTest;
+using testing::Return;
+
+using rocketmq::RemotingCommand;
+using rocketmq::SendMessageRequestHeader;
+using rocketmq::SessionCredentials;
+
+using rocketmq::ClientRPCHook;
+
+TEST(clientRPCHook, doBeforeRequest) {
+ SessionCredentials sessionCredentials;
+ sessionCredentials.setAccessKey("accessKey");
+ sessionCredentials.setSecretKey("secretKey");
+ sessionCredentials.setAuthChannel("onsChannel");
+
+ ClientRPCHook clientRPCHook(sessionCredentials);
+
+ RemotingCommand remotingCommand;
+ clientRPCHook.doBeforeRequest("127.0.0.1:9876", remotingCommand);
+
+ SendMessageRequestHeader *sendMessageRequestHeader = new
SendMessageRequestHeader();
+ RemotingCommand headeRremotingCommand(17, sendMessageRequestHeader);
+ clientRPCHook.doBeforeRequest("127.0.0.1:9876", headeRremotingCommand);
+
+ headeRremotingCommand.setMsgBody("1231231");
+ clientRPCHook.doBeforeRequest("127.0.0.1:9876", headeRremotingCommand);
+}
+
+int main(int argc, char *argv[]) {
+ InitGoogleMock(&argc, argv);
+ testing::GTEST_FLAG(throw_on_failure) = true;
+ testing::GTEST_FLAG(filter) = "clientRPCHook.doBeforeRequest";
+ int itestts = RUN_ALL_TESTS();
+ return itestts;
+}
diff --git a/test/src/common/MemoryBlockTest.cpp
b/test/src/common/MemoryBlockTest.cpp
new file mode 100644
index 0000000..e1f350d
--- /dev/null
+++ b/test/src/common/MemoryBlockTest.cpp
@@ -0,0 +1,175 @@
+/*
+ * 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 <string>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+#include "dataBlock.h"
+
+using std::string;
+
+using ::testing::InitGoogleMock;
+using ::testing::InitGoogleTest;
+using testing::Return;
+
+using rocketmq::MemoryBlock;
+
+TEST(memoryBlock, init) {
+ MemoryBlock memoryBlock;
+ EXPECT_EQ(memoryBlock.getSize(), 0);
+ EXPECT_TRUE(memoryBlock.getData() == nullptr);
+
+ MemoryBlock twoMemoryBlock(-1, true);
+ EXPECT_EQ(twoMemoryBlock.getSize(), 0);
+ EXPECT_TRUE(twoMemoryBlock.getData() == nullptr);
+
+ MemoryBlock threeMemoryBlock(10, true);
+ EXPECT_EQ(threeMemoryBlock.getSize(), 10);
+ EXPECT_TRUE(threeMemoryBlock.getData() != nullptr);
+
+ MemoryBlock frouMemoryBlock(12, false);
+ EXPECT_EQ(frouMemoryBlock.getSize(), 12);
+ EXPECT_TRUE(frouMemoryBlock.getData() != nullptr);
+
+ char *buf = (char *) malloc(sizeof(char) * 9);
+ strcpy(buf, "RocketMQ");
+ MemoryBlock fiveMemoryBlock(buf, -1);
+ EXPECT_EQ(fiveMemoryBlock.getSize(), -1);
+ EXPECT_TRUE(fiveMemoryBlock.getData() == nullptr);
+
+ char *bufNull = NULL;
+ MemoryBlock sixMemoryBlock(bufNull, 16);
+ EXPECT_EQ(sixMemoryBlock.getSize(), 16);
+ EXPECT_TRUE(sixMemoryBlock.getData() != nullptr);
+
+ MemoryBlock sevenMemoryBlock(buf, 20);
+ EXPECT_EQ(sevenMemoryBlock.getSize(), 20);
+ sevenMemoryBlock.getData();
+ EXPECT_EQ(string(sevenMemoryBlock.getData()), string(buf));
+
+ MemoryBlock nineMemoryBlock(sevenMemoryBlock);
+ EXPECT_EQ(nineMemoryBlock.getSize(), sevenMemoryBlock.getSize());
+ EXPECT_EQ(string(nineMemoryBlock.getData()),
string(sevenMemoryBlock.getData()));
+
+ MemoryBlock eightMemoryBlock(fiveMemoryBlock);
+ EXPECT_EQ(eightMemoryBlock.getSize(), fiveMemoryBlock.getSize());
+ EXPECT_TRUE(eightMemoryBlock.getData() == nullptr);
+
+ free(buf);
+}
+
+TEST(memoryBlock, operators) {
+ MemoryBlock memoryBlock(12, false);
+
+ MemoryBlock operaterMemoryBlock = memoryBlock;
+
+ EXPECT_TRUE(operaterMemoryBlock == memoryBlock);
+
+ char *buf = (char *) malloc(sizeof(char) * 9);
+ strcpy(buf, "RocketMQ");
+ MemoryBlock twoMemoryBlock(buf, 12);
+ EXPECT_FALSE(memoryBlock == twoMemoryBlock);
+
+ MemoryBlock threeMemoryBlock(buf, 16);
+ EXPECT_FALSE(memoryBlock == threeMemoryBlock);
+ EXPECT_TRUE(twoMemoryBlock != threeMemoryBlock);
+
+ threeMemoryBlock.fillWith(49);
+ EXPECT_EQ(string(threeMemoryBlock.getData()), "1111111111111111");
+
+ threeMemoryBlock.reset();
+ EXPECT_EQ(threeMemoryBlock.getSize(), 0);
+ EXPECT_TRUE(threeMemoryBlock.getData() == nullptr);
+
+ threeMemoryBlock.setSize(16, 0);
+ EXPECT_EQ(threeMemoryBlock.getSize(), 16);
+ // EXPECT_EQ(threeMemoryBlock.getData() , buf);
+
+ threeMemoryBlock.setSize(0, 0);
+ EXPECT_EQ(threeMemoryBlock.getSize(), 0);
+ EXPECT_TRUE(threeMemoryBlock.getData() == nullptr);
+
+ MemoryBlock appendMemoryBlock;
+ EXPECT_EQ(appendMemoryBlock.getSize(), 0);
+ EXPECT_TRUE(appendMemoryBlock.getData() == nullptr);
+
+ appendMemoryBlock.append(buf, -1);
+ EXPECT_EQ(appendMemoryBlock.getSize(), 0);
+ EXPECT_TRUE(appendMemoryBlock.getData() == nullptr);
+
+ appendMemoryBlock.append(buf, 8);
+ EXPECT_EQ(appendMemoryBlock.getSize(), 8);
+
+ EXPECT_EQ(string(appendMemoryBlock.getData()), "RocketMQ11111111");
+
+ MemoryBlock replaceWithMemoryBlock;
+ replaceWithMemoryBlock.append(buf, 8);
+
+ char *aliyunBuf = (char *) malloc(sizeof(char) * 8);
+ strcpy(aliyunBuf, "aliyun");
+ replaceWithMemoryBlock.replaceWith(aliyunBuf, 0);
+ EXPECT_EQ(replaceWithMemoryBlock.getSize(), 8);
+ EXPECT_EQ(string(replaceWithMemoryBlock.getData()), "RocketMQ");
+
+ replaceWithMemoryBlock.replaceWith(aliyunBuf, 6);
+ EXPECT_EQ(replaceWithMemoryBlock.getSize(), 6);
+ EXPECT_EQ(string(replaceWithMemoryBlock.getData()), "aliyunMQ");
+
+ MemoryBlock insertMemoryBlock;
+ insertMemoryBlock.append(buf, 8);
+
+ insertMemoryBlock.insert(aliyunBuf, -1, -1);
+ EXPECT_EQ(string(insertMemoryBlock.getData()), "RocketMQ");
+
+ /* MemoryBlock fourInsertMemoryBlock;
+ fourInsertMemoryBlock.append(buf , 8);
+ // 6+ (-1)
+ fourInsertMemoryBlock.insert(aliyunBuf , 8 , -1);
+ string fourStr( fourInsertMemoryBlock.getData());
+ EXPECT_TRUE( fourStr == "liyun");*/
+
+ MemoryBlock twoInsertMemoryBlock;
+ twoInsertMemoryBlock.append(buf, 8);
+ twoInsertMemoryBlock.insert(aliyunBuf, 8, 0);
+ EXPECT_EQ(string(twoInsertMemoryBlock.getData()), "aliyun");
+
+ MemoryBlock threeInsertMemoryBlock;
+ threeInsertMemoryBlock.append(buf, 8);
+ threeInsertMemoryBlock.insert(aliyunBuf, 6, 100);
+ EXPECT_EQ(string(threeInsertMemoryBlock.getData()), "RocketMQaliyun");
+
+ MemoryBlock removeSectionMemoryBlock(buf, 8);
+ removeSectionMemoryBlock.removeSection(8, -1);
+ EXPECT_EQ(string(removeSectionMemoryBlock.getData()), "RocketMQ");
+
+ MemoryBlock twoRemoveSectionMemoryBlock(buf, 8);
+ twoRemoveSectionMemoryBlock.removeSection(1, 4);
+ string str(twoRemoveSectionMemoryBlock.getData(), 4);
+ EXPECT_TRUE(str == "RtMQ");
+
+ free(buf);
+ free(aliyunBuf);
+}
+
+int main(int argc, char *argv[]) {
+ InitGoogleMock(&argc, argv);
+ testing::GTEST_FLAG(throw_on_failure) = true;
+ testing::GTEST_FLAG(filter) = "memoryBlock.*";
+ int itestts = RUN_ALL_TESTS();
+ return itestts;
+}
diff --git a/test/src/common/big_endianTest.cpp
b/test/src/common/big_endianTest.cpp
new file mode 100644
index 0000000..d4c6fe3
--- /dev/null
+++ b/test/src/common/big_endianTest.cpp
@@ -0,0 +1,100 @@
+/*
+ * 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 <stdlib.h>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+#include "big_endian.h"
+
+using ::testing::InitGoogleMock;
+using ::testing::InitGoogleTest;
+using testing::Return;
+
+using rocketmq::BigEndianReader;
+using rocketmq::BigEndianWriter;
+
+TEST(big_endian, bigEndianObject) {
+ char *buf = (char *) malloc(sizeof(char) * 32);
+
+ BigEndianWriter writer(buf, 32);
+ BigEndianReader reader(buf, 32);
+
+ uint8_t *unit8 = (uint8_t *) malloc(sizeof(uint8_t));
+ EXPECT_TRUE(writer.WriteU8((uint8_t) 12));
+ EXPECT_TRUE(reader.ReadU8(unit8));
+ EXPECT_EQ(*unit8, 12);
+ free(unit8);
+
+ uint16_t *unit16 = (uint16_t *) malloc(sizeof(uint16_t));
+ EXPECT_TRUE(writer.WriteU16((uint16_t) 1200));
+ EXPECT_TRUE(reader.ReadU16(unit16));
+ EXPECT_EQ(*unit16, 1200);
+ free(unit16);
+
+ uint32_t *unit32 = (uint32_t *) malloc(sizeof(uint32_t));
+
+ EXPECT_TRUE(writer.WriteU32((uint32_t) 120000));
+ EXPECT_TRUE(reader.ReadU32(unit32));
+ EXPECT_EQ(*unit32, 120000);
+ free(unit32);
+
+ uint64_t *unit64 = (uint64_t *) malloc(sizeof(uint64_t));
+
+ EXPECT_TRUE(writer.WriteU64((uint64_t) 120000));
+ EXPECT_TRUE(reader.ReadU64(unit64));
+ EXPECT_EQ(*unit64, 120000);
+ free(unit64);
+
+ char *newBuf = (char *) malloc(sizeof(char) * 8);
+ char *writeBuf = (char *) malloc(sizeof(char) * 8);
+ strcpy(writeBuf, "RocketMQ");
+ EXPECT_TRUE(writer.WriteBytes(writeBuf, (size_t) 8));
+ EXPECT_TRUE(reader.ReadBytes(newBuf, (size_t) 8));
+ EXPECT_EQ(*writeBuf, *newBuf);
+
+ free(newBuf);
+ free(writeBuf);
+}
+
+TEST(big_endian, bigEndian) {
+ char writeBuf[8];
+
+ /*TODO
+ char *newBuf = (char *) malloc(sizeof(char) * 8);
+ strcpy(newBuf, "RocketMQ");
+
+ char readBuf[8];
+ rocketmq::WriteBigEndian(writeBuf, newBuf);
+ rocketmq::ReadBigEndian(writeBuf, readBuf);
+ EXPECT_EQ(writeBuf, readBuf);
+ */
+
+ rocketmq::WriteBigEndian(writeBuf, (uint8_t) 12);
+ uint8_t *out = (uint8_t *) malloc(sizeof(uint8_t));
+ rocketmq::ReadBigEndian(writeBuf, out);
+ EXPECT_EQ(*out, 12);
+ free(out);
+}
+
+int main(int argc, char *argv[]) {
+ InitGoogleMock(&argc, argv);
+ testing::GTEST_FLAG(throw_on_failure) = true;
+ testing::GTEST_FLAG(filter) = "big_endian.*";
+ int itestts = RUN_ALL_TESTS();
+ return itestts;
+}