Skip to content

Commit

Permalink
Re-instate Rails 7.0 support
Browse files Browse the repository at this point in the history
It was removed to allow the latest cache behaviour tests to pass, but
instead we'll switch in different behaviour tests for each Rails
version.
  • Loading branch information
djmb committed Mar 20, 2024
1 parent 1bdeb6d commit 259e3da
Show file tree
Hide file tree
Showing 33 changed files with 3,909 additions and 23 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ GEM
minitest (5.22.2)
mocha (2.1.0)
ruby2_keywords (>= 0.0.5)
msgpack (1.7.2)
mutex_m (0.2.0)
mysql2 (0.5.5)
nokogiri (1.16.2-arm64-darwin)
Expand Down Expand Up @@ -184,6 +185,7 @@ DEPENDENCIES
appraisal
debug
mocha
msgpack
mysql2
pg
rubocop (>= 1.25.1)
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/rails_7.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ GEM
minitest (5.21.1)
mocha (2.1.0)
ruby2_keywords (>= 0.0.5)
msgpack (1.7.2)
mutex_m (0.2.0)
mysql2 (0.5.5)
nokogiri (1.16.0-arm64-darwin)
Expand Down Expand Up @@ -179,6 +180,7 @@ DEPENDENCIES
appraisal
debug
mocha
msgpack
mysql2
pg
railties!
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/rails_7_1.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ GEM
minitest (5.21.1)
mocha (2.1.0)
ruby2_keywords (>= 0.0.5)
msgpack (1.7.2)
mutex_m (0.2.0)
mysql2 (0.5.5)
nokogiri (1.16.0-arm64-darwin)
Expand Down Expand Up @@ -191,6 +192,7 @@ DEPENDENCIES
appraisal
debug
mocha
msgpack
mysql2
pg
railties!
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/rails_main.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ GEM
minitest (5.21.2)
mocha (2.1.0)
ruby2_keywords (>= 0.0.5)
msgpack (1.7.2)
mysql2 (0.5.5)
nokogiri (1.16.2-arm64-darwin)
racc (~> 1.4)
Expand Down Expand Up @@ -190,6 +191,7 @@ DEPENDENCIES
appraisal
debug
mocha
msgpack
mysql2
pg
railties!
Expand Down
1 change: 1 addition & 0 deletions solid_cache.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
spec.add_dependency "railties", rails_version
spec.add_development_dependency "debug"
spec.add_development_dependency "mocha"
spec.add_development_dependency "msgpack"
end
60 changes: 45 additions & 15 deletions test/unit/behaviors.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
# frozen_string_literal: true

require_relative "behaviors/cache_delete_matched_behavior"
require_relative "behaviors/cache_increment_decrement_behavior"
require_relative "behaviors/cache_instrumentation_behavior"
require_relative "behaviors/cache_logging_behavior"
require_relative "behaviors/cache_store_behavior"
require_relative "behaviors/cache_store_version_behavior"
require_relative "behaviors/cache_store_coder_behavior"
require_relative "behaviors/cache_store_compression_behavior"
require_relative "behaviors/cache_store_format_version_behavior"
require_relative "behaviors/cache_store_serializer_behavior"
require_relative "behaviors/connection_pool_behavior"
require_relative "behaviors/encoded_key_cache_behavior"
require_relative "behaviors/failure_safety_behavior"
require_relative "behaviors/failure_raising_behavior"
require_relative "behaviors/local_cache_behavior"
if Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 0
require_relative "behaviors_rails_7_0/cache_delete_matched_behavior"
require_relative "behaviors_rails_7_0/cache_increment_decrement_behavior"
require_relative "behaviors_rails_7_0/cache_instrumentation_behavior"
require_relative "behaviors_rails_7_0/cache_store_behavior"
require_relative "behaviors_rails_7_0/cache_store_version_behavior"
require_relative "behaviors_rails_7_0/cache_store_coder_behavior"
require_relative "behaviors_rails_7_0/connection_pool_behavior"
require_relative "behaviors_rails_7_0/encoded_key_cache_behavior"
require_relative "behaviors_rails_7_0/failure_safety_behavior"
require_relative "behaviors_rails_7_0/failure_raising_behavior"
require_relative "behaviors_rails_7_0/local_cache_behavior"
elsif Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
require_relative "behaviors_rails_7_1/cache_delete_matched_behavior"
require_relative "behaviors_rails_7_1/cache_increment_decrement_behavior"
require_relative "behaviors_rails_7_1/cache_instrumentation_behavior"
require_relative "behaviors_rails_7_1/cache_logging_behavior"
require_relative "behaviors_rails_7_1/cache_store_behavior"
require_relative "behaviors_rails_7_1/cache_store_version_behavior"
require_relative "behaviors_rails_7_1/cache_store_coder_behavior"
require_relative "behaviors_rails_7_1/cache_store_compression_behavior"
require_relative "behaviors_rails_7_1/cache_store_format_version_behavior"
require_relative "behaviors_rails_7_1/cache_store_serializer_behavior"
require_relative "behaviors_rails_7_1/connection_pool_behavior"
require_relative "behaviors_rails_7_1/encoded_key_cache_behavior"
require_relative "behaviors_rails_7_1/failure_safety_behavior"
require_relative "behaviors_rails_7_1/failure_raising_behavior"
require_relative "behaviors_rails_7_1/local_cache_behavior"
else
require_relative "behaviors/cache_delete_matched_behavior"
require_relative "behaviors/cache_increment_decrement_behavior"
require_relative "behaviors/cache_instrumentation_behavior"
require_relative "behaviors/cache_logging_behavior"
require_relative "behaviors/cache_store_behavior"
require_relative "behaviors/cache_store_version_behavior"
require_relative "behaviors/cache_store_coder_behavior"
require_relative "behaviors/cache_store_compression_behavior"
require_relative "behaviors/cache_store_format_version_behavior"
require_relative "behaviors/cache_store_serializer_behavior"
require_relative "behaviors/connection_pool_behavior"
require_relative "behaviors/encoded_key_cache_behavior"
require_relative "behaviors/failure_safety_behavior"
require_relative "behaviors/failure_raising_behavior"
require_relative "behaviors/local_cache_behavior"
end
15 changes: 15 additions & 0 deletions test/unit/behaviors_rails_7_0/cache_delete_matched_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module CacheDeleteMatchedBehavior
def test_delete_matched
@cache.write("foo", "bar")
@cache.write("fu", "baz")
@cache.write("foo/bar", "baz")
@cache.write("fu/baz", "bar")
@cache.delete_matched(/oo/)
assert_not @cache.exist?("foo")
assert @cache.exist?("fu")
assert_not @cache.exist?("foo/bar")
assert @cache.exist?("fu/baz")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module CacheIncrementDecrementBehavior
def test_increment
key = SecureRandom.uuid
@cache.write(key, 1, raw: true)
assert_equal 1, @cache.read(key, raw: true).to_i
assert_equal 2, @cache.increment(key)
assert_equal 2, @cache.read(key, raw: true).to_i
assert_equal 3, @cache.increment(key)
assert_equal 3, @cache.read(key, raw: true).to_i

missing = @cache.increment(SecureRandom.alphanumeric)
assert(missing.nil? || missing == 1)
end

def test_decrement
key = SecureRandom.uuid
@cache.write(key, 3, raw: true)
assert_equal 3, @cache.read(key, raw: true).to_i
assert_equal 2, @cache.decrement(key)
assert_equal 2, @cache.read(key, raw: true).to_i
assert_equal 1, @cache.decrement(key)
assert_equal 1, @cache.read(key, raw: true).to_i

missing = @cache.decrement(SecureRandom.alphanumeric)
assert(missing.nil? || missing == -1)
end
end
97 changes: 97 additions & 0 deletions test/unit/behaviors_rails_7_0/cache_instrumentation_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# frozen_string_literal: true

module CacheInstrumentationBehavior
def test_fetch_multi_uses_write_multi_entries_store_provider_interface
assert_called(@cache, :write_multi_entries) do
@cache.fetch_multi "a", "b", "c" do |key|
key * 2
end
end
end

def test_write_multi_instrumentation
key_1 = SecureRandom.uuid
key_2 = SecureRandom.uuid
value_1 = SecureRandom.alphanumeric
value_2 = SecureRandom.alphanumeric
writes = { key_1 => value_1, key_2 => value_2 }

events = with_instrumentation "write_multi" do
@cache.write_multi(writes)
end

assert_equal %w[ cache_write_multi.active_support ], events.map(&:name)
assert_nil events[0].payload[:super_operation]
assert_equal({ key_1 => value_1, key_2 => value_2 }, events[0].payload[:key])
end

def test_instrumentation_with_fetch_multi_as_super_operation
key_1 = SecureRandom.uuid
@cache.write(key_1, SecureRandom.alphanumeric)

key_2 = SecureRandom.uuid

events = with_instrumentation "read_multi" do
@cache.fetch_multi(key_2, key_1) { |key| key * 2 }
end

assert_equal %w[ cache_read_multi.active_support ], events.map(&:name)
assert_equal :fetch_multi, events[0].payload[:super_operation]
assert_equal [key_2, key_1], events[0].payload[:key]
assert_equal [key_1], events[0].payload[:hits]
assert_equal @cache.class.name, events[0].payload[:store]
end

def test_instrumentation_empty_fetch_multi
events = with_instrumentation "read_multi" do
@cache.fetch_multi() { |key| key * 2 }
end

assert_equal %w[ cache_read_multi.active_support ], events.map(&:name)
assert_equal :fetch_multi, events[0].payload[:super_operation]
assert_equal [], events[0].payload[:key]
assert_equal [], events[0].payload[:hits]
assert_equal @cache.class.name, events[0].payload[:store]
end

def test_read_multi_instrumentation
key_1 = SecureRandom.uuid
@cache.write(key_1, SecureRandom.alphanumeric)

key_2 = SecureRandom.uuid

events = with_instrumentation "read_multi" do
@cache.read_multi(key_2, key_1)
end

assert_equal %w[ cache_read_multi.active_support ], events.map(&:name)
assert_equal [key_2, key_1], events[0].payload[:key]
assert_equal [key_1], events[0].payload[:hits]
assert_equal @cache.class.name, events[0].payload[:store]
end

def test_empty_read_multi_instrumentation
events = with_instrumentation "read_multi" do
@cache.read_multi()
end

assert_equal %w[ cache_read_multi.active_support ], events.map(&:name)
assert_equal [], events[0].payload[:key]
assert_equal [], events[0].payload[:hits]
assert_equal @cache.class.name, events[0].payload[:store]
end

private
def with_instrumentation(method)
event_name = "cache_#{method}.active_support"

[].tap do |events|
ActiveSupport::Notifications.subscribe event_name do |*args|
events << ActiveSupport::Notifications::Event.new(*args)
end
yield
end
ensure
ActiveSupport::Notifications.unsubscribe event_name
end
end
Loading

0 comments on commit 259e3da

Please sign in to comment.