mcp: use testing allocator in tests

This commit is contained in:
Adrià Arrufat
2026-03-03 14:32:29 +09:00
parent 6b80cd6109
commit c8d5665653
3 changed files with 17 additions and 12 deletions

View File

@@ -84,6 +84,7 @@ pub fn sendError(self: *Self, id: std.json.Value, code: protocol.ErrorCode, mess
}
test "MCP.Server - Integration: synchronous smoke test" {
defer testing.reset();
const allocator = testing.allocator;
const app = testing.test_app;
@@ -91,8 +92,8 @@ test "MCP.Server - Integration: synchronous smoke test" {
\\{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
;
var in_reader = std.io.Reader.fixed(input);
var out_alloc = std.io.Writer.Allocating.init(allocator);
var in_reader: std.io.Reader = .fixed(input);
var out_alloc: std.io.Writer.Allocating = .init(testing.arena_allocator);
defer out_alloc.deinit();
var server = try Self.init(allocator, app, &out_alloc.writer);

View File

@@ -186,6 +186,7 @@ pub const JsonEscapingWriter = struct {
const testing = @import("../testing.zig");
test "MCP.protocol - request parsing" {
defer testing.reset();
const raw_json =
\\{
\\ "jsonrpc": "2.0",
@@ -202,7 +203,7 @@ test "MCP.protocol - request parsing" {
\\}
;
const parsed = try std.json.parseFromSlice(Request, testing.allocator, raw_json, .{ .ignore_unknown_fields = true });
const parsed = try std.json.parseFromSlice(Request, testing.arena_allocator, raw_json, .{ .ignore_unknown_fields = true });
defer parsed.deinit();
const req = parsed.value;
@@ -213,7 +214,7 @@ test "MCP.protocol - request parsing" {
try testing.expect(req.params != null);
// Test nested parsing of InitializeParams
const init_params = try std.json.parseFromValue(InitializeParams, testing.allocator, req.params.?, .{ .ignore_unknown_fields = true });
const init_params = try std.json.parseFromValue(InitializeParams, testing.arena_allocator, req.params.?, .{ .ignore_unknown_fields = true });
defer init_params.deinit();
try testing.expectString("2024-11-05", init_params.value.protocolVersion);
@@ -222,12 +223,13 @@ test "MCP.protocol - request parsing" {
}
test "MCP.protocol - response formatting" {
defer testing.reset();
const response = Response{
.id = .{ .integer = 42 },
.result = .{ .string = "success" },
};
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
var aw: std.Io.Writer.Allocating = .init(testing.arena_allocator);
defer aw.deinit();
try std.json.Stringify.value(response, .{ .emit_null_optional_fields = false }, &aw.writer);
@@ -235,6 +237,7 @@ test "MCP.protocol - response formatting" {
}
test "MCP.protocol - error formatting" {
defer testing.reset();
const response = Response{
.id = .{ .string = "abc" },
.@"error" = .{
@@ -243,7 +246,7 @@ test "MCP.protocol - error formatting" {
},
};
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
var aw: std.Io.Writer.Allocating = .init(testing.arena_allocator);
defer aw.deinit();
try std.json.Stringify.value(response, .{ .emit_null_optional_fields = false }, &aw.writer);
@@ -251,7 +254,8 @@ test "MCP.protocol - error formatting" {
}
test "MCP.protocol - JsonEscapingWriter" {
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
defer testing.reset();
var aw: std.Io.Writer.Allocating = .init(testing.arena_allocator);
defer aw.deinit();
var escaping_writer = JsonEscapingWriter.init(&aw.writer);
@@ -264,6 +268,7 @@ test "MCP.protocol - JsonEscapingWriter" {
}
test "MCP.protocol - Tool serialization" {
defer testing.reset();
const t = Tool{
.name = "test",
.inputSchema = minify(
@@ -276,7 +281,7 @@ test "MCP.protocol - Tool serialization" {
),
};
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
var aw: std.Io.Writer.Allocating = .init(testing.arena_allocator);
defer aw.deinit();
try std.json.Stringify.value(t, .{}, &aw.writer);

View File

@@ -95,18 +95,17 @@ fn handleInitialize(server: *Server, req: protocol.Request) !void {
const testing = @import("../testing.zig");
test "MCP.router - handleMessage - synchronous unit tests" {
defer testing.reset();
const allocator = testing.allocator;
const app = testing.test_app;
var out_alloc = std.io.Writer.Allocating.init(allocator);
var out_alloc: std.io.Writer.Allocating = .init(testing.arena_allocator);
defer out_alloc.deinit();
var server = try Server.init(allocator, app, &out_alloc.writer);
defer server.deinit();
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
const aa = arena.allocator();
const aa = testing.arena_allocator;
// 1. Valid handshake
try handleMessage(server, aa,