summaryrefslogtreecommitdiff
path: root/tests/logic_.cpp
blob: 0259bd6dd26443606200e5cc1d5edb1f9aaefd59 (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
/*
 * Copyright (c) 2015 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        logic_.cpp
 * @author      Janusz Kozerski (j.kozerski@samsung.com)
 * @version     1.0
 * @brief       This file is the implementation of SQL queries
 */

/*
 * This Class makes all methods from Logic Class public - for testing purpose.
 * Some of methods are stubbed, and some of them just calls corresponding methods from Logic Class.
 */

#include <cchecker/log.h>
#include <chrono>
#include <boost/test/unit_test.hpp>

#include <logic_.h>

namespace CCHECKER {

Logic_::Logic_(void) :
        Logic(),
        m_installCnt(0),
        m_uninstallCnt(0),
        m_bufferCnt(0)
{}

Logic_::~Logic_(void)
{
    clean();
}
void Logic_::clean(void)
{
    LogDebug("Cert-checker cleaning.");

    // wait and join processing thread
    if (m_thread.joinable()) {
        LogDebug("Waiting for join processing thread");
        {
            std::lock_guard < std::mutex > lock(m_mutex_cv);
            set_should_exit();
            m_to_process.notify_one();
        }
        m_thread.join();
        LogDebug("Processing thread joined");
    } else
        LogDebug("No thread to join");
}

void Logic_::connman_callback_manual_(bool state)
{
    Logic::set_online(state);
}

void Logic_::pkgmgr_install_manual_(const app_t &app)
{
    push_event(event_t(app, event_t::event_type_t::APP_INSTALL));
}

void Logic_::pkgmgr_uninstall_manual_(const app_t &app)
{
    push_event(event_t(app, event_t::event_type_t::APP_UNINSTALL));
}

void Logic_::process_event(const event_t &event)
{
    Logic::process_event(event);

    std::lock_guard<std::mutex> lock(_m_mutex_wait_cv);
    switch(event.event_type)
    {
    case event_t::event_type_t::APP_INSTALL:
        m_installCnt++;
        LogDebug(m_installCnt << " " << m_uninstallCnt << " " << m_bufferCnt);
        break;
    case event_t::event_type_t::APP_UNINSTALL:
        m_uninstallCnt++;
        LogDebug(m_installCnt << " " << m_uninstallCnt << " " << m_bufferCnt);
        break;
    default:
        return;
    }
    // notify caller
    _m_wait_for_process.notify_one();
}

void Logic_::app_processed()
{
    std::lock_guard<std::mutex> lock(_m_mutex_wait_cv);
    m_bufferCnt++;
    LogDebug(m_installCnt << " " << m_uninstallCnt << " " << m_bufferCnt);

    // notify caller
    _m_wait_for_process.notify_one();
}

void Logic_::reset_cnt()
{
    m_installCnt = 0;
    m_uninstallCnt = 0;
    m_bufferCnt = 0;
}

void Logic_::wait_for_worker(int installCnt, int uninstallCnt, int bufferCnt)
{
    LogDebug("Wait for: " << installCnt << " " << uninstallCnt << " " << bufferCnt);
    std::unique_lock<std::mutex> lock(_m_mutex_wait_cv);
    bool timeout = !_m_wait_for_process.wait_for(
            lock,
            std::chrono::seconds(10),
            [this, installCnt, uninstallCnt, bufferCnt]{
                return m_installCnt == installCnt &&
                       m_uninstallCnt == uninstallCnt &&
                       m_bufferCnt == bufferCnt;
            }
    );
    _m_mutex_wait_cv.unlock();
    BOOST_REQUIRE(!timeout);
    reset_cnt();
}

const std::list<app_t>& Logic_::get_buffer_()
{
    return m_buffer;
}

} // CCHECKER