summaryrefslogtreecommitdiff
path: root/src/test/DBusLoadTest.cpp
blob: 3eed056730857043e23599c0aa0571b83c7e6752 (plain)
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
// Copyright (C) 2013-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <gtest/gtest.h>

#include <cassert>
#include <cstdint>
#include <iostream>
#include <functional>
#include <memory>
#include <stdint.h>
#include <string>
#include <array>
#include <utility>
#include <tuple>
#include <type_traits>
#include <future>

#include <CommonAPI/CommonAPI.hpp>

#ifndef COMMONAPI_INTERNAL_COMPILATION
#define COMMONAPI_INTERNAL_COMPILATION
#endif

#include <CommonAPI/DBus/DBusConnection.hpp>
#include <CommonAPI/DBus/DBusProxy.hpp>

#include "commonapi/tests/PredefinedTypeCollection.hpp"
#include "commonapi/tests/DerivedTypeCollection.hpp"
#include "v1/commonapi/tests/TestInterfaceProxy.hpp"
#include "v1/commonapi/tests/TestInterfaceStubDefault.hpp"

#include "v1/commonapi/tests/TestInterfaceDBusProxy.hpp"

#define VERSION v1_0

class TestInterfaceStubFinal : public VERSION::commonapi::tests::TestInterfaceStubDefault {

public:
    void testPredefinedTypeMethod(const std::shared_ptr<CommonAPI::ClientId> _client, 
                                    uint32_t _uint32InValue, 
                                    std::string _stringInValue, 
                                    testPredefinedTypeMethodReply_t _reply) {
        (void)_client;
        uint32_t uint32OutValue = _uint32InValue;
        std::string stringOutValue = _stringInValue;
        _reply(uint32OutValue, stringOutValue);
    }
};

class Environment: public ::testing::Environment {
public:
    virtual ~Environment() {
    }

    virtual void SetUp() {
        CommonAPI::Runtime::setProperty("LibraryBase", "fakeGlueCode");
    }

    virtual void TearDown() {
    }
};

class DBusLoadTest: public ::testing::Test {
protected:
    virtual void SetUp() {
        runtime_ = CommonAPI::Runtime::get();
        ASSERT_TRUE((bool )runtime_);

        callSucceeded_.resize(numCallsPerProxy_ * numProxies_);
        std::fill(callSucceeded_.begin(), callSucceeded_.end(), false);
    }

    virtual void TearDown() {
        usleep(1000000);
    }

public:
    void TestPredefinedTypeMethodAsyncCallback(
                                               const uint32_t callId,
                                               const uint32_t in1,
                                               const std::string in2,
                                               const CommonAPI::CallStatus& callStatus,
                                               const uint32_t& out1,
                                               const std::string& out2) {
        EXPECT_EQ(callStatus, CommonAPI::CallStatus::SUCCESS);
        EXPECT_EQ(out1, in1);
        EXPECT_EQ(out2, in2);
        mutexCallSucceeded_.lock();
        ASSERT_FALSE(callSucceeded_[callId]);
        callSucceeded_[callId] = true;
        mutexCallSucceeded_.unlock();
    }

    std::shared_ptr<CommonAPI::Runtime> runtime_;
    std::vector<bool> callSucceeded_;
    std::mutex mutexCallSucceeded_;

    static const std::string domain_;
    static const std::string serviceAddress_;
    static const uint32_t numCallsPerProxy_;
    static const uint32_t numProxies_;
};

const std::string DBusLoadTest::domain_ = "local";
const std::string DBusLoadTest::serviceAddress_ = "CommonAPI.DBus.tests.DBusProxyTestService";
const uint32_t DBusLoadTest::numCallsPerProxy_ = 100;

#ifdef WIN32
// test with just 50 proxies under windows as it becomes very slow with more ones
const uint32_t DBusLoadTest::numProxies_ = 50;
#else
const uint32_t DBusLoadTest::numProxies_ = 65;
#endif

// Multiple proxies in one thread, one stub
TEST_F(DBusLoadTest, SingleClientMultipleProxiesSingleStubCallsSucceed) {
    std::array<std::shared_ptr<VERSION::commonapi::tests::TestInterfaceProxyBase>, numProxies_> testProxies;

    for (unsigned int i = 0; i < numProxies_; i++) {
        testProxies[i] = runtime_->buildProxy < VERSION::commonapi::tests::TestInterfaceProxy >(domain_, serviceAddress_);
        ASSERT_TRUE((bool )testProxies[i]);
    }

    auto stub = std::make_shared<TestInterfaceStubFinal>();
    bool serviceRegistered = runtime_->registerService(domain_, serviceAddress_, stub, "connection");
    for (auto i = 0; !serviceRegistered && i < 100; ++i) {
        serviceRegistered = runtime_->registerService(domain_, serviceAddress_, stub, "connection");
        usleep(10000);
    }
    ASSERT_TRUE(serviceRegistered);

    bool allProxiesAvailable = false;
    for (unsigned int i = 0; !allProxiesAvailable && i < 100; ++i) {
        allProxiesAvailable = true;
        for (unsigned int j = 0; j < numProxies_; ++j) {
            allProxiesAvailable = allProxiesAvailable && testProxies[j]->isAvailable();
        }
        if (!allProxiesAvailable)
            usleep(10000);
    }
    ASSERT_TRUE(allProxiesAvailable);

    uint32_t callId = 0;
    for (unsigned int i = 0; i < numCallsPerProxy_; i++) {
        for (unsigned int j = 0; j < numProxies_; j++) {
            uint32_t in1 = i;
            std::string in2 = "string" + std::to_string(i) + "_" + std::to_string(j);
            testProxies[j]->testPredefinedTypeMethodAsync(
                            in1,
                            in2,
                            std::bind(
                                      &DBusLoadTest::TestPredefinedTypeMethodAsyncCallback,
                                      this,
                                      callId++,
                                      in1,
                                      in2,
                                      std::placeholders::_1,
                                      std::placeholders::_2,
                                      std::placeholders::_3));
        }
    }

    bool allCallsSucceeded = false;
    for (unsigned int i = 0; !allCallsSucceeded && i < 100; ++i) {
        allCallsSucceeded = std::all_of(callSucceeded_.cbegin(), callSucceeded_.cend(), [](int b){ return b; });
        if (!allCallsSucceeded)
            usleep(100000);
    }
    ASSERT_TRUE(allCallsSucceeded);

    runtime_->unregisterService(domain_, stub->getStubAdapter()->getInterface(), serviceAddress_);
}

// Multiple proxies in separate threads, one stub
TEST_F(DBusLoadTest, MultipleClientsSingleStubCallsSucceed) {
    std::array<std::shared_ptr<CommonAPI::Factory>, numProxies_> testProxyFactories;
    std::array<std::shared_ptr<VERSION::commonapi::tests::TestInterfaceProxyBase>, numProxies_> testProxies;

    for (unsigned int i = 0; i < numProxies_; i++) {
        testProxies[i] = runtime_->buildProxy < VERSION::commonapi::tests::TestInterfaceProxy >(domain_, serviceAddress_);
        ASSERT_TRUE((bool )testProxies[i]);
    }

    auto stub = std::make_shared<TestInterfaceStubFinal>();
    bool serviceRegistered = false;
    for (auto i = 0; !serviceRegistered && i < 100; ++i) {
        serviceRegistered = runtime_->registerService(domain_, serviceAddress_, stub, "connection");
        if(!serviceRegistered)
            usleep(10000);
    }
    ASSERT_TRUE(serviceRegistered);

    bool allProxiesAvailable = false;
    for (unsigned int i = 0; !allProxiesAvailable && i < 100; ++i) {
        allProxiesAvailable = true;
        for (unsigned int j = 0; j < numProxies_; ++j) {
            allProxiesAvailable = allProxiesAvailable && testProxies[j]->isAvailable();
        }
        if (!allProxiesAvailable)
            usleep(10000);
    }
    ASSERT_TRUE(allProxiesAvailable);

    uint32_t callId = 0;
    for (unsigned int i = 0; i < numCallsPerProxy_; i++) {
        for (unsigned int j = 0; j < numProxies_; j++) {
            uint32_t in1 = i;
            std::string in2 = "string" + std::to_string(i) + "_" + std::to_string(j);
            testProxies[j]->testPredefinedTypeMethodAsync(
                            in1,
                            in2,
                            std::bind(
                                      &DBusLoadTest::TestPredefinedTypeMethodAsyncCallback,
                                      this,
                                      callId++,
                                      in1,
                                      in2,
                                      std::placeholders::_1,
                                      std::placeholders::_2,
                                      std::placeholders::_3));
        }
    }

    bool allCallsSucceeded = false;
    for (unsigned int i = 0; !allCallsSucceeded && i < 100; ++i) {
        allCallsSucceeded = std::all_of(callSucceeded_.cbegin(), callSucceeded_.cend(), [](int b){ return b; });
        if (!allCallsSucceeded)
            usleep(100000);
    }
    ASSERT_TRUE(allCallsSucceeded);

    runtime_->unregisterService(domain_, stub->getStubAdapter()->getInterface(), serviceAddress_);
}

// Multiple proxies in separate threads, multiple stubs in separate threads
TEST_F(DBusLoadTest, MultipleClientsMultipleServersCallsSucceed) {
    std::array<std::shared_ptr<CommonAPI::Factory>, numProxies_> testProxyFactories;
    std::array<std::shared_ptr<CommonAPI::Factory>, numProxies_> testStubFactories;
    std::array<std::shared_ptr<VERSION::commonapi::tests::TestInterfaceProxyBase>, numProxies_> testProxies;
    std::array<std::shared_ptr<VERSION::commonapi::tests::TestInterfaceStub>, numProxies_> testStubs;

    for (unsigned int i = 0; i < numProxies_; i++) {
        testProxies[i] = runtime_->buildProxy < VERSION::commonapi::tests::TestInterfaceProxy >(domain_, serviceAddress_ + std::to_string(i));
        ASSERT_TRUE((bool )testProxies[i]);
    }

    for (unsigned int i = 0; i < numProxies_; i++) {
        testStubs[i] = std::make_shared<TestInterfaceStubFinal>();
        ASSERT_TRUE((bool )testStubs[i]);
        bool serviceRegistered = false;
        for (auto j = 0; !serviceRegistered && j < 100; ++j) {
            serviceRegistered = runtime_->registerService(domain_, serviceAddress_ + std::to_string(i), testStubs[i], "connection");
            if(!serviceRegistered)
                usleep(10000);
        }
        ASSERT_TRUE(serviceRegistered);
    }

    bool allProxiesAvailable = false;
    for (unsigned int i = 0; !allProxiesAvailable && i < 100; ++i) {
        allProxiesAvailable = true;
        for (unsigned int j = 0; j < numProxies_; ++j) {
            allProxiesAvailable = allProxiesAvailable && testProxies[j]->isAvailable();
        }
        if (!allProxiesAvailable)
            usleep(1000000);
    }
    ASSERT_TRUE(allProxiesAvailable);

    uint32_t callId = 0;
    for (unsigned int i = 0; i < numCallsPerProxy_; i++) {
        for (unsigned int j = 0; j < numProxies_; j++) {
            uint32_t in1 = i;
            std::string in2 = "string" + std::to_string(i) + "_" + std::to_string(j);
            testProxies[j]->testPredefinedTypeMethodAsync(
                            in1,
                            in2,
                            std::bind(
                                      &DBusLoadTest::TestPredefinedTypeMethodAsyncCallback,
                                      this,
                                      callId++,
                                      in1,
                                      in2,
                                      std::placeholders::_1,
                                      std::placeholders::_2,
                                      std::placeholders::_3));
        }
    }

    bool allCallsSucceeded = false;
    for (auto i = 0; !allCallsSucceeded && i < 100; ++i) {
        allCallsSucceeded = std::all_of(callSucceeded_.cbegin(), callSucceeded_.cend(), [](int b){ return b; });
        if (!allCallsSucceeded)
            usleep(100000);
    }
    ASSERT_TRUE(allCallsSucceeded);

    for (unsigned int i = 0; i < numProxies_; i++) {
        runtime_->unregisterService(domain_, testStubs[i]->getStubAdapter()->getInterface(), serviceAddress_ + std::to_string(i));
    }
}

#ifndef __NO_MAIN__
int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    ::testing::AddGlobalTestEnvironment(new Environment());
    return RUN_ALL_TESTS();
}
#endif