Leosac  0.8.0
Open Source Access Control
Registry.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
21 #include "gtest/gtest.h"
22 
23 using namespace Leosac;
24 
25 namespace Leosac
26 {
27 namespace Test
28 {
29 
30 TEST(TestRegistry, valid_get_set)
31 {
33  r.set("test", 42);
34  r.set("test0", -42);
35  r.set("test1", std::string("a_string"));
36 
37  ASSERT_EQ(42, r.get<int>("test"));
38  ASSERT_EQ(-42, r.get<int>("test0"));
39  ASSERT_EQ("a_string", r.get<std::string>("test1"));
40 }
41 
42 TEST(TestRegistry, invalid_get_set)
43 {
45  r.set("test", 42);
46  r.set("test0", -42);
47  r.set("test1", "a_string");
48 
49  ASSERT_THROW(r.get<int>("this_key_doesnt_exists"), RegistryKeyNotFoundException);
50 }
51 
52 TEST(TestRegistry, expiration)
53 {
54  Registry<int> r;
55  auto expire_at = Registry<int>::Clock::now() + std::chrono::milliseconds(3000);
56 
57  r.set(1, 3, expire_at);
58  r.set(2, 6, expire_at);
59  r.set(3, 9, expire_at);
60 
61  r.purge();
62  ASSERT_EQ(3, r.get<int>(1));
63  r.purge();
64  ASSERT_EQ(6, r.get<int>(2));
65  r.purge();
66  ASSERT_EQ(9, r.get<int>(3));
67 
68  std::this_thread::sleep_for(std::chrono::milliseconds(3001));
69  r.purge();
70  ASSERT_THROW(r.get<int>(1), RegistryKeyNotFoundException);
71  ASSERT_THROW(r.get<int>(2), RegistryKeyNotFoundException);
72  ASSERT_THROW(r.get<int>(3), RegistryKeyNotFoundException);
73 }
74 
75 TEST(TestRegistry, auto_expiration)
76 {
77  Registry<int> r;
78  auto expire_at = Registry<int>::Clock::now() + std::chrono::milliseconds(3000);
79 
80  r.set(1, 3, expire_at);
81  r.set(2, 6, expire_at);
82  r.set(3, 9, expire_at);
83 
84  ASSERT_EQ(3, r.get<int>(1));
85  ASSERT_EQ(6, r.get<int>(2));
86  ASSERT_EQ(9, r.get<int>(3));
87 
88  std::this_thread::sleep_for(std::chrono::milliseconds(3001));
89 
90  // Object still here, no autopurge yet.
91  ASSERT_NO_THROW(r.get<int>(1));
92  ASSERT_NO_THROW(r.get<int>(2));
93  ASSERT_NO_THROW(r.get<int>(3));
94 
95  // Setting some value will trigger purge().
96  r.set(42, 10);
97  ASSERT_THROW(r.get<int>(1), RegistryKeyNotFoundException);
98  ASSERT_THROW(r.get<int>(2), RegistryKeyNotFoundException);
99  ASSERT_THROW(r.get<int>(3), RegistryKeyNotFoundException);
100 }
101 }
102 }
Leosac::Test::TEST
TEST(TestCredentialValidator, alias_length)
Definition: CredentialValidator.cpp:34
Leosac::Registry
A simple container that can holds object of any type.
Definition: Registry.hpp:55
Registry.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Registry::get
std::enable_if_t< std::is_same< KeyType, std::string >::value &&!std::is_same< KeyType, const char * >::value, T > get(const std::string &key) const
Specialization for key of type std::string.
Definition: Registry.hpp:81
Leosac::Registry::set
void set(const KeyType &key, const T &obj)
Definition: Registry.hpp:123
Leosac::RegistryKeyNotFoundException
Definition: Registry.hpp:30
Leosac::Registry::purge
void purge()
Remove expired entry from the store.
Definition: Registry.hpp:157