Rewrite executor to template & come back executor unit tests
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "executor.h"
|
||||
#include "pimutex.h"
|
||||
|
||||
const int WAIT_THREAD_TIME_MS = 30;
|
||||
#include "testutil.h"
|
||||
|
||||
TEST(ExcutorIntegrationTest, execute_is_runnable_invoke) {
|
||||
PIMutex m;
|
||||
@@ -14,11 +13,13 @@ TEST(ExcutorIntegrationTest, execute_is_runnable_invoke) {
|
||||
m.unlock();
|
||||
});
|
||||
piMSleep(WAIT_THREAD_TIME_MS);
|
||||
m.lock();
|
||||
ASSERT_EQ(invokedRunnables, 1);
|
||||
m.unlock();
|
||||
}
|
||||
|
||||
TEST(ExcutorIntegrationTest, execute_is_not_execute_after_shutdown) {
|
||||
bool isRunnableInvoke = false;
|
||||
volatile bool isRunnableInvoke = false;
|
||||
PIThreadPoolExecutor executorService(1);
|
||||
executorService.shutdown();
|
||||
executorService.execute([&]() {
|
||||
@@ -29,7 +30,7 @@ TEST(ExcutorIntegrationTest, execute_is_not_execute_after_shutdown) {
|
||||
}
|
||||
|
||||
TEST(ExcutorIntegrationTest, execute_is_execute_before_shutdown) {
|
||||
bool isRunnableInvoke = false;
|
||||
volatile bool isRunnableInvoke = false;
|
||||
PIThreadPoolExecutor executorService(1);
|
||||
executorService.execute([&]() {
|
||||
piMSleep(WAIT_THREAD_TIME_MS);
|
||||
|
||||
102
lib/concurrent/test/ExecutorUnitTest.cpp
Normal file
102
lib/concurrent/test/ExecutorUnitTest.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "executor.h"
|
||||
#include "testutil.h"
|
||||
|
||||
using ::testing::_;
|
||||
using ::testing::SetArgReferee;
|
||||
using ::testing::DoAll;
|
||||
using ::testing::DeleteArg;
|
||||
using ::testing::Return;
|
||||
using ::testing::AtLeast;
|
||||
using ::testing::ByRef;
|
||||
using ::testing::Eq;
|
||||
using ::testing::Ge;
|
||||
using ::testing::Pointee;
|
||||
using ::testing::IsNull;
|
||||
using ::testing::NiceMock;
|
||||
|
||||
typedef std::function<void()> VoidFunc;
|
||||
|
||||
namespace std {
|
||||
inline bool operator ==(const VoidFunc& s, const VoidFunc& v) {
|
||||
// TODO VoidFunc operator ==
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class MockThread {
|
||||
public:
|
||||
std::function<void()> runnnable;
|
||||
|
||||
MockThread(std::function<void()> runnnable) : runnnable(runnnable) { }
|
||||
|
||||
MOCK_METHOD0(start, bool());
|
||||
MOCK_METHOD0(stop, void());
|
||||
MOCK_METHOD1(waitForStart, bool(int timeout_msecs));
|
||||
MOCK_METHOD1(waitForFinish, bool(int timeout_msecs));
|
||||
};
|
||||
|
||||
class MockDeque : public PIBlockingDequeue<VoidFunc> {
|
||||
public:
|
||||
MOCK_METHOD1(offer, bool(const VoidFunc&));
|
||||
MOCK_METHOD0(take, VoidFunc());
|
||||
MOCK_METHOD1(poll, VoidFunc(int));
|
||||
MOCK_METHOD0(capacity, size_t());
|
||||
MOCK_METHOD0(remainingCapacity, size_t());
|
||||
};
|
||||
|
||||
typedef PIThreadPoolExecutorTemplate<NiceMock<MockThread>, MockDeque> PIThreadPoolExecutorMoc_t;
|
||||
|
||||
class PIThreadPoolExecutorMoc : public PIThreadPoolExecutorMoc_t {
|
||||
public:
|
||||
explicit PIThreadPoolExecutorMoc(size_t corePoolSize) : PIThreadPoolExecutorMoc_t(corePoolSize) { }
|
||||
|
||||
template<typename Function>
|
||||
explicit PIThreadPoolExecutorMoc(size_t corePoolSize, Function onBeforeStart) : PIThreadPoolExecutorMoc_t(corePoolSize, onBeforeStart) { }
|
||||
|
||||
PIVector<testing::NiceMock<MockThread>*>* getThreadPool() { return &threadPool; }
|
||||
bool isShutdown() { return isShutdown_; }
|
||||
MockDeque* getTaskQueue() { return &taskQueue; }
|
||||
};
|
||||
|
||||
TEST(ExecutorUnitTest, is_corePool_created) {
|
||||
PIThreadPoolExecutorMoc executor(THREAD_COUNT);
|
||||
ASSERT_EQ(THREAD_COUNT, executor.getThreadPool()->size());
|
||||
}
|
||||
|
||||
TEST(ExecutorUnitTest, is_corePool_started) {
|
||||
PIThreadPoolExecutorMoc executor(THREAD_COUNT, [](MockThread* thread){
|
||||
EXPECT_CALL(*thread, start())
|
||||
.WillOnce(Return(true));
|
||||
});
|
||||
EXPECT_EQ(THREAD_COUNT, executor.getThreadPool()->size());
|
||||
executor.getThreadPool()->forEach([](MockThread* thread){
|
||||
EXPECT_CALL(*thread, stop())
|
||||
.WillOnce(Return());
|
||||
});
|
||||
}
|
||||
|
||||
TEST(ExecutorUnitTest, execute_is_added_to_taskQueue) {
|
||||
VoidFunc voidFunc = [](){};
|
||||
PIThreadPoolExecutorMoc executor(THREAD_COUNT);
|
||||
EXPECT_CALL(*executor.getTaskQueue(), offer(Eq(voidFunc)))
|
||||
.WillOnce(Return(true));
|
||||
executor.execute(voidFunc);
|
||||
}
|
||||
|
||||
TEST(ExecutorUnitTest, is_corePool_execute_queue_elements) {
|
||||
bool is_executed = false;
|
||||
PIThreadPoolExecutorMoc executor(1);
|
||||
EXPECT_EQ(executor.getThreadPool()->size(), 1);
|
||||
EXPECT_CALL(*executor.getTaskQueue(), poll(Ge(0)))
|
||||
.WillOnce(Return([&](){ is_executed = true; }));
|
||||
executor.getThreadPool()->at(0)->runnnable();
|
||||
ASSERT_TRUE(is_executed);
|
||||
}
|
||||
/* FIXME
|
||||
TEST(ExecutorUnitTest, shutdown_is_stop_threads) {
|
||||
PIThreadPoolExecutorMoc executor(THREAD_COUNT);
|
||||
executor.shutdown();
|
||||
}
|
||||
*/
|
||||
@@ -8,9 +8,9 @@
|
||||
* Minimum wait thread start, switch context or another interthread communication action time. Increase it if tests
|
||||
* write "Start thread timeout reach!" message. You can reduce it if you want increase test performance.
|
||||
*/
|
||||
const int WAIT_THREAD_TIME_MS = 40;
|
||||
const int WAIT_THREAD_TIME_MS = 10;
|
||||
|
||||
const int THREAD_COUNT = 5;
|
||||
const int THREAD_COUNT = 2;
|
||||
|
||||
class TestUtil: public PIObject {
|
||||
PIOBJECT(TestUtil)
|
||||
@@ -55,6 +55,7 @@ public:
|
||||
|
||||
return !isTimeout;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif //AWRCANFLASHER_TESTUTIL_H
|
||||
|
||||
Reference in New Issue
Block a user