summaryrefslogtreecommitdiff
path: root/tests/queue_test_thread.cpp
blob: b20f10c1c86478d827f357f01d7151dc91755ead (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
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
/*
 * @file        queue_test_thread.h
 * @author      Janusz Kozerski (j.kozerski@samsung.com)
 * @version     1.0
 * @brief       Implementation of class for multi-thread testing Queue
 */

#include <thread>
#include <unistd.h>

#include "service/app.h"
#include "service/queue.h"
#include "common/log.h"

#include <queue_test_thread.h>

namespace CCHECKER {

#define APPS_COUNT 10

app_t app[APPS_COUNT] = {
	app_t("app_id0", "###",     0, {}),
	app_t("app_id1", "pkg_id1", 1, {}),
	app_t("app_id2", "pkg_id2", 2, {}),
	app_t("app_id3", "###",     3, {}),
	app_t("app_id4", "###",     4, {}),
	app_t("app_id5", "###",     5, {}),
	app_t("app_id6", "###",     6, {}),
	app_t("app_id7", "###",     7, {}),
	app_t("app_id8", "###",     8, {}),
	app_t("app_id9", "###",     9, {})
};

void TestQueue::add_events_th(int number_of_threads)
{
	if (number_of_threads < 1) {
		LogDebug("Too few threads, at least 1 should be used");
		number_of_threads = 1;
	}

	m_sent = 0;
	m_max_events = number_of_threads * 2 * APPS_COUNT;
	LogDebug("Running threads");

	for (int i = 0; i < number_of_threads; i++)
		std::thread(&TestQueue::add_events, this).detach();

	LogDebug("Running threads done");
}

void TestQueue::add_events()
{
	for (int i = 0; i < APPS_COUNT; i++) {
		m_queue.push_event(event_t(app[i], event_t::event_type_t::APP_INSTALL));
		m_sent++;
		LogDebug("Add event : " << i);
	}

	for (int i = 0; i < APPS_COUNT; i++) {
		m_queue.push_event(event_t(app[i], event_t::event_type_t::APP_UNINSTALL));
		m_sent++;
		LogDebug("Add event : " << i);
	}
}

bool TestQueue::pop_events()
{
	int i_install = 0;
	int i_uninstall = 0;
	event_t ev;
	LogDebug("Looking events...");

	do {
		while (m_queue.pop_event(ev)) {
			if (ev.event_type == event_t::event_type_t::APP_INSTALL)
				i_install++;
			else if (ev.event_type == event_t::event_type_t::APP_UNINSTALL)
				i_uninstall++;
		}
	} while (m_sent < m_max_events);

	// Read all events once again - just for case
	while (m_queue.pop_event(ev)) {
		if (ev.event_type == event_t::event_type_t::APP_INSTALL)
			i_install++;
		else if (ev.event_type == event_t::event_type_t::APP_UNINSTALL)
			i_uninstall++;
	}

	LogDebug("Found " << i_install + i_uninstall << " events.");

	if (i_install != m_max_events / 2 || i_uninstall != m_max_events / 2)
		return false;

	return true;
}

} // CCHECKER