Skip to content

Commit

Permalink
File handles: Use ObjectSpace for counting open files
Browse files Browse the repository at this point in the history
  • Loading branch information
haslo committed Nov 23, 2018
1 parent 4c57766 commit 2c87a2e
Showing 1 changed file with 6 additions and 28 deletions.
34 changes: 6 additions & 28 deletions spec/prawn/images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,13 @@
end

it 'closes opened files again after getting Pathname objects' do
# lsof version
system_has_lsof = system('lsof -v > /dev/null 2>&1')
system_has_grep = system('grep --version > /dev/null 2>&1')
if system_has_lsof && system_has_grep
# linux and macOS will actually run this spec
gc_was_disabled = GC.disable # make sure GC doesn't close file

open_files_before = `lsof -c ruby | grep "#{filename}"`
_info = pdf.image(Pathname.new(filename))
expect(`lsof -c ruby | grep "#{filename}"`).to eq(open_files_before)

GC.enable unless gc_was_disabled
else
# can't test this on every OS
expect(system_has_lsof && system_has_grep).to be_falsy
end
end
gc_was_disabled = GC.disable
open_files_before = ObjectSpace.each_object(File).count { |f| !f.closed? }
_info = pdf.image(Pathname.new(filename))
open_files_after = ObjectSpace.each_object(File).count { |f| !f.closed? }
GC.enable unless gc_was_disabled

context 'with File message spy' do
before do
file_path = Pathname.new(filename)
file_content = File.new(filename, 'rb').read
allow(File).to receive(:binread).with(file_path).and_return(file_content)
end

it 'uses binread, which closes opened files from pathnames' do
_info = pdf.image(Pathname.new(filename))
expect(File).to have_received(:binread)
end
expect(open_files_after).to eq(open_files_before)
end

context 'setting the length of the bytestream' do
Expand Down

0 comments on commit 2c87a2e

Please sign in to comment.