Skip to content

Commit

Permalink
Add specs for Action#bytesize
Browse files Browse the repository at this point in the history
  • Loading branch information
spuun committed Apr 4, 2024
1 parent aeb94aa commit e91532e
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions spec/replication/actions_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "../spec_helper"

def with_datadir_tempfile(&)
relative_path = Path.new "data.spec"
def with_datadir_tempfile(filename = "data.spec", &)
relative_path = Path.new filename
absolute_path = Path.new(LavinMQ::Config.instance.data_dir).join relative_path
yield relative_path.to_s, absolute_path.to_s
ensure
Expand Down Expand Up @@ -55,6 +55,16 @@ describe LavinMQ::Replication::Action do
end
end
end

describe "#bytesize" do
it "should count filename and filesize" do
with_datadir_tempfile("data") do |_relative, absolute|
File.write absolute, "foo"
action = LavinMQ::Replication::AddAction.new absolute
action.bytesize.should eq(sizeof(Int32) + "data".bytesize + sizeof(Int64) + "foo".bytesize)
end
end
end
end

describe "with @mfile" do
Expand All @@ -72,6 +82,15 @@ describe LavinMQ::Replication::Action do
end
end
end
describe "#bytesize" do
it "should count filename and filesize" do
with_datadir_tempfile("data") do |_relative, absolute|
File.write absolute, "foo"
action = LavinMQ::Replication::AddAction.new absolute
action.bytesize.should eq(sizeof(Int32) + "data".bytesize + sizeof(Int64) + "foo".bytesize)
end
end
end
end
end

Expand All @@ -90,9 +109,17 @@ describe LavinMQ::Replication::Action do
end
end
end
describe "#bytesize" do
it "should count filename and size of Int32" do
with_datadir_tempfile("data") do |_relative, absolute|
action = LavinMQ::Replication::AppendAction.new absolute, 123i32
action.bytesize.should eq(sizeof(Int32) + "data".bytesize + sizeof(Int64) + sizeof(Int32))
end
end
end
end

describe "with Int32" do
describe "with UInt32" do
describe "#send" do
it "writes filename and data to IO" do
with_datadir_tempfile do |relative, absolute|
Expand All @@ -106,6 +133,14 @@ describe LavinMQ::Replication::Action do
end
end
end
describe "#bytesize" do
it "should count filename and size of UInt32" do
with_datadir_tempfile("data") do |_relative, absolute|
action = LavinMQ::Replication::AppendAction.new absolute, 123u32
action.bytesize.should eq(sizeof(Int32) + "data".bytesize + sizeof(Int64) + sizeof(UInt32))
end
end
end
end

describe "with Bytes" do
Expand All @@ -122,6 +157,14 @@ describe LavinMQ::Replication::Action do
end
end
end
describe "#bytesize" do
it "should count filename and size of Bytes" do
with_datadir_tempfile("data") do |_relative, absolute|
action = LavinMQ::Replication::AppendAction.new absolute, "foo".to_slice
action.bytesize.should eq(sizeof(Int32) + "data".bytesize + sizeof(Int64) + "foo".to_slice.bytesize)
end
end
end
end

describe "with FileRange" do
Expand All @@ -141,6 +184,16 @@ describe LavinMQ::Replication::Action do
end
end
end
describe "#bytesize" do
it "should count filename and size of FileRange" do
with_datadir_tempfile("data") do |_relative, absolute|
File.write absolute, "foo bar baz"
range = LavinMQ::Replication::FileRange.new(MFile.new(absolute), 4, 3)
action = LavinMQ::Replication::AppendAction.new absolute, range
action.bytesize.should eq(sizeof(Int32) + "data".bytesize + sizeof(Int64) + range.len)
end
end
end
end
end
end

0 comments on commit e91532e

Please sign in to comment.