-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathIntegrationTestInfrastructure.hpp
More file actions
142 lines (118 loc) · 4.67 KB
/
IntegrationTestInfrastructure.hpp
File metadata and controls
142 lines (118 loc) · 4.67 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
// SPDX-FileCopyrightText: 2022 Vector Informatik GmbH
//
// SPDX-License-Identifier: MIT
#pragma once
#include "silkit/vendor/CreateSilKitRegistry.hpp"
#include "silkit/SilKit.hpp"
#include "silkit/services/all.hpp"
#include "ConfigurationTestUtils.hpp"
#include "IParticipantInternal.hpp"
#include "CreateParticipantImpl.hpp"
using namespace SilKit;
using namespace SilKit::Services::Orchestration;
using namespace SilKit::Config;
class TestInfrastructure
{
public:
TestInfrastructure() {}
void ShutdownOnException(const std::exception& error)
{
std::stringstream ss;
ss << "Something went wrong: " << error.what() << std::endl;
if (_systemMaster.systemController)
{
_systemMaster.systemController->AbortSimulation();
}
FAIL() << ss.str();
}
void SetupRegistryAndSystemMaster(const std::string& registryUri, bool sync,
std::vector<std::string> requiredParticipantNames)
{
try
{
_registryUri = RunRegistry(registryUri);
if (sync)
{
RunSystemMaster(_registryUri, std::move(requiredParticipantNames));
}
}
catch (const std::exception& error)
{
ShutdownOnException(error);
}
}
auto GetRegistryUri() -> std::string
{
return _registryUri;
}
void SystemMasterStop()
{
using namespace std::chrono_literals;
ASSERT_EQ(_systemMaster.systemStateRunning.wait_for(1s), std::future_status::ready);
_systemMaster.lifecycleService->Stop("SystemMaster Stop");
}
void ShutdownInfrastructure()
{
_systemMaster.participant.reset();
_registry.reset();
}
private:
auto RunRegistry(const std::string& registryUri) -> std::string
{
_registry = SilKit::Vendor::Vector::CreateSilKitRegistry(SilKit::Config::MakeEmptyParticipantConfiguration());
return _registry->StartListening(registryUri);
}
void RunSystemMaster(const std::string& registryUri, std::vector<std::string> requiredParticipantNames)
{
_systemMaster.participant = SilKit::CreateParticipantImpl(
SilKit::Config::MakeEmptyParticipantConfigurationImpl(), systemMasterName, registryUri);
auto participantInternal = dynamic_cast<SilKit::Core::IParticipantInternal*>(_systemMaster.participant.get());
_systemMaster.systemController = participantInternal->GetSystemController();
_systemMaster.systemMonitor = _systemMaster.participant->CreateSystemMonitor();
_systemMaster.lifecycleService =
_systemMaster.participant->CreateLifecycleService({OperationMode::Coordinated});
ITimeSyncService* timeSyncService = _systemMaster.lifecycleService->CreateTimeSyncService();
timeSyncService->SetSimulationStepHandler(
[](std::chrono::nanoseconds /*now*/, std::chrono::nanoseconds /*duration*/) {}, std::chrono::seconds{1});
requiredParticipantNames.push_back(systemMasterName);
_systemMaster.systemController->SetWorkflowConfiguration({requiredParticipantNames});
_systemMaster.systemStateRunning = _systemMaster.systemStateRunningPromise.get_future();
_systemMaster.systemMonitor->AddSystemStateHandler([this, requiredParticipantNames](SystemState newState) {
switch (newState)
{
case SystemState::Error:
_systemMaster.systemController->AbortSimulation();
break;
case SystemState::Running:
_systemMaster.systemStateRunningPromise.set_value();
break;
default:
break;
}
});
_systemMaster.systemMonitor->AddParticipantStatusHandler([this](const ParticipantStatus& newStatus) {
switch (newStatus.state)
{
case ParticipantState::Error:
_systemMaster.systemController->AbortSimulation();
break;
default:
break;
}
});
_systemMaster.lifecycleService->StartLifecycle();
}
std::unique_ptr<SilKit::Vendor::Vector::ISilKitRegistry> _registry;
std::string _registryUri{"not yet defined"};
const std::string systemMasterName{"SystemMaster"};
struct SystemMaster
{
std::unique_ptr<IParticipant> participant;
SilKit::Experimental::Services::Orchestration::ISystemController* systemController{nullptr};
ISystemMonitor* systemMonitor;
ILifecycleService* lifecycleService;
std::promise<void> systemStateRunningPromise;
std::future<void> systemStateRunning;
};
SystemMaster _systemMaster;
};