|
4 | 4 | using ModelContextProtocol.Protocol; |
5 | 5 | using ModelContextProtocol.Server; |
6 | 6 | using System.ComponentModel; |
| 7 | +using System.Globalization; |
| 8 | +using System.Text.RegularExpressions; |
7 | 9 |
|
8 | 10 | namespace ModelContextProtocol.Tests.Server; |
9 | 11 |
|
10 | 12 | public class McpServerRequestDurationLoggingTests : ClientServerTestBase |
11 | 13 | { |
| 14 | + private static readonly Regex DurationRegex = new(@"completed in (\d+(?:\.\d+)?)ms", RegexOptions.Compiled); |
| 15 | + |
12 | 16 | public McpServerRequestDurationLoggingTests(ITestOutputHelper testOutputHelper) |
13 | 17 | : base(testOutputHelper) |
14 | 18 | { |
@@ -45,13 +49,13 @@ public async Task RequestHandlerCompleted_LogsElapsedTime() |
45 | 49 | } |
46 | 50 |
|
47 | 51 | // Extract duration from log message (should be in format "...completed in XXXms.") |
48 | | - var match = System.Text.RegularExpressions.Regex.Match(log.Message, @"completed in (\d+(?:\.\d+)?)ms"); |
| 52 | + var match = DurationRegex.Match(log.Message); |
49 | 53 | if (!match.Success) |
50 | 54 | { |
51 | 55 | return false; |
52 | 56 | } |
53 | 57 |
|
54 | | - double elapsedMs = double.Parse(match.Groups[1].Value); |
| 58 | + double elapsedMs = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture); |
55 | 59 |
|
56 | 60 | // Duration should be at least 50ms (the delay we introduced) |
57 | 61 | // and less than 5 seconds |
@@ -82,13 +86,13 @@ public async Task RequestHandlerCompleted_LogsForQuickRequests() |
82 | 86 | } |
83 | 87 |
|
84 | 88 | // Extract duration from log message |
85 | | - var match = System.Text.RegularExpressions.Regex.Match(log.Message, @"completed in (\d+(?:\.\d+)?)ms"); |
| 89 | + var match = DurationRegex.Match(log.Message); |
86 | 90 | if (!match.Success) |
87 | 91 | { |
88 | 92 | return false; |
89 | 93 | } |
90 | 94 |
|
91 | | - double elapsedMs = double.Parse(match.Groups[1].Value); |
| 95 | + double elapsedMs = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture); |
92 | 96 |
|
93 | 97 | // Even quick requests should log some duration (should be very small) |
94 | 98 | // Should complete quickly (less than 1 second) |
|
0 commit comments