forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_Services.cxx
More file actions
272 lines (233 loc) · 12 KB
/
test_Services.cxx
File metadata and controls
272 lines (233 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "Framework/ServiceHandle.h"
#include "Framework/ServiceRegistry.h"
#include "Framework/CallbackService.h"
#include "Framework/CommonServices.h"
#include "Framework/ServiceRegistryHelpers.h"
#include <Framework/DeviceState.h>
#include <catch_amalgamated.hpp>
#include <fairmq/ProgOptions.h>
#include <memory>
TEST_CASE("TestServiceRegistry")
{
o2::framework::clean_all_runtime_errors();
using namespace o2::framework;
struct InterfaceA {
virtual bool method() = 0;
};
struct ConcreteA : InterfaceA {
bool method() final { return true; }
};
struct InterfaceB {
virtual bool method() = 0;
};
struct ConcreteB : InterfaceB {
bool method() final { return false; }
};
struct InterfaceC {
[[nodiscard]] virtual bool method() const = 0;
};
struct ConcreteC : InterfaceC {
[[nodiscard]] bool method() const final { return false; }
};
ServiceRegistry registry;
ServiceRegistryRef ref{registry};
ConcreteA serviceA;
ConcreteB serviceB;
ConcreteC const serviceC;
ref.registerService(ServiceRegistryHelpers::handleForService<InterfaceA>(&serviceA));
ref.registerService(ServiceRegistryHelpers::handleForService<InterfaceB>(&serviceB));
ref.registerService(ServiceRegistryHelpers::handleForService<InterfaceC const>(&serviceC));
REQUIRE(registry.get<InterfaceA>(ServiceRegistry::globalDeviceSalt()).method() == true);
REQUIRE(registry.get<InterfaceB>(ServiceRegistry::globalDeviceSalt()).method() == false);
REQUIRE(registry.get<InterfaceC const>(ServiceRegistry::globalDeviceSalt()).method() == false);
REQUIRE(registry.active<InterfaceA>(ServiceRegistry::globalDeviceSalt()) == true);
REQUIRE(registry.active<InterfaceB>(ServiceRegistry::globalDeviceSalt()) == true);
REQUIRE(registry.active<InterfaceC>(ServiceRegistry::globalDeviceSalt()) == false);
REQUIRE_THROWS_AS(registry.get<InterfaceA const>(ServiceRegistry::globalDeviceSalt()), RuntimeErrorRef);
REQUIRE_THROWS_AS(registry.get<InterfaceC>(ServiceRegistry::globalDeviceSalt()), RuntimeErrorRef);
}
TEST_CASE("TestCallbackService")
{
using namespace o2::framework;
ServiceRegistry registry;
ServiceRegistryRef ref{registry};
auto service = std::make_unique<CallbackService>();
ref.registerService(ServiceRegistryHelpers::handleForService<CallbackService>(service.get()));
// the callback simply sets the captured variable to indicated that it was called
bool cbCalled = false;
auto cb = [&]() { cbCalled = true; };
registry.get<CallbackService>(ServiceRegistry::globalDeviceSalt()).set<CallbackService::Id::Stop>(cb);
// execute and check
registry.get<CallbackService>(ServiceRegistry::globalDeviceSalt()).call<CallbackService::Id::Stop>();
REQUIRE(cbCalled);
}
struct DummyService {
int threadId;
};
namespace o2::framework
{
static ServiceRegistry::Salt salt_0 = ServiceRegistry::Salt{0, 0};
static ServiceRegistry::Salt salt_1 = ServiceRegistry::Salt{1, 0};
static ServiceRegistry::Salt salt_2 = ServiceRegistry::Salt{2, 0};
static ServiceRegistry::Salt salt_3 = ServiceRegistry::Salt{3, 0};
static ServiceRegistry::Salt salt_1_1 = ServiceRegistry::Salt{1, 1};
} // namespace o2::framework
TEST_CASE("TestSerialServices")
{
using namespace o2::framework;
ServiceRegistry registry;
DummyService t0{0};
/// We register it pretending to be on thread 0
registry.registerService({TypeIdHelpers::uniqueId<DummyService>()}, &t0, ServiceKind::Serial, salt_0);
auto tt0 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_0, ServiceKind::Serial));
auto tt1 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_1, ServiceKind::Serial));
auto tt2 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_2, ServiceKind::Serial));
REQUIRE(tt0->threadId == 0);
REQUIRE(tt1->threadId == 0);
REQUIRE(tt2->threadId == 0);
}
TEST_CASE("TestGlobalServices")
{
using namespace o2::framework;
ServiceRegistry registry;
DummyService t0{0};
/// We register it pretending to be on thread 0
registry.registerService({TypeIdHelpers::uniqueId<DummyService>()}, &t0, ServiceKind::Global, salt_0);
auto tt0 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_0, ServiceKind::Serial));
auto tt1 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_1, ServiceKind::Serial));
auto tt2 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_2, ServiceKind::Serial));
REQUIRE(tt0->threadId == 0);
REQUIRE(tt1->threadId == 0);
REQUIRE(tt2->threadId == 0);
}
TEST_CASE("TestGlobalServices02")
{
using namespace o2::framework;
ServiceRegistry registry;
DeviceState state;
fair::mq::ProgOptions options;
ServiceSpec spec{.name = "dummy-service",
.uniqueId = CommonServices::simpleServiceId<DummyService>(),
.init = [](ServiceRegistryRef, DeviceState&, fair::mq::ProgOptions&) -> ServiceHandle {
// this is needed to check we register it only once
static int i = 1;
return ServiceHandle{TypeIdHelpers::uniqueId<DummyService>(), new DummyService{i++}};
},
.configure = CommonServices::noConfiguration(),
.kind = ServiceKind::Global};
// If the service was not declared, we should not be able to register it from a stream context.
REQUIRE_THROWS_AS(registry.registerService({TypeIdHelpers::uniqueId<DummyService>()}, nullptr, ServiceKind::Global, salt_1), RuntimeErrorRef);
// Declare the service
registry.declareService(spec, state, options, ServiceRegistry::globalDeviceSalt());
/// We register it pretending to be on thread 0
try {
registry.registerService({TypeIdHelpers::uniqueId<DummyService>()}, nullptr, ServiceKind::Global, salt_1);
} catch (RuntimeErrorRef e) {
INFO(error_from_ref(e).what);
REQUIRE(false);
}
auto tt0 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_0, ServiceKind::Global));
auto tt1 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_1, ServiceKind::Global));
auto tt2 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_2, ServiceKind::Global));
REQUIRE(tt0->threadId == 1);
REQUIRE(tt1->threadId == 1);
REQUIRE(tt2->threadId == 1);
}
TEST_CASE("TestStreamServices")
{
using namespace o2::framework;
ServiceRegistry registry;
DummyService t1{1};
DummyService t2{2};
DummyService t3{3};
DummyService t2_d1{2};
ServiceSpec spec{.name = "dummy-service",
.uniqueId = CommonServices::simpleServiceId<DummyService>(),
.init = CommonServices::simpleServiceInit<DummyService, DummyService>(),
.configure = CommonServices::noConfiguration(),
.kind = ServiceKind::Stream};
DeviceState state;
fair::mq::ProgOptions options;
// This will raise an exception because we have not declared the service yet.
REQUIRE_THROWS_AS(registry.registerService({TypeIdHelpers::uniqueId<DummyService>()}, nullptr, ServiceKind::Stream, ServiceRegistry::Salt{1, 0}), RuntimeErrorRef);
registry.declareService(spec, state, options, ServiceRegistry::globalDeviceSalt());
/// We register it pretending to be on thread 0
registry.registerService({TypeIdHelpers::uniqueId<DummyService>()}, &t1, ServiceKind::Stream, ServiceRegistry::Salt{1, 0}, "dummy-service1", ServiceRegistry::SpecIndex{0});
registry.registerService({TypeIdHelpers::uniqueId<DummyService>()}, &t2, ServiceKind::Stream, ServiceRegistry::Salt{2, 0}, "dummy-service2", ServiceRegistry::SpecIndex{0});
registry.registerService({TypeIdHelpers::uniqueId<DummyService>()}, &t3, ServiceKind::Stream, ServiceRegistry::Salt{3, 0}, "dummy-service3", ServiceRegistry::SpecIndex{0});
auto tt1 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_1, ServiceKind::Stream));
auto tt2 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_2, ServiceKind::Stream));
auto tt3 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_3, ServiceKind::Stream));
REQUIRE(tt1->threadId == 1);
REQUIRE(tt2->threadId == 2);
REQUIRE(tt3->threadId == 3);
// Check that Context{1,1} throws, because we registerd it for a different data processor.
REQUIRE_THROWS_AS(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_1_1, ServiceKind::Stream), RuntimeErrorRef);
// Check that Context{0,0} throws.
REQUIRE_THROWS_AS(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_0, ServiceKind::Stream), RuntimeErrorRef);
registry.registerService({TypeIdHelpers::uniqueId<DummyService>()}, &t2_d1, ServiceKind::Stream, ServiceRegistry::Salt{3, 1}, "dummy-service", ServiceRegistry::SpecIndex{0});
auto tt2_dp1 = reinterpret_cast<DummyService*>(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, ServiceRegistry::Salt{3, 1}, ServiceKind::Stream));
REQUIRE(tt2_dp1->threadId == 2);
REQUIRE_THROWS_AS(registry.get({TypeIdHelpers::uniqueId<DummyService>()}, salt_1_1, ServiceKind::Stream), RuntimeErrorRef);
}
TEST_CASE("TestServiceRegistryCtor")
{
using namespace o2::framework;
ServiceRegistry registry;
registry = ServiceRegistry();
}
TEST_CASE("TestServiceDeclaration")
{
using namespace o2::framework;
ServiceRegistry registry;
DeviceState state;
fair::mq::ProgOptions options;
options.SetProperty("monitoring-backend", "no-op://");
options.SetProperty("infologger-mode", "no-op://");
options.SetProperty("infologger-severity", "info");
options.SetProperty("configuration", "command-line");
registry.declareService(CommonServices::callbacksSpec(), state, options);
REQUIRE(registry.active<CallbackService>(ServiceRegistry::globalDeviceSalt()) == true);
REQUIRE(registry.active<DummyService>(ServiceRegistry::globalDeviceSalt()) == false);
}
TEST_CASE("TestServiceOverride")
{
using namespace o2::framework;
auto overrides = ServiceSpecHelpers::parseOverrides("foo:enable,bar:disable");
REQUIRE(overrides.size() == 2);
REQUIRE(overrides[0].name == "foo");
REQUIRE(overrides[0].active == true);
REQUIRE(overrides[1].name == "bar");
REQUIRE(overrides[1].active == false);
auto overrides2 = ServiceSpecHelpers::parseOverrides("foo:enable");
REQUIRE(overrides2.size() == 1);
REQUIRE(overrides[0].name == "foo");
REQUIRE(overrides[0].active == true);
REQUIRE_THROWS_AS(ServiceSpecHelpers::parseOverrides("foo:enabledisabl"), std::runtime_error);
REQUIRE_THROWS_AS(ServiceSpecHelpers::parseOverrides("foo"), std::runtime_error);
REQUIRE_THROWS_AS(ServiceSpecHelpers::parseOverrides("foo:"), std::runtime_error);
REQUIRE_THROWS_AS(ServiceSpecHelpers::parseOverrides("foo:a,"), std::runtime_error);
REQUIRE_THROWS_AS(ServiceSpecHelpers::parseOverrides("foo:,"), std::runtime_error);
REQUIRE(ServiceSpecHelpers::parseOverrides("").size() == 0);
REQUIRE(ServiceSpecHelpers::parseOverrides(nullptr).size() == 0);
auto overrides3 = ServiceSpecHelpers::parseOverrides("foo:disable,bar:enable,baz:enable");
ServiceSpecs originalServices{
{.name = "foo", .active = true},
{.name = "bar", .active = false},
};
REQUIRE(overrides3.size() == 3);
auto services = ServiceSpecHelpers::filterDisabled(originalServices, overrides3);
REQUIRE(services.size() == 1);
REQUIRE(services[0].name == "bar");
REQUIRE(services[0].active == true);
}