const std = @import("std"); const Client = @import("client.zig").Client; const test_options = @import("test_options"); fn connect(alloc: std.mem.Allocator) !Client { const port = test_options.redis_port orelse return error.IsolatedRedisPortRequired; return Client.connectWithConfig(std.testing.io, alloc, .{ .host = "127.0.0.1", .port = port, .read_timeout_ms = 3000, .write_timeout_ms = 3000, }); } fn readLargeReply(alloc: std.mem.Allocator, expected: []const u8) !void { var client = try connect(alloc); defer client.close(); const response = try client.sendCommand(&.{ "GET", "redis-client:ownership-regression" }); try std.testing.expectEqualSlices(u8, expected, response.asString().?); const ping = try client.sendCommand(&.{"PING"}); try std.testing.expectEqualStrings("PONG", ping.asString().?); } test "large replies release replacement buffers when retirement allocation fails" { var control = try connect(std.testing.allocator); defer control.close(); defer { const result = control.sendCommand(&.{ "DEL", "redis-client:ownership-regression" }) catch @panic("Redis cleanup failed"); std.debug.assert(!result.isError()); } const payload = try std.testing.allocator.alloc(u8, 128 * 1024); defer std.testing.allocator.free(payload); @memset(payload, 'r'); const result = try control.sendCommand(&.{ "SET", "redis-client:ownership-regression", payload }); try std.testing.expect(!result.isError()); try std.testing.checkAllAllocationFailures(std.testing.allocator, readLargeReply, .{payload}); } fn reserveReply(alloc: std.mem.Allocator, expected: []const u8) !void { var client = try connect(alloc); defer client.close(); const previous = (try client.sendCommand(&.{"PING"})).asString().?; client.reserveBulkReply(expected.len) catch |err| { try std.testing.expectEqualStrings("PONG", previous); try std.testing.expectEqualStrings("PONG", (try client.sendCommand(&.{"PING"})).asString().?); return err; }; try std.testing.expectEqualStrings("PONG", previous); const response = try client.sendCommand(&.{ "GET", "redis-client:reservation-regression" }); try std.testing.expectEqualSlices(u8, expected, response.asString().?); } test "bulk reply reservation preserves borrowed slices and unwinds allocation failures" { var control = try connect(std.testing.allocator); defer control.close(); defer _ = control.sendCommand(&.{ "DEL", "redis-client:reservation-regression" }) catch @panic("Redis cleanup failed"); const payload = try std.testing.allocator.alloc(u8, 128 * 1024); defer std.testing.allocator.free(payload); @memset(payload, 's'); _ = try control.sendCommand(&.{ "SET", "redis-client:reservation-regression", payload }); try std.testing.checkAllAllocationFailures(std.testing.allocator, reserveReply, .{payload}); var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{}); var client = try connect(failing.allocator()); defer client.close(); try client.reserveBulkReply(payload.len); failing.fail_index = failing.alloc_index; try std.testing.expectEqualSlices(u8, payload, (try client.sendCommand(&.{ "GET", "redis-client:reservation-regression" })).asString().?); try std.testing.expect(!failing.has_induced_failure); try std.testing.expectError(error.BufferOverflow, client.reserveBulkReply(std.math.maxInt(usize))); try std.testing.expectEqualStrings("PONG", (try client.sendCommand(&.{"PING"})).asString().?); } test "failed command buffer growth preserves owned storage and sends no command" { const key = "redis-client:command-growth-regression"; var control = try connect(std.testing.allocator); defer control.close(); defer _ = control.sendCommand(&.{ "DEL", key }) catch @panic("Redis cleanup failed"); const small = try std.testing.allocator.alloc(u8, 20 * 1024); defer std.testing.allocator.free(small); @memset(small, 'a'); const large = try std.testing.allocator.alloc(u8, 40 * 1024); defer std.testing.allocator.free(large); @memset(large, 'b'); var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{}); var client = try connect(failing.allocator()); defer client.close(); try std.testing.expectEqualStrings("OK", (try client.sendCommand(&.{ "SET", key, small })).asString().?); failing.fail_index = failing.alloc_index; try std.testing.expectError(error.OutOfMemory, client.sendCommand(&.{ "SET", key, large })); try std.testing.expect(failing.has_induced_failure); try std.testing.expectEqualSlices(u8, small, (try control.sendCommand(&.{ "GET", key })).asString().?); failing.fail_index = std.math.maxInt(usize); try std.testing.expectEqualStrings("OK", (try client.sendCommand(&.{ "SET", key, small })).asString().?); try std.testing.expectEqualStrings("OK", (try client.sendCommand(&.{ "SET", key, large })).asString().?); try std.testing.expectEqualSlices(u8, large, (try control.sendCommand(&.{ "GET", key })).asString().?); }