Leosac  0.8.0
Open Source Access Control
ServiceRegistry.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 
31 {
32  virtual bool is_even(int number) const = 0;
33 };
34 
36 {
37 };
38 
40 {
41  virtual bool is_even(int number) const override
42  {
43  return (number % 2) == 0;
44  }
45 };
46 
47 TEST(TestRegistry, register_get_service)
48 {
49  ServiceRegistry srv_registry;
50 
51  // No service registered for this interface.
52  // Will return -1.
53  ASSERT_EQ(-1, srv_registry.use_count<DummyServiceInterface>());
54  // No service yet, will return null.
55  ASSERT_EQ(nullptr, srv_registry.get_service<DummyServiceInterface>());
56 
57 
58  bool event_listener_invoked = false;
59  srv_registry.register_event_listener([&](const service_event::Event &ev) {
60  event_listener_invoked = true;
62  ASSERT_NO_THROW(dynamic_cast<const service_event::ServiceRegistered &>(ev));
63  });
64 
65  auto handle =
67  ASSERT_TRUE(event_listener_invoked);
68 
69  ASSERT_EQ(0, srv_registry.use_count<DummyServiceInterface>());
70  {
71  std::shared_ptr<DummyServiceInterface> srv =
72  srv_registry.get_service<DummyServiceInterface>();
73  ASSERT_EQ(1, srv_registry.use_count<DummyServiceInterface>());
74  ASSERT_TRUE(srv->is_even(0));
75  ASSERT_FALSE(srv->is_even(1));
76  }
77  ASSERT_EQ(0, srv_registry.use_count<DummyServiceInterface>());
78 }
79 
80 TEST(TestRegistry, unregister_simple)
81 {
82  ServiceRegistry srv_registry;
83 
84  bool event_listener_invoked = false;
85 
86  auto handle =
88  ASSERT_NE(nullptr, srv_registry.get_service<DummyServiceInterface>());
89  ASSERT_FALSE(event_listener_invoked);
90 
91  srv_registry.register_event_listener([&](const service_event::Event &ev) {
92  event_listener_invoked = true;
94  ASSERT_NO_THROW(
95  dynamic_cast<const service_event::ServiceUnregistered &>(ev));
96  });
97 
98  ASSERT_TRUE(srv_registry.unregister_service(handle));
99  ASSERT_TRUE(event_listener_invoked);
100  ASSERT_EQ(nullptr, srv_registry.get_service<DummyServiceInterface>());
101 }
102 
103 TEST(TestRegistry, unregister_while_used)
104 {
105  ServiceRegistry srv_registry;
106 
107  auto handle =
109  ASSERT_NE(nullptr, srv_registry.get_service<DummyServiceInterface>());
110 
111  {
112  auto srv = srv_registry.get_service<DummyServiceInterface>();
113  ASSERT_FALSE(srv_registry.unregister_service(handle));
114  }
115  ASSERT_TRUE(srv_registry.unregister_service(handle));
116 }
117 
118 TEST(TestRegistry, unregister_no_param)
119 {
120  ServiceRegistry srv_registry;
121 
122  auto handle =
124  ASSERT_FALSE(srv_registry.unregister_service<DummyServiceInterface2>());
125  ASSERT_TRUE(srv_registry.unregister_service<DummyServiceInterface>());
126 }
127 }
128 }
Leosac::ServiceRegistry::unregister_service
bool unregister_service(RegistrationHandle h)
Unregister a service using the RegistrationHandle that was returned from the register_service() call.
Definition: ServiceRegistry.hpp:204
Leosac::Test::DummyServiceImpl::is_even
virtual bool is_even(int number) const override
Definition: ServiceRegistry.cpp:41
Leosac::Test::TEST
TEST(TestCredentialValidator, alias_length)
Definition: CredentialValidator.cpp:34
Leosac::ServiceRegistry::use_count
long use_count() const
How many strong (shared) pointer points to the service that provides ServiceInterface.
Definition: ServiceRegistry.hpp:316
Leosac::service_event::Event::type
EventType type() const
Definition: ServiceRegistry.hpp:45
ServiceRegistry.hpp
Leosac::service_event::UNREGISTERED
@ UNREGISTERED
Definition: ServiceRegistry.hpp:35
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::service_event::ServiceRegistered
Definition: ServiceRegistry.hpp:54
Leosac::ServiceRegistry
A class that manages services.
Definition: ServiceRegistry.hpp:110
Leosac::Test::DummyServiceInterface::is_even
virtual bool is_even(int number) const =0
Leosac::ServiceRegistry::register_event_listener
bs2::connection register_event_listener(T &&callable)
Register a service-event listener.
Definition: ServiceRegistry.hpp:349
Leosac::Test::DummyServiceInterface
Definition: ServiceRegistry.cpp:30
Leosac::ServiceRegistry::register_service
RegistrationHandle register_service(std::unique_ptr< ServiceInterface > srv)
Register a service by passing an unique_ptr to it.
Definition: ServiceRegistry.hpp:146
Leosac::ServiceRegistry::get_service
std::shared_ptr< ServiceInterface > get_service() const
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was re...
Definition: ServiceRegistry.hpp:290
Leosac::Test::DummyServiceImpl
Definition: ServiceRegistry.cpp:39
Leosac::service_event::Event
Definition: ServiceRegistry.hpp:37
Leosac::service_event::REGISTERED
@ REGISTERED
Definition: ServiceRegistry.hpp:34
Leosac::Test::DummyServiceInterface2
Definition: ServiceRegistry.cpp:35
Leosac::service_event::ServiceUnregistered
Definition: ServiceRegistry.hpp:71