From 4041c557d84532eced3034b6ed73a22c919ad222 Mon Sep 17 00:00:00 2001 From: Dario Date: Mon, 18 Nov 2019 19:09:09 +0100 Subject: [PATCH] fill test with code, fix extra qualification error --- src/cpp/SingletonManager/LanguageManager.h | 16 +-- src/cpp/main.cpp | 2 + src/cpp/test/Test.h | 15 ++ src/cpp/test/TestTasks.cpp | 152 +++++++++++++++++++++ src/cpp/test/TestTasks.h | 56 ++++++++ src/cpp/test/main.cpp | 48 +++++++ src/cpp/test/main.h | 2 + 7 files changed, 283 insertions(+), 8 deletions(-) diff --git a/src/cpp/SingletonManager/LanguageManager.h b/src/cpp/SingletonManager/LanguageManager.h index 5fb0be9e0..483fa3fc1 100644 --- a/src/cpp/SingletonManager/LanguageManager.h +++ b/src/cpp/SingletonManager/LanguageManager.h @@ -46,15 +46,15 @@ public: // catalog overload api - const char * LanguageCatalog::gettext(const char * msgid); - const char * LanguageCatalog::ngettext(const char * msgid, const char * msgid_plural, spirit_po::uint plural); - const char * LanguageCatalog::pgettext(const char * msgctxt, const char * msgid); - const char * LanguageCatalog::npgettext(const char * msgctxt, const char * msgid, const char * msgid_plural, spirit_po::uint plural); + const char * gettext(const char * msgid); + const char * ngettext(const char * msgid, const char * msgid_plural, spirit_po::uint plural); + const char * pgettext(const char * msgctxt, const char * msgid); + const char * npgettext(const char * msgctxt, const char * msgid, const char * msgid_plural, spirit_po::uint plural); - std::string LanguageCatalog::gettext_str(const std::string & msgid); - std::string LanguageCatalog::ngettext_str(const std::string & msgid, const std::string & msgid_plural, spirit_po::uint plural); - std::string LanguageCatalog::pgettext_str(const std::string & msgctxt, const std::string & msgid); - std::string LanguageCatalog::npgettext_str(const std::string & msgctxt, const std::string & msgid, const std::string & msgid_plural, spirit_po::uint plural); + std::string gettext_str(const std::string & msgid); + std::string ngettext_str(const std::string & msgid, const std::string & msgid_plural, spirit_po::uint plural); + std::string pgettext_str(const std::string & msgctxt, const std::string & msgid); + std::string npgettext_str(const std::string & msgctxt, const std::string & msgid, const std::string & msgid_plural, spirit_po::uint plural); inline Languages getLanguage() { return mThisLanguage; } diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index fa8c5c2ce..68d6bf2c4 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -6,6 +6,7 @@ #include "model/User.h" #include "model/Session.h" +#ifndef _TEST_BUILD int main(int argc, char** argv) { GOOGLE_PROTOBUF_VERIFY_VERSION; @@ -21,3 +22,4 @@ int main(int argc, char** argv) Gradido_LoginServer app; return app.run(argc, argv); } +#endif \ No newline at end of file diff --git a/src/cpp/test/Test.h b/src/cpp/test/Test.h index e69de29bb..2e2a57b77 100644 --- a/src/cpp/test/Test.h +++ b/src/cpp/test/Test.h @@ -0,0 +1,15 @@ +#ifndef __GRADIDO_LOGIN_SERVER_TEST_ +#define __GRADIDO_LOGIN_SERVER_TEST_ + +class Test +{ +public: + //! \return 0 if init okay, else return != 0 + virtual int init() = 0; + + //! \return 0 if okay, else return != 0 + virtual int test() = 0; + virtual const char* getName() = 0; +}; + +#endif //__GRADIDO_LOGIN_SERVER_TEST_ \ No newline at end of file diff --git a/src/cpp/test/TestTasks.cpp b/src/cpp/test/TestTasks.cpp index e69de29bb..9250d7cd5 100644 --- a/src/cpp/test/TestTasks.cpp +++ b/src/cpp/test/TestTasks.cpp @@ -0,0 +1,152 @@ + +#include "TestTasks.h" +#include "Poco/Environment.h" +#include + +#include "Poco/Logger.h" +#include "Poco/Path.h" +#include "Poco/AsyncChannel.h" +#include "Poco/SimpleFileChannel.h" +#include "Poco/ConsoleChannel.h" +#include "Poco/SplitterChannel.h" + +RandomCPUTask::RandomCPUTask(UniLib::controller::CPUSheduler* scheduler, TestTasks* parent, int nr) + : UniLib::controller::CPUTask(scheduler), mParent(parent), mHeaviness(0), mNr(nr) +{ + int zahl = randombytes_random() % 2; + if (zahl == 0) { + mType = RANDOM_CPU_TASK_CALCULATE; + mHeaviness = randombytes_random() % 10000 + 1000; + } + else { + mType = RANDOM_CPU_TASK_SLEEP; + mHeaviness = randombytes_random() % 1000; + } +} + +const char* RandomCPUTask::getName() const +{ + if (mType == RANDOM_CPU_TASK_CALCULATE) return "Calculate"; + if (mType == RANDOM_CPU_TASK_SLEEP) return "Sleep"; + return "not set"; +} + +RandomCPUTask::~RandomCPUTask() +{ + mParent->releaseTask(mNr); +} + +int RandomCPUTask::run() +{ + if (mType == RANDOM_CPU_TASK_SLEEP) { + Poco::Thread::sleep(mHeaviness); + } + else if (mType == RANDOM_CPU_TASK_CALCULATE) { + for (int i = 0; i < mHeaviness; i++) { + double zahl = sqrtf(powf(i, sqrtf(i))); + } + } + return 0; +} + +// ************************************************************************************* + +TestTasks::TestTasks() + : mTaskScheduler(Poco::Environment::processorCount() * 4, "testScheduler") +{ + +} + +TestTasks::~TestTasks() +{ + +} + + +int TestTasks::init() +{ + // init speed logger + Poco::AutoPtr speedLogFileChannel(new Poco::SimpleFileChannel("speedLog.txt")); + /* + The optional log file rotation mode: + never: no rotation (default) + : rotate if file size exceeds bytes + K: rotate if file size exceeds Kilobytes + M: rotate if file size exceeds Megabytes + */ + speedLogFileChannel->setProperty("rotation", "500 K"); + Poco::AutoPtr speedLogAsyncChannel(new Poco::AsyncChannel(speedLogFileChannel)); + + Poco::Logger& speedLogger = Poco::Logger::get("SpeedLog"); + speedLogger.setChannel(speedLogAsyncChannel); + speedLogger.setLevel("information"); + + return 0; +} + +int TestTasks::test() +{ + auto workerCount = Poco::Environment::processorCount() * 4; + auto taskCount = workerCount + workerCount * (randombytes_random() % 4); + printf("[TestTasks::test] taskCount: %d\n", taskCount); + for (int i = 1; i <= taskCount; i++) { + Poco::AutoPtr task = new RandomCPUTask(&mTaskScheduler, this, i); + lock(); + mTasks.insert(std::pair(i, task)); + unlock(); + task->scheduleTask(task); + } + int maxWaitCylces = 3000; + bool finished = false; + do { + maxWaitCylces--; + Poco::Thread::sleep(5); + lock(); + if (mTasks.size() == 0) { + finished = true; + } + unlock(); + } while (!finished && maxWaitCylces > 0); + + lock(); + bool hasErrors = false; + if (mTasks.size() > 0 || mErrors.size() > 0) { + hasErrors = true; + printf("[TestTasks::test] error running TestTasks\n"); + if (mTasks.size() > 0) { + printf("%d not exited\n", mTasks.size()); + for (auto it = mTasks.begin(); it != mTasks.end(); it++) { + if (!it->second->isReady()) { + printf("task %d not ready\n", it->first); + } + if (!it->second->isTaskFinished()) { + printf("task %d isn't finished\n", it->first); + } + } + } + for (int i = 0; i < mErrors.size(); i++) { + printf("error: %s\n", mErrors[i].data()); + } + } + unlock(); + + if (hasErrors) { + return -1; + } + + return 0; +} + + +void TestTasks::releaseTask(int nr) +{ + lock(); + auto it = mTasks.find(nr); + if (it != mTasks.end()) { + mTasks.erase(it); + } + else { + mErrors.push_back("[TestTasks] task entry not found" + std::to_string(nr)); + } + unlock(); +} \ No newline at end of file diff --git a/src/cpp/test/TestTasks.h b/src/cpp/test/TestTasks.h index e69de29bb..c12079aa8 100644 --- a/src/cpp/test/TestTasks.h +++ b/src/cpp/test/TestTasks.h @@ -0,0 +1,56 @@ +#ifndef __GRADIDO_LOGIN_SERVER_TEST_TASKS_ +#define __GRADIDO_LOGIN_SERVER_TEST_TASKS_ + +#include "Test.h" +#include + +#include "../tasks/CPUSheduler.h" +#include "../tasks/CPUTask.h" + +#include "../lib/MultithreadContainer.h" + +enum RandomCPUTaskType { + RANDOM_CPU_TASK_CALCULATE, + RANDOM_CPU_TASK_SLEEP +}; +class TestTasks; + +class RandomCPUTask : public UniLib::controller::CPUTask +{ +public: + RandomCPUTask(UniLib::controller::CPUSheduler* scheduler, TestTasks* parent, int nr); + virtual ~RandomCPUTask(); + + int run(); + + const char* getResourceType() const { return "RandomCPUTask"; }; + const char* getName() const; + + +protected: + TestTasks* mParent; + RandomCPUTaskType mType; + Poco::UInt32 mHeaviness; + int mNr; +}; + +class TestTasks : public Test, public UniLib::lib::MultithreadContainer +{ +public: + TestTasks(); + virtual ~TestTasks(); + + int init(); + int test(); + inline const char* getName() { return "TestTasks"; } + + void releaseTask(int nr); + +protected: + UniLib::controller::CPUSheduler mTaskScheduler; + std::map mTasks; + + std::vector mErrors; +}; + +#endif //__GRADIDO_LOGIN_SERVER_TEST_TASKS_ \ No newline at end of file diff --git a/src/cpp/test/main.cpp b/src/cpp/test/main.cpp index 5c254651e..b2a4ee04d 100644 --- a/src/cpp/test/main.cpp +++ b/src/cpp/test/main.cpp @@ -1,6 +1,54 @@ +#include "main.h" +#include + +std::list gTests; + +void fillTests() +{ + gTests.push_back(new TestTasks()); + // gTests.push_back(new LoginTest()); +} + +int load() { + fillTests(); + for (std::list::iterator it = gTests.begin(); it != gTests.end(); it++) + { + if ((*it)->init()) printf("Fehler bei Init test: %s\n", (*it)->getName()); + } + return 0; +} + +int run() +{ + //printf("running tests\n"); + printf("running tests\n"); + for (std::list::iterator it = gTests.begin(); it != gTests.end(); it++) + { + //printf("running: %s\n", it->getName()); + printf("running test: %s", (*it)->getName()); + if (!(*it)->test()) printf("success\n"); + } + return 0; +} + +void ende() +{ + for (std::list::iterator it = gTests.begin(); it != gTests.end(); it++) + { + if (*it) { + delete *it; + } + + } + gTests.clear(); +} + int main(int argc, char** argv) { + load(); + run(); + ende(); return 42; } \ No newline at end of file diff --git a/src/cpp/test/main.h b/src/cpp/test/main.h index e69de29bb..06d8ecf35 100644 --- a/src/cpp/test/main.h +++ b/src/cpp/test/main.h @@ -0,0 +1,2 @@ + +#include "TestTasks.h"