| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 19 мар. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/test-fw/utest.h> |
| 23 |
#include <lsp-plug.in/ipc/Thread.h> |
| 24 |
#include <lsp-plug.in/ipc/NativeExecutor.h> |
| 25 |
|
| 26 |
using namespace lsp; |
| 27 |
|
| 28 |
static const status_t statuses[] = |
| 29 |
{ |
| 30 |
STATUS_OK, STATUS_NOT_FOUND, STATUS_BAD_ARGUMENTS, STATUS_CANCELLED |
| 31 |
}; |
| 32 |
|
| 33 |
UTEST_BEGIN("runtime.ipc", executor) |
| 34 |
|
| 35 |
class TestTask: public ipc::ITask |
| 36 |
{ |
| 37 |
private: |
| 38 |
size_t nDelay; |
| 39 |
status_t nResult; |
| 40 |
|
| 41 |
public: |
| 42 |
explicit TestTask(size_t delay, status_t result) : nDelay(delay), nResult(result) {} |
| 43 |
virtual ~TestTask() {} |
| 44 |
|
| 45 |
public: |
| 46 |
virtual status_t run() |
| 47 |
{ |
| 48 |
ipc::Thread::sleep(nDelay); |
| 49 |
return nResult; |
| 50 |
} |
| 51 |
}; |
| 52 |
|
| 53 |
UTEST_MAIN |
| 54 |
{ |
| 55 |
TestTask *tasks[4]; |
| 56 |
|
| 57 |
for (size_t i=0; i<4; ++i) |
| 58 |
{ |
| 59 |
tasks[i] = new TestTask(100 + (rand() % 100), statuses[i]); |
| 60 |
UTEST_ASSERT(tasks[i] != NULL); |
| 61 |
UTEST_ASSERT(tasks[i]->idle()); |
| 62 |
} |
| 63 |
|
| 64 |
printf("Starting native executor...\n"); |
| 65 |
ipc::NativeExecutor executor; |
| 66 |
UTEST_ASSERT(executor.start() == STATUS_OK); |
| 67 |
|
| 68 |
printf("Submitting tasks...\n"); |
| 69 |
for (size_t i=0; i<4; ++i) |
| 70 |
{ |
| 71 |
executor.submit(tasks[i]); |
| 72 |
ipc::ITask::task_state_t ts = tasks[i]->state(); |
| 73 |
UTEST_ASSERT( |
| 74 |
(ts == ipc::ITask::TS_SUBMITTED) || |
| 75 |
(ts == ipc::ITask::TS_RUNNING) |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
printf("Shuttind down executor...\n"); |
| 80 |
executor.shutdown(); |
| 81 |
|
| 82 |
printf("Checking tasks...\n"); |
| 83 |
for (size_t i=0; i<4; ++i) |
| 84 |
{ |
| 85 |
UTEST_ASSERT(tasks[i]->completed()); |
| 86 |
UTEST_ASSERT(tasks[i]->code() == statuses[i]); |
| 87 |
UTEST_ASSERT(tasks[i]->reset()); |
| 88 |
UTEST_ASSERT(tasks[i]->idle()); |
| 89 |
} |
| 90 |
|
| 91 |
printf("Destroying tasks...\n"); |
| 92 |
for (size_t i=0; i<4; ++i) |
| 93 |
delete tasks[i]; |
| 94 |
} |
| 95 |
|
| 96 |
UTEST_END |
| 97 |
|