Skip to content

Commit

Permalink
Stop using minitest/mock
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Aug 21, 2024
1 parent dad3440 commit 9299e55
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
8 changes: 8 additions & 0 deletions lib/redis_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/redis_client/circuit_breaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/redis_client/ruby_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/shared/redis_client_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 20 additions & 2 deletions test/support/client_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9299e55

Please sign in to comment.