-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
33 changed files
with
3,909 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
test/unit/behaviors_rails_7_0/cache_delete_matched_behavior.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
29 changes: 29 additions & 0 deletions
29
test/unit/behaviors_rails_7_0/cache_increment_decrement_behavior.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
97
test/unit/behaviors_rails_7_0/cache_instrumentation_behavior.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.