summaryrefslogtreecommitdiffstats
path: root/common/Unit.cpp
blob: 875c976866be67ae603a7aeea5fc1d3ec1db68a8 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * 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 <config.h>

#include <iostream>
#include "Unit.hpp"

#include <cassert>
#include <dlfcn.h>
#include <fstream>
#include <sysexits.h>
#include <thread>

#include <Poco/Util/LayeredConfiguration.h>

#include "Log.hpp"
#include "Util.hpp"

#include <common/SigUtil.hpp>

UnitBase *UnitBase::Global = nullptr;
char * UnitBase::UnitLibPath;
static std::thread TimeoutThread;
static std::atomic<bool> TimeoutThreadRunning(false);
std::timed_mutex TimeoutThreadMutex;

UnitBase *UnitBase::linkAndCreateUnit(UnitType type, const std::string &unitLibPath)
{
#if !MOBILEAPP
    void *dlHandle = dlopen(unitLibPath.c_str(), RTLD_GLOBAL|RTLD_NOW);
    if (!dlHandle)
    {
        LOG_ERR("Failed to load " << unitLibPath << ": " << dlerror());
        return nullptr;
    }

    // avoid std:string de-allocation during failure / exit.
    UnitLibPath = strdup(unitLibPath.c_str());

    const char *symbol = nullptr;
    switch (type)
    {
        case UnitType::Wsd:
            symbol = "unit_create_wsd";
            break;
        case UnitType::Kit:
            symbol = "unit_create_kit";
            break;
    }
    CreateUnitHooksFunction* createHooks;
    createHooks = reinterpret_cast<CreateUnitHooksFunction *>(dlsym(dlHandle, symbol));
    if (!createHooks)
    {
        LOG_ERR("No " << symbol << " symbol in " << unitLibPath);
        return nullptr;
    }
    UnitBase *hooks = createHooks();

    if (hooks)
        hooks->setHandle(dlHandle);

    return hooks;
#else
    return nullptr;
#endif
}

bool UnitBase::init(UnitType type, const std::string &unitLibPath)
{
#if !MOBILEAPP
    assert(!Global);
#else
    // The LOOLWSD initialization is called in a loop on mobile, allow reuse
    if (Global)
        return true;
#endif

    if (!unitLibPath.empty())
    {
        Global = linkAndCreateUnit(type, unitLibPath);
        if (Global && type == UnitType::Kit)
        {
            TimeoutThreadMutex.lock();
            TimeoutThread = std::thread([]{
                    TimeoutThreadRunning = true;
                    Util::setThreadName("unit timeout");

                    if (TimeoutThreadMutex.try_lock_for(std::chrono::milliseconds(Global->_timeoutMilliSeconds)))
                    {
                        LOG_DBG("Unit test finished in time");
                        TimeoutThreadMutex.unlock();
                    }
                    else
                    {
                        LOG_ERR("Unit test timeout");
                        Global->timeout();
                    }
                    TimeoutThreadRunning = false;
                });
        }
    }
    else
    {
        switch (type)
        {
        case UnitType::Wsd:
            Global = new UnitWSD();
            break;
        case UnitType::Kit:
            Global = new UnitKit();
            break;
        default:
            assert(false);
            break;
        }
    }

    if (Global)
        Global->_type = type;

    return Global != nullptr;
}

bool UnitBase::isUnitTesting()
{
    return Global && Global->_dlHandle;
}

void UnitBase::setTimeout(int timeoutMilliSeconds)
{
    assert(!TimeoutThreadRunning);
    _timeoutMilliSeconds = timeoutMilliSeconds;
}

UnitBase::UnitBase()
    : _dlHandle(nullptr),
      _setRetValue(false),
      _retValue(0),
      _timeoutMilliSeconds(30 * 1000),
      _type(UnitType::Wsd)
{
}

UnitBase::~UnitBase()
{
// FIXME: we should really clean-up properly.
//    if (_dlHandle)
//        dlclose(_dlHandle);
    _dlHandle = nullptr;
}

UnitWSD::UnitWSD()
    : _hasKitHooks(false)
{
}

UnitWSD::~UnitWSD()
{
}

void UnitWSD::configure(Poco::Util::LayeredConfiguration &config)
{
    if (isUnitTesting())
    {
        // Force HTTP - helps stracing.
        config.setBool("ssl.enable", false);
        // Use http:// everywhere.
        config.setBool("ssl.termination", false);
        // Force console output - easier to debug.
        config.setBool("logging.file[@enable]", false);
    }
}

void UnitWSD::lookupTile(int part, int width, int height, int tilePosX, int tilePosY,
                         int tileWidth, int tileHeight,
                         std::shared_ptr<std::vector<char>> &tile)
{
    if (tile)
        onTileCacheHit(part, width, height, tilePosX, tilePosY, tileWidth, tileHeight);
    else
        onTileCacheMiss(part, width, height, tilePosX, tilePosY, tileWidth, tileHeight);
}

UnitKit::UnitKit()
{
}

UnitKit::~UnitKit()
{
}

void UnitBase::exitTest(TestResult result)
{
    LOG_INF("exitTest: " << (int)result << ". Flagging for termination.");
    _setRetValue = true;
    _retValue = result == TestResult::Ok ? EX_OK : EX_SOFTWARE;
    SigUtil::setTerminationFlag();
    SocketPoll::wakeupWorld();
}

void UnitBase::timeout()
{
    if (isUnitTesting())
    {
        LOG_ERR("Timed out waiting for unit test to complete");
        exitTest(TestResult::TimedOut);
    }
}

void UnitBase::returnValue(int &retValue)
{
    if (_setRetValue)
        retValue = _retValue;

    // tell the timeout thread that the work has finished
    TimeoutThreadMutex.unlock();
    if (TimeoutThread.joinable())
        TimeoutThread.join();

    delete Global;
    Global = nullptr;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */