262 lines
10 KiB
C++
262 lines
10 KiB
C++
// ================================================================================================
|
|
// Audio Backend - 消息路由测试
|
|
// ================================================================================================
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <gmock/gmock.h>
|
|
#include "communication/manager/communication_manager.h"
|
|
#include "tests/common/test_fixtures.h"
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace audio_backend;
|
|
using namespace audio_backend::communication;
|
|
|
|
// 路由测试消息类
|
|
class RouteTestMessage : public Message {
|
|
public:
|
|
RouteTestMessage(const std::string& content = "")
|
|
: Message("RouteTestMessage"), content_(content) {}
|
|
|
|
void set_content(const std::string& content) { content_ = content; }
|
|
const std::string& content() const { return content_; }
|
|
|
|
// 实现虚函数
|
|
size_t estimated_size() const override { return sizeof(*this) + content_.size(); }
|
|
Priority priority() const override { return priority_; }
|
|
TransportChannel preferred_channel() const override { return preferred_channel_; }
|
|
|
|
// 允许测试中修改优先级和首选通道
|
|
void set_priority(Priority priority) { priority_ = priority; }
|
|
void set_preferred_channel(TransportChannel channel) { preferred_channel_ = channel; }
|
|
|
|
private:
|
|
std::string content_;
|
|
Priority priority_ = Priority::Normal;
|
|
TransportChannel preferred_channel_ = TransportChannel::Auto;
|
|
};
|
|
|
|
// 消息路由测试固定装置
|
|
class MessageRouteTest : public test::CommunicationTest {
|
|
protected:
|
|
void SetUp() override {
|
|
test::CommunicationTest::SetUp();
|
|
}
|
|
|
|
void TearDown() override {
|
|
test::CommunicationTest::TearDown();
|
|
}
|
|
|
|
// 创建基本路由
|
|
MessageRoute create_basic_route(const std::string& message_type,
|
|
const std::string& destination = "tcp://localhost:5555",
|
|
RoutingStrategy strategy = RoutingStrategy::Auto,
|
|
QoS qos = QoS::Reliable,
|
|
int priority = 100) {
|
|
MessageRoute route;
|
|
route.message_type = message_type;
|
|
route.destination = destination;
|
|
route.strategy = strategy;
|
|
route.qos = qos;
|
|
route.priority = priority;
|
|
return route;
|
|
}
|
|
};
|
|
|
|
// 测试路由基本属性和比较
|
|
TEST_F(MessageRouteTest, BasicProperties) {
|
|
// 创建路由
|
|
MessageRoute route = create_basic_route("TestMessage");
|
|
|
|
// 验证基本属性
|
|
EXPECT_EQ(route.message_type, "TestMessage");
|
|
EXPECT_EQ(route.destination, "tcp://localhost:5555");
|
|
EXPECT_EQ(route.strategy, RoutingStrategy::Auto);
|
|
EXPECT_EQ(route.qos, QoS::Reliable);
|
|
EXPECT_EQ(route.priority, 100);
|
|
|
|
// 测试比较运算符
|
|
MessageRoute route2 = create_basic_route("TestMessage");
|
|
EXPECT_EQ(route, route2);
|
|
|
|
// 修改优先级,应该不相等
|
|
route2.priority = 200;
|
|
EXPECT_NE(route, route2);
|
|
|
|
// 路由应该按照优先级排序
|
|
EXPECT_LT(route2, route); // 高优先级(数字小)应该排在前面
|
|
|
|
// 不同消息类型
|
|
MessageRoute route3 = create_basic_route("OtherMessage");
|
|
EXPECT_NE(route, route3);
|
|
}
|
|
|
|
// 测试路由匹配
|
|
TEST_F(MessageRouteTest, RouteMatching) {
|
|
// 创建不同类型的路由
|
|
MessageRoute zmq_route = create_basic_route("TestMessage", "tcp://localhost:5555", RoutingStrategy::ZeroMQOnly);
|
|
MessageRoute shm_route = create_basic_route("TestMessage", "shm://segment_name", RoutingStrategy::SharedMemoryOnly);
|
|
MessageRoute auto_route = create_basic_route("TestMessage", "auto://", RoutingStrategy::Auto);
|
|
MessageRoute hybrid_route = create_basic_route("TestMessage", "hybrid://", RoutingStrategy::Hybrid);
|
|
|
|
// 创建测试消息
|
|
RouteTestMessage message("测试消息");
|
|
|
|
// 设置不同的首选通道并测试匹配
|
|
message.set_preferred_channel(Message::TransportChannel::ZeroMQ);
|
|
EXPECT_TRUE(zmq_route.matches(message));
|
|
EXPECT_FALSE(shm_route.matches(message));
|
|
EXPECT_TRUE(auto_route.matches(message));
|
|
EXPECT_TRUE(hybrid_route.matches(message));
|
|
|
|
message.set_preferred_channel(Message::TransportChannel::SharedMemory);
|
|
EXPECT_FALSE(zmq_route.matches(message));
|
|
EXPECT_TRUE(shm_route.matches(message));
|
|
EXPECT_TRUE(auto_route.matches(message));
|
|
EXPECT_TRUE(hybrid_route.matches(message));
|
|
|
|
message.set_preferred_channel(Message::TransportChannel::Auto);
|
|
EXPECT_TRUE(zmq_route.matches(message)); // Auto应该匹配任何路由
|
|
EXPECT_TRUE(shm_route.matches(message));
|
|
EXPECT_TRUE(auto_route.matches(message));
|
|
EXPECT_TRUE(hybrid_route.matches(message));
|
|
}
|
|
|
|
// 测试路由优先级和QoS
|
|
TEST_F(MessageRouteTest, PriorityAndQoS) {
|
|
// 创建不同优先级和QoS的路由
|
|
MessageRoute high_priority = create_basic_route("TestMessage", "tcp://localhost:5555", RoutingStrategy::Auto, QoS::Reliable, 50);
|
|
MessageRoute normal_priority = create_basic_route("TestMessage", "tcp://localhost:5555", RoutingStrategy::Auto, QoS::Reliable, 100);
|
|
MessageRoute low_priority = create_basic_route("TestMessage", "tcp://localhost:5555", RoutingStrategy::Auto, QoS::BestEffort, 150);
|
|
|
|
// 创建消息
|
|
RouteTestMessage message("测试消息");
|
|
|
|
// 测试优先级排序
|
|
std::vector<MessageRoute> routes = {normal_priority, low_priority, high_priority};
|
|
std::sort(routes.begin(), routes.end());
|
|
|
|
EXPECT_EQ(routes[0].priority, 50); // 高优先级
|
|
EXPECT_EQ(routes[1].priority, 100); // 中优先级
|
|
EXPECT_EQ(routes[2].priority, 150); // 低优先级
|
|
|
|
// 测试消息优先级与路由QoS的匹配
|
|
message.set_priority(Message::Priority::Low);
|
|
EXPECT_TRUE(low_priority.matches_priority(message));
|
|
|
|
message.set_priority(Message::Priority::Normal);
|
|
EXPECT_TRUE(normal_priority.matches_priority(message));
|
|
|
|
message.set_priority(Message::Priority::High);
|
|
EXPECT_TRUE(high_priority.matches_priority(message));
|
|
|
|
// 紧急优先级消息应该匹配所有路由
|
|
message.set_priority(Message::Priority::Critical);
|
|
EXPECT_TRUE(high_priority.matches_priority(message));
|
|
EXPECT_TRUE(normal_priority.matches_priority(message));
|
|
EXPECT_TRUE(low_priority.matches_priority(message));
|
|
}
|
|
|
|
// 测试路由表管理
|
|
TEST_F(MessageRouteTest, RouteTableManagement) {
|
|
// 创建路由表
|
|
MessageRouteTable route_table;
|
|
|
|
// 添加路由
|
|
MessageRoute route1 = create_basic_route("Type1", "dest1", RoutingStrategy::Auto, QoS::Reliable, 100);
|
|
MessageRoute route2 = create_basic_route("Type2", "dest2", RoutingStrategy::ZeroMQOnly, QoS::Reliable, 90);
|
|
MessageRoute route3 = create_basic_route("Type3", "dest3", RoutingStrategy::SharedMemoryOnly, QoS::BestEffort, 80);
|
|
|
|
route_table.add_route(route1);
|
|
route_table.add_route(route2);
|
|
route_table.add_route(route3);
|
|
|
|
// 获取路由
|
|
auto routes = route_table.get_all_routes();
|
|
EXPECT_EQ(routes.size(), 3);
|
|
|
|
// 查找特定路由
|
|
auto found_route = route_table.find_route("Type2");
|
|
EXPECT_TRUE(found_route.has_value());
|
|
EXPECT_EQ(found_route->destination, "dest2");
|
|
|
|
// 查找不存在的路由
|
|
auto not_found = route_table.find_route("NonExistentType");
|
|
EXPECT_FALSE(not_found.has_value());
|
|
|
|
// 移除路由
|
|
route_table.remove_route("Type1");
|
|
routes = route_table.get_all_routes();
|
|
EXPECT_EQ(routes.size(), 2);
|
|
|
|
// 清空路由表
|
|
route_table.clear();
|
|
routes = route_table.get_all_routes();
|
|
EXPECT_TRUE(routes.empty());
|
|
}
|
|
|
|
// 测试路由查找和选择策略
|
|
TEST_F(MessageRouteTest, RouteSelectionStrategy) {
|
|
// 创建路由表
|
|
MessageRouteTable route_table;
|
|
|
|
// 添加多个相同消息类型但不同优先级的路由
|
|
MessageRoute route_high = create_basic_route("MultiRoute", "high_priority", RoutingStrategy::Auto, QoS::Reliable, 50);
|
|
MessageRoute route_normal = create_basic_route("MultiRoute", "normal_priority", RoutingStrategy::Auto, QoS::Reliable, 100);
|
|
MessageRoute route_low = create_basic_route("MultiRoute", "low_priority", RoutingStrategy::Auto, QoS::BestEffort, 150);
|
|
|
|
route_table.add_route(route_normal);
|
|
route_table.add_route(route_low);
|
|
route_table.add_route(route_high);
|
|
|
|
// 创建消息
|
|
RouteTestMessage message("测试消息");
|
|
message.set_message_type("MultiRoute");
|
|
|
|
// 测试最佳路由选择(应该选择最高优先级的路由)
|
|
auto best_route = route_table.find_best_route(message);
|
|
ASSERT_TRUE(best_route.has_value());
|
|
EXPECT_EQ(best_route->destination, "high_priority");
|
|
EXPECT_EQ(best_route->priority, 50);
|
|
|
|
// 设置消息为低优先级,测试是否会选择合适的路由
|
|
message.set_priority(Message::Priority::Low);
|
|
best_route = route_table.find_best_route(message);
|
|
ASSERT_TRUE(best_route.has_value());
|
|
EXPECT_EQ(best_route->destination, "low_priority");
|
|
|
|
// 设置消息为紧急优先级,应该还是选择最高优先级的路由
|
|
message.set_priority(Message::Priority::Critical);
|
|
best_route = route_table.find_best_route(message);
|
|
ASSERT_TRUE(best_route.has_value());
|
|
EXPECT_EQ(best_route->destination, "high_priority");
|
|
}
|
|
|
|
// 测试路由配置与传输协议的兼容性
|
|
TEST_F(MessageRouteTest, RouteTransportCompatibility) {
|
|
// 创建各种传输策略的路由
|
|
MessageRoute zmq_route = create_basic_route("TestMessage", "tcp://localhost:5555", RoutingStrategy::ZeroMQOnly);
|
|
MessageRoute shm_route = create_basic_route("TestMessage", "shm://segment", RoutingStrategy::SharedMemoryOnly);
|
|
MessageRoute direct_route = create_basic_route("TestMessage", "direct://local", RoutingStrategy::DirectOnly);
|
|
MessageRoute auto_route = create_basic_route("TestMessage", "auto://", RoutingStrategy::Auto);
|
|
|
|
// 测试端点格式验证
|
|
EXPECT_TRUE(zmq_route.validate_endpoint());
|
|
EXPECT_TRUE(shm_route.validate_endpoint());
|
|
EXPECT_TRUE(direct_route.validate_endpoint());
|
|
EXPECT_TRUE(auto_route.validate_endpoint());
|
|
|
|
// 测试无效端点格式
|
|
MessageRoute invalid_route = create_basic_route("TestMessage", "invalid://format", RoutingStrategy::Auto);
|
|
EXPECT_TRUE(invalid_route.validate_endpoint()); // 应该在更高层检查端点有效性
|
|
|
|
// 测试空端点
|
|
MessageRoute empty_route = create_basic_route("TestMessage", "", RoutingStrategy::Auto);
|
|
EXPECT_FALSE(empty_route.validate_endpoint());
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
} |