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