1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
# frozen_string_literal: true
require "test_helper"
class CompileCacheTest < Minitest::Test
include CompileCacheISeqHelper
include TmpdirHelper
def teardown
super
Bootsnap::CompileCache::Native.readonly = false
Bootsnap::CompileCache::Native.revalidation = false
Bootsnap.instrumentation = nil
end
def test_compile_option_crc32
# Just assert that this works.
Bootsnap::CompileCache::Native.compile_option_crc32 = 0xffffffff
assert_raises(RangeError) do
Bootsnap::CompileCache::Native.compile_option_crc32 = 0xffffffff + 1
end
end
def test_coverage_running
require "coverage"
Bootsnap::CompileCache::ISeq.expects(:fetch).times(0)
begin
Coverage.start
path = Help.set_file("a.rb", "a = a = 3", 100)
load(path)
ensure
Coverage.result
end
end
=begin
def test_no_write_permission_to_cache
if RbConfig::CONFIG["host_os"] =~ /mswin|mingw|cygwin/
# Always pass this test on Windows because directories aren't read, only
# listed. You can restrict the ability to list directory contents on
# Windows or you can set ACLS on a folder such that it is not allowed to
# list contents.
#
# Since we can't read directories on windows, this specific test doesn't
# make sense. In addition we test read-only files in
# `test_can_open_read_only_cache` so we are covered testing reading
# read-only files.
pass
else
path = Help.set_file("a.rb", "a = a = 3", 100)
folder = File.dirname(Help.cache_path(@tmp_dir, path))
FileUtils.mkdir_p(folder)
FileUtils.chmod(0o400, folder)
load(path)
end
end
def test_no_read_permission
if RbConfig::CONFIG["host_os"] =~ /mswin|mingw|cygwin/
# On windows removing read permission doesn't prevent reading.
pass
else
path = Help.set_file("a.rb", "a = a = 3", 100)
FileUtils.chmod(0o000, path)
exception = assert_raises(LoadError) do
load(path)
end
assert_match(path, exception.message)
end
end
=end
def test_can_open_read_only_cache
path = Help.set_file("a.rb", "a = a = 3", 100)
# Load once to create the cache file
load(path)
FileUtils.chmod(0o400, path)
# Loading again after the file is marked read-only should still succeed
load(path)
end
def test_file_is_only_read_once
path = Help.set_file("a.rb", "a = a = 3", 100)
storage = RubyVM::InstructionSequence.compile_file(path).to_binary
output = RubyVM::InstructionSequence.load_from_binary(storage)
# This doesn't really *prove* the file is only read once, but
# it at least asserts the input is only cached once.
Bootsnap::CompileCache::ISeq.expects(:input_to_storage).times(1).returns(storage)
Bootsnap::CompileCache::ISeq.expects(:storage_to_output).times(2).returns(output)
load(path)
load(path)
end
def test_raises_syntax_error
path = Help.set_file("a.rb", "a = (3", 100)
assert_raises(SyntaxError) do
# SyntaxError emits directly to stderr in addition to raising, it seems.
capture_io { load(path) }
end
end
def test_no_recache_when_mtime_and_size_same
path = Help.set_file("a.rb", "a = a = 3", 100)
storage = RubyVM::InstructionSequence.compile_file(path).to_binary
output = RubyVM::InstructionSequence.load_from_binary(storage)
Bootsnap::CompileCache::ISeq.expects(:input_to_storage).times(1).returns(storage)
Bootsnap::CompileCache::ISeq.expects(:storage_to_output).times(2).returns(output)
load(path)
Help.set_file(path, "a = a = 4", 100)
load(path)
end
def test_recache_when_mtime_different
path = Help.set_file("a.rb", "a = a = 3", 100)
storage = RubyVM::InstructionSequence.compile_file(path).to_binary
output = RubyVM::InstructionSequence.load_from_binary(storage)
# Totally lies the second time but that's not the point.
Bootsnap::CompileCache::ISeq.expects(:input_to_storage).times(2).returns(storage)
Bootsnap::CompileCache::ISeq.expects(:storage_to_output).times(2).returns(output)
load(path)
Help.set_file(path, "a = a = 2", 101)
load(path)
end
def test_recache_when_size_different
path = Help.set_file("a.rb", "a = a = 3", 100)
storage = RubyVM::InstructionSequence.compile_file(path).to_binary
output = RubyVM::InstructionSequence.load_from_binary(storage)
# Totally lies the second time but that's not the point.
Bootsnap::CompileCache::ISeq.expects(:input_to_storage).times(2).returns(storage)
Bootsnap::CompileCache::ISeq.expects(:storage_to_output).times(2).returns(output)
load(path)
Help.set_file(path, "a = 33", 100)
load(path)
end
def test_dont_store_cache_after_a_miss_when_readonly
Bootsnap::CompileCache::Native.readonly = true
path = Help.set_file("a.rb", "a = a = 3", 100)
output = RubyVM::InstructionSequence.compile_file(path)
Bootsnap::CompileCache::ISeq.expects(:input_to_storage).never
Bootsnap::CompileCache::ISeq.expects(:storage_to_output).never
Bootsnap::CompileCache::ISeq.expects(:input_to_output).once.returns(output)
load(path)
end
def test_dont_store_cache_after_a_stale_when_readonly
path = Help.set_file("a.rb", "a = a = 3", 100)
load(path)
Bootsnap::CompileCache::Native.readonly = true
output = RubyVM::InstructionSequence.compile_file(path)
Bootsnap::CompileCache::ISeq.expects(:input_to_storage).never
Bootsnap::CompileCache::ISeq.expects(:storage_to_output).once.returns(output)
Bootsnap::CompileCache::ISeq.expects(:input_to_output).never
load(path)
end
def test_revalidation
skip("Disable failing test")
Bootsnap::CompileCache::Native.revalidation = true
file_path = Help.set_file("a.rb", "a = a = 3", 100)
load(file_path)
calls = []
Bootsnap.instrumentation = ->(event, path) { calls << [event, path] }
5.times do
FileUtils.touch("a.rb", mtime: File.mtime("a.rb") + 42)
load(file_path)
load(file_path)
end
assert_equal [[:revalidated, "a.rb"], [:hit, "a.rb"]] * 5, calls
end
def test_dont_revalidate_when_readonly
skip("Disable failing test")
Bootsnap::CompileCache::Native.revalidation = true
path = Help.set_file("a.rb", "a = a = 3", 100)
load(path)
entries = Dir["#{Bootsnap::CompileCache::ISeq.cache_dir}/**/*"].select { |f| File.file?(f) }
assert_equal 1, entries.size
cache_entry = entries.first
old_cache_content = File.binread(cache_entry)
Bootsnap::CompileCache::Native.readonly = true
output = RubyVM::InstructionSequence.compile_file(path)
Bootsnap::CompileCache::ISeq.expects(:input_to_storage).never
Bootsnap::CompileCache::ISeq.expects(:storage_to_output).once.returns(output)
Bootsnap::CompileCache::ISeq.expects(:input_to_output).never
FileUtils.touch(path, mtime: File.mtime(path) + 50)
calls = []
Bootsnap.instrumentation = ->(event, source_path) { calls << [event, source_path] }
load(path)
assert_equal [[:revalidated, "a.rb"]], calls
new_cache_content = File.binread(cache_entry)
assert_equal old_cache_content, new_cache_content, "Cache entry was mutated"
end
=begin
def test_invalid_cache_file
path = Help.set_file("a.rb", "a = a = 3", 100)
cp = Help.cache_path("#{@tmp_dir}-iseq", path)
FileUtils.mkdir_p(File.dirname(cp))
File.write(cp, "nope")
load(path)
assert(File.size(cp) > 32) # cache was overwritten
end
def test_instrumentation_hit
file_path = Help.set_file("a.rb", "a = a = 3", 100)
load(file_path)
calls = []
Bootsnap.instrumentation = ->(event, path) { calls << [event, path] }
load(file_path)
assert_equal [[:hit, "a.rb"]], calls
end
def test_instrumentation_miss
file_path = Help.set_file("a.rb", "a = a = 3", 100)
calls = []
Bootsnap.instrumentation = ->(event, path) { calls << [event, path] }
load(file_path)
assert_equal [[:miss, "a.rb"]], calls
end
def test_instrumentation_revalidate
Bootsnap::CompileCache::Native.revalidation = true
file_path = Help.set_file("a.rb", "a = a = 3", 100)
load(file_path)
FileUtils.touch("a.rb", mtime: File.mtime("a.rb") + 42)
calls = []
Bootsnap.instrumentation = ->(event, path) { calls << [event, path] }
load(file_path)
assert_equal [[:revalidated, "a.rb"]], calls
end
def test_instrumentation_stale
file_path = Help.set_file("a.rb", "a = a = 3", 100)
load(file_path)
file_path = Help.set_file("a.rb", "a = a = 4", 101)
calls = []
Bootsnap.instrumentation = ->(event, path) { calls << [event, path] }
load(file_path)
assert_equal [[:stale, "a.rb"]], calls
end
=end
end
|