From 9299e558e8bb25e0d35baabc4425fbdcb49baf04 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 21 Aug 2024 10:31:45 +0200 Subject: [PATCH] Stop using minitest/mock --- lib/redis_client.rb | 8 ++++++++ lib/redis_client/circuit_breaker.rb | 4 ++-- lib/redis_client/ruby_connection.rb | 4 ++-- test/shared/redis_client_tests.rb | 4 ++-- test/support/client_test_helper.rb | 22 ++++++++++++++++++++-- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/lib/redis_client.rb b/lib/redis_client.rb index 92c59a5..1592f5c 100644 --- a/lib/redis_client.rb +++ b/lib/redis_client.rb @@ -45,6 +45,14 @@ def default_driver def default_driver=(name) @default_driver = driver(name) end + + def now + Process.clock_gettime(Process::CLOCK_MONOTONIC) + end + + def now_ms + Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) + end end register_driver :ruby do diff --git a/lib/redis_client/circuit_breaker.rb b/lib/redis_client/circuit_breaker.rb index f31176b..5d68a4f 100644 --- a/lib/redis_client/circuit_breaker.rb +++ b/lib/redis_client/circuit_breaker.rb @@ -63,7 +63,7 @@ def protect private def refresh_state - now = Process.clock_gettime(Process::CLOCK_MONOTONIC) + now = RedisClient.now @lock.synchronize do if @errors.last < (now - @error_timeout) if @success_threshold > 0 @@ -78,7 +78,7 @@ def refresh_state end def record_error - now = Process.clock_gettime(Process::CLOCK_MONOTONIC) + now = RedisClient.now expiry = now - @error_timeout @lock.synchronize do if @state == :closed diff --git a/lib/redis_client/ruby_connection.rb b/lib/redis_client/ruby_connection.rb index 18d1d02..d1190ac 100644 --- a/lib/redis_client/ruby_connection.rb +++ b/lib/redis_client/ruby_connection.rb @@ -104,9 +104,9 @@ def read(timeout = nil) end def measure_round_trip_delay - start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) + start = RedisClient.now_ms call(["PING"], @read_timeout) - Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start + RedisClient.now_ms - start end private diff --git a/test/shared/redis_client_tests.rb b/test/shared/redis_client_tests.rb index d802603..82e1d3b 100644 --- a/test/shared/redis_client_tests.rb +++ b/test/shared/redis_client_tests.rb @@ -470,9 +470,9 @@ def test_blocking_call_long_timeout def test_blocking_call_timeout_retries redis = new_client(timeout: 0.5, reconnect_attempts: [3.0]) - start = Process.clock_gettime(Process::CLOCK_MONOTONIC) + start = RedisClient.now assert_nil redis.blocking_call(0.1, "BRPOP", "list", "0.1") - duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start + duration = RedisClient.now - start assert duration < 0.5 # if we retried we'd have waited much long end diff --git a/test/support/client_test_helper.rb b/test/support/client_test_helper.rb index e5b952b..5ecef59 100644 --- a/test/support/client_test_helper.rb +++ b/test/support/client_test_helper.rb @@ -46,8 +46,26 @@ def setup private def travel(seconds, &block) - now = Process.clock_gettime(Process::CLOCK_MONOTONIC) + seconds - Process.stub(:clock_gettime, now, &block) + original_now = RedisClient.singleton_class.instance_method(:now) + original_now_ms = RedisClient.singleton_class.instance_method(:now_ms) + begin + RedisClient.singleton_class.alias_method(:now, :now) + RedisClient.define_singleton_method(:now) do + original_now.bind(RedisClient).call + seconds + end + + RedisClient.singleton_class.alias_method(:now_ms, :now_ms) + RedisClient.define_singleton_method(:now_ms) do + original_now_ms.bind(RedisClient).call + (seconds * 1000.0) + end + + yield + ensure + RedisClient.singleton_class.alias_method(:now, :now) + RedisClient.define_singleton_method(:now, original_now) + RedisClient.singleton_class.alias_method(:now_ms, :now_ms) + RedisClient.define_singleton_method(:now_ms, original_now_ms) + end end def simulate_network_errors(client, failures)