PIThreadPoolExecutor & PIBlockingDequeue improvements
- add support move & copy semantic - introduce submit method for executor with future result
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "testutil.h"
|
||||
#include "piblockingdequeue.h"
|
||||
|
||||
class MockConditionVar: public PIConditionVariable {
|
||||
@@ -236,10 +237,10 @@ TEST(BlockingDequeueUnitTest, size_is_eq_to_capacity_when_capacity_reach) {
|
||||
|
||||
TEST(BlockingDequeueUnitTest, drainTo_is_elements_moved) {
|
||||
size_t capacity = 10;
|
||||
PIDeque<int> refDeque;
|
||||
PIBlockingDequeue<int>::QueueType refDeque;
|
||||
for (size_t i = 0; i < capacity / 2; ++i) refDeque.push_back(i * 10);
|
||||
PIBlockingDequeue<int> blockingDequeue(refDeque);
|
||||
PIDeque<int> deque;
|
||||
PIBlockingDequeue<int>::QueueType deque;
|
||||
blockingDequeue.drainTo(deque);
|
||||
ASSERT_EQ(blockingDequeue.size(), 0);
|
||||
ASSERT_TRUE(deque == refDeque);
|
||||
@@ -247,18 +248,18 @@ TEST(BlockingDequeueUnitTest, drainTo_is_elements_moved) {
|
||||
|
||||
TEST(BlockingDequeueUnitTest, drainTo_is_ret_eq_to_size_when_all_moved) {
|
||||
size_t capacity = 10;
|
||||
PIDeque<int> refDeque;
|
||||
PIBlockingDequeue<int>::QueueType refDeque;
|
||||
for (size_t i = 0; i < capacity / 2; ++i) refDeque.push_back(i * 10);
|
||||
PIBlockingDequeue<int> blockingDequeue(refDeque);
|
||||
PIDeque<int> deque;
|
||||
PIBlockingDequeue<int>::QueueType deque;
|
||||
ASSERT_EQ(blockingDequeue.drainTo(deque), refDeque.size());
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, drainTo_is_ret_eq_to_maxCount) {
|
||||
size_t capacity = 10;
|
||||
PIDeque<int> refDeque;
|
||||
PIBlockingDequeue<int>::QueueType refDeque;
|
||||
for (size_t i = 0; i < capacity / 2; ++i) refDeque.push_back(i * 10);
|
||||
PIBlockingDequeue<int> blockingDequeue(refDeque);
|
||||
PIDeque<int> deque;
|
||||
PIBlockingDequeue<int>::QueueType deque;
|
||||
ASSERT_EQ(blockingDequeue.drainTo(deque, refDeque.size() - 1), refDeque.size() - 1);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "pithread.h"
|
||||
#include "testutil.h"
|
||||
|
||||
class ConditionVariable : public ::testing::Test, public TestUtil {
|
||||
class ConditionVariableIntegrationTest : public ::testing::Test, public TestUtil {
|
||||
public:
|
||||
PIMutex m;
|
||||
PIConditionVariable* variable;
|
||||
@@ -19,30 +19,30 @@ protected:
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(ConditionVariable, wait_is_block) {
|
||||
TEST_F(ConditionVariableIntegrationTest, wait_is_block) {
|
||||
createThread();
|
||||
ASSERT_FALSE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_is_block_when_notifyOne_before_wait) {
|
||||
TEST_F(ConditionVariableIntegrationTest, wait_is_block_when_notifyOne_before_wait) {
|
||||
variable->notifyOne();
|
||||
createThread();
|
||||
ASSERT_FALSE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_is_block_when_notifyAll_before_wait) {
|
||||
TEST_F(ConditionVariableIntegrationTest, wait_is_block_when_notifyAll_before_wait) {
|
||||
variable->notifyAll();
|
||||
createThread();
|
||||
ASSERT_FALSE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_is_unblock_when_notifyOne_after_wait) {
|
||||
TEST_F(ConditionVariableIntegrationTest, wait_is_unblock_when_notifyOne_after_wait) {
|
||||
createThread();
|
||||
variable->notifyOne();
|
||||
ASSERT_TRUE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_is_unblock_when_notifyAll_after_wait) {
|
||||
TEST_F(ConditionVariableIntegrationTest, wait_is_unblock_when_notifyAll_after_wait) {
|
||||
PIVector<PIThread*> threads;
|
||||
|
||||
for (int i = 0; i < THREAD_COUNT; ++i) {
|
||||
@@ -61,7 +61,7 @@ TEST_F(ConditionVariable, wait_is_unblock_when_notifyAll_after_wait) {
|
||||
piForeach(PIThread* thread, threads) delete thread;
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_is_one_unblock_when_notifyOne) {
|
||||
TEST_F(ConditionVariableIntegrationTest, wait_is_one_unblock_when_notifyOne) {
|
||||
PIVector<PIThread*> threads;
|
||||
|
||||
for (int i = 0; i < THREAD_COUNT; ++i) {
|
||||
@@ -77,7 +77,7 @@ TEST_F(ConditionVariable, wait_is_one_unblock_when_notifyOne) {
|
||||
ASSERT_EQ(runningThreadCount, THREAD_COUNT - 1);
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_is_protected_unblock_when_notifyOne) {
|
||||
TEST_F(ConditionVariableIntegrationTest, wait_is_protected_unblock_when_notifyOne) {
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
variable->wait(m);
|
||||
@@ -89,7 +89,7 @@ TEST_F(ConditionVariable, wait_is_protected_unblock_when_notifyOne) {
|
||||
ASSERT_FALSE(m.tryLock());
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_condition_is_block) {
|
||||
TEST_F(ConditionVariableIntegrationTest, wait_condition_is_block) {
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
variable->wait(m, [](){ return false; });
|
||||
@@ -98,7 +98,7 @@ TEST_F(ConditionVariable, wait_condition_is_block) {
|
||||
ASSERT_FALSE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_condition_is_check_condition_before_block) {
|
||||
TEST_F(ConditionVariableIntegrationTest, wait_condition_is_check_condition_before_block) {
|
||||
bool isConditionChecked = false;
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
@@ -113,7 +113,7 @@ TEST_F(ConditionVariable, wait_condition_is_check_condition_before_block) {
|
||||
m.unlock();
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_condition_is_check_condition_when_notifyOne) {
|
||||
TEST_F(ConditionVariableIntegrationTest, wait_condition_is_check_condition_when_notifyOne) {
|
||||
bool isConditionChecked;
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
@@ -133,7 +133,7 @@ TEST_F(ConditionVariable, wait_condition_is_check_condition_when_notifyOne) {
|
||||
m.unlock();
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_condition_is_unblock_when_condition_and_notifyOne) {
|
||||
TEST_F(ConditionVariableIntegrationTest, wait_condition_is_unblock_when_condition_and_notifyOne) {
|
||||
bool condition = false;
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
@@ -147,7 +147,7 @@ TEST_F(ConditionVariable, wait_condition_is_unblock_when_condition_and_notifyOne
|
||||
ASSERT_TRUE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, DISABLED_waitFor_is_block_before_timeout) {
|
||||
TEST_F(ConditionVariableIntegrationTest, DISABLED_waitFor_is_block_before_timeout) {
|
||||
createThread([&](){
|
||||
PITimeMeasurer measurer;
|
||||
m.lock();
|
||||
@@ -159,7 +159,7 @@ TEST_F(ConditionVariable, DISABLED_waitFor_is_block_before_timeout) {
|
||||
EXPECT_TRUE(thread->waitForFinish(WAIT_THREAD_TIME_MS * 3));
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, waitFor_is_unblock_when_timeout) {
|
||||
TEST_F(ConditionVariableIntegrationTest, waitFor_is_unblock_when_timeout) {
|
||||
std::atomic_bool isUnblock(false);
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
@@ -172,7 +172,7 @@ TEST_F(ConditionVariable, waitFor_is_unblock_when_timeout) {
|
||||
ASSERT_TRUE(isUnblock);
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, waitFor_is_false_when_timeout) {
|
||||
TEST_F(ConditionVariableIntegrationTest, waitFor_is_false_when_timeout) {
|
||||
bool waitRet = true;
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
@@ -183,7 +183,7 @@ TEST_F(ConditionVariable, waitFor_is_false_when_timeout) {
|
||||
ASSERT_FALSE(waitRet);
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, waitFor_is_unblock_when_condition_and_notifyOne) {
|
||||
TEST_F(ConditionVariableIntegrationTest, waitFor_is_unblock_when_condition_and_notifyOne) {
|
||||
bool condition = false;
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "piexecutor.h"
|
||||
#include "pimutex.h"
|
||||
#include "testutil.h"
|
||||
#include "pimutex.h"
|
||||
#include "piexecutor.h"
|
||||
|
||||
TEST(ExcutorIntegrationTest, execute_is_runnable_invoke) {
|
||||
PIMutex m;
|
||||
|
||||
@@ -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(); });
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
#include "pithread.h"
|
||||
#include "testutil.h"
|
||||
|
||||
class Mutex : public ::testing::Test, public TestUtil {
|
||||
class MutexIntegartionTest : public ::testing::Test, public TestUtil {
|
||||
public:
|
||||
PIMutex* m = new PIMutex();
|
||||
};
|
||||
|
||||
TEST_F(Mutex, lock_is_protect) {
|
||||
TEST_F(MutexIntegartionTest, lock_is_protect) {
|
||||
m->lock();
|
||||
bool* isProtect = new bool(true);
|
||||
|
||||
@@ -20,7 +20,7 @@ TEST_F(Mutex, lock_is_protect) {
|
||||
ASSERT_TRUE(*isProtect);
|
||||
}
|
||||
|
||||
TEST_F(Mutex, unlock_is_release) {
|
||||
TEST_F(MutexIntegartionTest, unlock_is_release) {
|
||||
m->lock();
|
||||
bool* isReleased = new bool(false);
|
||||
m->unlock();
|
||||
@@ -33,7 +33,7 @@ TEST_F(Mutex, unlock_is_release) {
|
||||
ASSERT_TRUE(*isReleased);
|
||||
}
|
||||
|
||||
TEST_F(Mutex, tryLock_is_false_when_locked) {
|
||||
TEST_F(MutexIntegartionTest, tryLock_is_false_when_locked) {
|
||||
createThread([&](){
|
||||
m->lock();
|
||||
piMSleep(WAIT_THREAD_TIME_MS);
|
||||
@@ -41,11 +41,11 @@ TEST_F(Mutex, tryLock_is_false_when_locked) {
|
||||
ASSERT_FALSE(m->tryLock());
|
||||
}
|
||||
|
||||
TEST_F(Mutex, tryLock_is_true_when_unlocked) {
|
||||
TEST_F(MutexIntegartionTest, tryLock_is_true_when_unlocked) {
|
||||
ASSERT_TRUE(m->tryLock());
|
||||
}
|
||||
|
||||
TEST_F(Mutex, tryLock_is_recursive_lock_enable) {
|
||||
TEST_F(MutexIntegartionTest, tryLock_is_recursive_lock_enable) {
|
||||
m->lock();
|
||||
ASSERT_TRUE(m->tryLock());
|
||||
}
|
||||
|
||||
@@ -4,11 +4,19 @@
|
||||
#include "pithread.h"
|
||||
#include <atomic>
|
||||
|
||||
template<typename T>
|
||||
void print_type_info() {
|
||||
std::cout << typeid(T).name() << " is a "
|
||||
<< (std::is_const<typename std::remove_reference<T>::type>::value ? "const " : "")
|
||||
<< (std::is_lvalue_reference<T>::value ? "lvalue" : "rvalue")
|
||||
<< " reference" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = 10;
|
||||
const int WAIT_THREAD_TIME_MS = 30;
|
||||
|
||||
const int THREAD_COUNT = 2;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user