Skip to content

Commit ccc0dc1

Browse files
igchorguptask
authored andcommitted
Adjust and enable tests for ShmFileSegment
1 parent e4bfebd commit ccc0dc1

File tree

6 files changed

+331
-192
lines changed

6 files changed

+331
-192
lines changed

cachelib/allocator/memory/tests/SlabAllocatorTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ TEST_F(SlabAllocatorTest, AdviseRelease) {
584584
shmName += std::to_string(::getpid());
585585
shmManager.createShm(shmName, allocSize, memory);
586586

587-
SCOPE_EXIT { shmManager.removeShm(shmName); };
587+
SCOPE_EXIT { shmManager.removeShm(shmName, PosixSysVSegmentOpts(false)); };
588588

589589
memory = util::align(Slab::kSize, size, memory, allocSize);
590590

@@ -714,7 +714,7 @@ TEST_F(SlabAllocatorTest, AdviseSaveRestore) {
714714
ShmManager shmManager(cacheDir, false /* posix */);
715715
shmManager.createShm(shmName, allocSize, memory);
716716

717-
SCOPE_EXIT { shmManager.removeShm(shmName); };
717+
SCOPE_EXIT { shmManager.removeShm(shmName, PosixSysVSegmentOpts(false)); };
718718

719719
{
720720
SlabAllocator s(memory, size, config);

cachelib/shm/tests/common.h

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class ShmTest : public ShmTestBase {
6969
// parallel by fbmake runtests.
7070
const std::string segmentName{};
7171
const size_t shmSize{0};
72+
ShmSegmentOpts opts;
7273

7374
protected:
7475
void SetUp() final {
@@ -87,17 +88,19 @@ class ShmTest : public ShmTestBase {
8788
virtual void clearSegment() = 0;
8889

8990
// common tests
90-
void testCreateAttach(bool posix);
91-
void testAttachReadOnly(bool posix);
92-
void testMapping(bool posix);
93-
void testMappingAlignment(bool posix);
94-
void testLifetime(bool posix);
95-
void testPageSize(PageSizeT, bool posix);
91+
void testCreateAttach();
92+
void testAttachReadOnly();
93+
void testMapping();
94+
void testMappingAlignment();
95+
void testLifetime();
96+
void testPageSize(PageSizeT);
9697
};
9798

9899
class ShmTestPosix : public ShmTest {
99100
public:
100-
ShmTestPosix() {}
101+
ShmTestPosix() {
102+
opts.typeOpts = PosixSysVSegmentOpts(true);
103+
}
101104

102105
private:
103106
void clearSegment() override {
@@ -113,7 +116,9 @@ class ShmTestPosix : public ShmTest {
113116

114117
class ShmTestSysV : public ShmTest {
115118
public:
116-
ShmTestSysV() {}
119+
ShmTestSysV() {
120+
opts.typeOpts = PosixSysVSegmentOpts(false);
121+
}
117122

118123
private:
119124
void clearSegment() override {
@@ -126,6 +131,25 @@ class ShmTestSysV : public ShmTest {
126131
}
127132
}
128133
};
134+
135+
class ShmTestFile : public ShmTest {
136+
public:
137+
ShmTestFile() {
138+
opts.typeOpts = FileShmSegmentOpts("/tmp/" + segmentName);
139+
}
140+
141+
private:
142+
void clearSegment() override {
143+
try {
144+
auto path = std::get<FileShmSegmentOpts>(opts.typeOpts).path;
145+
FileShmSegment::removeByPath(path);
146+
} catch (const std::system_error& e) {
147+
if (e.code().value() != ENOENT) {
148+
throw;
149+
}
150+
}
151+
}
152+
};
129153
} // namespace tests
130154
} // namespace cachelib
131155
} // namespace facebook

cachelib/shm/tests/test_page_size.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ namespace facebook {
2828
namespace cachelib {
2929
namespace tests {
3030

31-
void ShmTest::testPageSize(PageSizeT p, bool posix) {
32-
ShmSegmentOpts opts{p};
31+
void ShmTest::testPageSize(PageSizeT p) {
32+
opts.pageSize = p;
3333
size_t size = getPageAlignedSize(4096, p);
3434
ASSERT_TRUE(isPageAlignedSize(size, p));
3535

3636
// create with unaligned size
3737
ASSERT_NO_THROW({
38-
ShmSegment s(ShmNew, segmentName, size, posix, opts);
38+
ShmSegment s(ShmNew, segmentName, size, opts);
3939
ASSERT_TRUE(s.mapAddress(nullptr));
4040
ASSERT_EQ(p, getPageSizeInSMap(s.getCurrentMapping().addr));
4141
});
4242

4343
ASSERT_NO_THROW({
44-
ShmSegment s2(ShmAttach, segmentName, posix, opts);
44+
ShmSegment s2(ShmAttach, segmentName, opts);
4545
ASSERT_TRUE(s2.mapAddress(nullptr));
4646
ASSERT_EQ(p, getPageSizeInSMap(s2.getCurrentMapping().addr));
4747
});
@@ -52,13 +52,17 @@ void ShmTest::testPageSize(PageSizeT p, bool posix) {
5252
// complete yet. See https://fburl.com/f0umrcwq . We will re-enable these
5353
// tests on sandcastle when these get fixed.
5454

55-
TEST_F(ShmTestPosix, PageSizesNormal) { testPageSize(PageSizeT::NORMAL, true); }
55+
TEST_F(ShmTestPosix, PageSizesNormal) { testPageSize(PageSizeT::NORMAL); }
5656

57-
TEST_F(ShmTestPosix, PageSizesTwoMB) { testPageSize(PageSizeT::TWO_MB, true); }
57+
TEST_F(ShmTestPosix, PageSizesTwoMB) { testPageSize(PageSizeT::TWO_MB); }
5858

59-
TEST_F(ShmTestSysV, PageSizesNormal) { testPageSize(PageSizeT::NORMAL, false); }
59+
TEST_F(ShmTestSysV, PageSizesNormal) { testPageSize(PageSizeT::NORMAL); }
6060

61-
TEST_F(ShmTestSysV, PageSizesTwoMB) { testPageSize(PageSizeT::TWO_MB, false); }
61+
TEST_F(ShmTestSysV, PageSizesTwoMB) { testPageSize(PageSizeT::TWO_MB); }
62+
63+
TEST_F(ShmTestFile, PageSizesNormal) { testPageSize(PageSizeT::NORMAL); }
64+
65+
TEST_F(ShmTestFile, PageSizesTwoMB) { testPageSize(PageSizeT::TWO_MB); }
6266

6367
} // namespace tests
6468
} // namespace cachelib

cachelib/shm/tests/test_shm.cpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,26 @@ using facebook::cachelib::detail::getPageSize;
2828
using facebook::cachelib::detail::getPageSizeInSMap;
2929
using facebook::cachelib::detail::isPageAlignedSize;
3030

31-
void ShmTest::testCreateAttach(bool posix) {
31+
void ShmTest::testCreateAttach() {
3232
const unsigned char magicVal = 'd';
3333
{
3434
// create with 0 size should round up to page size
35-
ShmSegment s(ShmNew, segmentName, 0, posix);
35+
ShmSegment s(ShmNew, segmentName, 0, opts);
3636
ASSERT_EQ(getPageSize(), s.getSize());
3737
s.markForRemoval();
3838
}
3939

4040
{
4141
// create with unaligned size
4242
ASSERT_TRUE(isPageAlignedSize(shmSize));
43-
ShmSegment s(ShmNew, segmentName, shmSize + 500, posix);
43+
ShmSegment s(ShmNew, segmentName, shmSize + 500, opts);
4444
ASSERT_EQ(shmSize + getPageSize(), s.getSize());
4545
s.markForRemoval();
4646
}
4747
auto addr = getNewUnmappedAddr();
4848

4949
{
50-
ShmSegment s(ShmNew, segmentName, shmSize, posix);
50+
ShmSegment s(ShmNew, segmentName, shmSize, opts);
5151
ASSERT_EQ(s.getSize(), shmSize);
5252
ASSERT_FALSE(s.isMapped());
5353
ASSERT_TRUE(s.mapAddress(addr));
@@ -57,14 +57,14 @@ void ShmTest::testCreateAttach(bool posix) {
5757
ASSERT_TRUE(s.isMapped());
5858
checkMemory(addr, s.getSize(), 0);
5959
writeToMemory(addr, s.getSize(), magicVal);
60-
ASSERT_THROW(ShmSegment(ShmNew, segmentName, shmSize, posix),
60+
ASSERT_THROW(ShmSegment(ShmNew, segmentName, shmSize, opts),
6161
std::system_error);
6262
const auto m = s.getCurrentMapping();
6363
ASSERT_EQ(m.size, shmSize);
6464
}
6565

6666
ASSERT_NO_THROW({
67-
ShmSegment s2(ShmAttach, segmentName, posix);
67+
ShmSegment s2(ShmAttach, segmentName, opts);
6868
ASSERT_EQ(s2.getSize(), shmSize);
6969
ASSERT_TRUE(s2.mapAddress(addr));
7070
checkMemory(addr, s2.getSize(), magicVal);
@@ -73,15 +73,17 @@ void ShmTest::testCreateAttach(bool posix) {
7373
});
7474
}
7575

76-
TEST_F(ShmTestPosix, CreateAttach) { testCreateAttach(true); }
76+
TEST_F(ShmTestPosix, CreateAttach) { testCreateAttach(); }
7777

78-
TEST_F(ShmTestSysV, CreateAttach) { testCreateAttach(false); }
78+
TEST_F(ShmTestSysV, CreateAttach) { testCreateAttach(); }
7979

80-
void ShmTest::testMapping(bool posix) {
80+
TEST_F(ShmTestFile, CreateAttach) { testCreateAttach(); }
81+
82+
void ShmTest::testMapping() {
8183
const unsigned char magicVal = 'z';
8284
auto addr = getNewUnmappedAddr();
8385
{ // create a segment
84-
ShmSegment s(ShmNew, segmentName, shmSize, posix);
86+
ShmSegment s(ShmNew, segmentName, shmSize, opts);
8587
ASSERT_TRUE(s.mapAddress(addr));
8688
ASSERT_TRUE(s.isMapped());
8789
// creating another mapping should fail
@@ -95,7 +97,7 @@ void ShmTest::testMapping(bool posix) {
9597

9698
// map with nullptr
9799
{
98-
ShmSegment s(ShmAttach, segmentName, posix);
100+
ShmSegment s(ShmAttach, segmentName, opts);
99101
ASSERT_TRUE(s.mapAddress(nullptr));
100102
ASSERT_TRUE(s.isMapped());
101103
const auto m = s.getCurrentMapping();
@@ -107,7 +109,7 @@ void ShmTest::testMapping(bool posix) {
107109
}
108110

109111
{
110-
ShmSegment s(ShmAttach, segmentName, posix);
112+
ShmSegment s(ShmAttach, segmentName, opts);
111113
// can map again.
112114
ASSERT_TRUE(s.mapAddress(addr));
113115
ASSERT_TRUE(s.isMapped());
@@ -148,13 +150,15 @@ void ShmTest::testMapping(bool posix) {
148150
}
149151
}
150152

151-
TEST_F(ShmTestPosix, Mapping) { testMapping(true); }
153+
TEST_F(ShmTestPosix, Mapping) { testMapping(); }
154+
155+
TEST_F(ShmTestSysV, Mapping) { testMapping(); }
152156

153-
TEST_F(ShmTestSysV, Mapping) { testMapping(false); }
157+
TEST_F(ShmTestFile, Mapping) { testMapping(); }
154158

155-
void ShmTest::testMappingAlignment(bool posix) {
159+
void ShmTest::testMappingAlignment() {
156160
{ // create a segment
157-
ShmSegment s(ShmNew, segmentName, shmSize, posix);
161+
ShmSegment s(ShmNew, segmentName, shmSize, opts);
158162

159163
// 0 alignment is wrong.
160164
ASSERT_FALSE(s.mapAddress(nullptr, 0));
@@ -171,11 +175,13 @@ void ShmTest::testMappingAlignment(bool posix) {
171175
}
172176
}
173177

174-
TEST_F(ShmTestPosix, MappingAlignment) { testMappingAlignment(true); }
178+
TEST_F(ShmTestPosix, MappingAlignment) { testMappingAlignment(); }
179+
180+
TEST_F(ShmTestSysV, MappingAlignment) { testMappingAlignment(); }
175181

176-
TEST_F(ShmTestSysV, MappingAlignment) { testMappingAlignment(false); }
182+
TEST_F(ShmTestFile, MappingAlignment) { testMappingAlignment(); }
177183

178-
void ShmTest::testLifetime(bool posix) {
184+
void ShmTest::testLifetime() {
179185
const size_t safeSize = getRandomSize();
180186
const char magicVal = 'x';
181187
ASSERT_NO_THROW({
@@ -184,7 +190,7 @@ void ShmTest::testLifetime(bool posix) {
184190
// from address space. this should not actually delete the segment and
185191
// we should be able to map it back as long as the object is within the
186192
// scope.
187-
ShmSegment s(ShmNew, segmentName, safeSize, posix);
193+
ShmSegment s(ShmNew, segmentName, safeSize, opts);
188194
s.mapAddress(nullptr);
189195
auto m = s.getCurrentMapping();
190196
writeToMemory(m.addr, m.size, magicVal);
@@ -200,14 +206,14 @@ void ShmTest::testLifetime(bool posix) {
200206
// should be able to create a new segment with same segmentName after the
201207
// previous scope exit destroys the segment.
202208
const size_t newSize = getRandomSize();
203-
ShmSegment s(ShmNew, segmentName, newSize, posix);
209+
ShmSegment s(ShmNew, segmentName, newSize, opts);
204210
s.mapAddress(nullptr);
205211
auto m = s.getCurrentMapping();
206212
checkMemory(m.addr, m.size, 0);
207213
writeToMemory(m.addr, m.size, magicVal);
208214
}
209215
// attaching should have the same behavior.
210-
ShmSegment s(ShmAttach, segmentName, posix);
216+
ShmSegment s(ShmAttach, segmentName, opts);
211217
s.mapAddress(nullptr);
212218
s.markForRemoval();
213219
ASSERT_TRUE(s.isMarkedForRemoval());
@@ -218,5 +224,6 @@ void ShmTest::testLifetime(bool posix) {
218224
});
219225
}
220226

221-
TEST_F(ShmTestPosix, Lifetime) { testLifetime(true); }
222-
TEST_F(ShmTestSysV, Lifetime) { testLifetime(false); }
227+
TEST_F(ShmTestPosix, Lifetime) { testLifetime(); }
228+
TEST_F(ShmTestSysV, Lifetime) { testLifetime(); }
229+
TEST_F(ShmTestFile, Lifetime) { testLifetime(); }

cachelib/shm/tests/test_shm_death_style.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,24 @@ using namespace facebook::cachelib::tests;
2626

2727
using facebook::cachelib::detail::isPageAlignedSize;
2828

29-
void ShmTest::testAttachReadOnly(bool posix) {
29+
void ShmTest::testAttachReadOnly() {
3030
unsigned char magicVal = 'd';
3131
ShmSegmentOpts ropts{PageSizeT::NORMAL, true /* read Only */};
32+
ropts.typeOpts = opts.typeOpts;
3233
ShmSegmentOpts rwopts{PageSizeT::NORMAL, false /* read Only */};
34+
rwopts.typeOpts = opts.typeOpts;
3335

3436
{
3537
// attaching to something that does not exist should fail in read only
3638
// mode.
3739
ASSERT_TRUE(isPageAlignedSize(shmSize));
38-
ASSERT_THROW(ShmSegment(ShmAttach, segmentName, posix, ropts),
40+
ASSERT_THROW(ShmSegment(ShmAttach, segmentName, ropts),
3941
std::system_error);
4042
}
4143

4244
// create a new segment
4345
{
44-
ShmSegment s(ShmNew, segmentName, shmSize, posix, rwopts);
46+
ShmSegment s(ShmNew, segmentName, shmSize, rwopts);
4547
ASSERT_EQ(s.getSize(), shmSize);
4648
ASSERT_TRUE(s.mapAddress(nullptr));
4749
ASSERT_TRUE(s.isMapped());
@@ -51,7 +53,7 @@ void ShmTest::testAttachReadOnly(bool posix) {
5153
}
5254

5355
ASSERT_NO_THROW({
54-
ShmSegment s(ShmAttach, segmentName, posix, rwopts);
56+
ShmSegment s(ShmAttach, segmentName, rwopts);
5557
ASSERT_EQ(s.getSize(), shmSize);
5658
ASSERT_TRUE(s.mapAddress(nullptr));
5759
void* addr = s.getCurrentMapping().addr;
@@ -65,8 +67,8 @@ void ShmTest::testAttachReadOnly(bool posix) {
6567
// reading in read only mode should work fine. while another one is
6668
// attached.
6769
ASSERT_NO_THROW({
68-
ShmSegment s(ShmAttach, segmentName, posix, ropts);
69-
ShmSegment s2(ShmAttach, segmentName, posix, rwopts);
70+
ShmSegment s(ShmAttach, segmentName, ropts);
71+
ShmSegment s2(ShmAttach, segmentName, rwopts);
7072
ASSERT_EQ(s.getSize(), shmSize);
7173
ASSERT_TRUE(s.mapAddress(nullptr));
7274
void* addr = s.getCurrentMapping().addr;
@@ -89,7 +91,7 @@ void ShmTest::testAttachReadOnly(bool posix) {
8991
// detached. segment should be present after it.
9092
ASSERT_DEATH(
9193
{
92-
ShmSegment s(ShmAttach, segmentName, posix, ropts);
94+
ShmSegment s(ShmAttach, segmentName, ropts);
9395
ASSERT_EQ(s.getSize(), shmSize);
9496
ASSERT_TRUE(s.mapAddress(nullptr));
9597
void* addr = s.getCurrentMapping().addr;
@@ -101,12 +103,14 @@ void ShmTest::testAttachReadOnly(bool posix) {
101103
},
102104
".*");
103105

104-
ASSERT_NO_THROW(ShmSegment s(ShmAttach, segmentName, posix, ropts));
106+
ASSERT_NO_THROW(ShmSegment s(ShmAttach, segmentName, ropts));
105107
}
106108

107-
TEST_F(ShmTestPosix, AttachReadOnlyDeathTest) { testAttachReadOnly(true); }
109+
TEST_F(ShmTestPosix, AttachReadOnlyDeathTest) { testAttachReadOnly(); }
108110

109-
TEST_F(ShmTestSysV, AttachReadOnlyDeathTest) { testAttachReadOnly(false); }
111+
TEST_F(ShmTestSysV, AttachReadOnlyDeathTest) { testAttachReadOnly(); }
112+
113+
TEST_F(ShmTestFile, AttachReadOnlyDeathTest) { testAttachReadOnly(); }
110114

111115
int main(int argc, char** argv) {
112116
testing::InitGoogleTest(&argc, argv);

0 commit comments

Comments
 (0)