ddnet/src/test/jobs.cpp

148 lines
2.8 KiB
C++
Raw Normal View History

2017-11-24 09:33:42 +00:00
#include "test.h"
#include <gtest/gtest.h>
#include <base/system.h>
#include <engine/engine.h>
#include <engine/shared/jobs.h>
2017-11-24 09:33:42 +00:00
#include <functional>
static const int TEST_NUM_THREADS = 4;
class Jobs : public ::testing::Test
{
protected:
CJobPool m_Pool;
Jobs()
{
m_Pool.Init(TEST_NUM_THREADS);
}
void Add(std::shared_ptr<IJob> pJob)
{
m_Pool.Add(std::move(pJob));
}
void RunBlocking(IJob *pJob)
{
CJobPool::RunBlocking(pJob);
}
2017-11-24 09:33:42 +00:00
};
class CJob : public IJob
{
std::function<void()> m_JobFunction;
void Run() override { m_JobFunction(); }
2017-11-24 09:33:42 +00:00
public:
CJob(std::function<void()> &&JobFunction) :
m_JobFunction(JobFunction) {}
2017-11-24 09:33:42 +00:00
};
TEST_F(Jobs, Constructor)
{
}
TEST_F(Jobs, Simple)
{
Add(std::make_shared<CJob>([] {}));
}
TEST_F(Jobs, RunBlocking)
{
int Result = 0;
CJob Job([&] { Result = 1; });
EXPECT_EQ(Result, 0);
RunBlocking(&Job);
EXPECT_EQ(Result, 1);
}
2017-11-24 09:33:42 +00:00
TEST_F(Jobs, Wait)
{
SEMAPHORE sphore;
sphore_init(&sphore);
Add(std::make_shared<CJob>([&] { sphore_signal(&sphore); }));
sphore_wait(&sphore);
sphore_destroy(&sphore);
}
TEST_F(Jobs, LookupHost)
{
static const char *HOST = "example.com";
static const int NETTYPE = NETTYPE_ALL;
auto pJob = std::make_shared<CHostLookup>(HOST, NETTYPE);
EXPECT_STREQ(pJob->m_aHostname, HOST);
EXPECT_EQ(pJob->m_Nettype, NETTYPE);
Add(pJob);
while(pJob->Status() != IJob::STATE_DONE)
{
// yay, busy loop...
thread_yield();
}
EXPECT_STREQ(pJob->m_aHostname, HOST);
EXPECT_EQ(pJob->m_Nettype, NETTYPE);
if(pJob->m_Result == 0)
{
EXPECT_EQ(pJob->m_Addr.type & NETTYPE, pJob->m_Addr.type);
}
2017-11-24 09:33:42 +00:00
}
TEST_F(Jobs, LookupHostWebsocket)
{
static const char *HOST = "ws://example.com";
static const int NETTYPE = NETTYPE_ALL;
auto pJob = std::make_shared<CHostLookup>(HOST, NETTYPE);
EXPECT_STREQ(pJob->m_aHostname, HOST);
EXPECT_EQ(pJob->m_Nettype, NETTYPE);
Add(pJob);
while(pJob->Status() != IJob::STATE_DONE)
{
// yay, busy loop...
thread_yield();
}
EXPECT_STREQ(pJob->m_aHostname, HOST);
EXPECT_EQ(pJob->m_Nettype, NETTYPE);
if(pJob->m_Result == 0)
{
EXPECT_EQ(pJob->m_Addr.type & NETTYPE_WEBSOCKET_IPV4, pJob->m_Addr.type);
}
}
2017-11-24 09:33:42 +00:00
TEST_F(Jobs, Many)
{
std::atomic<int> ThreadsRunning(0);
2022-06-15 17:34:41 +00:00
std::vector<std::shared_ptr<IJob>> vpJobs;
2017-11-24 09:33:42 +00:00
SEMAPHORE sphore;
sphore_init(&sphore);
for(int i = 0; i < TEST_NUM_THREADS; i++)
{
std::shared_ptr<IJob> pJob = std::make_shared<CJob>([&] {
2017-11-24 09:33:42 +00:00
int Prev = ThreadsRunning.fetch_add(1);
if(Prev == TEST_NUM_THREADS - 1)
{
sphore_signal(&sphore);
}
});
EXPECT_EQ(pJob->Status(), IJob::STATE_PENDING);
2022-06-15 17:34:41 +00:00
vpJobs.push_back(pJob);
2017-11-24 09:33:42 +00:00
}
2022-06-15 17:34:41 +00:00
for(auto &pJob : vpJobs)
2017-11-24 09:33:42 +00:00
{
Add(pJob);
}
sphore_wait(&sphore);
sphore_destroy(&sphore);
m_Pool.~CJobPool();
2022-06-15 17:34:41 +00:00
for(auto &pJob : vpJobs)
2017-11-24 09:33:42 +00:00
{
EXPECT_EQ(pJob->Status(), IJob::STATE_DONE);
}
new(&m_Pool) CJobPool();
}