手撕基于AMQP协议的简易消息队列-8(单元测试的编写)

在MQTest中编写模块的单元测试

在MQTest中编写makefile文件来编译客户端模块
all:Test_FileHelper Test_Exchange Test_Queue Test_Binding Test_Message Test_VirtualHost Test_Route Test_Consumer Test_Channel Test_Connection Test_VirtualHost:Test_VirtualHost.cpp ../MQCommon/message.pb.ccg++ -g -o $@ $^ -std=c++11 -lgtest -lprotobuf -lsqlite3 -pthreadTest_Binding:Test_Binding.cppg++ -g -o $@ $^ -std=c++11 -lgtest -lprotobuf -lsqlite3 -pthreadTest_Queue:Test_Queue.cppg++ -g -o $@ $^ -std=c++11 -lgtest -lprotobuf -lsqlite3 -pthreadTest_FileHelper:Test_FileHelper.cppg++ -o $@ $^ -std=c++11Test_Exchange:Test_Exchange.cppg++ -g -std=c++11 $^ -o $@ -lgtest -lprotobuf -lsqlite3Test_Message:Test_Message.cpp ../MQCommon/message.pb.ccg++ -g -std=c++11 $^ -o $@ -lgtest -lprotobuf -lsqlite3Test_Route:Test_Route.cpp ../MQCommon/message.pb.ccg++ -g -std=c++11 $^ -o $@ -lgtest -lprotobuf -lsqlite3Test_Consumer:Test_Consumer.cpp ../MQCommon/message.pb.ccg++ -g -std=c++11 $^ -o $@ -lgtest -lprotobuf -lsqlite3Test_Channel:Test_Channel.cpp ../MQCommon/message.pb.cc ../MQCommon/request.pb.ccg++ -g -std=c++11 $^ -o $@ -lgtest -lprotobuf -lsqlite3 -I../ThirdLib/lib/includeTest_Connection:Test_Connection.cpp ../MQCommon/message.pb.cc ../MQCommon/request.pb.ccg++ -g -std=c++11 $^ -o $@ -lgtest -lprotobuf -lsqlite3 -I../ThirdLib/lib/include.PHONY:
clean:rm -rf Test_FileHelper Test_Exchange Test_Queue Test_Binding Test_Message Test_VirtualHost Test_Route Test_Consumer Test_Channel Test_Connection
在MQTest中编写Test_Exchange.cpp文件来编译交换机模块单元测试
#include "../MQServer/Exchange.hpp"
#include <gtest/gtest.h>
#include<unordered_map>
MQ::ExchangeManager::ptr emp;class ExchangeTest : public testing::Environment
{
public:virtual void SetUp() override{emp = std::make_shared<MQ::ExchangeManager>("./data/meta.db");}virtual void TearDown() override{emp->clear();std::cout << "最后的清理!!\n";}
};TEST(exchange_test, insert_test)
{std::unordered_map<std::string, std::string> tmp =  {{"k1","v1"},{"k2","v2"}};std::unordered_map<std::string, std::string> map;map.insert(tmp.begin(),tmp.end());for(auto& a : map){std::cout<<a.first<<' '<<a.second<<std::endl;}emp->declareExchange("exchange1", MQ::ExchangeType::DIRECT, true, false, map);emp->declareExchange("exchange2", MQ::ExchangeType::DIRECT, true, false, map);emp->declareExchange("exchange3", MQ::ExchangeType::DIRECT, true, false, map);emp->declareExchange("exchange4", MQ::ExchangeType::DIRECT, true, false, map);ASSERT_EQ(emp->size(), 4);
}TEST(exchange_test, select_test)
{ASSERT_EQ(emp->exists("exchange1"), false);ASSERT_EQ(emp->exists("exchange2"), true);ASSERT_EQ(emp->exists("exchange3"), true);ASSERT_EQ(emp->exists("exchange4"), true);MQ::Exchange::ptr exp = emp->selectExchange("exchange1");ASSERT_NE(exp.get(), nullptr);ASSERT_EQ(exp->_name, "exchange1");ASSERT_EQ(exp->_durable, true);ASSERT_EQ(exp->_auto_delete, false);ASSERT_EQ(exp->_type, MQ::ExchangeType::DIRECT);ASSERT_EQ(exp->getArgs(), std::string("k1=v1&k2=v2&"));
}TEST(exchange_test, remove_test)
{emp->deleteExchange("exchange2");MQ::Exchange::ptr exp = emp->selectExchange("exchange2");ASSERT_EQ(exp.get(), nullptr);ASSERT_EQ(emp->exists("exchange2"), false);
}int main(int argc, char *argv[])
{testing::InitGoogleTest(&argc, argv);testing::AddGlobalTestEnvironment(new ExchangeTest);RUN_ALL_TESTS();return 0;
}
在MQTest中编写Test_Binding.cpp文件来编译绑定模块单元测试
#include "../MQServer/Binding.hpp"
#include <gtest/gtest.h>MQ::BindingManager::ptr bmp;class QueueTest : public testing::Environment {public:virtual void SetUp() override {bmp = std::make_shared<MQ::BindingManager>("./data/meta.db");}virtual void TearDown() override {// bmp->clear();}
};// TEST(queue_test, insert_test) {
//     bmp->bind("exchange1", "queue1", "news.music.#", true);
//     bmp->bind("exchange1", "queue2", "news.sport.#", true);
//     bmp->bind("exchange1", "queue3", "news.gossip.#", true);
//     bmp->bind("exchange2", "queue1", "news.music.pop", true);
//     bmp->bind("exchange2", "queue2", "news.sport.football", true);
//     bmp->bind("exchange2", "queue3", "news.gossip.#", true);
//     ASSERT_EQ(bmp->size(), 6);
// }TEST(queue_test, recovery_test) {ASSERT_EQ(bmp->exist("exchange1", "queue1"), false);ASSERT_EQ(bmp->exist("exchange1", "queue2"), false);ASSERT_EQ(bmp->exist("exchange1", "queue3"), false);ASSERT_EQ(bmp->exist("exchange2", "queue1"), true);ASSERT_EQ(bmp->exist("exchange2", "queue2"), false);ASSERT_EQ(bmp->exist("exchange2", "queue3"), true);
}TEST(queue_test, select_test) {ASSERT_EQ(bmp->exist("exchange1", "queue1"), false);ASSERT_EQ(bmp->exist("exchange1", "queue2"), false);ASSERT_EQ(bmp->exist("exchange1", "queue3"), false);ASSERT_EQ(bmp->exist("exchange2", "queue1"), true);ASSERT_EQ(bmp->exist("exchange2", "queue2"), false);ASSERT_EQ(bmp->exist("exchange2", "queue3"), true);MQ::Binding::ptr bp = bmp->getBinding("exchange2", "queue1");ASSERT_NE(bp.get(), nullptr);ASSERT_EQ(bp->name_exchange, std::string("exchange2"));ASSERT_EQ(bp->name_queue, std::string("queue1"));ASSERT_EQ(bp->binding_key, std::string("news.music.pop"));
}TEST(queue_test, select_exchange_test) {MQ::QueueBindingMap mqbm = bmp->getExchangeBindings("exchange2");ASSERT_EQ(mqbm.size(), 2);ASSERT_NE(mqbm.find("queue1"), mqbm.end());ASSERT_EQ(mqbm.find("queue2"), mqbm.end());ASSERT_NE(mqbm.find("queue3"), mqbm.end());
}// // e2-q3// TEST(queue_test, remove_exchange_test) {
//     bmp->unbindByExchange("exchange1");
//     ASSERT_EQ(bmp->exist("exchange1", "queue1"), false);
//     ASSERT_EQ(bmp->exist("exchange1", "queue2"), false);
//     ASSERT_EQ(bmp->exist("exchange1", "queue3"), false);
// }// TEST(queue_test, remove_single_test) {
//     ASSERT_EQ(bmp->exist("exchange2", "queue2"), true);
//     bmp->unbind("exchange2", "queue2");
//     ASSERT_EQ(bmp->exist("exchange2", "queue2"), false);
//     ASSERT_EQ(bmp->exist("exchange2", "queue3"), true);
// }int main(int argc,char *argv[])
{testing::InitGoogleTest(&argc, argv);testing::AddGlobalTestEnvironment(new QueueTest);RUN_ALL_TESTS();return 0;
}
在MQTest中编写Test_Channel.cpp文件来编译信道模块单元测试
#include "../MQServer/Channel.hpp"int main()
{MQ::ChannelManager::ptr cmp = std::make_shared<MQ::ChannelManager>();cmp->openChannel("c1", std::make_shared<MQ::VirtualHost>("host1", "./data/host1/message/", "./data/host1/host1.db"),std::make_shared<MQ::ConsumerManager>(),MQ::ProtobufCodecPtr(),muduo::net::TcpConnectionPtr(),MQ::ThreadPool::ptr());return 0;
}
在MQTest中编写Test_Connection.cpp文件来编译链接模块单元测试
#include "../MQServer/Connection.hpp"int main()
{MQ::ConnectionManager::ptr cmp = std::make_shared<MQ::ConnectionManager>();cmp->newConnection(std::make_shared<MQ::VirtualHost>("host1", "./data/host1/message/", "./data/host1/host1.db"),std::make_shared<MQ::ConsumerManager>(),MQ::ProtobufCodecPtr(),muduo::net::TcpConnectionPtr(),MQ::ThreadPool::ptr());return 0;
}
在MQTest中编写Test_Consumer.cpp文件来编译消费者模块单元测试
#include "../MQServer/Consumer.hpp"
#include <gtest/gtest.h>MQ::ConsumerManager::ptr cmp;class ConsumerTest : public testing::Environment {public:virtual void SetUp() override {cmp = std::make_shared<MQ::ConsumerManager>();cmp->initQueueConsumer("queue1");}virtual void TearDown() override {cmp->clear();}
};void cb(const std::string &tag, const MQ::BasicProperties *bp, const std::string &body) 
{std::cout << tag << " 消费了消息:" << body << std::endl;
}TEST(consumer_test, insert_test) {cmp->createConsumer("consumer1", "queue1", false, cb);cmp->createConsumer("consumer2", "queue1", false, cb);cmp->createConsumer("consumer3", "queue1", false, cb);ASSERT_EQ(cmp->isExist("consumer1", "queue1"), true);ASSERT_EQ(cmp->isExist("consumer2", "queue1"), true);ASSERT_EQ(cmp->isExist("consumer3", "queue1"), true);
}
TEST(consumer_test, remove_test) {cmp->removeConsumer("consumer1", "queue1");ASSERT_EQ(cmp->isExist("consumer1", "queue1"), false);ASSERT_EQ(cmp->isExist("consumer2", "queue1"), true);ASSERT_EQ(cmp->isExist("consumer3", "queue1"), true);
}TEST(consumer_test, choose_test) {MQ::Consumer::ptr cp = cmp->chooseConsumer("queue1");ASSERT_NE(cp.get(), nullptr);ASSERT_EQ(cp->_consumer_tag, "consumer2");cp = cmp->chooseConsumer("queue1");ASSERT_NE(cp.get(), nullptr);ASSERT_EQ(cp->_consumer_tag, "consumer3");cp = cmp->chooseConsumer("queue1");ASSERT_NE(cp.get(), nullptr);ASSERT_EQ(cp->_consumer_tag, "consumer2");
}int main(int argc,char *argv[])
{testing::InitGoogleTest(&argc, argv);testing::AddGlobalTestEnvironment(new ConsumerTest);RUN_ALL_TESTS();return 0;
}
在MQTest中编写Test_FileHelper.cpp文件来文件操作类单元测试
#include "../MQCommon/Helper.hpp"int main()
{FileHelper helper("../MQCommon/Helper.hpp");DLOG("是否存在:%d", helper.exists());DLOG("文件大小:%ld", helper.size());FileHelper tmp_helper("./aaa/bbb/ccc/tmp.hpp");if (tmp_helper.exists() == false){std::string path = FileHelper::parentDirectory("./aaa/bbb/ccc/tmp.hpp");DLOG("path:%s",path.c_str());if (FileHelper(path).exists() == false){FileHelper::createDirectory(path);}FileHelper::createFile("./aaa/bbb/ccc/tmp.hpp");}std::string body;helper.read(body);DLOG("读测试,读出的内容:%s",body.c_str());tmp_helper.write(body);FileHelper tmp_helper1("./aaa/bbb/ccc/tmp.hpp");char str[16] = {0};tmp_helper.read(str, 8, 11);DLOG("[%s]", str);tmp_helper.write("12345678901", 8, 11);tmp_helper.rename("./aaa/bbb/ccc/test.hpp");FileHelper::removeFile("./aaa/bbb/ccc/test.hpp");FileHelper::removeDirectory("./aaa");return 0;
}
在MQTest中编写Test_Message.cpp文件来编译消息模块单元测试
#include "../MQServer/Message.hpp"
#include <gtest/gtest.h>MQ::MessageManager::ptr mmp;class MessageTest : public testing::Environment {public:virtual void SetUp() override {mmp = std::make_shared<MQ::MessageManager>("./data/message/");mmp->initQueueMessage("queue1");}virtual void TearDown() override {// mmp->clear();}
};//新增消息测试:新增消息,然后观察可获取消息数量,以及持久化消息数量
// TEST(message_test, insert_test) 
// {
//   MQ::BasicProperties properties;
//     properties.set_id(UUIDHelper::uuid());
//     properties.set_delivery_mode(MQ::DeliveryMode::DURABLE);
//     properties.set_routing_key("news.music.pop");
//     mmp->insert("queue1", &properties, "Hello World-1", true);
//     mmp->insert("queue1", nullptr, "Hello World-2", true);
//     mmp->insert("queue1", nullptr, "Hello World-3", true);
//     mmp->insert("queue1", nullptr, "Hello World-4", true);
//     mmp->insert("queue1", nullptr, "Hello World-5", false);
//     ASSERT_EQ(mmp->getAbleCount("queue1"), 5);
//     ASSERT_EQ(mmp->getTotalCount("queue1"), 4);
//     ASSERT_EQ(mmp->getDurableCount("queue1"), 4);
//     ASSERT_EQ(mmp->getWaitAckCount("queue1"), 0);
// }//获取消息测试:获取一条消息,然后在不进行确认的情况下,查看可获取消息数量,待确认消息数量,以及测试消息获取的顺序
// TEST(message_test, select_test) {
//     ASSERT_EQ(mmp->getAbleCount("queue1"), 5);
//     MQ::MessagePtr msg1 = mmp->front("queue1");
//     ASSERT_NE(msg1.get(), nullptr);
//     ASSERT_EQ(msg1->payload().body(), std::string("Hello World-1"));
//     ASSERT_EQ(mmp->getAbleCount("queue1"), 4);
//     ASSERT_EQ(mmp->getWaitAckCount("queue1"), 1);//     MQ::MessagePtr msg2 = mmp->front("queue1");
//     ASSERT_NE(msg2.get(), nullptr);
//     ASSERT_EQ(msg2->payload().body(), std::string("Hello World-2"));
//     ASSERT_EQ(mmp->getAbleCount("queue1"), 3);
//     ASSERT_EQ(mmp->getWaitAckCount("queue1"), 2);//     MQ::MessagePtr msg3 = mmp->front("queue1");
//     ASSERT_NE(msg3.get(), nullptr);
//     ASSERT_EQ(msg3->payload().body(), std::string("Hello World-3"));
//     ASSERT_EQ(mmp->getAbleCount("queue1"), 2);
//     ASSERT_EQ(mmp->getWaitAckCount("queue1"), 3);//     MQ::MessagePtr msg4 = mmp->front("queue1");
//     ASSERT_NE(msg4.get(), nullptr);
//     ASSERT_EQ(msg4->payload().body(), std::string("Hello World-4"));
//     ASSERT_EQ(mmp->getAbleCount("queue1"), 1);
//     ASSERT_EQ(mmp->getWaitAckCount("queue1"), 4);//     MQ::MessagePtr msg5 = mmp->front("queue1");
//     ASSERT_NE(msg5.get(), nullptr);
//     ASSERT_EQ(msg5->payload().body(), std::string("Hello World-5"));
//     ASSERT_EQ(mmp->getAbleCount("queue1"), 0);
//     ASSERT_EQ(mmp->getWaitAckCount("queue1"), 5);
// }
//删除消息测试:确认一条消息,查看持久化消息数量,待确认消息数量
// TEST(message_test, delete_test) {
//     ASSERT_EQ(mmp->getAbleCount("queue1"), 5);
//     MQ::MessagePtr msg1 = mmp->front("queue1");
//     ASSERT_NE(msg1.get(), nullptr);
//     ASSERT_EQ(msg1->payload().body(), std::string("Hello World-1"));
//     ASSERT_EQ(mmp->getAbleCount("queue1"), 4);
//     ASSERT_EQ(mmp->getWaitAckCount("queue1"), 1);//     mmp->ack("queue1", msg1->payload().properties().id());
//     ASSERT_EQ(mmp->getWaitAckCount("queue1"), 0);
//     ASSERT_EQ(mmp->getDurableCount("queue1"), 3);
//     ASSERT_EQ(mmp->getTotalCount("queue1"), 4);
// }
// 销毁测试
// TEST(message_test, clear) {
//     mmp->destroyQueueMessage("queue1");
// }//恢复历史数据测试MQ::MessageManager::ptr mmp1;
class MessageTest2 : public testing::Environment {public:virtual void SetUp() override {mmp1 = std::make_shared<MQ::MessageManager>("./data/message/");}virtual void TearDown() override {//mmp->clear();}
};
TEST(message_test2, recovery_test) {mmp1->initQueueMessage("queue1");ASSERT_EQ(mmp1->getAbleCount("queue1"), 4);MQ::MessagePtr msg1 = mmp1->front("queue1");ASSERT_NE(msg1.get(), nullptr);ASSERT_EQ(msg1->payload().body(), std::string("Hello World-1"));ASSERT_EQ(mmp1->getAbleCount("queue1"), 3);ASSERT_EQ(mmp1->getWaitAckCount("queue1"), 1);MQ::MessagePtr msg2 = mmp1->front("queue1");ASSERT_NE(msg2.get(), nullptr);ASSERT_EQ(msg2->payload().body(), std::string("Hello World-2"));ASSERT_EQ(mmp1->getAbleCount("queue1"), 2);ASSERT_EQ(mmp1->getWaitAckCount("queue1"), 2);MQ::MessagePtr msg3 = mmp1->front("queue1");ASSERT_NE(msg3.get(), nullptr);ASSERT_EQ(msg3->payload().body(), std::string("Hello World-3"));ASSERT_EQ(mmp1->getAbleCount("queue1"), 1);ASSERT_EQ(mmp1->getWaitAckCount("queue1"), 3);MQ::MessagePtr msg4 = mmp1->front("queue1");ASSERT_NE(msg4.get(), nullptr);ASSERT_EQ(msg4->payload().body(), std::string("Hello World-4"));ASSERT_EQ(mmp1->getAbleCount("queue1"), 0);ASSERT_EQ(mmp1->getWaitAckCount("queue1"), 4);
}int main(int argc,char *argv[])
{testing::InitGoogleTest(&argc, argv);//testing::AddGlobalTestEnvironment(new MessageTest);testing::AddGlobalTestEnvironment(new MessageTest2);RUN_ALL_TESTS();return 0;
}
在MQTest中编写Test_Queue.cpp文件来编译队列模块单元测试
#include "../MQServer/Queue.hpp"
#include <gtest/gtest.h>MQ::QueueManager::ptr mqmp;class QueueTest : public testing::Environment {public:virtual void SetUp() override {mqmp = std::make_shared<MQ::QueueManager>("./data/meta.db");DLOG("创建数据库文件");}virtual void TearDown() override {// mqmp->clear();}
};// TEST(queue_test, insert_test) {
//     std::unordered_map<std::string, std::string> map = {{"k1", "v1"}};
//     mqmp->declareQueue("queue1", true, false, false, map);
//     mqmp->declareQueue("queue2", true, false, false, map);
//     mqmp->declareQueue("queue3", true, false, false, map);
//     mqmp->declareQueue("queue4", true, false, false, map);
//     ASSERT_EQ(mqmp->size(), 4);
// }TEST(queue_test, select_test) {ASSERT_EQ(mqmp->exist("queue1"), true);ASSERT_EQ(mqmp->exist("queue2"), true);ASSERT_EQ(mqmp->exist("queue3"), false);ASSERT_EQ(mqmp->exist("queue4"), true);MQ::Queue::ptr mqp = mqmp->selectQueue("queue1");ASSERT_NE(mqp.get(), nullptr);ASSERT_EQ(mqp->_name, "queue1");ASSERT_EQ(mqp->_durable, true);ASSERT_EQ(mqp->_exclusive, false);ASSERT_EQ(mqp->_auto_delete, false);ASSERT_EQ(mqp->getArgs(), std::string("k1=v1&"));
}TEST(queue_test, remove_test) {mqmp->deleteQueue("queue3");ASSERT_EQ(mqmp->exist("queue3"), false);
}int main(int argc,char *argv[])
{testing::InitGoogleTest(&argc, argv);testing::AddGlobalTestEnvironment(new QueueTest);RUN_ALL_TESTS();return 0;
}
在MQTest中编写Test_Route.cpp文件来编译路由模块单元测试
#include "../MQServer/Route.hpp"
#include <gtest/gtest.h>class QueueTest : public testing::Environment {public:virtual void SetUp() override {}virtual void TearDown() override {//bmp->clear();}
};TEST(route_test, legal_routing_key) {std::string rkey1 = "news.music.pop";std::string rkey2 = "news..music.pop";std::string rkey3 = "news.,music.pop";std::string rkey4 = "news.music_123.pop";ASSERT_EQ(MQ::RouteManager::isValidRoutingKey(rkey1), true);ASSERT_EQ(MQ::RouteManager::isValidRoutingKey(rkey2), false);ASSERT_EQ(MQ::RouteManager::isValidRoutingKey(rkey3), false);ASSERT_EQ(MQ::RouteManager::isValidRoutingKey(rkey4), true);
}
TEST(route_test, legal_binding_key) {std::string bkey1 = "news.music.pop";std::string bkey2 = "news.#.music.pop";std::string bkey3 = "news.#.*.music.pop";//std::string bkey4 = "news.*.#.music.pop";//std::string bkey5 = "news.#.#.music.pop";//std::string bkey6 = "news.*.*.music.pop";std::string bkey7 = "news.,music_123.pop";//ASSERT_EQ(MQ::RouteManager::isValidBindingKey(bkey1), true);ASSERT_EQ(MQ::RouteManager::isValidBindingKey(bkey2), true);ASSERT_EQ(MQ::RouteManager::isValidBindingKey(bkey3), false);ASSERT_EQ(MQ::RouteManager::isValidBindingKey(bkey4), false);ASSERT_EQ(MQ::RouteManager::isValidBindingKey(bkey5), false);ASSERT_EQ(MQ::RouteManager::isValidBindingKey(bkey6), true);ASSERT_EQ(MQ::RouteManager::isValidBindingKey(bkey7), false);
}
TEST(route_test, route) {
// aaa                  aaa                     true
// aaa.bbb              aaa.bbb                 true
// aaa.bbb              aaa.bbb.ccc             false
// aaa.bbb              aaa.ccc                 false
// aaa.#.bbb            aaa.bbb.ccc             false
// aaa.bbb.#            aaa.ccc.bbb             false
// #.bbb.ccc            aaa.bbb.ccc.ddd         false
// aaa.bbb.ccc          aaa.bbb.ccc             true
// aaa.*                aaa.bbb                 true
// aaa.*.bbb            aaa.bbb.ccc             false
// *.aaa.bbb            aaa.bbb                 false
// #                    aaa.bbb.ccc             true
// aaa.#                aaa.bbb                 true
// aaa.#                aaa.bbb.ccc             true
// aaa.#.ccc            aaa.ccc                 true
// aaa.#.ccc            aaa.bbb.ccc             true
// aaa.#.ccc            aaa.aaa.bbb.ccc         true
// #.ccc                ccc                     true
// #.ccc                aaa.bbb.ccc             true
// aaa.#.ccc.ccc        aaa.bbb.ccc.ccc.ccc     true
// aaa.#.bbb.*.bbb      aaa.ddd.ccc.bbb.eee.bbb truestd::vector<std::string> bkeys = {"aaa","aaa.bbb","aaa.bbb", "aaa.bbb","aaa.#.bbb","aaa.bbb.#","#.bbb.ccc","aaa.bbb.ccc","aaa.*","aaa.*.bbb","*.aaa.bbb", "#",   "aaa.#", "aaa.#",  "aaa.#.ccc","aaa.#.ccc","aaa.#.ccc","#.ccc","#.ccc","aaa.#.ccc.ccc","aaa.#.bbb.*.bbb"};std::vector<std::string> rkeys = {"aaa","aaa.bbb",    "aaa.bbb.ccc",        "aaa.ccc",        "aaa.bbb.ccc",        "aaa.ccc.bbb",        "aaa.bbb.ccc.ddd",    "aaa.bbb.ccc",       "aaa.bbb",         "aaa.bbb.ccc",      "aaa.bbb",         "aaa.bbb.ccc",       "aaa.bbb",        "aaa.bbb.ccc",     "aaa.ccc",        "aaa.bbb.ccc",       "aaa.aaa.bbb.ccc",  "ccc",         "aaa.bbb.ccc",    "aaa.bbb.ccc.ccc.ccc","aaa.ddd.ccc.bbb.eee.bbb"};std::vector<bool> result = {true,true,false,false,false,false,false,true,true,false,false,true,true,true,true,true,true,true,true,true,true};for (int i = 0; i < bkeys.size(); i++) {ASSERT_EQ(MQ::RouteManager::route(MQ::ExchangeType::TOPIC, rkeys[i], bkeys[i]), result[i]);}
}int main(int argc,char *argv[])
{testing::InitGoogleTest(&argc, argv);testing::AddGlobalTestEnvironment(new QueueTest);RUN_ALL_TESTS();return 0;
}
在MQTest中编写Test_VirtualHost.cpp文件来编译虚拟机模块单元测试
#include <gtest/gtest.h>
#include "../MQServer/VirtualHost.hpp"class HostTest : public testing::Test {public:void SetUp() override {std::unordered_map<std::string, std::string> empty_map = std::unordered_map<std::string, std::string>();_host = std::make_shared<MQ::VirtualHost>("host1", "./data/host1/message/", "./data/host1/host1.db");_host->declareExchange("exchange1", MQ::ExchangeType::DIRECT, true, false, empty_map);_host->declareExchange("exchange2", MQ::ExchangeType::DIRECT, true, false, empty_map);_host->declareExchange("exchange3", MQ::ExchangeType::DIRECT, true, false, empty_map);_host->declareQueue("queue1", true, false, false, empty_map);_host->declareQueue("queue2", true, false, false, empty_map);_host->declareQueue("queue3", true, false, false, empty_map);_host->bind("exchange1", "queue1", "news.music.#");_host->bind("exchange1", "queue2", "news.music.#");_host->bind("exchange1", "queue3", "news.music.#");_host->bind("exchange2", "queue1", "news.music.#");_host->bind("exchange2", "queue2", "news.music.#");_host->bind("exchange2", "queue3", "news.music.#");_host->bind("exchange3", "queue1", "news.music.#");_host->bind("exchange3", "queue2", "news.music.#");_host->bind("exchange3", "queue3", "news.music.#");_host->basicPublish("queue1", nullptr, "Hello World-1");_host->basicPublish("queue1", nullptr, "Hello World-2");_host->basicPublish("queue1", nullptr, "Hello World-3");_host->basicPublish("queue2", nullptr, "Hello World-1");_host->basicPublish("queue2", nullptr, "Hello World-2");_host->basicPublish("queue2", nullptr, "Hello World-3");_host->basicPublish("queue3", nullptr, "Hello World-1");_host->basicPublish("queue3", nullptr, "Hello World-2");_host->basicPublish("queue3", nullptr, "Hello World-3");}void TearDown() override {_host->clear();}public:MQ::VirtualHost::ptr _host;
};TEST_F(HostTest, init_test) {ASSERT_EQ(_host->existExchange("exchange1"), true);ASSERT_EQ(_host->existExchange("exchange2"), true);ASSERT_EQ(_host->existExchange("exchange3"), true);ASSERT_EQ(_host->existQueue("queue1"), true);ASSERT_EQ(_host->existQueue("queue2"), true);ASSERT_EQ(_host->existQueue("queue3"), true);ASSERT_EQ(_host->existBinding("exchange1", "queue1"), true);ASSERT_EQ(_host->existBinding("exchange1", "queue2"), true);ASSERT_EQ(_host->existBinding("exchange1", "queue3"), true);ASSERT_EQ(_host->existBinding("exchange2", "queue1"), true);ASSERT_EQ(_host->existBinding("exchange2", "queue2"), true);ASSERT_EQ(_host->existBinding("exchange2", "queue3"), true);ASSERT_EQ(_host->existBinding("exchange3", "queue1"), true);ASSERT_EQ(_host->existBinding("exchange3", "queue2"), true);ASSERT_EQ(_host->existBinding("exchange3", "queue3"), true);MQ::MessagePtr msg1 = _host->basicConsume("queue1");ASSERT_EQ(msg1->payload().body(), std::string("Hello World-1"));MQ::MessagePtr msg2 = _host->basicConsume("queue1");ASSERT_EQ(msg2->payload().body(), std::string("Hello World-2"));MQ::MessagePtr msg3 = _host->basicConsume("queue1");ASSERT_EQ(msg3->payload().body(), std::string("Hello World-3"));MQ::MessagePtr msg4 = _host->basicConsume("queue1");ASSERT_EQ(msg4.get(), nullptr);
}TEST_F(HostTest, remove_exchange) {_host->deleteExchange("exchange1");ASSERT_EQ(_host->existBinding("exchange1", "queue1"), false);ASSERT_EQ(_host->existBinding("exchange1", "queue2"), false);ASSERT_EQ(_host->existBinding("exchange1", "queue3"), false);
}TEST_F(HostTest, remove_queue) {_host->deleteQueue("queue1");ASSERT_EQ(_host->existBinding("exchange1", "queue1"), false);ASSERT_EQ(_host->existBinding("exchange2", "queue1"), false);ASSERT_EQ(_host->existBinding("exchange3", "queue1"), false);MQ::MessagePtr msg1 = _host->basicConsume("queue1");ASSERT_EQ(msg1.get(), nullptr);
}TEST_F(HostTest, ack_message) {MQ::MessagePtr msg1 = _host->basicConsume("queue1");ASSERT_EQ(msg1->payload().body(), std::string("Hello World-1"));_host->basicAck(std::string("queue1"), msg1->payload().properties().id());MQ::MessagePtr msg2 = _host->basicConsume("queue1");ASSERT_EQ(msg2->payload().body(), std::string("Hello World-2"));_host->basicAck(std::string("queue1"), msg2->payload().properties().id());MQ::MessagePtr msg3 = _host->basicConsume("queue1");ASSERT_EQ(msg3->payload().body(), std::string("Hello World-3"));_host->basicAck(std::string("queue1"), msg3->payload().properties().id());}int main(int argc, char *argv[])
{testing::InitGoogleTest(&argc, argv);RUN_ALL_TESTS();return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/80008.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spark 之 metrics

peak memory.//sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/HashAggregateExec.scala: “peakMemory” -> SQLMetrics.createSizeMetric(sparkContext, “peak memory”), .//sql/core/src/main/scala/org/apache/spark/sql/execution/SortExec.scal…

HTTP/HTTPS协议(请求响应模型、状态码)

目录 HTTP/HTTPS协议简介 HTTP协议 HTTPS协议 请求 - 响应模型 HTTP请求 &#xff08;二&#xff09;HTTP响应 HTTPS协议与HTTP协议在请求 - 响应模型中的区别 HTTP/HTTPS协议简介 HTTP协议 定义 HTTP&#xff08;HyperText Transfer Protocol&#xff09;即超文本传输…

OpenHarmony 5.0 切换已连接过的wifi切换失败

目录 1.背景 2.流程分析 3.方案 1.背景 在OpenHarmony 5.0的设置中,输入密码进行wifi连接可以正常连接,然后多个已经连接过的wifi进行切换发现大概率切换失败 2.流程分析 wifi连接过程其实是先进行断开当前的wifi连接,然后再连接另一个wifi,如下: 虽然上述流程看起来没…

【Ollama】docker离线部署Ollama+deepseek

因为要做项目&#xff0c;实验室的服务器不联网&#xff0c;所以只能先打包一个基础的docker环境&#xff0c;然后再在实验室的服务器上进行解压和配置环境 参考&#xff1a;https://zhuanlan.zhihu.com/p/23377266873 1.打包基础的docker环境 这里最好用有cuda的&#xff0c…

如何使用极狐GitLab 软件包仓库功能托管 terraform?

极狐GitLab 是 GitLab 在中国的发行版&#xff0c;关于中文参考文档和资料有&#xff1a; 极狐GitLab 中文文档极狐GitLab 中文论坛极狐GitLab 官网 Terraform 模块库 (BASIC ALL) 基础设施仓库和 Terraform 模块仓库合并到单个 Terraform 模块仓库功能引入于极狐GitLab 15.1…

【QT】深入理解 Qt 中的对象树:机制、用途与最佳实践

深入理解 Qt 中的对象树&#xff1a;机制、用途与最佳实践 在使用 Qt 编程时&#xff0c;你是否注意到很多对象可以设置“父对象”&#xff1f;比如&#xff1a; QPushButton* btn new QPushButton(parentWidget);这不是简单的层级结构&#xff0c;而是 Qt 强大而优雅的 对象…

比较入站和出站防火墙规则

组织需要仔细配置防火墙规则&#xff0c;监控网络的传入和传出流量&#xff0c;从而最大限度降低遭受攻击的风险。在有效管理入站和出站防火墙规则前&#xff0c;了解入站与出站流量的区别至关重要。 一、什么是入站流量&#xff1f; 入站流量指的是并非源自网络内部&#xf…

Unity-Shader详解-其五

关于Unity的Shader部分的基础知识其实已经讲解得差不多了&#xff0c;今天我们来一些实例分享&#xff1a; 溶解 效果如下&#xff1a; 代码如下&#xff1a; Shader "Chapter8/chapter8_1" {Properties{// 定义属性[NoScaleOffset]_Albedo("Albedo", 2…

COLT_CMDB_linux_userInfo_20250508.sh修复历史脚本输出指标信息中userName与输出信息不一致问题

#!/bin/bash #IT_BEGIN #IT_TYPE3 #IT SYSTEM_LINUX_AGENTUSERDISCOVER|discovery.user[disc] #原型指标 #IT_RULE SYSTEM_LINUX_AGENTUSERGROUPID|groupId[{#USERNAME}] #IT_RULE SYSTEM_LINUX_AGENTUSERHOME|userHome[{#USERNAME}] #IT_RULE SYSTEM_LINUX_AGENTUSERNAME|user…

TCP 与 UDP报文

** TCP 与 UDP报文** 1. 引言 在网络通信中&#xff0c;TCP&#xff08;传输控制协议&#xff09; 和 UDP&#xff08;用户数据报协议&#xff09; 是两种最核心的传输层协议。它们各自适用于不同的场景&#xff0c;理解其工作原理对开发高性能网络应用至关重要。本文将详细解…

LabVIEW燃气轮机测控系统

在能源需求不断增长以及生态环境保护备受重视的背景下&#xff0c;微型燃气轮机凭借其在经济性、可靠性、维护性及排放性等方面的显著优势&#xff0c;在航空航天、分布式发电等众多领域得到广泛应用。随着计算机技术的快速发展&#xff0c;虚拟仪器应运而生&#xff0c;LabVIE…

基于vueflow可拖拽元素的示例(基于官网示例的单文件示例)

效果图 代码 <template><div style"width: 100%;height: calc(100vh - 84px)"><VueFlow :nodes"nodes" :edges"edges" drop"onDrop" dragover"onDragOver" dragleave"onDragLeave"><div cl…

笔试专题(十六)

文章目录 相差不超过k的最多数题解代码 最长公共子序列&#xff08;一&#xff09;题解代码 小红的口罩题解代码 春游题解代码 相差不超过k的最多数 题目链接 题解 1. 排序 滑动窗口 2. 为什么使用滑动窗口&#xff1f; 因为max-min < k&#xff0c;求这个区间内的数最…

技术视界 | 青龙机器人训练地形详解(三):复杂地形精讲之台阶

在前两篇中&#xff0c;我们依次讲解了“如何创建一个地形”以及“如何将地形添加到训练环境中”。从基础出发&#xff0c;逐步构建机器人可交互的三维仿真环境。在机器人强化学习训练中&#xff0c;地形的复杂度决定了策略的泛化能力&#xff0c;仅靠 jump_plat 和 jump_pit 等…

MYSQL之索引结构,为何要用B+树

索引的目的就是为了提高查询效率 索引的结构是B树&#xff0c;那么说到B树&#xff0c;必须提一下其他三种结构&#xff0c;分别是&#xff1a;二叉查找树、平衡二叉树、B树 我们来看看各自的结构特征 二叉查找树 特点:任何节点的左子节点的值都小于当前节点的值&#xff0c;右…

3.2.3 掌握RDD转换算子 - 2. 过滤算子 - filter()

在本节课中&#xff0c;我们深入学习了Spark RDD的过滤算子filter()。filter()算子能够通过指定的函数对RDD中的元素进行筛选&#xff0c;返回一个满足条件的新RDD&#xff0c;通常新RDD的元素个数会比源RDD少。通过案例演示&#xff0c;我们掌握了如何使用filter()来过滤列表中…

vue3使用轮播图组件swiper

一、在swiper的官网源码下载地址 下载Swiper - Swiper中文网 二、官网浏览轮播图类型地址 Swiper演示 - Swiper中文网 三、swiper配置参数地址 中文api - Swiper中文网 四、在vue3项目引入swiper npm install swiper 五、在vue3中使用 官网vue3中使用&#xff1a;Swiper…

MySQL优化-MySQL故障排查与监控

MySQL优化-MySQL故障排查与监控 一、MySQL监控 实时了解数据库的运行状态&#xff0c;通过不同的监控指标&#xff0c;识别潜在问题并进行预防。常见得到MySQL监控指标包括&#xff1a;连接数、缓存池命中率、磁盘I/O、查询执行情况等。 1、监控数据库状态变量 MySQL的状态…

【MongoDB篇】MongoDB的分片操作!

目录 引言第一节&#xff1a;分片核心概念&#xff1a;为什么要分片&#xff1f;它是什么&#xff1f; &#x1f914;&#x1f4a5;&#x1f680;第二节&#xff1a;分片架构的“三大金刚”&#xff1a;核心组件解析 &#x1f9f1;&#x1f9e0;&#x1f6e3;️第三节&#xff…

C++ 函数类型及实用例题

请各位大佬一键三连支持一下 目录 请各位大佬一键三连支持一下 1. 无参数无返回值函数 2. 有参数无返回值函数 3. 无参数有返回值函数 4. 有参数有返回值函数 5. 函数重载 6. 递归函数 7. 带默认参数的函数 8. 内联函数 下面我将介绍 C 中不同类型的函数&#xff0c;…