1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/traced/probes/filesystem/lru_inode_cache.h"
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21
22 #include <string>
23 #include <tuple>
24
25 namespace perfetto {
26
27 namespace {
28
29 using ::testing::Eq;
30 using ::testing::IsNull;
31 using ::testing::Pointee;
32
33 const std::pair<BlockDeviceID, Inode> key1{0, 0};
34 const std::pair<BlockDeviceID, Inode> key2{0, 1};
35 const std::pair<BlockDeviceID, Inode> key3{0, 2};
36
val1()37 InodeMapValue val1() {
38 return InodeMapValue(protos::pbzero::InodeFileMap_Entry_Type_DIRECTORY,
39 std::set<std::string>{"Value 1"});
40 }
41
val2()42 InodeMapValue val2() {
43 return InodeMapValue(protos::pbzero::InodeFileMap_Entry_Type_UNKNOWN,
44 std::set<std::string>{"Value 2"});
45 }
46
val3()47 InodeMapValue val3() {
48 return InodeMapValue(protos::pbzero::InodeFileMap_Entry_Type_UNKNOWN,
49 std::set<std::string>{"Value 2"});
50 }
51
TEST(LRUInodeCacheTest,Basic)52 TEST(LRUInodeCacheTest, Basic) {
53 LRUInodeCache cache(2);
54 cache.Insert(key1, val1());
55 EXPECT_THAT(cache.Get(key1), Pointee(Eq(val1())));
56 cache.Insert(key2, val2());
57 EXPECT_THAT(cache.Get(key1), Pointee(Eq(val1())));
58 EXPECT_THAT(cache.Get(key2), Pointee(Eq(val2())));
59 cache.Insert(key1, val2());
60 EXPECT_THAT(cache.Get(key1), Pointee(Eq(val2())));
61 }
62
TEST(LRUInodeCacheTest,Overflow)63 TEST(LRUInodeCacheTest, Overflow) {
64 LRUInodeCache cache(2);
65 cache.Insert(key1, val1());
66 cache.Insert(key2, val2());
67 EXPECT_THAT(cache.Get(key1), Pointee(Eq(val1())));
68 EXPECT_THAT(cache.Get(key2), Pointee(Eq(val2())));
69 cache.Insert(key3, val3());
70 // key1 is the LRU and should be evicted.
71 EXPECT_THAT(cache.Get(key1), IsNull());
72 EXPECT_THAT(cache.Get(key2), Pointee(Eq(val2())));
73 EXPECT_THAT(cache.Get(key3), Pointee(Eq(val3())));
74 }
75
76 } // namespace
77 } // namespace perfetto
78