Skip to content

Commit

Permalink
Test signature for each cache format version
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanhefner committed Jul 5, 2023
1 parent bafe142 commit 04bca8b
Showing 1 changed file with 38 additions and 1 deletion.
Expand Up @@ -5,7 +5,22 @@
module CacheStoreFormatVersionBehavior
extend ActiveSupport::Concern

FORMAT_VERSIONS = [6.1, 7.0, 7.1]
FORMAT_VERSION_SIGNATURES = {
6.1 => [
"\x04\x08o".b, # Marshal.dump(entry)
"\x04\x08o".b, # Marshal.dump(entry.compressed(...))
],
7.0 => [
"\x00\x04\x08[".b, # "\x00" + Marshal.dump(entry.pack)
"\x01\x78".b, # "\x01" + Zlib::Deflate.deflate(...)
],
7.1 => [
"\x00\x04\x08[".b, # "\x00" + Marshal.dump(entry.pack)
"\x01\x78".b, # "\x01" + Zlib::Deflate.deflate(...)
],
}

FORMAT_VERSIONS = FORMAT_VERSION_SIGNATURES.keys

included do
test "format version affects default coder" do
Expand All @@ -26,6 +41,28 @@ module CacheStoreFormatVersionBehavior
end
end

FORMAT_VERSION_SIGNATURES.each do |format_version, (uncompressed_signature, compressed_signature)|
test "format version #{format_version.inspect} uses correct signature for uncompressed entries" do
serialized = with_format(format_version) do
lookup_store.send(:serialize_entry, ActiveSupport::Cache::Entry.new(["value"] * 100))
end

skip if !serialized.is_a?(String)

assert_operator serialized, :start_with?, uncompressed_signature
end

test "format version #{format_version.inspect} uses correct signature for compressed entries" do
serialized = with_format(format_version) do
lookup_store.send(:serialize_entry, ActiveSupport::Cache::Entry.new(["value"] * 100), compress_threshold: 1)
end

skip if !serialized.is_a?(String)

assert_operator serialized, :start_with?, compressed_signature
end
end

FORMAT_VERSIONS.product(FORMAT_VERSIONS) do |read_version, write_version|
test "format version #{read_version.inspect} can read #{write_version.inspect} entries" do
key = SecureRandom.uuid
Expand Down

0 comments on commit 04bca8b

Please sign in to comment.