PIThreadPoolExecutor & PIBlockingDequeue improvements
- add support move & copy semantic - introduce submit method for executor with future result
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "piexecutor.h"
|
||||
#include "testutil.h"
|
||||
#include "piexecutor.h"
|
||||
|
||||
using ::testing::_;
|
||||
using ::testing::SetArgReferee;
|
||||
using ::testing::DoAll;
|
||||
using ::testing::DeleteArg;
|
||||
using ::testing::Return;
|
||||
using ::testing::ByMove;
|
||||
using ::testing::AtLeast;
|
||||
using ::testing::ByRef;
|
||||
using ::testing::Eq;
|
||||
@@ -27,9 +28,9 @@ namespace std {
|
||||
|
||||
class MockThread {
|
||||
public:
|
||||
std::function<void()> runnnable;
|
||||
VoidFunc runnnable;
|
||||
|
||||
MockThread(std::function<void()> runnnable) : runnnable(runnnable) { }
|
||||
MockThread(VoidFunc runnnable) : runnnable(runnnable) { }
|
||||
|
||||
MOCK_METHOD0(start, bool());
|
||||
MOCK_METHOD0(stop, void());
|
||||
@@ -37,11 +38,12 @@ public:
|
||||
MOCK_METHOD1(waitForFinish, bool(int timeout_msecs));
|
||||
};
|
||||
|
||||
class MockDeque : public PIBlockingDequeue<VoidFunc> {
|
||||
template<typename F>
|
||||
class MockDeque : public PIBlockingDequeue<F> {
|
||||
public:
|
||||
MOCK_METHOD1(offer, bool(const VoidFunc&));
|
||||
MOCK_METHOD0(take, VoidFunc());
|
||||
MOCK_METHOD1(poll, VoidFunc(int));
|
||||
MOCK_METHOD1(offer, bool(const FunctionWrapper&));
|
||||
MOCK_METHOD0(take, FunctionWrapper());
|
||||
MOCK_METHOD1(poll, FunctionWrapper(int));
|
||||
MOCK_METHOD0(capacity, size_t());
|
||||
MOCK_METHOD0(remainingCapacity, size_t());
|
||||
};
|
||||
@@ -57,7 +59,7 @@ public:
|
||||
|
||||
PIVector<testing::NiceMock<MockThread>*>* getThreadPool() { return &threadPool; }
|
||||
bool isShutdown() { return isShutdown_; }
|
||||
MockDeque* getTaskQueue() { return &taskQueue; }
|
||||
MockDeque<FunctionWrapper>* getTaskQueue() { return &taskQueue; }
|
||||
};
|
||||
|
||||
TEST(ExecutorUnitTest, is_corePool_created) {
|
||||
@@ -73,10 +75,30 @@ TEST(ExecutorUnitTest, is_corePool_started) {
|
||||
EXPECT_EQ(THREAD_COUNT, executor.getThreadPool()->size());
|
||||
}
|
||||
|
||||
TEST(ExecutorUnitTest, submit_is_added_to_taskQueue) {
|
||||
VoidFunc voidFunc = [](){};
|
||||
PIThreadPoolExecutorMoc executor(THREAD_COUNT);
|
||||
// TODO add check of offered
|
||||
EXPECT_CALL(*executor.getTaskQueue(), offer)
|
||||
.WillOnce(Return(true));
|
||||
executor.submit(voidFunc);
|
||||
}
|
||||
|
||||
TEST(ExecutorUnitTest, submit_is_return_valid_future) {
|
||||
VoidFunc voidFunc = [](){};
|
||||
PIThreadPoolExecutorMoc executor(THREAD_COUNT);
|
||||
// TODO add check of offered
|
||||
EXPECT_CALL(*executor.getTaskQueue(), offer)
|
||||
.WillOnce(Return(true));
|
||||
auto future = executor.submit(voidFunc);
|
||||
EXPECT_TRUE(future.valid());
|
||||
}
|
||||
|
||||
TEST(ExecutorUnitTest, execute_is_added_to_taskQueue) {
|
||||
VoidFunc voidFunc = [](){};
|
||||
PIThreadPoolExecutorMoc executor(THREAD_COUNT);
|
||||
EXPECT_CALL(*executor.getTaskQueue(), offer(Eq(voidFunc)))
|
||||
// TODO add check of offered
|
||||
EXPECT_CALL(*executor.getTaskQueue(), offer)
|
||||
.WillOnce(Return(true));
|
||||
executor.execute(voidFunc);
|
||||
}
|
||||
@@ -85,8 +107,10 @@ 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; }));
|
||||
EXPECT_CALL(*executor.getTaskQueue(), poll(Ge(0)))
|
||||
.WillOnce([&is_executed](int){
|
||||
return FunctionWrapper([&is_executed](){ is_executed = true; });
|
||||
});
|
||||
executor.getThreadPool()->at(0)->runnnable();
|
||||
ASSERT_TRUE(is_executed);
|
||||
}
|
||||
@@ -102,7 +126,7 @@ TEST(ExecutorUnitTest, shutdown_is_stop_threads) {
|
||||
testing::Mock::AllowLeak(executor->getTaskQueue());
|
||||
|
||||
EXPECT_CALL(*executor->getTaskQueue(), poll(Ge(0)))
|
||||
.WillRepeatedly(Return(std::function<VoidFunc()>()));
|
||||
.WillRepeatedly([](int){ return FunctionWrapper(); });
|
||||
executor->shutdown();
|
||||
executor->getThreadPool()->forEach([](MockThread* thread){ thread->runnnable(); });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user