Merge remote-tracking branch 'origin/master' into concurrent
# Conflicts: # lib/main/thread/pithreadpoolexecutor.cpp # lib/main/thread/pithreadpoolexecutor.h # tests/concurrent/ExecutorIntegrationTest.cpp # tests/concurrent/ExecutorUnitTest.cpp # tests/concurrent/testutil.h
This commit is contained in:
16
tests/CMakeLists.txt
Normal file
16
tests/CMakeLists.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
include(DownloadGTest)
|
||||
|
||||
macro(pip_test NAME LIBS)
|
||||
file(GLOB _CPPS "${NAME}/*.cpp")
|
||||
file(GLOB _HDRS "${NAME}/*.h")
|
||||
set(_target pip_${NAME}_test)
|
||||
add_executable(${_target} ${_CPPS} ${_HDRS})
|
||||
target_link_libraries(${_target} pip ${LIBS} gtest_main gmock_main)
|
||||
add_test(NAME ${_target} COMMAND tests)
|
||||
add_custom_target(${_target}_perform ALL COMMAND ${_target})
|
||||
list(APPEND PIP_TESTS_LIST "${NAME}")
|
||||
set(PIP_TESTS_LIST ${PIP_TESTS_LIST} PARENT_SCOPE)
|
||||
endmacro()
|
||||
|
||||
# Concurrent tests
|
||||
pip_test(concurrent "")
|
||||
16
tests/GTestCMakeLists.txt.in
Normal file
16
tests/GTestCMakeLists.txt.in
Normal file
@@ -0,0 +1,16 @@
|
||||
cmake_minimum_required(VERSION 2.8.2)
|
||||
|
||||
project(googletest-download NONE)
|
||||
|
||||
include(ExternalProject)
|
||||
ExternalProject_Add(googletest
|
||||
GIT_REPOSITORY https://github.com/google/googletest.git
|
||||
GIT_TAG "dea0216d0c6bc5e63cf5f6c8651cd268668032ec"
|
||||
GIT_CONFIG "advice.detachedHead=false"
|
||||
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
|
||||
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_COMMAND ""
|
||||
INSTALL_COMMAND ""
|
||||
TEST_COMMAND ""
|
||||
)
|
||||
264
tests/concurrent/BlockingDequeueUnitTest.cpp
Normal file
264
tests/concurrent/BlockingDequeueUnitTest.cpp
Normal file
@@ -0,0 +1,264 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "piblockingdequeue.h"
|
||||
|
||||
class MockConditionVar: public PIConditionVariable {
|
||||
public:
|
||||
bool isWaitCalled = false;
|
||||
bool isWaitForCalled = false;
|
||||
bool isTrueCondition = false;
|
||||
int timeout = -1;
|
||||
|
||||
void wait(PIMutex& lk) override {
|
||||
isWaitCalled = true;
|
||||
}
|
||||
|
||||
void wait(PIMutex& lk, const std::function<bool()>& condition) override {
|
||||
isWaitCalled = true;
|
||||
lk.lock();
|
||||
isTrueCondition = condition();
|
||||
lk.unlock();
|
||||
}
|
||||
|
||||
bool waitFor(PIMutex& lk, int timeoutMs) override {
|
||||
isWaitForCalled = true;
|
||||
timeout = timeoutMs;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool waitFor(PIMutex& lk, int timeoutMs, const std::function<bool()>& condition) override {
|
||||
isWaitForCalled = true;
|
||||
lk.lock();
|
||||
isTrueCondition = condition();
|
||||
timeout = timeoutMs;
|
||||
lk.unlock();
|
||||
return isTrueCondition;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(BlockingDequeueUnitTest, put_is_block_when_capacity_reach) {
|
||||
size_t capacity = 0;
|
||||
auto conditionVarAdd = new MockConditionVar();
|
||||
auto conditionVarRem = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVarAdd, conditionVarRem);
|
||||
dequeue.put(11);
|
||||
ASSERT_TRUE(conditionVarRem->isWaitCalled);
|
||||
ASSERT_FALSE(conditionVarRem->isTrueCondition);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, offer_timedout_is_false_when_capacity_reach) {
|
||||
size_t capacity = 0;
|
||||
int timeout = 11;
|
||||
auto conditionVarAdd = new MockConditionVar();
|
||||
auto conditionVarRem = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVarAdd, conditionVarRem);
|
||||
ASSERT_FALSE(dequeue.offer(11, timeout));
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, offer_timedout_is_block_when_capacity_reach) {
|
||||
size_t capacity = 0;
|
||||
int timeout = 11;
|
||||
auto conditionVarAdd = new MockConditionVar();
|
||||
auto conditionVarRem = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVarAdd, conditionVarRem);
|
||||
dequeue.offer(11, timeout);
|
||||
EXPECT_TRUE(conditionVarRem->isWaitForCalled);
|
||||
EXPECT_EQ(timeout, conditionVarRem->timeout);
|
||||
ASSERT_FALSE(conditionVarRem->isTrueCondition);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, offer_is_true_before_capacity_reach) {
|
||||
size_t capacity = 1;
|
||||
PIBlockingDequeue<int> dequeue(capacity);
|
||||
ASSERT_TRUE(dequeue.offer(10));
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, offer_is_false_when_capacity_reach) {
|
||||
size_t capacity = 1;
|
||||
PIBlockingDequeue<int> dequeue(capacity);
|
||||
dequeue.offer(11);
|
||||
ASSERT_FALSE(dequeue.offer(10));
|
||||
}
|
||||
|
||||
// TODO change take_is_block_when_empty to prevent segfault
|
||||
TEST(DISABLED_BlockingDequeueUnitTest, take_is_block_when_empty) {
|
||||
size_t capacity = 1;
|
||||
auto conditionVar = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVar);
|
||||
// May cause segfault because take front of empty queue
|
||||
dequeue.take();
|
||||
EXPECT_TRUE(conditionVar->isWaitCalled);
|
||||
ASSERT_FALSE(conditionVar->isTrueCondition);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, take_is_not_block_when_not_empty) {
|
||||
size_t capacity = 1;
|
||||
auto conditionVar = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVar);
|
||||
dequeue.offer(111);
|
||||
dequeue.take();
|
||||
|
||||
EXPECT_TRUE(conditionVar->isWaitCalled);
|
||||
ASSERT_TRUE(conditionVar->isTrueCondition);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, take_is_value_eq_to_offer_value) {
|
||||
size_t capacity = 1;
|
||||
auto conditionVar = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVar);
|
||||
|
||||
dequeue.offer(111);
|
||||
ASSERT_EQ(dequeue.take(), 111);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, take_is_last) {
|
||||
size_t capacity = 10;
|
||||
auto conditionVar = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVar);
|
||||
EXPECT_TRUE(dequeue.offer(111));
|
||||
EXPECT_TRUE(dequeue.offer(222));
|
||||
ASSERT_EQ(dequeue.take(), 111);
|
||||
ASSERT_EQ(dequeue.take(), 222);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, poll_is_not_block_when_empty) {
|
||||
size_t capacity = 1;
|
||||
bool isOk;
|
||||
auto conditionVar = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVar);
|
||||
dequeue.poll(111, &isOk);
|
||||
EXPECT_FALSE(conditionVar->isWaitForCalled);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, poll_is_default_value_when_empty) {
|
||||
size_t capacity = 1;
|
||||
bool isOk;
|
||||
auto conditionVar = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVar);
|
||||
ASSERT_EQ(dequeue.poll(111, &isOk), 111);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, poll_is_offer_value_when_not_empty) {
|
||||
size_t capacity = 1;
|
||||
bool isOk;
|
||||
auto conditionVar = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVar);
|
||||
dequeue.offer(111);
|
||||
ASSERT_EQ(dequeue.poll(-1, &isOk), 111);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, poll_timeouted_is_block_when_empty) {
|
||||
size_t capacity = 1;
|
||||
int timeout = 11;
|
||||
auto conditionVar = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVar);
|
||||
dequeue.poll(timeout, 111);
|
||||
EXPECT_TRUE(conditionVar->isWaitForCalled);
|
||||
EXPECT_EQ(timeout, conditionVar->timeout);
|
||||
ASSERT_FALSE(conditionVar->isTrueCondition);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, poll_timeouted_is_default_value_when_empty) {
|
||||
size_t capacity = 1;
|
||||
int timeout = 11;
|
||||
auto conditionVar = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVar);
|
||||
ASSERT_EQ(dequeue.poll(timeout, 111), 111);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, poll_timeouted_is_not_block_when_not_empty) {
|
||||
size_t capacity = 1;
|
||||
int timeout = 11;
|
||||
auto conditionVar = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVar);
|
||||
dequeue.offer(111);
|
||||
dequeue.poll(timeout, -1);
|
||||
|
||||
EXPECT_TRUE(conditionVar->isWaitForCalled);
|
||||
ASSERT_TRUE(conditionVar->isTrueCondition);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, poll_timeouted_is_offer_value_when_not_empty) {
|
||||
size_t capacity = 1;
|
||||
int timeout = 11;
|
||||
auto conditionVar = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVar);
|
||||
dequeue.offer(111);
|
||||
ASSERT_EQ(dequeue.poll(timeout, -1), 111);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, poll_timeouted_is_last) {
|
||||
size_t capacity = 10;
|
||||
auto conditionVar = new MockConditionVar();
|
||||
PIBlockingDequeue<int> dequeue(capacity, conditionVar);
|
||||
dequeue.offer(111);
|
||||
dequeue.offer(222);
|
||||
ASSERT_EQ(dequeue.poll(10, -1), 111);
|
||||
ASSERT_EQ(dequeue.poll(10, -1), 222);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, capacity_is_eq_constructor_capacity) {
|
||||
size_t capacity = 10;
|
||||
PIBlockingDequeue<int> dequeue(capacity);
|
||||
ASSERT_EQ(dequeue.capacity(), capacity);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, remainingCapacity_is_dif_of_capacity_and_size) {
|
||||
size_t capacity = 2;
|
||||
PIBlockingDequeue<int> dequeue(capacity);
|
||||
ASSERT_EQ(dequeue.remainingCapacity(), capacity);
|
||||
dequeue.offer(111);
|
||||
ASSERT_EQ(dequeue.remainingCapacity(), capacity - 1);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, remainingCapacity_is_zero_when_capacity_reach) {
|
||||
size_t capacity = 1;
|
||||
PIBlockingDequeue<int> dequeue(capacity);
|
||||
dequeue.offer(111);
|
||||
dequeue.offer(111);
|
||||
ASSERT_EQ(dequeue.remainingCapacity(), 0);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, size_is_eq_to_num_of_elements) {
|
||||
size_t capacity = 1;
|
||||
PIBlockingDequeue<int> dequeue(capacity);
|
||||
ASSERT_EQ(dequeue.size(), 0);
|
||||
dequeue.offer(111);
|
||||
ASSERT_EQ(dequeue.size(), 1);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, size_is_eq_to_capacity_when_capacity_reach) {
|
||||
size_t capacity = 1;
|
||||
PIBlockingDequeue<int> dequeue(capacity);
|
||||
dequeue.offer(111);
|
||||
dequeue.offer(111);
|
||||
ASSERT_EQ(dequeue.size(), capacity);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, drainTo_is_elements_moved) {
|
||||
size_t capacity = 10;
|
||||
PIDeque<int> refDeque;
|
||||
for (size_t i = 0; i < capacity / 2; ++i) refDeque.push_back(i * 10);
|
||||
PIBlockingDequeue<int> blockingDequeue(refDeque);
|
||||
PIDeque<int> deque;
|
||||
blockingDequeue.drainTo(deque);
|
||||
ASSERT_EQ(blockingDequeue.size(), 0);
|
||||
ASSERT_TRUE(deque == refDeque);
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, drainTo_is_ret_eq_to_size_when_all_moved) {
|
||||
size_t capacity = 10;
|
||||
PIDeque<int> refDeque;
|
||||
for (size_t i = 0; i < capacity / 2; ++i) refDeque.push_back(i * 10);
|
||||
PIBlockingDequeue<int> blockingDequeue(refDeque);
|
||||
PIDeque<int> deque;
|
||||
ASSERT_EQ(blockingDequeue.drainTo(deque), refDeque.size());
|
||||
}
|
||||
|
||||
TEST(BlockingDequeueUnitTest, drainTo_is_ret_eq_to_maxCount) {
|
||||
size_t capacity = 10;
|
||||
PIDeque<int> refDeque;
|
||||
for (size_t i = 0; i < capacity / 2; ++i) refDeque.push_back(i * 10);
|
||||
PIBlockingDequeue<int> blockingDequeue(refDeque);
|
||||
PIDeque<int> deque;
|
||||
ASSERT_EQ(blockingDequeue.drainTo(deque, refDeque.size() - 1), refDeque.size() - 1);
|
||||
}
|
||||
53
tests/concurrent/ConditionLockIntegrationTest.cpp
Normal file
53
tests/concurrent/ConditionLockIntegrationTest.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "gmock/gmock.h"
|
||||
|
||||
#include "piconditionvar.h"
|
||||
#include "pithread.h"
|
||||
#include "testutil.h"
|
||||
|
||||
class ConditionLock : public ::testing::Test, public TestUtil {
|
||||
public:
|
||||
PIMutex* m = new PIMutex();
|
||||
};
|
||||
|
||||
TEST_F(ConditionLock, lock_is_protect) {
|
||||
m->lock();
|
||||
bool* isProtect = new bool(true);
|
||||
|
||||
createThread([&](){
|
||||
m->lock();
|
||||
*isProtect = false;
|
||||
});
|
||||
EXPECT_FALSE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
|
||||
ASSERT_TRUE(*isProtect);
|
||||
}
|
||||
|
||||
TEST_F(ConditionLock, unlock_is_release) {
|
||||
m->lock();
|
||||
bool* isReleased = new bool(false);
|
||||
m->unlock();
|
||||
|
||||
createThread([&](){
|
||||
m->lock();
|
||||
*isReleased = true;
|
||||
m->unlock();
|
||||
});
|
||||
ASSERT_TRUE(*isReleased);
|
||||
}
|
||||
|
||||
TEST_F(ConditionLock, tryLock_is_false_when_locked) {
|
||||
createThread([&](){
|
||||
m->lock();
|
||||
piMSleep(WAIT_THREAD_TIME_MS);
|
||||
});
|
||||
ASSERT_FALSE(m->tryLock());
|
||||
}
|
||||
|
||||
TEST_F(ConditionLock, tryLock_is_true_when_unlocked) {
|
||||
ASSERT_TRUE(m->tryLock());
|
||||
}
|
||||
|
||||
TEST_F(ConditionLock, tryLock_is_recursive_lock_enable) {
|
||||
m->lock();
|
||||
ASSERT_TRUE(m->tryLock());
|
||||
}
|
||||
200
tests/concurrent/ConditionVariableIntegrationTest.cpp
Normal file
200
tests/concurrent/ConditionVariableIntegrationTest.cpp
Normal file
@@ -0,0 +1,200 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "piconditionvar.h"
|
||||
#include "pithread.h"
|
||||
#include "testutil.h"
|
||||
|
||||
class ConditionVariable : public ::testing::Test, public TestUtil {
|
||||
public:
|
||||
PIMutex m;
|
||||
PIConditionVariable* variable;
|
||||
|
||||
protected:
|
||||
void SetUp() override {
|
||||
variable = new PIConditionVariable();
|
||||
adapterFunctionDefault = [&](){
|
||||
m.lock();
|
||||
variable->wait(m);
|
||||
m.unlock();
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(ConditionVariable, wait_is_block) {
|
||||
createThread();
|
||||
ASSERT_FALSE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, 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) {
|
||||
variable->notifyAll();
|
||||
createThread();
|
||||
ASSERT_FALSE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, 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) {
|
||||
PIVector<PIThread*> threads;
|
||||
|
||||
for (int i = 0; i < THREAD_COUNT; ++i) {
|
||||
threads.push_back(new PIThread([=](){ adapterFunctionDefault(); }));
|
||||
}
|
||||
|
||||
piForeach(PIThread* thread, threads) thread->startOnce();
|
||||
piMSleep(WAIT_THREAD_TIME_MS * THREAD_COUNT);
|
||||
variable->notifyAll();
|
||||
PITimeMeasurer measurer;
|
||||
piForeach(PIThread* thread, threads) {
|
||||
int timeout = WAIT_THREAD_TIME_MS * THREAD_COUNT - (int)measurer.elapsed_m();
|
||||
thread->waitForFinish(timeout > 0 ? timeout : 0);
|
||||
}
|
||||
for (size_t i = 0; i < threads.size(); ++i) EXPECT_FALSE(threads[i]->isRunning()) << "Thread " << i << " still running";
|
||||
piForeach(PIThread* thread, threads) delete thread;
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_is_one_unblock_when_notifyOne) {
|
||||
PIVector<PIThread*> threads;
|
||||
|
||||
for (int i = 0; i < THREAD_COUNT; ++i) {
|
||||
threads.push_back(new PIThread(adapterFunctionDefault));
|
||||
}
|
||||
|
||||
piForeach(PIThread* thread, threads) thread->startOnce();
|
||||
piMSleep(WAIT_THREAD_TIME_MS * THREAD_COUNT);
|
||||
variable->notifyOne();
|
||||
piMSleep(WAIT_THREAD_TIME_MS * THREAD_COUNT);
|
||||
int runningThreadCount = 0;
|
||||
piForeach(PIThread* thread, threads) if (thread->isRunning()) runningThreadCount++;
|
||||
ASSERT_EQ(runningThreadCount, THREAD_COUNT - 1);
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_is_protected_unblock_when_notifyOne) {
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
variable->wait(m);
|
||||
piMSleep(2 * WAIT_THREAD_TIME_MS);
|
||||
// Missing unlock
|
||||
});
|
||||
variable->notifyOne();
|
||||
msleep(WAIT_THREAD_TIME_MS);
|
||||
ASSERT_FALSE(m.tryLock());
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_condition_is_block) {
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
variable->wait(m, [](){ return false; });
|
||||
m.unlock();
|
||||
});
|
||||
ASSERT_FALSE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_condition_is_check_condition_before_block) {
|
||||
bool isConditionChecked = false;
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
variable->wait(m, [&](){
|
||||
isConditionChecked = true;
|
||||
return false;
|
||||
});
|
||||
m.unlock();
|
||||
});
|
||||
m.lock();
|
||||
ASSERT_TRUE(isConditionChecked);
|
||||
m.unlock();
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_condition_is_check_condition_when_notifyOne) {
|
||||
bool isConditionChecked;
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
variable->wait(m, [&](){
|
||||
isConditionChecked = true;
|
||||
return false;
|
||||
});
|
||||
m.unlock();
|
||||
});
|
||||
m.lock();
|
||||
isConditionChecked = false;
|
||||
m.unlock();
|
||||
variable->notifyOne();
|
||||
msleep(threadStartTime + 1);
|
||||
m.lock();
|
||||
ASSERT_TRUE(isConditionChecked);
|
||||
m.unlock();
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_condition_is_unblock_when_condition_and_notifyOne) {
|
||||
bool condition = false;
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
variable->wait(m, [&](){ return condition; });
|
||||
m.unlock();
|
||||
});
|
||||
m.lock();
|
||||
condition = true;
|
||||
m.unlock();
|
||||
variable->notifyOne();
|
||||
ASSERT_TRUE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, DISABLED_waitFor_is_block_before_timeout) {
|
||||
createThread([&](){
|
||||
PITimeMeasurer measurer;
|
||||
m.lock();
|
||||
variable->waitFor(m, WAIT_THREAD_TIME_MS * 2);
|
||||
m.unlock();
|
||||
// Not reliable because spurious wakeup may happen
|
||||
ASSERT_GE(measurer.elapsed_m(), WAIT_THREAD_TIME_MS);
|
||||
});
|
||||
EXPECT_TRUE(thread->waitForFinish(WAIT_THREAD_TIME_MS * 3));
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, waitFor_is_unblock_when_timeout) {
|
||||
std::atomic_bool isUnblock(false);
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
variable->waitFor(m, WAIT_THREAD_TIME_MS);
|
||||
isUnblock = true;
|
||||
m.unlock();
|
||||
});
|
||||
// Test failed if suspend forever
|
||||
EXPECT_TRUE(thread->waitForFinish(2 * WAIT_THREAD_TIME_MS));
|
||||
ASSERT_TRUE(isUnblock);
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, waitFor_is_false_when_timeout) {
|
||||
bool waitRet = true;
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
waitRet = variable->waitFor(m, WAIT_THREAD_TIME_MS);
|
||||
m.unlock();
|
||||
});
|
||||
EXPECT_TRUE(thread->waitForFinish(2 * WAIT_THREAD_TIME_MS));
|
||||
ASSERT_FALSE(waitRet);
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, waitFor_is_unblock_when_condition_and_notifyOne) {
|
||||
bool condition = false;
|
||||
createThread([&](){
|
||||
m.lock();
|
||||
variable->waitFor(m, 3 * WAIT_THREAD_TIME_MS, [&](){ return condition; });
|
||||
m.unlock();
|
||||
});
|
||||
EXPECT_TRUE(thread->isRunning());
|
||||
m.lock();
|
||||
condition = true;
|
||||
m.unlock();
|
||||
variable->notifyOne();
|
||||
msleep(WAIT_THREAD_TIME_MS);
|
||||
ASSERT_FALSE(thread->isRunning());
|
||||
}
|
||||
55
tests/concurrent/ExecutorIntegrationTest.cpp
Normal file
55
tests/concurrent/ExecutorIntegrationTest.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "pithreadpoolexecutor.h"
|
||||
#include "pimutex.h"
|
||||
#include "testutil.h"
|
||||
|
||||
TEST(ExcutorIntegrationTest, execute_is_runnable_invoke) {
|
||||
PIMutex m;
|
||||
int invokedRunnables = 0;
|
||||
PIThreadPoolExecutor executorService(1);
|
||||
executorService.execute([&]() {
|
||||
m.lock();
|
||||
invokedRunnables++;
|
||||
m.unlock();
|
||||
});
|
||||
piMSleep(WAIT_THREAD_TIME_MS);
|
||||
m.lock();
|
||||
ASSERT_EQ(invokedRunnables, 1);
|
||||
m.unlock();
|
||||
}
|
||||
|
||||
TEST(ExcutorIntegrationTest, execute_is_not_execute_after_shutdown) {
|
||||
volatile bool isRunnableInvoke = false;
|
||||
PIThreadPoolExecutor executorService(1);
|
||||
executorService.shutdown();
|
||||
executorService.execute([&]() {
|
||||
isRunnableInvoke = true;
|
||||
});
|
||||
piMSleep(WAIT_THREAD_TIME_MS);
|
||||
ASSERT_FALSE(isRunnableInvoke);
|
||||
}
|
||||
|
||||
TEST(ExcutorIntegrationTest, execute_is_execute_before_shutdown) {
|
||||
volatile bool isRunnableInvoke = false;
|
||||
PIThreadPoolExecutor executorService(1);
|
||||
executorService.execute([&]() {
|
||||
piMSleep(WAIT_THREAD_TIME_MS);
|
||||
isRunnableInvoke = true;
|
||||
});
|
||||
executorService.shutdown();
|
||||
piMSleep(2 * WAIT_THREAD_TIME_MS);
|
||||
ASSERT_TRUE(isRunnableInvoke);
|
||||
}
|
||||
|
||||
TEST(ExcutorIntegrationTest, execute_is_awaitTermination_wait) {
|
||||
PIThreadPoolExecutor executorService(1);
|
||||
executorService.execute([&]() {
|
||||
piMSleep(2 * WAIT_THREAD_TIME_MS);
|
||||
});
|
||||
executorService.shutdown();
|
||||
PITimeMeasurer measurer;
|
||||
ASSERT_TRUE(executorService.awaitTermination(3 * WAIT_THREAD_TIME_MS));
|
||||
double waitTime = measurer.elapsed_m();
|
||||
ASSERT_GE(waitTime, WAIT_THREAD_TIME_MS);
|
||||
ASSERT_LE(waitTime, 4 * WAIT_THREAD_TIME_MS);
|
||||
}
|
||||
102
tests/concurrent/ExecutorUnitTest.cpp
Normal file
102
tests/concurrent/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();
|
||||
}
|
||||
*/
|
||||
60
tests/concurrent/testutil.h
Normal file
60
tests/concurrent/testutil.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#ifndef AWRCANFLASHER_TESTUTIL_H
|
||||
#define AWRCANFLASHER_TESTUTIL_H
|
||||
|
||||
#include "pithread.h"
|
||||
#include <atomic>
|
||||
|
||||
/**
|
||||
* 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 THREAD_COUNT = 2;
|
||||
|
||||
class TestUtil: public PIObject {
|
||||
PIOBJECT(TestUtil)
|
||||
public:
|
||||
double threadStartTime;
|
||||
PIThread* thread = new PIThread();
|
||||
std::atomic_bool isRunning;
|
||||
std::function<void()> adapterFunctionDefault;
|
||||
|
||||
TestUtil() : isRunning(false) {}
|
||||
|
||||
bool createThread(const std::function<void()>& fun = nullptr, PIThread* thread_ = nullptr) {
|
||||
std::function<void()> actualFun = fun == nullptr ? adapterFunctionDefault : fun;
|
||||
if (thread_ == nullptr) thread_ = thread;
|
||||
thread_->startOnce([=](void*){
|
||||
isRunning = true;
|
||||
actualFun();
|
||||
});
|
||||
return waitThread(thread_);
|
||||
}
|
||||
|
||||
bool waitThread(PIThread* thread_, bool runningStatus = true) {
|
||||
PITimeMeasurer measurer;
|
||||
bool isTimeout = !thread_->waitForStart(WAIT_THREAD_TIME_MS);
|
||||
while (!isRunning) {
|
||||
isTimeout = WAIT_THREAD_TIME_MS <= measurer.elapsed_m();
|
||||
if (isTimeout) break;
|
||||
piUSleep(100);
|
||||
}
|
||||
|
||||
threadStartTime = measurer.elapsed_m();
|
||||
|
||||
if (isTimeout) piCout << "Start thread timeout reach!";
|
||||
|
||||
if (threadStartTime > 1) {
|
||||
piCout << "Start time" << threadStartTime << "ms";
|
||||
} else if (threadStartTime > 0.001) {
|
||||
piCout << "Start time" << threadStartTime * 1000 << "mcs";
|
||||
} else {
|
||||
piCout << "Start time" << threadStartTime * 1000 * 1000 << "ns";
|
||||
}
|
||||
|
||||
return !isTimeout;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //AWRCANFLASHER_TESTUTIL_H
|
||||
Reference in New Issue
Block a user