Commit f4f8269a authored by knowledge4igor's avatar knowledge4igor

Add first reactor test

parent 89ff57be
......@@ -29,6 +29,7 @@ pistache_test(rest_server_test)
pistache_test(string_view_test)
pistache_test(mailbox_test)
pistache_test(stream_test)
pistache_test(reactor_test)
if (PISTACHE_SSL)
......
#include <pistache/reactor.h>
#include <pistache/mailbox.h>
#include "gtest/gtest.h"
#include <memory>
#include <iostream>
#include <thread>
#include <chrono>
#include <unordered_set>
using namespace Pistache;
class TransportMock : public Aio::Handler
{
PROTOTYPE_OF(Aio::Handler, TransportMock)
public:
TransportMock()
: queue_()
{ }
TransportMock(const TransportMock&)
: queue_()
{ }
void onReady(const Aio::FdSet& fds) override
{
for (const auto& entry: fds)
{
if (entry.getTag() == queue_.tag())
{
while(true)
{
auto value = queue_.popSafe();
if (!value)
break;
values_.insert(*value);
}
}
}
}
void registerPoller(Polling::Epoll& poller) override
{
queue_.bind(poller);
}
void push(int value)
{
queue_.push(value);
}
const std::unordered_set<int>& values()
{
return values_;
}
private:
PollableQueue<int> queue_;
std::unordered_set<int> values_;
};
TEST(reactor_test, reactor_creation)
{
const size_t NUM_THREADS = 2;
std::shared_ptr<Aio::Reactor> reactor = Aio::Reactor::create();
reactor->init(Aio::AsyncContext(NUM_THREADS));
auto key = reactor->addHandler(std::make_shared<TransportMock>());
reactor->run();
auto handlers = reactor->handlers(key);
ASSERT_EQ(handlers.size(), NUM_THREADS);
const size_t NUM_VALUES = 4;
int values[NUM_THREADS][NUM_VALUES] = { {1, 2, 3, 4}, {5, 6, 7, 8} };
for(size_t i = 0; i < handlers.size(); ++i)
{
auto transport = std::static_pointer_cast<TransportMock>(handlers[i]);
for (size_t j = 0; j < NUM_VALUES; ++j)
{
transport->push(values[i][j]);
}
}
std::this_thread::sleep_for(std::chrono::seconds(1));
reactor->shutdown();
for(size_t i = 0; i < handlers.size(); ++i)
{
auto transport = std::static_pointer_cast<TransportMock>(handlers[i]);
const auto& resulted_values = transport->values();
for (size_t j = 0; j < NUM_VALUES; ++j)
{
ASSERT_NE(resulted_values.find(values[i][j]), resulted_values.end());
}
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment