ADTF
Loading...
Searching...
No Matches
test_runtime.h
Go to the documentation of this file.
1
7#ifndef _ADTF_UCOM_ANT_TESTING_RUNTIME_INCLUDES_HEADER_
8#define _ADTF_UCOM_ANT_TESTING_RUNTIME_INCLUDES_HEADER_
9
10namespace adtf
11{
12namespace ucom
13{
14namespace testing
15{
16namespace ant
17{
21typedef enum : int8_t
22{
23 RL_Shutdown = IRuntime::RL_Shutdown,
24 RL_System = 1,
25 RL_Application = 2,
26 RL_Running = 3
27} tTestRL;
28
32class cTestRuntime : public adtf::ucom::runtime<tTestRL::RL_Running>
33{
36
37public:
39 cTestRuntime(): base_type()
40 {
41 }
42};
43
50const adtf_util::cFilename find_plugin(const adtf::util::cString& strPluginName);
51
61const adtf_util::cFilename load_plugin(const adtf::util::cString& strPluginName, int8_t ui8Level, ucom::ant::IRuntime& oRuntime);
62
70const adtf_util::cFilename load_plugin(const adtf::util::cString& strPluginName, int8_t ui8Level);
71
78const adtf_util::cFilename load_plugin(const adtf::util::cString& strPluginName);
89void load_service(const adtf_util::cString& strPluginName, const adtf_util::cString& strClassId,
90 const adtf_util::cString& strObjectId,
91 int8_t nPluginLevel,
92 int8_t nServiceLevel);
93
103void load_service(const adtf_util::cString& strPluginName, const adtf_util::cString& strClassId,
104 const adtf_util::cString& strObjectId,
105 int8_t nServiceLevel);
106
111void clean_up();
112
116class cExecutorThread
117{
118 private:
119 std::thread m_oThread;
120 std::mutex m_oActionMutex;
121 typedef std::function<void()> action_type;
122 action_type m_fnAction;
123 bool m_bQuitThread = false;
124 std::condition_variable m_oNewAction;
125
126 public:
127 cExecutorThread()
128 {
129 m_oThread = std::thread([&]
130 {
131 std::unique_lock<std::mutex> oLocker(m_oActionMutex);
132 for (;;)
133 {
134 m_oNewAction.wait(oLocker, [&]
135 {
136 return m_fnAction || m_bQuitThread;
137 });
138
139 if (m_bQuitThread)
140 {
141 return;
142 }
143
144 m_fnAction();
145 m_fnAction = action_type();
146 }
147 });
148 }
149
150 ~cExecutorThread()
151 {
152 {
153 std::lock_guard<std::mutex> oLocker(m_oActionMutex);
154 m_bQuitThread = true;
155 }
156
157 m_oNewAction.notify_one();
158 m_oThread.join();
159 }
160
161 tResult Execute(std::function<tResult()> fnAction)
162 {
163 std::promise<tResult> oPromisedResult;
164 auto oResult = oPromisedResult.get_future();
165
166 {
167 std::lock_guard<std::mutex> oLocker(m_oActionMutex);
168 m_fnAction = [&]
169 {
170 oPromisedResult.set_value(fnAction());
171 };
172 }
173
174 m_oNewAction.notify_one();
175 return oResult.get();
176 }
177};
178
183template<typename RuntimeClass>
184class exec_forward
185{
186 private:
187 std::unique_ptr<RuntimeClass> m_pRuntime;
188
189 public:
190 exec_forward():
191 m_pRuntime(new RuntimeClass)
192 {
193 _runtime = m_pRuntime.get();
194 }
195
196 ~exec_forward()
197 {
198 m_pRuntime->SetRunLevel(adtf::ucom::ant::IRuntime::RL_Shutdown, true);
199 _runtime = nullptr;
200 }
201
202 tResult Exec(std::function<void()> fnStartup)
203 {
204 return m_pRuntime->Exec(adtf::ucom::ant::IRuntime::RL_Shutdown + 1,
205 fnStartup);
206 }
207};
208
213template<typename ExecuteForward>
214class runtime_executor: private cExecutorThread
215{
216 private:
217 std::unique_ptr<ExecuteForward> m_pExecuteForward;
218 std::thread m_oExecWaitThread;
219
220 public:
222 {
223 if (m_oExecWaitThread.joinable())
224 {
225 m_oExecWaitThread.join();
226 }
227 }
228
229 tResult Create()
230 {
231 return Execute([&]() -> tResult
232 {
233 m_pExecuteForward.reset(new ExecuteForward());
235 });
236 }
237
238 tResult Start()
239 {
240 if (m_oExecWaitThread.joinable())
241 {
242 RETURN_ERROR(ERR_INVALID_STATE);
243 }
244
245 std::promise<tResult> oExecLoopStarted;
246 auto oStarted = oExecLoopStarted.get_future();
247 m_oExecWaitThread = std::thread([&]
248 {
249 Execute([&]() -> tResult
250 {
251 auto nExecResult = m_pExecuteForward->Exec([&]
252 {
253 oExecLoopStarted.set_value(ERR_NOERROR);
254 });
255
256 if (IS_FAILED(nExecResult))
257 {
258 oExecLoopStarted.set_value(nExecResult);
259 }
261 });
262 });
263
264 return oStarted.get();
265 }
266};
267
268
269}//ns ant
270
271using ant::tTestRL;
273
274using ant::find_plugin;
275using ant::load_plugin;
277using ant::clean_up;
278
281
282}//ns testing
283}//ns ucom
284}//ns adtf
285
286#endif // _ADTF_UCOM_ANT_TESTING_RUNTIME_INCLUDES_HEADER_
#define IS_FAILED(s)
Check if result is failed.
Definition result.h:20
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
Definition result.h:42
@ RL_Shutdown
The system is shut down.
Definition runtime_intf.h:117
Definition runtime_intf.h:104
@ RL_Shutdown
The system is shut down.
Definition runtime_intf.h:117
Definition runtime.h:150
Definition test_runtime.h:33
cTestRuntime()
CTOR.
Definition test_runtime.h:39
Definition test_runtime.h:185
Definition test_runtime.h:215
Namespace for all functionality of the ADTF UCOM SDK provided since v3.0.
Definition adtf_system.h:328
tTestRL
Definition test_runtime.h:22
void load_service(const adtf_util::cString &strPluginName, const adtf_util::cString &strClassId, const adtf_util::cString &strObjectId, int8_t nPluginLevel, int8_t nServiceLevel)
const adtf_util::cFilename find_plugin(const adtf::util::cString &strPluginName)
const adtf_util::cFilename load_plugin(const adtf::util::cString &strPluginName, int8_t ui8Level, ucom::ant::IRuntime &oRuntime)
Namespace for all provided testing functionality helper.
Definition adtf_system.h:326
Namespace for the ADTF uCOM SDK.
Definition adtf_system.h:324
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14