Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
|
Alexander Potapenko
2016/01/14 10:54:16
Dunno if this should be 2016 or it's fine to keep
bcwhite
2016/01/19 19:49:39
Acknowledged.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/metrics/persistent_memory_allocator.h" | |
| 6 | |
| 7 #include "base/files/file.h" | |
| 8 #include "base/files/file_util.h" | |
| 9 #include "base/files/memory_mapped_file.h" | |
| 10 #include "base/files/scoped_temp_dir.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/metrics/histogram.h" | |
| 13 #include "base/rand_util.h" | |
| 14 #include "base/strings/safe_sprintf.h" | |
| 15 #include "base/threading/simple_thread.h" | |
| 16 #include "testing/gmock/include/gmock/gmock.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const uint32_t TEST_MEMORY_SIZE = 1 << 20; // 1 MiB | |
| 21 const uint32_t TEST_MEMORY_PAGE = 64 << 10; // 64 KiB | |
| 22 const uint32_t TEST_ID = 12345; | |
| 23 const char TEST_NAME[] = "TestAllocator"; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 namespace base { | |
| 28 | |
| 29 typedef PersistentMemoryAllocator::Reference Reference; | |
| 30 | |
| 31 class PersistentMemoryAllocatorTest : public testing::Test { | |
| 32 public: | |
| 33 struct TestObject1 { | |
| 34 int32_t onething; | |
|
Alexander Potapenko
2016/01/14 10:54:17
Not that it makes any difference, but can this be
bcwhite
2016/01/19 19:49:39
Done.
| |
| 35 char oranother; | |
| 36 }; | |
| 37 | |
| 38 struct TestObject2 { | |
| 39 int thiis; | |
| 40 long that; | |
| 41 float andthe; | |
| 42 char other; | |
| 43 double thing; | |
| 44 }; | |
| 45 | |
| 46 PersistentMemoryAllocatorTest() { | |
| 47 mem_segment_.reset(new char[TEST_MEMORY_SIZE]); | |
| 48 } | |
| 49 | |
| 50 void SetUp() override { | |
| 51 allocator_.reset(); | |
| 52 memset(mem_segment_.get(), 0, TEST_MEMORY_SIZE); | |
| 53 allocator_.reset(new PersistentMemoryAllocator( | |
| 54 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, | |
| 55 TEST_ID, TEST_NAME, false)); | |
| 56 allocator_->CreateHistograms(allocator_->Name()); | |
| 57 } | |
| 58 | |
| 59 void TearDown() override { | |
| 60 allocator_.reset(); | |
| 61 } | |
| 62 | |
| 63 unsigned CountIterables() { | |
| 64 PersistentMemoryAllocator::Iterator iter; | |
| 65 uint32_t type; | |
| 66 unsigned count = 0; | |
| 67 for (allocator_->CreateIterator(&iter); | |
| 68 allocator_->GetNextIterable(&iter, &type) != 0;) { | |
| 69 count++; | |
| 70 } | |
| 71 return count; | |
| 72 } | |
| 73 | |
| 74 scoped_ptr<char[]> mem_segment_; | |
|
Alexander Potapenko
2016/01/13 18:34:00
Should be protected.
bcwhite
2016/01/13 21:31:39
Done.
| |
| 75 scoped_ptr<PersistentMemoryAllocator> allocator_; | |
| 76 }; | |
| 77 | |
| 78 TEST_F(PersistentMemoryAllocatorTest, AllocateAndIterate) { | |
| 79 std::string base_name(TEST_NAME); | |
|
Alexander Potapenko
2016/01/13 18:34:00
Looks like this test case is testing several scena
bcwhite
2016/01/13 21:31:39
Done.
| |
| 80 EXPECT_EQ(TEST_ID, allocator_->Id()); | |
| 81 EXPECT_TRUE(allocator_->used_histogram_); | |
| 82 EXPECT_EQ(base_name + ".UsedKiB", | |
| 83 allocator_->used_histogram_->histogram_name()); | |
| 84 EXPECT_TRUE(allocator_->allocs_histogram_); | |
| 85 EXPECT_EQ(base_name + ".Allocs", | |
| 86 allocator_->allocs_histogram_->histogram_name()); | |
| 87 | |
| 88 PersistentMemoryAllocator::MemoryInfo meminfo0; | |
| 89 allocator_->GetMemoryInfo(&meminfo0); | |
| 90 EXPECT_EQ(TEST_MEMORY_SIZE, meminfo0.total); | |
| 91 EXPECT_GT(meminfo0.total, meminfo0.free); | |
| 92 | |
| 93 Reference block1 = allocator_->Allocate(sizeof(TestObject1), 1); | |
| 94 EXPECT_NE(0U, block1); | |
| 95 EXPECT_NE(nullptr, allocator_->GetAsObject<TestObject1>(block1, 1)); | |
| 96 EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block1, 1)); | |
| 97 EXPECT_LE(sizeof(TestObject1), allocator_->GetAllocSize(block1)); | |
| 98 EXPECT_GE(sizeof(TestObject1) + 7, allocator_->GetAllocSize(block1)); | |
|
Alexander Potapenko
2016/01/14 10:54:16
Where does this '7' comes from (here and below)?
bcwhite
2016/01/19 19:49:39
Done. Defined as constant.
| |
| 99 PersistentMemoryAllocator::MemoryInfo meminfo1; | |
| 100 allocator_->GetMemoryInfo(&meminfo1); | |
| 101 EXPECT_EQ(meminfo0.total, meminfo1.total); | |
| 102 EXPECT_GT(meminfo0.free, meminfo1.free); | |
| 103 | |
| 104 PersistentMemoryAllocator::Iterator iter; | |
| 105 uint32_t type; | |
| 106 allocator_->CreateIterator(&iter); | |
| 107 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); | |
| 108 allocator_->MakeIterable(block1); | |
| 109 EXPECT_EQ(block1, allocator_->GetNextIterable(&iter, &type)); | |
| 110 EXPECT_EQ(1U, type); | |
| 111 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); | |
| 112 | |
| 113 Reference block2 = allocator_->Allocate(sizeof(TestObject2), 2); | |
| 114 EXPECT_NE(0U, block2); | |
| 115 EXPECT_NE(nullptr, allocator_->GetAsObject<TestObject2>(block2, 2)); | |
| 116 EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block2, 1)); | |
| 117 EXPECT_LE(sizeof(TestObject2), allocator_->GetAllocSize(block2)); | |
| 118 EXPECT_GE(sizeof(TestObject2) + 7, allocator_->GetAllocSize(block2)); | |
| 119 PersistentMemoryAllocator::MemoryInfo meminfo2; | |
| 120 allocator_->GetMemoryInfo(&meminfo2); | |
| 121 EXPECT_EQ(meminfo1.total, meminfo2.total); | |
| 122 EXPECT_GT(meminfo1.free, meminfo2.free); | |
| 123 | |
| 124 allocator_->MakeIterable(block2); | |
| 125 EXPECT_EQ(block2, allocator_->GetNextIterable(&iter, &type)); | |
| 126 EXPECT_EQ(2U, type); | |
| 127 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); | |
| 128 | |
| 129 allocator_->CreateIterator(&iter, block1); | |
| 130 EXPECT_EQ(block2, allocator_->GetNextIterable(&iter, &type)); | |
| 131 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); | |
| 132 | |
| 133 EXPECT_FALSE(allocator_->IsFull()); | |
| 134 EXPECT_FALSE(allocator_->IsCorrupt()); | |
| 135 | |
| 136 allocator_->UpdateStaticHistograms(); | |
| 137 scoped_ptr<HistogramSamples> used_samples( | |
| 138 allocator_->used_histogram_->SnapshotSamples()); | |
| 139 EXPECT_TRUE(used_samples); | |
| 140 EXPECT_EQ(1, used_samples->TotalCount()); | |
| 141 | |
| 142 scoped_ptr<HistogramSamples> allocs_samples( | |
| 143 allocator_->allocs_histogram_->SnapshotSamples()); | |
| 144 EXPECT_TRUE(allocs_samples); | |
| 145 EXPECT_EQ(2, allocs_samples->TotalCount()); | |
| 146 EXPECT_EQ(0, allocs_samples->GetCount(0)); | |
| 147 EXPECT_EQ(1, allocs_samples->GetCount(sizeof(TestObject1))); | |
| 148 EXPECT_EQ(1, allocs_samples->GetCount(sizeof(TestObject2))); | |
| 149 #if !DCHECK_IS_ON() // DCHECK builds will die at a NOTREACHED(). | |
| 150 EXPECT_EQ(0U, allocator_->Allocate(TEST_MEMORY_SIZE + 1, 0)); | |
| 151 allocs_samples = allocator_->allocs_histogram_->SnapshotSamples(); | |
| 152 EXPECT_EQ(3, allocs_samples->TotalCount()); | |
| 153 EXPECT_EQ(1, allocs_samples->GetCount(0)); | |
| 154 #endif | |
| 155 | |
| 156 EXPECT_EQ(2U, allocator_->GetType(block2)); | |
| 157 allocator_->SetType(block2, 3); | |
| 158 EXPECT_EQ(3U, allocator_->GetType(block2)); | |
| 159 allocator_->SetType(block2, 2); | |
| 160 EXPECT_EQ(2U, allocator_->GetType(block2)); | |
| 161 | |
| 162 scoped_ptr<PersistentMemoryAllocator> allocator2( | |
| 163 new PersistentMemoryAllocator( | |
| 164 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, 0, "", | |
| 165 false)); | |
| 166 EXPECT_EQ(TEST_ID, allocator2->Id()); | |
| 167 EXPECT_FALSE(allocator2->used_histogram_); | |
| 168 EXPECT_FALSE(allocator2->allocs_histogram_); | |
| 169 EXPECT_NE(allocator2->allocs_histogram_, allocator_->allocs_histogram_); | |
| 170 | |
| 171 allocator2->CreateIterator(&iter); | |
| 172 EXPECT_EQ(block1, allocator2->GetNextIterable(&iter, &type)); | |
| 173 EXPECT_EQ(block2, allocator2->GetNextIterable(&iter, &type)); | |
| 174 EXPECT_EQ(0U, allocator2->GetNextIterable(&iter, &type)); | |
| 175 EXPECT_NE(nullptr, allocator2->GetAsObject<TestObject1>(block1, 1)); | |
| 176 EXPECT_NE(nullptr, allocator2->GetAsObject<TestObject2>(block2, 2)); | |
| 177 | |
| 178 scoped_ptr<const PersistentMemoryAllocator> allocator3( | |
| 179 new PersistentMemoryAllocator( | |
| 180 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, 0, "", true)); | |
| 181 EXPECT_EQ(TEST_ID, allocator3->Id()); | |
| 182 EXPECT_FALSE(allocator3->used_histogram_); | |
| 183 EXPECT_FALSE(allocator3->allocs_histogram_); | |
| 184 | |
| 185 allocator3->CreateIterator(&iter); | |
| 186 EXPECT_EQ(block1, allocator3->GetNextIterable(&iter, &type)); | |
| 187 EXPECT_EQ(block2, allocator3->GetNextIterable(&iter, &type)); | |
| 188 EXPECT_EQ(0U, allocator3->GetNextIterable(&iter, &type)); | |
| 189 EXPECT_NE(nullptr, allocator3->GetAsObject<TestObject1>(block1, 1)); | |
| 190 EXPECT_NE(nullptr, allocator3->GetAsObject<TestObject2>(block2, 2)); | |
| 191 } | |
| 192 | |
| 193 TEST_F(PersistentMemoryAllocatorTest, PageTest) { | |
| 194 Reference block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1); | |
| 195 EXPECT_LT(0U, block1); | |
| 196 EXPECT_GT(TEST_MEMORY_PAGE, block1); | |
| 197 | |
| 198 Reference block2 = allocator_->Allocate(TEST_MEMORY_PAGE - 16, 2); | |
|
Alexander Potapenko
2016/01/14 10:54:16
What are we testing here? Where does '16' come fro
bcwhite
2016/01/19 19:49:39
Done.
| |
| 199 EXPECT_EQ(TEST_MEMORY_PAGE, block2); | |
| 200 | |
| 201 Reference block3 = allocator_->Allocate(99, 3); | |
| 202 EXPECT_EQ(2U * TEST_MEMORY_PAGE, block3); | |
| 203 } | |
| 204 | |
| 205 class AllocatorThread : public SimpleThread { | |
| 206 public: | |
| 207 AllocatorThread(const std::string& name, | |
| 208 void* base, | |
| 209 uint32_t size, | |
| 210 uint32_t page_size) | |
| 211 : SimpleThread(name, Options()), | |
| 212 count_(0), | |
| 213 iterable_(0), | |
| 214 allocator_(base, size, page_size, 0, std::string(), false) {} | |
| 215 | |
| 216 void Run() override { | |
| 217 for (;;) { | |
| 218 uint32_t size = RandInt(1, 99); | |
| 219 uint32_t type = RandInt(100, 999); | |
| 220 Reference block = allocator_.Allocate(size, type); | |
| 221 if (!block) | |
| 222 break; | |
| 223 | |
| 224 count_++; | |
| 225 if (RandInt(0, 1)) { | |
| 226 allocator_.MakeIterable(block); | |
| 227 iterable_++; | |
| 228 } | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 unsigned count_; | |
|
Alexander Potapenko
2016/01/14 10:54:17
I think it's better to make count_ and iterable_ p
bcwhite
2016/01/19 19:49:39
That would have to be:
FRIEND_TEST_ALL_PREFIXES(P
| |
| 233 unsigned iterable_; | |
| 234 | |
| 235 private: | |
| 236 PersistentMemoryAllocator allocator_; | |
| 237 }; | |
| 238 | |
| 239 TEST_F(PersistentMemoryAllocatorTest, ParallelismTest) { | |
|
Alexander Potapenko
2016/01/14 10:54:16
Please add a commend describing what you're testin
bcwhite
2016/01/19 19:49:39
Done.
| |
| 240 void* memory = mem_segment_.get(); | |
| 241 AllocatorThread t1("t1", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); | |
|
Alexander Potapenko
2016/01/13 18:34:01
How about making t1..t5 an array and employing loo
bcwhite
2016/01/13 21:31:39
It was more complicated to do so because of the la
Alexander Potapenko
2016/01/14 10:54:17
I just thought this could let you shorten Parallel
bcwhite
2016/01/19 19:49:39
Acknowledged.
| |
| 242 AllocatorThread t2("t2", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); | |
| 243 AllocatorThread t3("t3", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); | |
| 244 AllocatorThread t4("t4", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); | |
| 245 AllocatorThread t5("t5", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); | |
| 246 | |
| 247 t1.Start(); | |
| 248 t2.Start(); | |
| 249 t3.Start(); | |
| 250 t4.Start(); | |
| 251 t5.Start(); | |
| 252 | |
| 253 unsigned last_count = 0; | |
| 254 do { | |
| 255 unsigned count = CountIterables(); | |
| 256 EXPECT_LE(last_count, count); | |
| 257 } while (!allocator_->IsCorrupt() && !allocator_->IsFull()); | |
| 258 | |
| 259 t1.Join(); | |
| 260 t2.Join(); | |
| 261 t3.Join(); | |
| 262 t4.Join(); | |
| 263 t5.Join(); | |
| 264 | |
| 265 EXPECT_FALSE(allocator_->IsCorrupt()); | |
| 266 EXPECT_EQ(CountIterables(), | |
| 267 t1.iterable_ + t2.iterable_ + t3.iterable_ + t4.iterable_ + | |
| 268 t5.iterable_); | |
| 269 } | |
| 270 | |
| 271 // This test doesn't verify anything other than it doesn't crash. | |
| 272 TEST_F(PersistentMemoryAllocatorTest, CorruptionTest) { | |
| 273 char* memory = mem_segment_.get(); | |
| 274 AllocatorThread t1("t1", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); | |
| 275 AllocatorThread t2("t2", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); | |
| 276 AllocatorThread t3("t3", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); | |
| 277 AllocatorThread t4("t4", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); | |
| 278 AllocatorThread t5("t5", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); | |
| 279 | |
| 280 t1.Start(); | |
| 281 t2.Start(); | |
| 282 t3.Start(); | |
| 283 t4.Start(); | |
| 284 t5.Start(); | |
| 285 | |
| 286 do { | |
| 287 size_t offset = RandInt(0, TEST_MEMORY_SIZE - 1); | |
| 288 char value = RandInt(0, 255); | |
| 289 memory[offset] = value; | |
|
Alexander Potapenko
2016/01/13 18:34:00
I'm concerned this may lead to test flakiness in t
bcwhite
2016/01/13 21:31:39
I'm not sure what you mean by "in the case of an a
Alexander Potapenko
2016/01/14 10:54:16
My point is that in the case there's a bug in the
bcwhite
2016/01/19 19:49:39
I know but at least there is indication that somet
| |
| 290 } while (!allocator_->IsCorrupt() && !allocator_->IsFull()); | |
| 291 | |
| 292 t1.Join(); | |
| 293 t2.Join(); | |
| 294 t3.Join(); | |
| 295 t4.Join(); | |
| 296 t5.Join(); | |
| 297 | |
| 298 CountIterables(); | |
| 299 } | |
| 300 | |
| 301 // Attempt to cause crashes or loops by expressly creating dangerous coditions. | |
|
Alexander Potapenko
2016/01/13 18:34:01
s/coditions/conditions
bcwhite
2016/01/13 21:31:39
Done.
| |
| 302 TEST_F(PersistentMemoryAllocatorTest, MaliciousTest) { | |
| 303 Reference block1 = allocator_->Allocate(sizeof(TestObject1), 1); | |
| 304 Reference block2 = allocator_->Allocate(sizeof(TestObject1), 2); | |
| 305 Reference block3 = allocator_->Allocate(sizeof(TestObject1), 3); | |
| 306 Reference block4 = allocator_->Allocate(sizeof(TestObject1), 3); | |
| 307 Reference block5 = allocator_->Allocate(sizeof(TestObject1), 3); | |
| 308 allocator_->MakeIterable(block1); | |
| 309 allocator_->MakeIterable(block2); | |
| 310 allocator_->MakeIterable(block3); | |
| 311 allocator_->MakeIterable(block4); | |
| 312 allocator_->MakeIterable(block5); | |
| 313 EXPECT_EQ(5U, CountIterables()); | |
|
Alexander Potapenko
2016/01/13 18:34:01
s/5U/5
(here and at other places, it doesn't real
bcwhite
2016/01/13 21:31:39
The "U" is required or there are signed/unsigned c
Alexander Potapenko
2016/01/14 10:54:16
This is quite strange.
`clang -Wsign-compare` does
bcwhite
2016/01/19 19:49:39
The errors were always in the EXPECT_ macros. The
| |
| 314 EXPECT_FALSE(allocator_->IsCorrupt()); | |
| 315 | |
| 316 // Create loop in iterable list and ensure it doesn't hang. | |
| 317 uint32_t* header4 = (uint32_t*)(mem_segment_.get() + block4); | |
| 318 EXPECT_EQ(block5, header4[3]); | |
| 319 header4[3] = block3; | |
| 320 CountIterables(); // loop: 1-2-3-4-3 | |
|
Alexander Potapenko
2016/01/13 18:34:01
Isn't the allocator corrupted already after this l
bcwhite
2016/01/13 21:31:39
Corruption doesn't stop or prevent iteration. It'
Alexander Potapenko
2016/01/14 10:54:16
If the flag is already set after the first CountIt
bcwhite
2016/01/19 19:49:39
Done.
| |
| 321 header4[3] = block2; | |
| 322 CountIterables(); // loop: 1-2-3-4-2 | |
| 323 header4[3] = block1; | |
| 324 CountIterables(); // loop: 1-2-3-4-1 | |
| 325 EXPECT_TRUE(allocator_->IsCorrupt()); | |
| 326 } | |
| 327 | |
| 328 | |
| 329 //----- LocalPersistentMemoryAllocator ----------------------------------------- | |
| 330 | |
| 331 TEST(LocalPersistentMemoryAllocatorTest, CreationTest) { | |
| 332 LocalPersistentMemoryAllocator allocator(TEST_MEMORY_SIZE, 42, ""); | |
| 333 EXPECT_EQ(42U, allocator.Id()); | |
| 334 EXPECT_NE(0U, allocator.Allocate(24, 1)); | |
| 335 EXPECT_FALSE(allocator.IsFull()); | |
|
Alexander Potapenko
2016/01/14 10:54:17
We need a positive test for IsFull() somewhere.
bcwhite
2016/01/19 19:49:39
There's an implicit one in the Parallelism test.
| |
| 336 EXPECT_FALSE(allocator.IsCorrupt()); | |
| 337 } | |
| 338 | |
| 339 | |
| 340 //----- FilePersistentMemoryAllocator ------------------------------------------ | |
| 341 | |
| 342 TEST(FilePersistentMemoryAllocatorTest, CreationTest) { | |
| 343 ScopedTempDir temp_dir; | |
| 344 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 345 FilePath file_path = temp_dir.path().AppendASCII("persistent_memory"); | |
| 346 | |
| 347 PersistentMemoryAllocator::MemoryInfo meminfo1; | |
| 348 Reference r123, r456, r789; | |
| 349 { | |
| 350 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); | |
| 351 EXPECT_FALSE(local.IsReadonly()); | |
| 352 r123 = local.Allocate(123, 123); | |
| 353 r456 = local.Allocate(456, 456); | |
| 354 r789 = local.Allocate(789, 789); | |
| 355 local.MakeIterable(r123); | |
| 356 local.SetType(r456, 654); | |
| 357 local.MakeIterable(r789); | |
| 358 local.GetMemoryInfo(&meminfo1); | |
| 359 EXPECT_FALSE(local.IsFull()); | |
| 360 EXPECT_FALSE(local.IsCorrupt()); | |
| 361 | |
| 362 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); | |
| 363 ASSERT_TRUE(writer.IsValid()); | |
| 364 writer.Write(0, (const char*)local.data(), local.used()); | |
| 365 } | |
| 366 | |
| 367 scoped_ptr<MemoryMappedFile> mmfile(new MemoryMappedFile()); | |
| 368 mmfile->Initialize(file_path); | |
| 369 EXPECT_TRUE(mmfile->IsValid()); | |
| 370 const size_t mmlength = mmfile->length(); | |
| 371 EXPECT_GE(meminfo1.total, mmlength); | |
| 372 | |
| 373 FilePersistentMemoryAllocator file(mmfile.release(), 0, ""); | |
| 374 EXPECT_TRUE(file.IsReadonly()); | |
| 375 EXPECT_EQ(TEST_ID, file.Id()); | |
| 376 EXPECT_FALSE(file.IsFull()); | |
| 377 EXPECT_FALSE(file.IsCorrupt()); | |
| 378 | |
| 379 PersistentMemoryAllocator::Iterator iter; | |
| 380 uint32_t type; | |
| 381 file.CreateIterator(&iter); | |
| 382 EXPECT_EQ(r123, file.GetNextIterable(&iter, &type)); | |
| 383 EXPECT_EQ(r789, file.GetNextIterable(&iter, &type)); | |
| 384 EXPECT_EQ(0U, file.GetNextIterable(&iter, &type)); | |
| 385 | |
| 386 EXPECT_EQ(123U, file.GetType(r123)); | |
| 387 EXPECT_EQ(654U, file.GetType(r456)); | |
| 388 EXPECT_EQ(789U, file.GetType(r789)); | |
| 389 | |
| 390 PersistentMemoryAllocator::MemoryInfo meminfo2; | |
| 391 file.GetMemoryInfo(&meminfo2); | |
| 392 EXPECT_GE(meminfo1.total, meminfo2.total); | |
| 393 EXPECT_GE(meminfo1.free, meminfo2.free); | |
| 394 EXPECT_EQ(mmlength, meminfo2.total); | |
| 395 EXPECT_EQ(0U, meminfo2.free); | |
| 396 } | |
| 397 | |
| 398 TEST(FilePersistentMemoryAllocatorTest, AcceptableTest) { | |
| 399 ScopedTempDir temp_dir; | |
| 400 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 401 FilePath file_path_base = temp_dir.path().AppendASCII("persistent_memory_"); | |
| 402 | |
| 403 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); | |
| 404 const size_t minsize = local.used(); | |
| 405 scoped_ptr<char[]> garbage(new char[minsize]); | |
| 406 RandBytes(garbage.get(), minsize); | |
| 407 | |
| 408 scoped_ptr<MemoryMappedFile> mmfile; | |
| 409 char filename[100]; | |
| 410 for (size_t filesize = minsize; filesize > 0; --filesize) { | |
| 411 strings::SafeSPrintf(filename, "memory_%d_A", filesize); | |
| 412 FilePath file_path = temp_dir.path().AppendASCII(filename); | |
| 413 ASSERT_FALSE(PathExists(file_path)); | |
| 414 { | |
| 415 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); | |
| 416 ASSERT_TRUE(writer.IsValid()); | |
| 417 writer.Write(0, (const char*)local.data(), filesize); | |
| 418 } | |
| 419 ASSERT_TRUE(PathExists(file_path)); | |
| 420 | |
| 421 mmfile.reset(new MemoryMappedFile()); | |
| 422 mmfile->Initialize(file_path); | |
| 423 EXPECT_EQ(filesize, mmfile->length()); | |
| 424 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) { | |
|
Alexander Potapenko
2016/01/13 18:34:00
Can you rephrase this as:
if (minsize==filesize)
bcwhite
2016/01/13 21:31:39
I need the function to run for all sizes. It must
| |
| 425 // Just need to make sure it doesn't crash. | |
| 426 FilePersistentMemoryAllocator allocator(mmfile.release(), 0, ""); | |
|
Alexander Potapenko
2016/01/13 18:34:00
This variable is unused, the compiler may warn abo
bcwhite
2016/01/13 21:31:39
The constructor needs to be run to ensure it can't
Alexander Potapenko
2016/01/14 10:54:16
You can cast the variable to void to prevent that:
bcwhite
2016/01/19 19:49:39
I wasn't aware of that trick. Done.
| |
| 427 } else { | |
| 428 EXPECT_GT(minsize, filesize); // Must be acceptable if minsize==filesize. | |
| 429 } | |
| 430 | |
| 431 #if !DCHECK_IS_ON() // DCHECK builds will die at a NOTREACHED(). | |
| 432 strings::SafeSPrintf(filename, "memory_%d_B", filesize); | |
| 433 file_path = temp_dir.path().AppendASCII(filename); | |
| 434 ASSERT_FALSE(PathExists(file_path)); | |
| 435 { | |
| 436 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); | |
| 437 ASSERT_TRUE(writer.IsValid()); | |
| 438 writer.Write(0, (const char*)garbage.get(), filesize); | |
| 439 } | |
| 440 ASSERT_TRUE(PathExists(file_path)); | |
| 441 | |
| 442 mmfile.reset(new MemoryMappedFile()); | |
| 443 mmfile->Initialize(file_path); | |
| 444 EXPECT_EQ(filesize, mmfile->length()); | |
| 445 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) { | |
| 446 // Just need to make sure it doesn't crash. | |
| 447 FilePersistentMemoryAllocator allocator(mmfile.release(), 0, "") ; | |
| 448 EXPECT_TRUE(allocator.IsCorrupt()); // Gargbage data so it should be. | |
|
Alexander Potapenko
2016/01/13 18:34:00
s/Gargbage/Garbage
bcwhite
2016/01/13 21:31:39
Done.
| |
| 449 } else { | |
| 450 EXPECT_GT(minsize, filesize); // Must be acceptable if minsize==filesize. | |
| 451 } | |
| 452 #endif | |
| 453 } | |
| 454 } | |
| 455 | |
| 456 } // namespace base | |
| OLD | NEW |