Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225const 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 rejectedAuthentication(alloc: std.mem.Allocator, password: []const u8) !void { var client = Client.connectWithConfig(std.testing.io, alloc, .{ .host = "127.0.0.1", .port = test_options.redis_port orelse return error.IsolatedRedisPortRequired, .username = "redis-client-invalid-construction-user", .password = password, .read_timeout_ms = 3000, .write_timeout_ms = 3000, }) catch |err| switch (err) { error.AuthenticationFailed => return, else => return err, }; defer client.close(); return error.ExpectedAuthenticationFailure;}
test "failed long authentication releases all partially constructed client storage" { var control = try connect(std.testing.allocator); defer control.close(); try std.testing.expectEqual(.nil, try control.sendCommand(&.{ "ACL", "GETUSER", "redis-client-invalid-construction-user" })); const password = try std.testing.allocator.alloc(u8, 20 * 1024); defer std.testing.allocator.free(password); @memset(password, 'x'); try std.testing.checkAllAllocationFailures(std.testing.allocator, rejectedAuthentication, .{password});}
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().?);}
test "native bulk pop survives caller allocation failure and resumes without another command" { const key = "redis-client:resumable-pop"; var control = try connect(std.testing.allocator); defer control.close(); defer _ = control.sendCommand(&.{ "DEL", key }) catch @panic("Redis cleanup failed"); const payload = try std.testing.allocator.alloc(u8, 128 * 1024); defer std.testing.allocator.free(payload); for (payload, 0..) |*byte, index| byte.* = @truncate(index); for ([_]usize{ 1, 65536 }) |read_size| { _ = try control.sendCommand(&.{ "LPUSH", key, payload }); var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{}); var client = try Client.connectWithConfig(std.testing.io, failing.allocator(), .{ .host = "127.0.0.1", .port = test_options.redis_port.?, .read_buffer_size = read_size, .read_timeout_ms = 3000, }); defer client.close(); failing.fail_index = failing.alloc_index; try std.testing.expectEqual(payload.len, (try client.beginBulkCommand(&.{ "RPOP", key })).?); try std.testing.expect(!failing.has_induced_failure); try std.testing.expectError(error.OutOfMemory, failing.allocator().alloc(u8, payload.len)); try std.testing.expectError(error.NotAllowed, client.sendCommand(&.{"PING"})); try std.testing.expectError(error.NotAllowed, client.beginBulkCommand(&.{ "RPOP", key })); var short: [1]u8 = undefined; try std.testing.expectError(error.BufferOverflow, client.finishBulkCommand(&short)); try std.testing.expectEqual(@as(i64, 0), (try control.sendCommand(&.{ "LLEN", key })).integer); const owned = try std.testing.allocator.alloc(u8, payload.len); defer std.testing.allocator.free(owned); try client.finishBulkCommand(owned); try std.testing.expectEqualSlices(u8, payload, owned); try std.testing.expectEqual(@as(?usize, null), try client.beginBulkCommand(&.{ "RPOP", key })); } _ = try control.sendCommand(&.{ "LPUSH", key, "" }); var client = try connect(std.testing.allocator); defer client.close(); try std.testing.expectEqual(@as(?usize, 0), try client.beginBulkCommand(&.{ "RPOP", key })); var empty: [0]u8 = .{}; try client.finishBulkCommand(&empty); try std.testing.expectEqualStrings("PONG", (try client.sendCommand(&.{"PING"})).asString().?);}
test "interrupted native bulk reception poisons the pending response until close" { const key = "redis-client:interrupted-pop"; var control = try connect(std.testing.allocator); defer control.close(); defer _ = control.sendCommand(&.{ "DEL", key }) catch @panic("Redis cleanup failed"); const payload = try std.testing.allocator.alloc(u8, 128 * 1024); defer std.testing.allocator.free(payload); @memset(payload, 'x'); _ = try control.sendCommand(&.{ "LPUSH", key, payload }); var client = try Client.connectWithConfig(std.testing.io, std.testing.allocator, .{ .host = "127.0.0.1", .port = test_options.redis_port.?, .read_buffer_size = 1, .read_timeout_ms = 3000, }); defer client.close(); try std.testing.expectEqual(@as(?usize, payload.len), try client.beginBulkCommand(&.{ "RPOP", key })); try client.stream.shutdown(std.testing.io, .recv); try std.testing.expectError(error.ConnectionClosed, client.finishBulkCommand(payload)); try std.testing.expectError(error.NotAllowed, client.finishBulkCommand(payload)); try std.testing.expectError(error.NotAllowed, client.sendCommand(&.{"PING"})); try std.testing.expectEqual(@as(i64, 0), (try control.sendCommand(&.{ "LLEN", key })).integer);}
test "failed native bulk command transmission prevents subsequent commands" { var client = try connect(std.testing.allocator); defer client.close(); try client.stream.shutdown(std.testing.io, .send); if (client.beginBulkCommand(&.{ "GET", "redis-client:send-failure" })) |_| { return error.ExpectedTransportFailure; } else |_| {} try std.testing.expectError(error.NotAllowed, client.sendCommand(&.{"PING"})); try std.testing.expectError(error.NotAllowed, client.beginBulkCommand(&.{ "GET", "redis-client:send-failure" }));}
test "native bulk command allocation failure before transmission leaves client usable" { const key = try std.testing.allocator.alloc(u8, 20 * 1024); defer std.testing.allocator.free(key); @memset(key, 'k'); var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{}); var client = try connect(failing.allocator()); defer client.close(); failing.fail_index = failing.alloc_index; try std.testing.expectError(error.OutOfMemory, client.beginBulkCommand(&.{ "GET", key })); try std.testing.expectEqualStrings("PONG", (try client.sendCommand(&.{"PING"})).asString().?);}
test "native bulk commands preserve Redis error classification without reusing unread replies" { const key = "redis-client:bulk-wrongtype"; var control = try connect(std.testing.allocator); defer control.close(); defer _ = control.sendCommand(&.{ "DEL", key }) catch @panic("Redis cleanup failed"); _ = try control.sendCommand(&.{ "SET", key, "unchanged" }); var client = try connect(std.testing.allocator); defer client.close(); try std.testing.expectError(error.RedisError, client.beginBulkCommand(&.{ "RPOP", key })); try std.testing.expectError(error.NotAllowed, client.sendCommand(&.{"PING"})); try std.testing.expectEqualStrings("unchanged", (try control.sendCommand(&.{ "GET", key })).asString().?);}