Skip to content
Snippets Groups Projects
Commit 4b60c0fd authored by Leon Dietrich's avatar Leon Dietrich
Browse files

add: timer unit test

parent 482f96ed
No related branches found
No related tags found
1 merge request!5Draft: Resolve "Add a timer class to the networking module"
Pipeline #7612 failed
......@@ -20,8 +20,8 @@ namespace rmrf::scheduling {
inline void timer::restart_timer() {
const auto next_sleep = std_chrono_to_timestamp(this->current_interval);
// According to the documentation this is the least efficient way to do this. But it also the only one working as it seams like ev_timer_set will be only called once by libev
this->evtimer.start(next_sleep);
this->evtimer.stop();
this->evtimer.start(next_sleep, 0.0);
}
void timer::set_active(bool val) {
......@@ -47,8 +47,8 @@ namespace rmrf::scheduling {
this->cb(this->shared_from_this());
}
//this->evtimer.again();
this->restart_timer();
this->evtimer.again();
//this->restart_timer();
}
std::chrono::milliseconds timer::get_remaining_time() {
......
......@@ -16,7 +16,7 @@ namespace rmrf::scheduling {
bool active = false;
std::chrono::milliseconds current_interval;
// We're not using ::ev::periodic as it is not avaiable on all platforms
// We're not using ::ev::periodic as we're aiming for ellapsed time and do not want to be system time dependant
::ev::timer evtimer;
timer_callback cb;
public:
......
#pragma once
#include <chrono>
#include <iostream>
#include <sstream>
#include <memory>
#include <thread>
#include "macros.hpp"
#include "lib/ev/ev.hpp"
#include "mumta/evloop.hpp"
void perform_timout_checks();
static void timeout_cb (::ev::timer& w, int revents) {
MARK_UNUSED(revents);
// Terminate the timer
w.stop();
perform_timout_checks();
// this causes the innermost ev_run to stop iterating
::ev_break (::ev::get_default_loop(), EVBREAK_ONE);
};
void ev_thread_callable() {
BOOST_TEST(check_version_libev());
//rmrf::ev::init_libev();
std::cout << "Configuring timeout." << std::endl;
::ev::timer timeout{};
timeout.set<timeout_cb>(0);
timeout.start(5);
std::cout << "Starting ev loop." << std::endl;
rmrf::ev::loop();
std::cout << "Stopped ev loop." << std::endl;
}
#define BOOST_AUTO_TEST_MAIN
#define BOOST_TEST_MODULE RMRF_TESTS
#include <boost/test/included/unit_test.hpp>
#include "libev_tools.hpp"
#include "net/scheduling/timer.hpp"
using namespace rmrf::scheduling;
unsigned short timer_invocation_counter = 0;
void perform_timout_checks() {
std::cout << "Scheduler test timeout triggered." << std::endl;
}
BOOST_AUTO_TEST_CASE(Timer_Test) {
using namespace std::chrono_literals;
std::thread ev_thread(ev_thread_callable);
std::this_thread::sleep_for(50ms);
auto t = std::make_shared<timer>([](timer::self_ptr_type timer_ptr){
MARK_UNUSED(timer_ptr);
timer_invocation_counter++;
std::cout << "timer cb triggered." << std::endl;
}, 50ms);
std::this_thread::sleep_for(860ms);
ev_thread.join();
BOOST_CHECK_GT(timer_invocation_counter, 1);
}
`pkg-config --cflags --libs libsystemd`
-pthread
-lev
......@@ -9,18 +9,15 @@
#include <thread>
#include <unistd.h>
#include "mumta/evloop.hpp"
#include "net/client_factory.hpp"
#include "net/socketaddress.hpp"
#include "net/sock_address_factory.hpp"
#include "net/tcp_server_socket.hpp"
#include "lib/ev/ev.hpp"
#include "mumta/evloop.hpp"
#include "net/udp_client.hpp"
#include "libev_tools.hpp"
using namespace rmrf::net;
const std::string udp_test_string = "TEST UDP PACKET";
......@@ -30,33 +27,11 @@ volatile bool udp_called = false;
int udp_source_family;
static void timeout_cb (::ev::timer& w, int revents) {
MARK_UNUSED(revents);
// Terminate the timer
w.stop();
void perform_timout_checks() {
if (!tcp_called && !udp_called) {
// Report failure
BOOST_CHECK_MESSAGE(false, "Timeout");
}
// this causes the innermost ev_run to stop iterating
::ev_break (::ev::get_default_loop(), EVBREAK_ONE);
};
void ev_thread_callable() {
BOOST_TEST(check_version_libev());
//rmrf::ev::init_libev();
std::cout << "Configuring timeout." << std::endl;
::ev::timer timeout{};
timeout.set<timeout_cb>(0);
timeout.start(2);
std::cout << "Starting ev loop." << std::endl;
rmrf::ev::loop();
std::cout << "Stopped ev loop." << std::endl;
}
void udp_test_cb(const iorecord& data) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment