Skip to content

Commit

Permalink
Add specs to verify ClosedError is raised
Browse files Browse the repository at this point in the history
  • Loading branch information
spuun committed Mar 15, 2024
1 parent c1b0e92 commit 0ef08dc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
63 changes: 63 additions & 0 deletions spec/mfile_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require "spec"
require "../src/lavinmq/mfile"

module MFileSpec
def self.with_file(&)
file = File.tempfile "mfile_spec"
yield file
ensure
file.delete unless file.nil?
end

describe MFile do
describe "#write" do
it "should raise ClosedError if closed" do
with_file do |file|
mfile = MFile.new file.path
mfile.close
expect_raises(MFile::ClosedError) { mfile.write "foo".to_slice }
end
end
end
describe "#read" do
it "should raise ClosedError if closed" do
with_file do |file|
mfile = MFile.new file.path
mfile.close
data = Bytes.new(1)
expect_raises(MFile::ClosedError) { mfile.read data }
end
end
end
describe "#to_slice" do
describe "without position and size" do
it "should raise ClosedError if closed" do
with_file do |file|
mfile = MFile.new file.path
mfile.close
expect_raises(MFile::ClosedError) { mfile.to_slice }
end
end
end
describe "with position and size" do
it "should raise ClosedError if closed" do
with_file do |file|
mfile = MFile.new file.path
mfile.close
expect_raises(MFile::ClosedError) { mfile.to_slice(1, 1) }
end
end
end
end
describe "#copy_to" do
it "should raise ClosedError if closed" do
with_file do |file|
mfile = MFile.new file.path
mfile.close
data = IO::Memory.new
expect_raises(MFile::ClosedError) { mfile.copy_to data }
end
end
end
end
end
1 change: 1 addition & 0 deletions src/lavinmq/mfile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class MFile < IO
# Copies the file to another IO
# Won't mmap the file if it's unmapped already
def copy_to(output : IO, size = @size) : Int64
raise ClosedError.new if @closed
if unmapped? # don't remap unmapped files
io = IO::FileDescriptor.new(@fd, blocking: true, close_on_finalize: false)
io.rewind
Expand Down

0 comments on commit 0ef08dc

Please sign in to comment.