diff --git a/src/mcp_server.cpp b/src/mcp_server.cpp index 6a22da2..662eed2 100644 --- a/src/mcp_server.cpp +++ b/src/mcp_server.cpp @@ -95,7 +95,7 @@ void server::stop() { return; } - LOG_INFO("Stopping MCP server..."); + LOG_INFO("Stopping MCP server on ", host_, ":", port_); running_ = false; // 关闭维护线程 diff --git a/test/mcp_test.cpp b/test/mcp_test.cpp index 4ab52d8..52e5d2c 100644 --- a/test/mcp_test.cpp +++ b/test/mcp_test.cpp @@ -93,9 +93,9 @@ TEST_F(MessageFormatTest, NotificationMessageFormat) { EXPECT_TRUE(notification.is_notification()); } -// 测试生命周期 -class LifecycleTest : public ::testing::Test { -protected: +// 生命周期测试环境 +class LifecycleEnvironment : public ::testing::Environment { +public: void SetUp() override { // 设置测试环境 server_ = std::make_unique("localhost", 8080); @@ -111,7 +111,7 @@ protected: server_->set_capabilities(server_capabilities); // 注册初始化方法处理器 - server_->register_method("initialize", [this, server_capabilities](const json& params) -> json { + server_->register_method("initialize", [server_capabilities](const json& params) -> json { // 验证初始化请求参数 EXPECT_EQ(params["protocolVersion"], MCP_VERSION); EXPECT_TRUE(params.contains("capabilities")); @@ -142,17 +142,43 @@ protected: void TearDown() override { // 清理测试环境 + client_.reset(); server_->stop(); server_.reset(); - client_.reset(); } - std::unique_ptr server_; - std::unique_ptr client_; + static std::unique_ptr& GetServer() { + return server_; + } + + static std::unique_ptr& GetClient() { + return client_; + } + +private: + static std::unique_ptr server_; + static std::unique_ptr client_; +}; + +// 静态成员变量定义 +std::unique_ptr LifecycleEnvironment::server_; +std::unique_ptr LifecycleEnvironment::client_; + +// 测试生命周期 +class LifecycleTest : public ::testing::Test { +protected: + void SetUp() override { + // 获取客户端指针 + client_ = LifecycleEnvironment::GetClient().get(); + } + + // 使用原始指针而不是引用 + sse_client* client_; }; // 测试初始化流程 TEST_F(LifecycleTest, InitializeProcess) { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 执行初始化 bool init_result = client_->initialize("TestClient", "1.0.0"); @@ -167,9 +193,9 @@ TEST_F(LifecycleTest, InitializeProcess) { EXPECT_TRUE(server_capabilities.contains("tools")); } -// 测试版本控制 -class VersioningTest : public ::testing::Test { -protected: +// 版本控制测试环境 +class VersioningEnvironment : public ::testing::Environment { +public: void SetUp() override { // 设置测试环境 server_ = std::make_unique("localhost", 8081); @@ -192,13 +218,38 @@ protected: void TearDown() override { // 清理测试环境 + client_.reset(); server_->stop(); server_.reset(); - client_.reset(); } - std::unique_ptr server_; - std::unique_ptr client_; + static std::unique_ptr& GetServer() { + return server_; + } + + static std::unique_ptr& GetClient() { + return client_; + } + +private: + static std::unique_ptr server_; + static std::unique_ptr client_; +}; + +// 静态成员变量定义 +std::unique_ptr VersioningEnvironment::server_; +std::unique_ptr VersioningEnvironment::client_; + +// 测试版本控制 +class VersioningTest : public ::testing::Test { +protected: + void SetUp() override { + // 获取客户端指针 + client_ = VersioningEnvironment::GetClient().get(); + } + + // 使用原始指针而不是引用 + sse_client* client_; }; // 测试支持的版本 @@ -312,9 +363,9 @@ TEST_F(VersioningTest, UnsupportedVersion) { } } -// 测试Ping功能 -class PingTest : public ::testing::Test { -protected: +// Ping测试环境 +class PingEnvironment : public ::testing::Environment { +public: void SetUp() override { // 设置测试环境 server_ = std::make_unique("localhost", 8082); @@ -333,13 +384,38 @@ protected: void TearDown() override { // 清理测试环境 + client_.reset(); server_->stop(); server_.reset(); - client_.reset(); } - std::unique_ptr server_; - std::unique_ptr client_; + static std::unique_ptr& GetServer() { + return server_; + } + + static std::unique_ptr& GetClient() { + return client_; + } + +private: + static std::unique_ptr server_; + static std::unique_ptr client_; +}; + +// 静态成员变量定义 +std::unique_ptr PingEnvironment::server_; +std::unique_ptr PingEnvironment::client_; + +// 测试Ping功能 +class PingTest : public ::testing::Test { +protected: + void SetUp() override { + // 获取客户端指针 + client_ = PingEnvironment::GetClient().get(); + } + + // 使用原始指针而不是引用 + sse_client* client_; }; // 测试Ping请求 @@ -351,6 +427,7 @@ TEST_F(PingTest, PingRequest) { } TEST_F(PingTest, DirectPing) { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); try { // 使用 httplib::Client 发送不支持的版本请求 std::unique_ptr sse_client = std::make_unique("localhost", 8082); @@ -448,9 +525,9 @@ TEST_F(PingTest, DirectPing) { } } -// 测试工具功能 -class ToolsTest : public ::testing::Test { -protected: +// 工具测试环境 +class ToolsEnvironment : public ::testing::Environment { +public: void SetUp() override { // 设置测试环境 server_ = std::make_unique("localhost", 8083); @@ -486,7 +563,7 @@ protected: }); // 注册工具列表方法 - server_->register_method("tools/list", [this](const json& params) -> json { + server_->register_method("tools/list", [](const json& params) -> json { return { {"tools", json::array({ { @@ -509,7 +586,7 @@ protected: }); // 注册工具调用方法 - server_->register_method("tools/call", [this](const json& params) -> json { + server_->register_method("tools/call", [](const json& params) -> json { // 验证参数 EXPECT_EQ(params["name"], "get_weather"); EXPECT_EQ(params["arguments"]["location"], "New York"); @@ -541,13 +618,38 @@ protected: void TearDown() override { // 清理测试环境 + client_.reset(); server_->stop(); server_.reset(); - client_.reset(); } - std::unique_ptr server_; - std::unique_ptr client_; + static std::unique_ptr& GetServer() { + return server_; + } + + static std::unique_ptr& GetClient() { + return client_; + } + +private: + static std::unique_ptr server_; + static std::unique_ptr client_; +}; + +// 静态成员变量定义 +std::unique_ptr ToolsEnvironment::server_; +std::unique_ptr ToolsEnvironment::client_; + +// 测试工具功能 +class ToolsTest : public ::testing::Test { +protected: + void SetUp() override { + // 获取客户端指针 + client_ = ToolsEnvironment::GetClient().get(); + } + + // 使用原始指针而不是引用 + sse_client* client_; }; // 测试列出工具 @@ -579,5 +681,12 @@ TEST_F(ToolsTest, CallTool) { int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); + + // 添加全局测试环境 + ::testing::AddGlobalTestEnvironment(new LifecycleEnvironment()); + ::testing::AddGlobalTestEnvironment(new VersioningEnvironment()); + ::testing::AddGlobalTestEnvironment(new PingEnvironment()); + ::testing::AddGlobalTestEnvironment(new ToolsEnvironment()); + return RUN_ALL_TESTS(); } \ No newline at end of file