File: async_execution_load_test.cpp

package info (click to toggle)
dbus-cpp 5.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,208 kB
  • sloc: cpp: 9,427; ansic: 2,012; xml: 1,156; makefile: 14; sh: 2
file content (146 lines) | stat: -rw-r--r-- 4,495 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
/*
 * Copyright © 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */

#include <core/dbus/fixture.h>
#include <core/dbus/macros.h>
#include <core/dbus/object.h>
#include <core/dbus/service.h>

#include <core/dbus/asio/executor.h>

#include <core/dbus/types/stl/string.h>
#include <core/dbus/types/stl/vector.h>

#include <core/testing/fork_and_run.h>

#include <gmock/gmock.h>

#include "test_data.h"

namespace
{
// Our fixture for starting up private system- and session-bus instances.
struct AsyncExecutionLoadTest : public core::dbus::testing::Fixture
{
};

auto session_bus_config_file =
        core::dbus::testing::Fixture::default_session_bus_config_file() =
        core::testing::session_bus_configuration_file();

auto system_bus_config_file =
        core::dbus::testing::Fixture::default_system_bus_config_file() =
        core::testing::system_bus_configuration_file();

struct DBus
{
    static const std::string& name()
    {
        static const std::string s{DBUS_SERVICE_DBUS};
        return s;
    }

    DBUS_CPP_METHOD_WITH_TIMEOUT_DEF(ListNames, DBus, 25000)
};

struct CountingEventCollector
{
    CountingEventCollector(int expected)
        : expected{expected},
          counter{0}
    {
    }

    void update()
    {
        if (++counter == expected)
            wait_condition.notify_all();
    }

    ::testing::AssertionResult wait_for(const std::chrono::milliseconds& ms)
    {
        std::unique_lock<std::mutex> ul(guard);

        auto result = wait_condition.wait_for(ul, ms, [this]() { return counter == expected; });

        if (result)
            return ::testing::AssertionSuccess();

        return ::testing::AssertionFailure() << "Current count of "
                                             << counter << " does not match " << expected;
    }

    int expected;
    std::atomic<int> counter;

    std::mutex guard;
    std::condition_variable wait_condition;
};

void invoke_list_names_n_times_and_update_event_collector(
        // The object referring to the bus daemon
        const core::dbus::Object::Ptr& dbus,
        // Number of iterations
        std::size_t n,
        // The event collector instance that should be updated
        const std::shared_ptr<CountingEventCollector>& ec)
{
    for (unsigned int i = 0; i < n; i++)
    {
        dbus->invoke_method_asynchronously_with_callback<DBus::ListNames, std::vector<std::string>>([ec](const core::dbus::Result<std::vector<std::string>>& vs)
        {
            if (not vs.is_error())
                ec->update();
        });
    }
}
}

TEST_F(AsyncExecutionLoadTest, RepeatedlyInvokingAnAsyncFunctionWorks)
{
    using namespace ::testing;

    auto bus = session_bus();
    bus->install_executor(core::dbus::asio::make_executor(bus));

    std::thread worker{[bus]() { bus->run(); }};

    auto service = core::dbus::Service::use_service(bus, DBus::name());
    auto dbus = service->object_for_path(core::dbus::types::ObjectPath{DBUS_PATH_DBUS});

    auto ec = std::make_shared<CountingEventCollector>(500);

    std::thread t1{[dbus, ec]() {invoke_list_names_n_times_and_update_event_collector(dbus, 100, ec);}};
    std::thread t2{[dbus, ec]() {invoke_list_names_n_times_and_update_event_collector(dbus, 100, ec);}};
    std::thread t3{[dbus, ec]() {invoke_list_names_n_times_and_update_event_collector(dbus, 100, ec);}};
    std::thread t4{[dbus, ec]() {invoke_list_names_n_times_and_update_event_collector(dbus, 100, ec);}};
    std::thread t5{[dbus, ec]() {invoke_list_names_n_times_and_update_event_collector(dbus, 100, ec);}};

    EXPECT_TRUE(ec->wait_for(std::chrono::minutes{10}));

    bus->stop();

    if (t1.joinable()) t1.join();
    if (t2.joinable()) t2.join();
    if (t3.joinable()) t3.join();
    if (t4.joinable()) t4.join();
    if (t5.joinable()) t5.join();

    if (worker.joinable())
        worker.join();
}