OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 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 "net/disk_cache/flash/flash_cache_test_base.h" |
| 6 #include "net/disk_cache/flash/format.h" |
| 7 #include "net/disk_cache/flash/log_store.h" |
| 8 #include "net/disk_cache/flash/segment.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace disk_cache { |
| 12 |
| 13 TEST_F(FlashCacheTest, LogStoreCreateEntry) { |
| 14 const int32 kSize = 100; |
| 15 const std::string buf(kSize, 0); |
| 16 |
| 17 int32 id; |
| 18 EXPECT_TRUE(log_store_->CreateEntry(kSize, &id)); |
| 19 EXPECT_TRUE(log_store_->WriteData(buf.data(), kSize/2)); |
| 20 EXPECT_TRUE(log_store_->WriteData(buf.data(), kSize/2)); |
| 21 log_store_->CloseEntry(id); |
| 22 } |
| 23 |
| 24 // Also tests reading from current segment. |
| 25 TEST_F(FlashCacheTest, LogStoreOpenEntry) { |
| 26 const int32 kSize = 100; |
| 27 const std::vector<char> expected(kSize, 'b'); |
| 28 |
| 29 int32 id; |
| 30 EXPECT_TRUE(log_store_->CreateEntry(kSize, &id)); |
| 31 EXPECT_TRUE(log_store_->WriteData(&expected[0], kSize)); |
| 32 log_store_->CloseEntry(id); |
| 33 |
| 34 EXPECT_TRUE(log_store_->OpenEntry(id)); |
| 35 std::vector<char> actual(kSize, 0); |
| 36 EXPECT_TRUE(log_store_->ReadData(id, &actual[0], kSize, 0)); |
| 37 log_store_->CloseEntry(id); |
| 38 |
| 39 EXPECT_EQ(expected, actual); |
| 40 } |
| 41 |
| 42 // Also tests that writing advances segments. |
| 43 TEST_F(FlashCacheTest, LogStoreReadFromClosedSegment) { |
| 44 const int32 kSize = disk_cache::kFlashSegmentFreeSpace; |
| 45 const std::vector<char> expected(kSize, 'a'); |
| 46 |
| 47 // First two entries go to segment 0. |
| 48 int32 id1; |
| 49 EXPECT_EQ(0, log_store_->write_index_); |
| 50 EXPECT_TRUE(log_store_->CreateEntry(kSize/2, &id1)); |
| 51 EXPECT_TRUE(log_store_->WriteData(&expected[0], kSize/2)); |
| 52 log_store_->CloseEntry(id1); |
| 53 |
| 54 int32 id2; |
| 55 EXPECT_EQ(0, log_store_->write_index_); |
| 56 EXPECT_TRUE(log_store_->CreateEntry(kSize/2, &id2)); |
| 57 EXPECT_TRUE(log_store_->WriteData(&expected[0], kSize/2)); |
| 58 log_store_->CloseEntry(id2); |
| 59 |
| 60 // This entry goes to segment 1. |
| 61 int32 id3; |
| 62 EXPECT_TRUE(log_store_->CreateEntry(kSize, &id3)); |
| 63 EXPECT_EQ(1, log_store_->write_index_); |
| 64 EXPECT_TRUE(log_store_->WriteData(&expected[0], kSize)); |
| 65 log_store_->CloseEntry(id3); |
| 66 |
| 67 // We read from segment 0. |
| 68 EXPECT_TRUE(log_store_->OpenEntry(id1)); |
| 69 std::vector<char> actual(kSize, 0); |
| 70 EXPECT_TRUE(log_store_->ReadData(id1, &actual[0], kSize, id1)); |
| 71 log_store_->CloseEntry(id1); |
| 72 |
| 73 EXPECT_EQ(expected, actual); |
| 74 } |
| 75 |
| 76 TEST_F(FlashCacheTest, LogStoreReadFromCurrentAfterClose) { |
| 77 const int32 kSize = disk_cache::kFlashSegmentFreeSpace; |
| 78 const std::vector<char> expected(kSize, 'a'); |
| 79 |
| 80 int32 id1; |
| 81 EXPECT_EQ(0, log_store_->write_index_); |
| 82 EXPECT_TRUE(log_store_->CreateEntry(kSize/2, &id1)); |
| 83 EXPECT_TRUE(log_store_->WriteData(&expected[0], kSize/2)); |
| 84 log_store_->CloseEntry(id1); |
| 85 |
| 86 // Create a reference to above entry. |
| 87 EXPECT_TRUE(log_store_->OpenEntry(id1)); |
| 88 |
| 89 // This entry fills the first segment. |
| 90 int32 id2; |
| 91 EXPECT_EQ(0, log_store_->write_index_); |
| 92 EXPECT_TRUE(log_store_->CreateEntry(kSize/2, &id2)); |
| 93 EXPECT_TRUE(log_store_->WriteData(&expected[0], kSize/2)); |
| 94 log_store_->CloseEntry(id2); |
| 95 |
| 96 // Creating this entry forces closing of the first segment. |
| 97 int32 id3; |
| 98 EXPECT_TRUE(log_store_->CreateEntry(kSize, &id3)); |
| 99 EXPECT_EQ(1, log_store_->write_index_); |
| 100 EXPECT_TRUE(log_store_->WriteData(&expected[0], kSize)); |
| 101 log_store_->CloseEntry(id3); |
| 102 |
| 103 // Now attempt to read from the closed segment. |
| 104 std::vector<char> actual(kSize, 0); |
| 105 EXPECT_TRUE(log_store_->ReadData(id1, &actual[0], kSize, id1)); |
| 106 log_store_->CloseEntry(id1); |
| 107 |
| 108 EXPECT_EQ(expected, actual); |
| 109 } |
| 110 |
| 111 // TODO(agayev): Add a test that confirms that in-use segment is not selected as |
| 112 // the next write segment. |
| 113 |
| 114 } // namespace disk_cache |
OLD | NEW |