-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* Icinga 2 | (c) 2024 Icinga GmbH | GPLv2+ */ | ||
|
||
#include "base/io-engine.hpp" | ||
#include "base/utility.hpp" | ||
#include <boost/asio.hpp> | ||
#include <boost/date_time/posix_time/posix_time.hpp> | ||
#include <BoostTestTargetConfig.h> | ||
|
||
using namespace icinga; | ||
|
||
BOOST_AUTO_TEST_SUITE(base_io_engine) | ||
|
||
BOOST_AUTO_TEST_CASE(timeout_run) | ||
{ | ||
boost::asio::io_context io; | ||
boost::asio::io_context::strand strand (io); | ||
int called = 0; | ||
|
||
boost::asio::spawn(strand, [&](boost::asio::yield_context yc) { | ||
boost::asio::deadline_timer timer (io); | ||
|
||
Timeout::Ptr timeout = new Timeout(io, strand, boost::posix_time::seconds(3), [&called] { ++called; }); | ||
BOOST_CHECK_EQUAL(called, 0); | ||
|
||
timer.expires_from_now(boost::posix_time::seconds(2)); | ||
timer.async_wait(yc); | ||
BOOST_CHECK_EQUAL(called, 0); | ||
|
||
timer.expires_from_now(boost::posix_time::seconds(2)); | ||
timer.async_wait(yc); | ||
}); | ||
|
||
io.run(); | ||
BOOST_CHECK_EQUAL(called, 1); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(timeout_cancelled) | ||
{ | ||
boost::asio::io_context io; | ||
boost::asio::io_context::strand strand (io); | ||
int called = 0; | ||
|
||
boost::asio::spawn(strand, [&](boost::asio::yield_context yc) { | ||
boost::asio::deadline_timer timer (io); | ||
Timeout::Ptr timeout = new Timeout(io, strand, boost::posix_time::seconds(3), [&called] { ++called; }); | ||
|
||
timer.expires_from_now(boost::posix_time::seconds(2)); | ||
timer.async_wait(yc); | ||
|
||
timeout->Cancel(); | ||
BOOST_CHECK_EQUAL(called, 0); | ||
|
||
timer.expires_from_now(boost::posix_time::seconds(2)); | ||
timer.async_wait(yc); | ||
}); | ||
|
||
io.run(); | ||
BOOST_CHECK_EQUAL(called, 0); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(timeout_scope) | ||
{ | ||
boost::asio::io_context io; | ||
boost::asio::io_context::strand strand (io); | ||
int called = 0; | ||
|
||
boost::asio::spawn(strand, [&](boost::asio::yield_context yc) { | ||
boost::asio::deadline_timer timer (io); | ||
|
||
{ | ||
Timeout::Ptr timeout = new Timeout(io, strand, boost::posix_time::seconds(3), [&called] { ++called; }); | ||
|
||
timer.expires_from_now(boost::posix_time::seconds(2)); | ||
timer.async_wait(yc); | ||
} | ||
|
||
BOOST_CHECK_EQUAL(called, 0); | ||
|
||
timer.expires_from_now(boost::posix_time::seconds(2)); | ||
timer.async_wait(yc); | ||
}); | ||
|
||
io.run(); | ||
BOOST_CHECK_EQUAL(called, 0); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(timeout_due_cancelled) | ||
{ | ||
boost::asio::io_context io; | ||
boost::asio::io_context::strand strand (io); | ||
int called = 0; | ||
|
||
boost::asio::spawn(strand, [&](boost::asio::yield_context yc) { | ||
boost::asio::deadline_timer timer (io); | ||
Timeout::Ptr timeout = new Timeout(io, strand, boost::posix_time::seconds(3), [&called] { ++called; }); | ||
|
||
// Give the timeout enough time to become due while blocking its strand to prevent it from actually running... | ||
Utility::Sleep(4); | ||
|
||
BOOST_CHECK_EQUAL(called, 0); | ||
|
||
// ... so that this shall still work: | ||
timeout->Cancel(); | ||
|
||
BOOST_CHECK_EQUAL(called, 0); | ||
|
||
timer.expires_from_now(boost::posix_time::seconds(1)); | ||
timer.async_wait(yc); | ||
}); | ||
|
||
io.run(); | ||
BOOST_CHECK_EQUAL(called, 0); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |