// ================================================================================================ // Audio Backend - 通信消息测试 // ================================================================================================ #include #include #include "communication/core/message.h" #include "tests/common/test_fixtures.h" using namespace audio_backend; using namespace audio_backend::communication; // 自定义测试消息类 class TestMessageImpl : public Message { public: TestMessageImpl(const std::string& content = "") : Message("TestMessage"), 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::Normal; } TransportChannel preferred_channel() const override { return TransportChannel::ZeroMQ; } private: std::string content_; }; // 消息测试固定装置 class MessageTest : public test::CommunicationTest { protected: void SetUp() override { test::CommunicationTest::SetUp(); } void TearDown() override { test::CommunicationTest::TearDown(); } }; // 测试基本消息属性 TEST_F(MessageTest, BasicAttributes) { // 创建测试消息 TestMessageImpl message("测试内容"); // 验证基本属性 EXPECT_EQ(message.message_type(), "TestMessage"); EXPECT_EQ(message.content(), "测试内容"); EXPECT_EQ(message.priority(), Message::Priority::Normal); EXPECT_EQ(message.preferred_channel(), Message::TransportChannel::ZeroMQ); EXPECT_GT(message.estimated_size(), sizeof(message)); // 验证时间戳 EXPECT_GT(message.timestamp().time_since_epoch().count(), 0); // 验证唯一ID EXPECT_FALSE(message.message_id().empty()); } // 测试消息修改 TEST_F(MessageTest, Modification) { // 创建测试消息 TestMessageImpl message("原始内容"); EXPECT_EQ(message.content(), "原始内容"); // 修改内容 message.set_content("内容"); EXPECT_EQ(message.content(), "新内容"); } // 测试消息ID唯一性 TEST_F(MessageTest, UniqueIds) { const int message_count = 100; std::set ids; // 创建多个消息,验证ID唯一性 for (int i = 0; i < message_count; ++i) { TestMessageImpl message(std::to_string(i)); ids.insert(message.message_id()); } // 验证所有ID都是唯的 EXPECT_EQ(ids.size(), message_count); } // 测试消息复制 TEST_F(MessageTest, MessageCloning) { // 创建原始消息 TestMessageImpl original("原始消息"); // 克隆消息 std::unique_ptr cloned = original.clone(); // 验证是否正确克隆 EXPECT_NE(cloned.get(), nullptr); EXPECT_EQ(cloned->message_type(), original.message_type()); // 转换为具体类型并验证内容 TestMessageImpl* cloned_typed = dynamic_cast(cloned.get()); ASSERT_NE(cloned_typed, nullptr); EXPECT_EQ(cloned_typed->content(), "原始消息"); // 验证ID和时间戳是立的 EXPECT_NE(cloned_typed->message_id(), original.message_id()); // 时间戳可能相同或不同,取决于实现,这里不做严格要求 } // 测试不同优先级消息 class PriorityMessage : public Message { public: enum class TestPriority { Low, Normal, High, Critical }; PriorityMessage(TestPriority priority) : Message("PriorityMessage"), test_priority_(priority) {} // 实现虚函数 size_t estimated_size() const override { return sizeof(*this); } Priority priority() const override { switch (test_priority_) { case TestPriority::Low: return Priority::Low; case TestPriority::Normal: return Priority::Normal; case TestPriority::High: return Priority::High; case TestPriority::Critical: return Priority::Critical; default: return Priority::Normal; } } TransportChannel preferred_channel() const override { return TransportChannel::ZeroMQ; } private: TestPriority test_priority_; }; TEST_F(MessageTest, MessagePriorities) { // 创建不同优先级的消息 PriorityMessage low_msg(PriorityMessage::TestPriority::Low); PriorityMessage normal_msg(PriorityMessage::TestPriority::Normal); PriorityMessage high_msg(PriorityMessage::TestPriority::High); PriorityMessage critical_msg(PriorityMessage::TestPriority::Critical); // 验证优先级 EXPECT_EQ(low_msg.priority(), Message::Priority::Low); EXPECT_EQ(normal_msg.priority(), Message::Priority::Normal); EXPECT_EQ(high_msg.priority(), Message::Priority::High); EXPECT_EQ(critical_msg.priority(), Message::Priority::Critical); // 验证优先级比较 EXPECT_LT(static_cast(low_msg.priority()), static_cast(normal_msg.priority())); EXPECT_LT(static_cast(normal_msg.priority()), static_cast(high_msg.priority())); EXPECT_LT(static_cast(high_msg.priority()), static_cast(critical_msg.priority())); } // 测试不同传输通道 class TransportChannelMessage : public Message { public: enum class TestChannel { ZeroMQ, SharedMemory, Direct, Auto }; TransportChannelMessage(TestChannel channel) : Message("ChannelMessage"), test_channel_(channel) {} // 实现虚函数 size_t estimated_size() const override { return sizeof(*this); } Priority priority() const override { return Priority::Normal; } TransportChannel preferred_channel() const override { switch (test_channel_) { case TestChannel::ZeroMQ: return TransportChannel::ZeroMQ; case TestChannel::SharedMemory: return TransportChannel::SharedMemory; case TestChannel::Direct: return TransportChannel::Direct; case TestChannel::Auto: return TransportChannel::Auto; default: return TransportChannel::Auto; } } private: TestChannel test_channel_; }; TEST_F(MessageTest, MessageChannels) { // 创建不同通道的消息 TransportChannelMessage zmq_msg(TransportChannelMessage::TestChannel::ZeroMQ); TransportChannelMessage shm_msg(TransportChannelMessage::TestChannel::SharedMemory); TransportChannelMessage direct_msg(TransportChannelMessage::TestChannel::Direct); TransportChannelMessage auto_msg(TransportChannelMessage::TestChannel::Auto); // 验证通道 EXPECT_EQ(zmq_msg.preferred_channel(), Message::TransportChannel::ZeroMQ); EXPECT_EQ(shm_msg.preferred_channel(), Message::TransportChannel::SharedMemory); EXPECT_EQ(direct_msg.preferred_channel(), Message::TransportChannel::Direct); EXPECT_EQ(auto_msg.preferred_channel(), Message::TransportChannel::Auto); } // 测试消息工厂 TEST_F(MessageTest, MessageFactoryRegistration) { // 创建消息工厂 MessageFactory factory; // 注册测试消息 factory.register_message("TestMessage"); // 创建消息 auto message = factory.create_message("TestMessage"); // 验证是否正确创建 EXPECT_NE(message.get(), nullptr); EXPECT_EQ(message->message_type(), "TestMessage"); // 转换为具体类型 auto* typed_message = dynamic_cast(message.get()); EXPECT_NE(typed_message, nullptr); } // 测试消息序列化与反序列化接口 TEST_F(MessageTest, SerializationInterface) { // 创建测试消息 TestMessageImpl message("要序列化内容"); // 模拟序列化 std::vector serialized_data; size_t expected_size = message.estimated_size(); // 验证预估大小合理性 EXPECT_GT(expected_size, sizeof(message)); EXPECT_GT(expected_size, message.content().size()); } int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }