summaryrefslogtreecommitdiff
path: root/org.genivi.commonapi.core.verification/src/THMainLoopTwoThreads.cpp
blob: e7158bdae2173a90353cd33e8cb559cc55adac1e (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
/* Copyright (C) 2014 - 2015 BMW Group
 * Author: Andrei Yagoubov
 * 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/. */

/**
* @file Threading
*/

#include <gtest/gtest.h>
#include "CommonAPI/CommonAPI.hpp"
#include "utils/VerificationMainLoopWithQueue.h"
#include "v1_0/commonapi/threading/TestInterfaceProxy.hpp"
#include "v1_0/commonapi/threading/TestInterfaceStubDefault.hpp"

const std::string domain = "local";
const std::string instance = "my.test.commonapi.address";

class PingPongTestStub : public v1_0::commonapi::threading::TestInterfaceStubDefault {
	virtual void testMethod(const std::shared_ptr<CommonAPI::ClientId> _client,
			uint8_t _x,
			testMethodReply_t _reply) {

		_reply(_x);
    }
};

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

		context_ = std::make_shared<CommonAPI::MainLoopContext>("client-sample");
		ASSERT_TRUE((bool)context_);

		eventQueue_ = std::make_shared<CommonAPI::VerificationMainLoopEventQueue>();
		mainLoop_ = new CommonAPI::VerificationMainLoop(context_, eventQueue_);

		stub_ = std::make_shared<PingPongTestStub>();

		bool stubRegistered = runtime_->registerService(domain, instance, stub_, "service-sample");
		ASSERT_TRUE((bool)stubRegistered);

		proxy_ = runtime_->buildProxy<v1_0::commonapi::threading::TestInterfaceProxy>(domain, instance, context_);
		ASSERT_TRUE((bool)proxy_);

		eventQueueThread_ = new std::thread([&]() { eventQueue_->run(); });
		mainLoopThread_ = new std::thread([&]() { mainLoop_->run(); });
    }

    void TearDown() {
		runtime_->unregisterService(domain, stub_->getStubAdapter()->getInterface(), instance);
		mainLoop_->stop();
		//mainLoopThread_->join();

		usleep(1000000);
    }

	std::shared_ptr<CommonAPI::Runtime> runtime_;
	std::shared_ptr<CommonAPI::MainLoopContext> context_;
	std::shared_ptr<CommonAPI::VerificationMainLoopEventQueue> eventQueue_;

	std::shared_ptr<PingPongTestStub> stub_;
	std::shared_ptr<v1_0::commonapi::threading::TestInterfaceProxy<>> proxy_;

	CommonAPI::VerificationMainLoop* mainLoop_;

	std::thread* eventQueueThread_;
	std::thread* mainLoopThread_;
};

/**
* @test Proxy Receives Available when MainLoop Dispatched sourced out to other thread.
*/
TEST_F(THMainLoopTwoThreads, ProxyGetsAvailableStatus) {
	std::condition_variable available;
	std::mutex m;
	bool isAvailable(false);
	
	proxy_->getProxyStatusEvent().subscribe([&](const CommonAPI::AvailabilityStatus& val) {
		if (val == CommonAPI::AvailabilityStatus::AVAILABLE) {
		    isAvailable = true;
			available.notify_one();
		}
	});

	if (!isAvailable) {
	    std::unique_lock<std::mutex> uniqueLock(m);
	    available.wait_for(uniqueLock, std::chrono::seconds(10));
	}
	
	ASSERT_TRUE(proxy_->isAvailable());
}

/**
* @test Proxy gets function response when MainLoop Dispatched sourced out to other thread.
*/
TEST_F(THMainLoopTwoThreads, ProxyGetsFunctionResponse) {
	std::condition_variable available;
	std::mutex m;
	bool isAvailable(false);

	proxy_->getProxyStatusEvent().subscribe([&](const CommonAPI::AvailabilityStatus& val) {
		if (val == CommonAPI::AvailabilityStatus::AVAILABLE) {
		    isAvailable = true;
			available.notify_one();
		}
	});

	if (!isAvailable) {
	    std::unique_lock<std::mutex> uniqueLock(m);
	    available.wait_for(uniqueLock, std::chrono::seconds(10));
	}

	ASSERT_TRUE(proxy_->isAvailable());

	CommonAPI::CallStatus callStatus;

	uint8_t x, y;
	x = 1;
	y = 0;

	proxy_->testMethod(x, callStatus, y);

	ASSERT_EQ(CommonAPI::CallStatus::SUCCESS, callStatus);
	ASSERT_EQ(1, y);
}

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