Skip to content

Commit

Permalink
Clean up tests, and make some connections module methods private
Browse files Browse the repository at this point in the history
  • Loading branch information
rossme authored and djmb committed Oct 22, 2024
1 parent e6bbc4e commit c342933
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
49 changes: 24 additions & 25 deletions lib/solid_cache/store/connections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,6 @@ def with_each_connection(async: false, &block)
end
end

def with_connection_for(key, async: false, &block)
connections.with_connection_for(key) do
execute(async, &block)
end
end

def with_connection(name, async: false, &block)
connections.with(name) do
execute(async, &block)
end
end

def group_by_connection(keys)
connections.assign(keys)
end

def connection_names
connections.names
end

def connections
@connections ||= SolidCache::Connections.from_config(@shard_options)
end
Expand All @@ -63,34 +43,53 @@ def setup!
connections
end

def with_connection_for(key, async: false, &block)
connections.with_connection_for(key) do
execute(async, &block)
end
end

def with_connection(name, async: false, &block)
connections.with(name) do
execute(async, &block)
end
end

def group_by_connection(keys)
connections.assign(keys)
end

def connection_names
connections.names
end

def reading_key(key, failsafe:, failsafe_returning: nil, &block)
failsafe(failsafe, returning: failsafe_returning) do
with_connection_for(key, &block)
end
end

def reading_keys(keys, failsafe:, failsafe_returning: nil)
group_by_connection(keys).map do |connection, keys|
group_by_connection(keys).map do |connection, grouped_keys|
failsafe(failsafe, returning: failsafe_returning) do
with_connection(connection) do
yield keys
yield grouped_keys
end
end
end
end


def writing_key(key, failsafe:, failsafe_returning: nil, &block)
failsafe(failsafe, returning: failsafe_returning) do
with_connection_for(key, &block)
end
end

def writing_keys(entries, failsafe:, failsafe_returning: nil)
group_by_connection(entries).map do |connection, entries|
group_by_connection(entries).map do |connection, grouped_entries|
failsafe(failsafe, returning: failsafe_returning) do
with_connection(connection) do
yield entries
yield grouped_entries
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/unit/behaviors/cache_increment_decrement_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ def test_decrement
assert_equal -100, missing
end

def test_ttl_isnt_updated
def test_ttl_is_not_updated
key = SecureRandom.uuid

assert_equal 1, @cache.increment(key, 1, expires_in: 1)
assert_equal 2, @cache.increment(key, 1, expires_in: 5000)

# having to sleep two seconds in a test is bad, but we're testing
# a wide range of backends with different TTL mecanisms, most without
# Having to sleep two seconds in a test is bad, but we're testing
# a wide range of backends with different TTL mechanisms, most without
# subsecond granularity, so this is the only reliable way.
sleep 2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ def test_decrement
assert_equal -100, missing
end

def test_ttl_isnt_updated
def test_ttl_is_not_updated
key = SecureRandom.uuid

assert_equal 1, @cache.increment(key, 1, expires_in: 1)
assert_equal 2, @cache.increment(key, 1, expires_in: 5000)

# having to sleep two seconds in a test is bad, but we're testing
# a wide range of backends with different TTL mecanisms, most without
# Having to sleep two seconds in a test is bad, but we're testing
# a wide range of backends with different TTL mechanisms, most without
# subsecond granularity, so this is the only reliable way.
sleep 2

Expand Down
2 changes: 1 addition & 1 deletion test/unit/delete_matched_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DeleteMatchedTest < ActiveSupport::TestCase
@peek = lookup_store(expires_in: 60)
end

test "deletes matched raises a NotImplementedError" do
test "delete matched raises a NotImplementedError" do
prefix = SecureRandom.alphanumeric
assert_raises(NotImplementedError) { @cache.delete_matched("#{prefix}%") }
end
Expand Down
4 changes: 2 additions & 2 deletions test/unit/execution_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_active_record_instrumention_expiry
}

ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
# Warm up the connections as they may log before instrumenation can be disabled
# Warm up the connections as they may log before instrumentation can be disabled
uninstrumented_cache.write("foo", "bar")
uninstrumented_cache.write("foo", "bar")
uninstrumented_cache.write("foo", "bar")
Expand All @@ -90,7 +90,7 @@ def test_active_record_instrumention_expiry
end

ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
# Warm up the connections as they may log before instrumenation can be disabled
# Warm up the connections as they may log before instrumentation can be disabled
instrumented_cache.write("foo", "bar")
instrumented_cache.write("foo", "bar")
instrumented_cache.write("foo", "bar")
Expand Down

0 comments on commit c342933

Please sign in to comment.