From c7db80b98e3edc2a2396f41d31b19ba723107334 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Thu, 19 Nov 2020 16:19:51 +1100 Subject: [PATCH] DEV: add test case covering partial writes This moves the responsibility of copying data into write_to_socket. The caller never mutates the string, this is cleaner cause we do no longer mutate the string we pass in. --- lib/redis/connection/ruby.rb | 2 +- test/internals_test.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/redis/connection/ruby.rb b/lib/redis/connection/ruby.rb index f2fbc2c05..68c7e84ef 100644 --- a/lib/redis/connection/ruby.rb +++ b/lib/redis/connection/ruby.rb @@ -68,6 +68,7 @@ def _read_from_socket(nbytes) end def _write_to_socket(data) + data = data.b total_bytes_written = 0 loop do case bytes_written = write_nonblock(data, exception: false) @@ -95,7 +96,6 @@ def _write_to_socket(data) def write(data) return super(data) unless @write_timeout - data = data.b length = data.bytesize total_count = 0 loop do diff --git a/test/internals_test.rb b/test/internals_test.rb index e19b56690..82ad78ef6 100644 --- a/test/internals_test.rb +++ b/test/internals_test.rb @@ -12,6 +12,16 @@ def test_logger assert log.string =~ /\[Redis\] call_time=\d+\.\d+ ms/ end + def test_large_payload + # see: https://github.com/redis/redis-rb/issues/962 + # large payloads will trigger write_nonblock to write a portion + # of the payload in connection/ruby.rb _write_to_socket + large = "\u3042" * 4_000_000 + r.setex("foo", 10, large) + result = r.get("foo") + assert_equal result, large + end + def test_logger_with_pipelining r.pipelined do r.set "foo", "bar"