From 9d8dc8b40170fbb954cb5ac189e86f556882a01c Mon Sep 17 00:00:00 2001 From: Vicente Cheng Date: Thu, 21 Mar 2024 23:17:48 +0800 Subject: [PATCH] replica: add unmap unittest Signed-off-by: Vicente Cheng --- pkg/replica/replica_test.go | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/pkg/replica/replica_test.go b/pkg/replica/replica_test.go index 44d68ff0d..7890f7e33 100644 --- a/pkg/replica/replica_test.go +++ b/pkg/replica/replica_test.go @@ -1119,3 +1119,65 @@ func (s *TestSuite) TestUnmapMarkDiskRemoved(c *C) { c.Assert(r.activeDiskData[1].Removed, Equals, false) c.Assert(r.activeDiskData[1].Parent, Equals, "") } + +func (s *TestSuite) TestUnmap(c *C) { + dir, err := os.MkdirTemp("", "replica") + c.Assert(err, IsNil) + defer os.RemoveAll(dir) + + r, err := New(8*b, b, dir, nil, false, false, 250, 0) + c.Assert(err, IsNil) + defer r.Close() + + buf := make([]byte, 8*b) + fill(buf, 1) + _, err = r.WriteAt(buf, 0) + c.Assert(err, IsNil) + + // default sector size is 4096, we follow this assumption in the test + // offset = 0, unmap length = 0: 0 sectors can be unmapped + ret, err := r.UnmapAt(0, 0) + c.Assert(err, IsNil) + c.Assert(ret, Equals, 0) + + // offset = 0, unmap length = 512: 0 sectors can be unmapped + ret, err = r.UnmapAt(512, 0) + c.Assert(err, IsNil) + c.Assert(ret, Equals, 0) + + // offset = 0, unmap length = 4096: 1 sector can be unmapped + ret, err = r.UnmapAt(b, 0) + c.Assert(err, IsNil) + c.Assert(ret, Equals, 4096) + _, err = r.WriteAt(buf, 0) + c.Assert(err, IsNil) + + // offset = 0, unmap length = 4097: 1 sector can be unmapped + ret, err = r.UnmapAt(b+1, 0) + c.Assert(err, IsNil) + c.Assert(ret, Equals, 4096) + _, err = r.WriteAt(buf, 0) + c.Assert(err, IsNil) + + // offset = 1, unmap length = 512: 0 sectors can be unmapped + ret, err = r.UnmapAt(512, 1) + c.Assert(err, IsNil) + c.Assert(ret, Equals, 0) + + // offset = 1, unmap length = 4096: 0 sector can be unmapped + ret, err = r.UnmapAt(b, 1) + c.Assert(err, IsNil) + c.Assert(ret, Equals, 0) + + // offset = 2048, unmap length = 8192: 1 sector can be unmapped + ret, err = r.UnmapAt(2*b, 2048) + c.Assert(err, IsNil) + c.Assert(ret, Equals, 4096) + _, err = r.WriteAt(buf, 0) + c.Assert(err, IsNil) + + // offset = 2048, unmap length = 3072: 0 sector can be unmapped + ret, err = r.UnmapAt(b+1024, 2048) + c.Assert(err, IsNil) + c.Assert(ret, Equals, 0) +}