1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-07-29 08:21:15 +03:00

MCOL-5044 This patch simplifies addJob interfaces removing extra bool that control mutex locking,

adds additional nullptr dereference check in removeJobs and fixes FairThreadPool
hashmap iter invalidation issues
This commit is contained in:
Roman Nozdrin
2022-07-04 09:18:26 +00:00
committed by Roman Nozdrin
parent 4d41a945db
commit 0907ca414f
4 changed files with 69 additions and 47 deletions

View File

@ -28,62 +28,64 @@ using namespace threadpool;
using ResultsType = std::vector<int>;
static ResultsType results;
class FairThreadPoolTest : public testing::Test {
public:
class FairThreadPoolTest : public testing::Test
{
public:
void SetUp() override
{
results.clear();
threadPool = new FairThreadPool(1, 1, 0, 0);
}
FairThreadPool* threadPool;
};
class TestFunctor: public FairThreadPool::Functor
class TestFunctor : public FairThreadPool::Functor
{
public:
TestFunctor(const size_t id, const size_t delay): id_(id), delay_(delay)
TestFunctor(const size_t id, const size_t delay) : id_(id), delay_(delay)
{
}
~TestFunctor() {};
~TestFunctor(){};
int operator()() override
{
usleep(delay_);
results.push_back(id_);
return 0;
}
private:
size_t id_;
size_t delay_;
};
class TestRescheduleFunctor: public FairThreadPool::Functor
class TestRescheduleFunctor : public FairThreadPool::Functor
{
public:
TestRescheduleFunctor(const size_t id, const size_t delay): id_(id), delay_(delay)
TestRescheduleFunctor(const size_t id, const size_t delay) : id_(id), delay_(delay)
{
}
~TestRescheduleFunctor() {};
~TestRescheduleFunctor(){};
int operator()() override
{
if (firstRun)
{
firstRun = false;
return 1; // re-schedule the Job
return 1; // re-schedule the Job
}
usleep(delay_);
results.push_back(id_);
return 0;
}
private:
size_t id_;
size_t delay_;
bool firstRun = true;
};
testing::AssertionResult isThisOrThat(const ResultsType& arr, const size_t idxA, const int a, const size_t idxB, const int b)
testing::AssertionResult isThisOrThat(const ResultsType& arr, const size_t idxA, const int a,
const size_t idxB, const int b)
{
if (arr.empty() || arr.size() <= max(idxA, idxB))
return testing::AssertionFailure() << "The supplied vector is either empty or not big enough.";
@ -91,8 +93,8 @@ testing::AssertionResult isThisOrThat(const ResultsType& arr, const size_t idxA,
return testing::AssertionSuccess();
if (arr[idxA] == b && arr[idxB] == a)
return testing::AssertionSuccess();
return testing::AssertionFailure() << "The values at positions "<< idxA << " " << idxB
<< " are not " << a << " and " << b << std::endl;
return testing::AssertionFailure() << "The values at positions " << idxA << " " << idxB << " are not " << a
<< " and " << b << std::endl;
}
TEST_F(FairThreadPoolTest, FairThreadPoolAdd)
@ -147,6 +149,33 @@ TEST_F(FairThreadPoolTest, FairThreadPoolRemove)
EXPECT_EQ(results[1], 3);
}
TEST_F(FairThreadPoolTest, FairThreadPoolCleanUp)
{
SP_UM_IOSOCK sock(new messageqcpp::IOSocket);
auto functor1 = boost::shared_ptr<FairThreadPool::Functor>(new TestFunctor(1, 100000));
FairThreadPool::Job job1(1, 1, 1, functor1, sock, 1, 0, 1);
auto functor2 = boost::shared_ptr<FairThreadPool::Functor>(new TestFunctor(2, 50000));
FairThreadPool::Job job2(2, 1, 1, functor2, sock, 1, 0, 2);
auto functor3 = boost::shared_ptr<FairThreadPool::Functor>(new TestFunctor(3, 50000));
FairThreadPool::Job job3(3, 1, 2, functor3, sock, 1, 0, 3);
threadPool->addJob(job1);
threadPool->removeJobs(job1.id_);
threadPool->addJob(job2);
threadPool->removeJobs(job2.id_);
threadPool->addJob(job3);
threadPool->removeJobs(job3.id_);
threadPool->removeJobs(job1.id_);
threadPool->removeJobs(job1.id_);
while (threadPool->queueSize())
{
usleep(250000);
}
EXPECT_EQ(threadPool->queueSize(), 0ULL);
}
TEST_F(FairThreadPoolTest, FairThreadPoolReschedule)
{
SP_UM_IOSOCK sock(new messageqcpp::IOSocket);