Skip to content

Commit b9c408e

Browse files
Copilotstephentoub
andcommitted
Address code review feedback: use static regex and culture-invariant parsing
Co-authored-by: stephentoub <[email protected]>
1 parent 08315a9 commit b9c408e

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

tests/ModelContextProtocol.Tests/Server/McpServerRequestDurationLoggingTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
using ModelContextProtocol.Protocol;
55
using ModelContextProtocol.Server;
66
using System.ComponentModel;
7+
using System.Globalization;
8+
using System.Text.RegularExpressions;
79

810
namespace ModelContextProtocol.Tests.Server;
911

1012
public class McpServerRequestDurationLoggingTests : ClientServerTestBase
1113
{
14+
private static readonly Regex DurationRegex = new(@"completed in (\d+(?:\.\d+)?)ms", RegexOptions.Compiled);
15+
1216
public McpServerRequestDurationLoggingTests(ITestOutputHelper testOutputHelper)
1317
: base(testOutputHelper)
1418
{
@@ -45,13 +49,13 @@ public async Task RequestHandlerCompleted_LogsElapsedTime()
4549
}
4650

4751
// 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);
4953
if (!match.Success)
5054
{
5155
return false;
5256
}
5357

54-
double elapsedMs = double.Parse(match.Groups[1].Value);
58+
double elapsedMs = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
5559

5660
// Duration should be at least 50ms (the delay we introduced)
5761
// and less than 5 seconds
@@ -82,13 +86,13 @@ public async Task RequestHandlerCompleted_LogsForQuickRequests()
8286
}
8387

8488
// 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);
8690
if (!match.Success)
8791
{
8892
return false;
8993
}
9094

91-
double elapsedMs = double.Parse(match.Groups[1].Value);
95+
double elapsedMs = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
9296

9397
// Even quick requests should log some duration (should be very small)
9498
// Should complete quickly (less than 1 second)

0 commit comments

Comments
 (0)