-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest_tool_timeout.cpp
More file actions
122 lines (100 loc) · 3.08 KB
/
Copy pathtest_tool_timeout.cpp
File metadata and controls
122 lines (100 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/// @file test_tool_timeout.cpp
/// @brief Tests for tool execution timeouts
#include "fastmcpp/tools/manager.hpp"
#include "fastmcpp/tools/tool.hpp"
#include <cassert>
#include <chrono>
#include <iostream>
#include <thread>
using namespace fastmcpp;
using namespace fastmcpp::tools;
using namespace std::chrono_literals;
static void sleep_for_at_least(std::chrono::milliseconds duration)
{
auto deadline = std::chrono::steady_clock::now() + duration;
while (std::chrono::steady_clock::now() < deadline)
std::this_thread::sleep_for(1ms);
}
void test_tool_timeout_triggers()
{
std::cout << " test_tool_timeout_triggers... " << std::flush;
Tool slow_tool("slow", Json::object(), Json::object(),
[](const Json&) -> Json
{
// Large sleep margin so the timeout fires reliably even on
// slow CI runners (macOS Debug) where scheduling jitter can
// delay future::wait_for() past the worker's sleep duration.
sleep_for_at_least(5s);
return Json{{"ok", true}};
});
slow_tool.set_timeout(50ms);
bool threw = false;
try
{
slow_tool.invoke(Json::object());
}
catch (const ToolTimeoutError&)
{
threw = true;
}
assert(threw);
std::cout << "PASSED\n";
}
void test_tool_timeout_disabled()
{
std::cout << " test_tool_timeout_disabled... " << std::flush;
Tool slow_tool("slow_no_timeout", Json::object(), Json::object(),
[](const Json&) -> Json
{
sleep_for_at_least(30ms);
return Json{{"ok", true}};
});
slow_tool.set_timeout(5ms);
auto result = slow_tool.invoke(Json::object(), false);
assert(result["ok"].get<bool>() == true);
std::cout << "PASSED\n";
}
void test_manager_timeout_toggle()
{
std::cout << " test_manager_timeout_toggle... " << std::flush;
Tool slow_tool("slow_manager", Json::object(), Json::object(),
[](const Json&) -> Json
{
sleep_for_at_least(5s);
return Json{{"ok", true}};
});
slow_tool.set_timeout(50ms);
ToolManager tm;
tm.register_tool(slow_tool);
bool threw = false;
try
{
tm.invoke("slow_manager", Json::object());
}
catch (const ToolTimeoutError&)
{
threw = true;
}
assert(threw);
auto result = tm.invoke("slow_manager", Json::object(), false);
assert(result["ok"].get<bool>() == true);
std::cout << "PASSED\n";
}
int main()
{
std::cout << "Tool Timeout Tests\n";
std::cout << "==================\n";
try
{
test_tool_timeout_triggers();
test_tool_timeout_disabled();
test_manager_timeout_toggle();
std::cout << "\nAll tests passed!\n";
return 0;
}
catch (const std::exception& e)
{
std::cerr << "\nTest failed with exception: " << e.what() << "\n";
return 1;
}
}