code format
This commit is contained in:
@@ -1,20 +1,21 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "piconditionvar.h"
|
||||
#include "pithread.h"
|
||||
#include "testutil.h"
|
||||
|
||||
class ConditionVariable : public ::testing::Test, public TestUtil {
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
class ConditionVariable
|
||||
: public ::testing::Test
|
||||
, public TestUtil {
|
||||
public:
|
||||
~ConditionVariable() {
|
||||
delete variable;
|
||||
}
|
||||
~ConditionVariable() { delete variable; }
|
||||
PIMutex m;
|
||||
PIConditionVariable* variable;
|
||||
PIConditionVariable * variable;
|
||||
|
||||
protected:
|
||||
void SetUp() override {
|
||||
variable = new PIConditionVariable();
|
||||
adapterFunctionDefault = [&](){
|
||||
variable = new PIConditionVariable();
|
||||
adapterFunctionDefault = [&]() {
|
||||
m.lock();
|
||||
variable->wait(m);
|
||||
m.unlock();
|
||||
@@ -46,42 +47,47 @@ TEST_F(ConditionVariable, wait_is_unblock_when_notifyOne_after_wait) {
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_is_unblock_when_notifyAll_after_wait) {
|
||||
PIVector<PIThread*> threads;
|
||||
PIVector<PIThread *> threads;
|
||||
|
||||
for (int i = 0; i < THREAD_COUNT; ++i) {
|
||||
threads.push_back(new PIThread([=](){ adapterFunctionDefault(); }));
|
||||
threads.push_back(new PIThread([=]() { adapterFunctionDefault(); }));
|
||||
}
|
||||
|
||||
piForeach(PIThread* thread, threads) thread->startOnce();
|
||||
piForeach(PIThread * thread, threads)
|
||||
thread->startOnce();
|
||||
piMSleep(WAIT_THREAD_TIME_MS * THREAD_COUNT);
|
||||
variable->notifyAll();
|
||||
PITimeMeasurer measurer;
|
||||
piForeach(PIThread* thread, threads) {
|
||||
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;
|
||||
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;
|
||||
PIVector<PIThread *> threads;
|
||||
|
||||
for (int i = 0; i < THREAD_COUNT; ++i) {
|
||||
threads.push_back(new PIThread(adapterFunctionDefault));
|
||||
}
|
||||
|
||||
piForeach(PIThread* thread, threads) thread->startOnce();
|
||||
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++;
|
||||
piForeach(PIThread * thread, threads)
|
||||
if (thread->isRunning()) runningThreadCount++;
|
||||
ASSERT_EQ(runningThreadCount, THREAD_COUNT - 1);
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_is_protected_unblock_when_notifyOne) {
|
||||
createThread([&](){
|
||||
createThread([&]() {
|
||||
m.lock();
|
||||
variable->wait(m);
|
||||
piMSleep(2 * WAIT_THREAD_TIME_MS);
|
||||
@@ -93,9 +99,9 @@ TEST_F(ConditionVariable, wait_is_protected_unblock_when_notifyOne) {
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, wait_condition_is_block) {
|
||||
createThread([&](){
|
||||
createThread([&]() {
|
||||
m.lock();
|
||||
variable->wait(m, [](){ return false; });
|
||||
variable->wait(m, []() { return false; });
|
||||
m.unlock();
|
||||
});
|
||||
ASSERT_FALSE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
|
||||
@@ -103,9 +109,9 @@ TEST_F(ConditionVariable, wait_condition_is_block) {
|
||||
|
||||
TEST_F(ConditionVariable, wait_condition_is_check_condition_before_block) {
|
||||
bool isConditionChecked = false;
|
||||
createThread([&](){
|
||||
createThread([&]() {
|
||||
m.lock();
|
||||
variable->wait(m, [&](){
|
||||
variable->wait(m, [&]() {
|
||||
isConditionChecked = true;
|
||||
return false;
|
||||
});
|
||||
@@ -118,9 +124,9 @@ TEST_F(ConditionVariable, wait_condition_is_check_condition_before_block) {
|
||||
|
||||
TEST_F(ConditionVariable, wait_condition_is_check_condition_when_notifyOne) {
|
||||
bool isConditionChecked;
|
||||
createThread([&](){
|
||||
createThread([&]() {
|
||||
m.lock();
|
||||
variable->wait(m, [&](){
|
||||
variable->wait(m, [&]() {
|
||||
isConditionChecked = true;
|
||||
return false;
|
||||
});
|
||||
@@ -138,9 +144,9 @@ TEST_F(ConditionVariable, wait_condition_is_check_condition_when_notifyOne) {
|
||||
|
||||
TEST_F(ConditionVariable, wait_condition_is_unblock_when_condition_and_notifyOne) {
|
||||
bool condition = false;
|
||||
createThread([&](){
|
||||
createThread([&]() {
|
||||
m.lock();
|
||||
variable->wait(m, [&](){ return condition; });
|
||||
variable->wait(m, [&]() { return condition; });
|
||||
m.unlock();
|
||||
});
|
||||
m.lock();
|
||||
@@ -151,7 +157,7 @@ TEST_F(ConditionVariable, wait_condition_is_unblock_when_condition_and_notifyOne
|
||||
}
|
||||
|
||||
TEST_F(ConditionVariable, DISABLED_waitFor_is_block_before_timeout) {
|
||||
createThread([&](){
|
||||
createThread([&]() {
|
||||
PITimeMeasurer measurer;
|
||||
m.lock();
|
||||
variable->waitFor(m, WAIT_THREAD_TIME_MS * 2);
|
||||
@@ -164,7 +170,7 @@ TEST_F(ConditionVariable, DISABLED_waitFor_is_block_before_timeout) {
|
||||
|
||||
TEST_F(ConditionVariable, waitFor_is_unblock_when_timeout) {
|
||||
std::atomic_bool isUnblock(false);
|
||||
createThread([&](){
|
||||
createThread([&]() {
|
||||
m.lock();
|
||||
variable->waitFor(m, WAIT_THREAD_TIME_MS);
|
||||
isUnblock = true;
|
||||
@@ -177,7 +183,7 @@ TEST_F(ConditionVariable, waitFor_is_unblock_when_timeout) {
|
||||
|
||||
TEST_F(ConditionVariable, waitFor_is_false_when_timeout) {
|
||||
bool waitRet = true;
|
||||
createThread([&](){
|
||||
createThread([&]() {
|
||||
m.lock();
|
||||
waitRet = variable->waitFor(m, WAIT_THREAD_TIME_MS);
|
||||
m.unlock();
|
||||
@@ -188,9 +194,9 @@ TEST_F(ConditionVariable, waitFor_is_false_when_timeout) {
|
||||
|
||||
TEST_F(ConditionVariable, waitFor_is_unblock_when_condition_and_notifyOne) {
|
||||
bool condition = false;
|
||||
createThread([&](){
|
||||
createThread([&]() {
|
||||
m.lock();
|
||||
variable->waitFor(m, 3 * WAIT_THREAD_TIME_MS, [&](){ return condition; });
|
||||
variable->waitFor(m, 3 * WAIT_THREAD_TIME_MS, [&]() { return condition; });
|
||||
m.unlock();
|
||||
});
|
||||
EXPECT_TRUE(thread->isRunning());
|
||||
|
||||
Reference in New Issue
Block a user