File: idle-detect.cpp

package info (click to toggle)
gdbuspp 3-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,116 kB
  • sloc: cpp: 9,462; python: 477; sh: 215; makefile: 5
file content (350 lines) | stat: -rw-r--r-- 12,860 bytes parent folder | download | duplicates (2)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//  GDBus++ - glib2 GDBus C++ wrapper
//
//  SPDX-License-Identifier: AGPL-3.0-only
//
//  Copyright (C)  OpenVPN Inc <sales@openvpn.net>
//  Copyright (C)  David Sommerseth <davids@openvpn.net>
//

/**
 * @file tests/idle-detect.cpp
 *
 * @brief  Simple test of the DBus::Feature::IdleDetect implementation
 *         This will only run some simple tests checking the detection logic
 *         is working.  It is NOT a complete D-Bus service implementation test
 */

#include <chrono>
#include <iostream>
#include <memory>
#include <thread>
#include <unistd.h>

#include "../gdbuspp/connection.hpp"
#include "../gdbuspp/mainloop.hpp"
#include "../gdbuspp/object/base.hpp"
#include "../gdbuspp/object/manager.hpp"



class ChildObject : public DBus::Object::Base
{
  public:
    using Ptr = std::shared_ptr<ChildObject>;

    ChildObject(const std::string &name, const std::string &interf)
        : DBus::Object::Base("/tests/idle/childs/" + name, interf)
    {
    }

    const bool Authorize(const DBus::Authz::Request::Ptr req) override
    {
        return true;
    }
};


class IdleCheckDisabled : public DBus::Object::Base
{
  public:
    IdleCheckDisabled(DBus::Object::Manager::Ptr objmgr = nullptr)
        : DBus::Object::Base("/tests/idle/disabled", "test.idle.disabled"),
          object_mgr(objmgr)
    {
        DisableIdleDetector(true);
    }

    const bool Authorize(const DBus::Authz::Request::Ptr req) override
    {
        return true;
    }

    auto CreateChild(const std::string &name)
    {
        if (!object_mgr)
        {
            throw DBus::Object::Exception(this, "No object manager available");
        }
        return object_mgr->CreateObject<ChildObject>(name, GetInterface());
    }

  private:
    DBus::Object::Manager::Ptr object_mgr{nullptr};
};


class IdleCheckEnabled : public DBus::Object::Base
{
  public:
    IdleCheckEnabled()
        : DBus::Object::Base("/tests/idle/enabled", "test.idle.enabled")
    {
        DisableIdleDetector(false);
    }

    const bool Authorize(const DBus::Authz::Request::Ptr req) override
    {
        return true;
    }
};


/**
 *  Very simple conversion of a std::chrono::time_point to std::string
 *
 * @param tp     std::chrono::time_point to convert
 * @return const std::string containing the time point as plain text
 */
const std::string conv_tstamp(std::chrono::time_point<std::chrono::system_clock> &tp)
{
    std::time_t ttp = std::chrono::system_clock::to_time_t(tp);
    return std::string(std::ctime(&ttp));
}


uint32_t success = 0; ///< Global: Count of successfully completed tests
uint32_t failed = 0;  ///< Global: Count of failed tests

/**
 *  Simple wrapper function which runs a lambda and calculates the runtime
 *  and does a simple evealuation based on an expected runtime with a +/-
 *  deviation
 *
 * @tparam FUNC        type template for the function
 * @param descr        std::string with a short test description
 * @param runtime_sec  uint32_t with the expected runtime
 * @param deviation    uint32_t of the allowed +/- deviation in runtime
 * @param testfunc     Lambda function containing the code to run
 */
template <typename FUNC>
void time_execution(const std::string &descr,
                    const uint32_t runtime_sec,
                    const uint32_t deviation,
                    FUNC &&testfunc)
{
    auto start = std::chrono::system_clock::now();
    bool exception = false;
    std::cout << "::: " << descr << ":    start=" << conv_tstamp(start);
    try
    {
        testfunc();
    }
    catch (const std::exception &excp)
    {
        std::cout << "::: " << descr << ": EXCEPTION ## " << excp.what() << std::endl;
        exception = true;
    }
    auto end = std::chrono::system_clock::now();
    std::cout << "::: " << descr << ":      end=" << conv_tstamp(end);

    auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start);
    std::cout << "::: " << descr << ": duration=" << duration.count() << "s, ";
    if (deviation == 0)
    {
        std::cout << "expected=" << runtime_sec << "s";
    }
    else
    {
        std::cout << "expected " << runtime_sec - deviation << "s "
                  << "to " << runtime_sec + deviation << "s";
    }
    std::cout << " ==> result=";


    bool runtime_pass = (duration.count() >= runtime_sec - deviation)
                        && (duration.count() <= runtime_sec + deviation);
    if (!exception && runtime_pass)
    {
        std::cout << "Pass" << std::endl;
        ++success;
    }
    else
    {
        std::cout << "FAILED" << std::endl;
        ++failed;
    }
}


/// Global: A single additional thread, used to run a some code after
/// a certain time.
std::shared_ptr<std::thread> delayed_run_thread{nullptr};

/**
 *  Run some code after a given time.  Used to stop a main loop out of the
 *  ordinary time_execution() scope.
 *
 * @tparam FUNC      type template for the function to run
 * @param after_sec  Run the lambda function after sleeping for after_sec seconds
 * @param func       The lambda function to run
 */
template <typename FUNC>
void delayed_execution(const int after_sec,
                       FUNC &&func)
{
    delayed_run_thread.reset(
        new std::thread([after_sec, func]()
                        {
                            sleep(after_sec);
                            func();
                        }));
}


/**
 *  std::thread cleanup helper, waits for the thread kicked off by
 *  delayed_execution() and resets the delayed_run_thread pointer.
 */
void delayed_join()
{
    if (delayed_run_thread && delayed_run_thread->joinable())
    {
        delayed_run_thread->join();
    }
    delayed_run_thread.reset();
}



int main(int argc, char **argv)
{
    // First simple test; just have an empty object manager.  It is expected
    // to shutdown automatically after 3 seconds.
    time_execution("No objects", 3, 0, []()
                   {
                       auto buscon = DBus::Connection::Create(DBus::BusType::SESSION);
                       auto mainloop = DBus::MainLoop::Create();
                       auto objmgr = DBus::Object::Manager::CreateManager(buscon);
                       objmgr->PrepareIdleDetector(std::chrono::seconds(3), mainloop);
                       objmgr->RunIdleDetector(true);
                       mainloop->Run();
                   });

    // A similar test to the test above, but here a DBus::Object::Base object
    // has been created via the DBus::Object::Manager.  This object has
    // disabled the idle detection, which means the presence of this object
    // will not be considered by the idle detector.  Thus, since the timeout
    // is set to 2 seconds in this test, the expected runtime is 2 seconds.
    time_execution("IdleCheckDisabled", 2, 0, []()
                   {
                       auto buscon = DBus::Connection::Create(DBus::BusType::SESSION);
                       auto mainloop = DBus::MainLoop::Create();
                       auto objmgr = DBus::Object::Manager::CreateManager(buscon);
                       objmgr->CreateObject<IdleCheckDisabled>();
                       objmgr->PrepareIdleDetector(std::chrono::seconds(2), mainloop);
                       objmgr->RunIdleDetector(true);
                       mainloop->Run();
                       objmgr->RemoveObject("/tests/idle/disabled");
                   });


    // This is the same test as above, but here the idle detector is considering
    // the presence of the IdleCheckEnabled object.  As long as this object
    // exists, it will continue to run.  The delayed execution will forcefully
    // stop the main loop after the idle detector should has run at least 2 times.
    // The idle timeout is set to 2 seconds and the delayed forceful shutdown at
    // 5 seconds, thus the expected runtime is roughly 5 seconds.
    time_execution("IdleCheckEnabled", 5, 0, []()
                   {
                       auto buscon = DBus::Connection::Create(DBus::BusType::SESSION);
                       auto mainloop = DBus::MainLoop::Create();
                       auto objmgr = DBus::Object::Manager::CreateManager(buscon);
                       objmgr->CreateObject<IdleCheckEnabled>();
                       objmgr->PrepareIdleDetector(std::chrono::seconds(2), mainloop);
                       delayed_execution(5, [mainloop]()
                                         {
                                             try
                                             {
                                                 mainloop->Stop();
                                             }
                                             catch (const DBus::MainLoop::Exception &)
                                             {
                                             }
                                         });
                       objmgr->RunIdleDetector(true);
                       mainloop->Run();
                       delayed_join();
                       objmgr->RemoveObject("/tests/idle/enabled");
                   });


    // This is an extension of the previous "IdleCheckEnabled", using the
    // the "IdleCheckDisabled" with a child object active and then removed.
    //
    // The "IdleCheckDisabled" object will not be considered, but the child
    // object has not disabled the idle detector.  This means that as long as
    // there is a child object present, the main loop will be running.
    //
    // The first test in this block will have the IdleCheckDisabled and the
    // ChildObject present.  The idle detection is set at 2 seconds but the
    // delayed execution stopping the main loop is set at 5 seconds, thus
    // this step will take 5 seconds to run.
    //
    // The next step removes the ChildObject and then re-runs the main loop.
    // Now the expected runtime is 2 seconds - as there are no objects needed
    // to be considered for the idle detector.
    //
    // Finally, we measure the time it takes before the delayed_execution()
    // thread stops.  The measurement here isn't too precise,so it is expected
    // to run roughly 2-4 seconds.  With a total expected runtime of 5 seconds,
    // the 2-4 seconds of waiting + the runtime of 2 seconds for ChildObject-2,
    // that is roughly 4-6 seconds of runtime - which is close enough.
    //
    {
        auto mainloop = DBus::MainLoop::Create();

        auto buscon = DBus::Connection::Create(DBus::BusType::SESSION);
        auto objmgr = DBus::Object::Manager::CreateManager(buscon);
        objmgr->PrepareIdleDetector(std::chrono::seconds(2), mainloop);

        auto rootobj = objmgr->CreateObject<IdleCheckDisabled>(objmgr);
        auto child = objmgr->CreateObject<ChildObject>("test1", rootobj->GetInterface());

        time_execution("ChildObject-1", 5, 0, [mainloop, objmgr]()
                       {
                           delayed_execution(5, [mainloop]()
                                             {
                                                 try
                                                 {
                                                     mainloop->Stop();
                                                 }
                                                 catch (const DBus::MainLoop::Exception &)
                                                 {
                                                 }
                                             });
                           objmgr->RunIdleDetector(true);
                           mainloop->Run();
                           delayed_join();
                           objmgr->RunIdleDetector(false);
                       });
        objmgr->RemoveObject(child->GetPath());

        delayed_execution(5, [mainloop]()
                          {
                              try
                              {
                                  mainloop->Stop();
                              }
                              catch (const DBus::MainLoop::Exception &)
                              {
                              }
                          });
        time_execution("ChildObject-2", 2, 0, [mainloop, objmgr]()
                       {
                           objmgr->RunIdleDetector(true);
                           mainloop->Run();
                       });
        time_execution("ChildObject-cleanup", 3, 1, []()
                       {
                           delayed_join();
                       });
    }

    // Summarize the test results
    std::cout << ">> Passed tests: " << std::to_string(success) << std::endl
              << ">> Failed tests: " << std::to_string(failed) << std::endl
              << ">> OVERALL TEST RESULT: "
              << (success > 0 && failed == 0 ? "PASS" : "FAIL")
              << std::endl;
    return (success > 0 && failed == 0) ? 0 : 2;
}