summaryrefslogtreecommitdiff
path: root/src/test/DBusServicePublisherTest.cpp
blob: b265e7121f5735b245df831291da80e52d6f7b8b (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
/* Copyright (C) 2013 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * 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 <utility>
#include <tuple>
#include <type_traits>

#include <CommonAPI/CommonAPI.h>

#define COMMONAPI_INTERNAL_COMPILATION

#include <CommonAPI/AttributeExtension.h>
#include <CommonAPI/types.h>

#include <CommonAPI/DBus/DBusConnection.h>
#include <CommonAPI/DBus/DBusProxy.h>
#include <CommonAPI/DBus/DBusRuntime.h>

#include "commonapi/tests/PredefinedTypeCollection.h"
#include "commonapi/tests/DerivedTypeCollection.h"
#include "commonapi/tests/TestInterfaceProxy.h"
#include "commonapi/tests/TestInterfaceStubDefault.h"
#include "commonapi/tests/TestInterfaceDBusStubAdapter.h"

#include "commonapi/tests/TestInterfaceDBusProxy.h"


class DBusServicePublisherTest: public ::testing::Test {
 protected:
    virtual void SetUp() {
        runtime_ = CommonAPI::Runtime::load();
        ASSERT_TRUE((bool)runtime_);
    }

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

    std::shared_ptr<CommonAPI::Runtime> runtime_;
};


TEST_F(DBusServicePublisherTest, HandlesManagementOfServices) {
    std::shared_ptr<CommonAPI::Factory> serviceFactory = runtime_->createFactory();
    ASSERT_TRUE((bool)serviceFactory);
    std::shared_ptr<CommonAPI::ServicePublisher> servicePublisher = runtime_->getServicePublisher();
    ASSERT_TRUE((bool)servicePublisher);

    const std::string serviceAddress = "local:commonapi.tests.TestInterface:commonapi.tests.TestInterface";
    const std::string serviceAddress2 = "local:commonapi.tests.TestInterfaceTwo:commonapi.tests.TestInterface";

    auto myStub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
    bool success = servicePublisher->registerService(myStub, serviceAddress, serviceFactory);
    EXPECT_TRUE(success);

    auto myStub2 = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
    success = servicePublisher->registerService(myStub2, serviceAddress2, serviceFactory);
    EXPECT_TRUE(success);

    std::shared_ptr<CommonAPI::Factory> clientFactory = runtime_->createFactory();

    usleep(10000);

    bool instanceIsAvailable = clientFactory->isServiceInstanceAlive(serviceAddress);
    EXPECT_TRUE(instanceIsAvailable);
    instanceIsAvailable = clientFactory->isServiceInstanceAlive(serviceAddress2);
    EXPECT_TRUE(instanceIsAvailable);

    success = servicePublisher->unregisterService("SomeOther:Unknown:Service");
    EXPECT_FALSE(success);

    success = servicePublisher->unregisterService(serviceAddress);
    EXPECT_TRUE(success);

    success = servicePublisher->unregisterService(serviceAddress2);
    EXPECT_TRUE(success);

    usleep(10000);

    instanceIsAvailable = clientFactory->isServiceInstanceAlive(serviceAddress);
    EXPECT_FALSE(instanceIsAvailable);
    instanceIsAvailable = clientFactory->isServiceInstanceAlive(serviceAddress2);
    EXPECT_FALSE(instanceIsAvailable);
}


TEST_F(DBusServicePublisherTest, GracefullyHandlesWrongAddresses) {
    std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime_->createFactory();
    ASSERT_TRUE((bool)proxyFactory);
    std::shared_ptr<CommonAPI::ServicePublisher> servicePublisher = runtime_->getServicePublisher();
    ASSERT_TRUE((bool)servicePublisher);

    auto myStub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();

    EXPECT_FALSE(servicePublisher->registerService(myStub, "", proxyFactory));
    EXPECT_FALSE(servicePublisher->registerService(myStub, "too:much:stuff:here", proxyFactory));
}

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