1 //===- CachePruningTest.cpp -----------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Support/CachePruning.h"
11 #include "llvm/Support/Error.h"
12 #include "gtest/gtest.h"
13 
14 using namespace llvm;
15 
TEST(CachePruningPolicyParser,Empty)16 TEST(CachePruningPolicyParser, Empty) {
17   auto P = parseCachePruningPolicy("");
18   ASSERT_TRUE(bool(P));
19   EXPECT_EQ(std::chrono::seconds(1200), P->Interval);
20   EXPECT_EQ(std::chrono::hours(7 * 24), P->Expiration);
21   EXPECT_EQ(75u, P->MaxSizePercentageOfAvailableSpace);
22 }
23 
TEST(CachePruningPolicyParser,Interval)24 TEST(CachePruningPolicyParser, Interval) {
25   auto P = parseCachePruningPolicy("prune_interval=1s");
26   ASSERT_TRUE(bool(P));
27   EXPECT_EQ(std::chrono::seconds(1), P->Interval);
28   P = parseCachePruningPolicy("prune_interval=2m");
29   ASSERT_TRUE(bool(P));
30   EXPECT_EQ(std::chrono::minutes(2), *P->Interval);
31   P = parseCachePruningPolicy("prune_interval=3h");
32   ASSERT_TRUE(bool(P));
33   EXPECT_EQ(std::chrono::hours(3), *P->Interval);
34 }
35 
TEST(CachePruningPolicyParser,Expiration)36 TEST(CachePruningPolicyParser, Expiration) {
37   auto P = parseCachePruningPolicy("prune_after=1s");
38   ASSERT_TRUE(bool(P));
39   EXPECT_EQ(std::chrono::seconds(1), P->Expiration);
40 }
41 
TEST(CachePruningPolicyParser,MaxSizePercentageOfAvailableSpace)42 TEST(CachePruningPolicyParser, MaxSizePercentageOfAvailableSpace) {
43   auto P = parseCachePruningPolicy("cache_size=100%");
44   ASSERT_TRUE(bool(P));
45   EXPECT_EQ(100u, P->MaxSizePercentageOfAvailableSpace);
46   EXPECT_EQ(0u, P->MaxSizeBytes);
47 }
48 
TEST(CachePruningPolicyParser,MaxSizeBytes)49 TEST(CachePruningPolicyParser, MaxSizeBytes) {
50   auto P = parseCachePruningPolicy("cache_size_bytes=1");
51   ASSERT_TRUE(bool(P));
52   EXPECT_EQ(75u, P->MaxSizePercentageOfAvailableSpace);
53   EXPECT_EQ(1u, P->MaxSizeBytes);
54   P = parseCachePruningPolicy("cache_size_bytes=2k");
55   ASSERT_TRUE(bool(P));
56   EXPECT_EQ(75u, P->MaxSizePercentageOfAvailableSpace);
57   EXPECT_EQ(2u * 1024u, P->MaxSizeBytes);
58   P = parseCachePruningPolicy("cache_size_bytes=3m");
59   ASSERT_TRUE(bool(P));
60   EXPECT_EQ(75u, P->MaxSizePercentageOfAvailableSpace);
61   EXPECT_EQ(3u * 1024u * 1024u, P->MaxSizeBytes);
62   P = parseCachePruningPolicy("cache_size_bytes=4G");
63   ASSERT_TRUE(bool(P));
64   EXPECT_EQ(75u, P->MaxSizePercentageOfAvailableSpace);
65   EXPECT_EQ(4ull * 1024ull * 1024ull * 1024ull, P->MaxSizeBytes);
66 }
67 
TEST(CachePruningPolicyParser,Multiple)68 TEST(CachePruningPolicyParser, Multiple) {
69   auto P = parseCachePruningPolicy("prune_after=1s:cache_size=50%");
70   ASSERT_TRUE(bool(P));
71   EXPECT_EQ(std::chrono::seconds(1200), P->Interval);
72   EXPECT_EQ(std::chrono::seconds(1), P->Expiration);
73   EXPECT_EQ(50u, P->MaxSizePercentageOfAvailableSpace);
74 }
75 
TEST(CachePruningPolicyParser,Errors)76 TEST(CachePruningPolicyParser, Errors) {
77   EXPECT_EQ("Duration must not be empty",
78             toString(parseCachePruningPolicy("prune_interval=").takeError()));
79   EXPECT_EQ("'foo' not an integer",
80             toString(parseCachePruningPolicy("prune_interval=foos").takeError()));
81   EXPECT_EQ("'24x' must end with one of 's', 'm' or 'h'",
82             toString(parseCachePruningPolicy("prune_interval=24x").takeError()));
83   EXPECT_EQ("'foo' must be a percentage",
84             toString(parseCachePruningPolicy("cache_size=foo").takeError()));
85   EXPECT_EQ("'foo' not an integer",
86             toString(parseCachePruningPolicy("cache_size=foo%").takeError()));
87   EXPECT_EQ("'101' must be between 0 and 100",
88             toString(parseCachePruningPolicy("cache_size=101%").takeError()));
89   EXPECT_EQ(
90       "'foo' not an integer",
91       toString(parseCachePruningPolicy("cache_size_bytes=foo").takeError()));
92   EXPECT_EQ(
93       "'foo' not an integer",
94       toString(parseCachePruningPolicy("cache_size_bytes=foom").takeError()));
95   EXPECT_EQ("Unknown key: 'foo'",
96             toString(parseCachePruningPolicy("foo=bar").takeError()));
97 }
98