mirror of
https://github.com/latentPrion/libspinscale.git
synced 2026-08-12 23:48:22 +00:00
Migrate test timer awaiters from deadline_timer to steady_timer.
Boost 1.90 marks deadline_timer deprecated; keep historical type aliases so existing spinscale tests need no renames. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
#ifndef SPINSCALE_TEST_SUPPORT_TIMER_AWAITERS_H
|
#ifndef SPINSCALE_TEST_SUPPORT_TIMER_AWAITERS_H
|
||||||
#define SPINSCALE_TEST_SUPPORT_TIMER_AWAITERS_H
|
#define SPINSCALE_TEST_SUPPORT_TIMER_AWAITERS_H
|
||||||
|
|
||||||
|
#include <boostAsioLinkageFix.h>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <coroutine>
|
#include <coroutine>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@@ -9,15 +12,17 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
#include <boost/asio/deadline_timer.hpp>
|
|
||||||
#include <boost/asio/error.hpp>
|
#include <boost/asio/error.hpp>
|
||||||
#include <boost/asio/io_context.hpp>
|
#include <boost/asio/io_context.hpp>
|
||||||
#include <boost/date_time/posix_time/posix_time_types.hpp>
|
#include <boost/asio/steady_timer.hpp>
|
||||||
#include <boost/system/error_code.hpp>
|
#include <boost/system/error_code.hpp>
|
||||||
|
|
||||||
namespace sscl::tests {
|
namespace sscl::tests {
|
||||||
|
|
||||||
using SharedDeadlineTimer = std::shared_ptr<boost::asio::deadline_timer>;
|
using SharedSteadyTimer = std::shared_ptr<boost::asio::steady_timer>;
|
||||||
|
|
||||||
|
/* Keep historical names as aliases so existing spinscale tests stay readable. */
|
||||||
|
using SharedDeadlineTimer = SharedSteadyTimer;
|
||||||
|
|
||||||
class CancelableDeadlineTimerRegistry
|
class CancelableDeadlineTimerRegistry
|
||||||
{
|
{
|
||||||
@@ -30,7 +35,7 @@ public:
|
|||||||
|
|
||||||
void registerTimer(
|
void registerTimer(
|
||||||
int labelMilliseconds,
|
int labelMilliseconds,
|
||||||
const SharedDeadlineTimer &timer)
|
const SharedSteadyTimer &timer)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(mutex);
|
std::lock_guard<std::mutex> guard(mutex);
|
||||||
timersByLabel[labelMilliseconds] = timer;
|
timersByLabel[labelMilliseconds] = timer;
|
||||||
@@ -43,15 +48,15 @@ public:
|
|||||||
|
|
||||||
if (iterator == timersByLabel.end()) {
|
if (iterator == timersByLabel.end()) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"No cancelable deadline_timer registered for label "
|
"No cancelable steady_timer registered for label "
|
||||||
+ std::to_string(labelMilliseconds));
|
+ std::to_string(labelMilliseconds));
|
||||||
}
|
}
|
||||||
|
|
||||||
const SharedDeadlineTimer timer = iterator->second.lock();
|
const SharedSteadyTimer timer = iterator->second.lock();
|
||||||
|
|
||||||
if (!timer) {
|
if (!timer) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"Cancelable deadline_timer expired before cancel for label "
|
"Cancelable steady_timer expired before cancel for label "
|
||||||
+ std::to_string(labelMilliseconds));
|
+ std::to_string(labelMilliseconds));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +65,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::mutex mutex;
|
std::mutex mutex;
|
||||||
std::unordered_map<int, std::weak_ptr<boost::asio::deadline_timer>>
|
std::unordered_map<int, std::weak_ptr<boost::asio::steady_timer>>
|
||||||
timersByLabel;
|
timersByLabel;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -69,13 +74,13 @@ struct DeadlineTimerAwaiter
|
|||||||
DeadlineTimerAwaiter(
|
DeadlineTimerAwaiter(
|
||||||
boost::asio::io_context &ioContext,
|
boost::asio::io_context &ioContext,
|
||||||
int delayMilliseconds)
|
int delayMilliseconds)
|
||||||
: timer(std::make_shared<boost::asio::deadline_timer>(ioContext))
|
: timer(std::make_shared<boost::asio::steady_timer>(ioContext))
|
||||||
{
|
{
|
||||||
start(delayMilliseconds);
|
start(delayMilliseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
DeadlineTimerAwaiter(
|
DeadlineTimerAwaiter(
|
||||||
SharedDeadlineTimer sharedTimer,
|
SharedSteadyTimer sharedTimer,
|
||||||
int delayMilliseconds)
|
int delayMilliseconds)
|
||||||
: timer(std::move(sharedTimer))
|
: timer(std::move(sharedTimer))
|
||||||
{
|
{
|
||||||
@@ -97,8 +102,8 @@ struct DeadlineTimerAwaiter
|
|||||||
private:
|
private:
|
||||||
void start(int delayMilliseconds)
|
void start(int delayMilliseconds)
|
||||||
{
|
{
|
||||||
timer->expires_from_now(
|
timer->expires_after(
|
||||||
boost::posix_time::milliseconds(delayMilliseconds));
|
std::chrono::milliseconds(delayMilliseconds));
|
||||||
timer->async_wait(
|
timer->async_wait(
|
||||||
[this](const boost::system::error_code &errorCode)
|
[this](const boost::system::error_code &errorCode)
|
||||||
{
|
{
|
||||||
@@ -110,7 +115,7 @@ private:
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedDeadlineTimer timer;
|
SharedSteadyTimer timer;
|
||||||
boost::system::error_code completionErrorCode;
|
boost::system::error_code completionErrorCode;
|
||||||
bool waitCompleted = false;
|
bool waitCompleted = false;
|
||||||
std::coroutine_handle<> resumeHandle;
|
std::coroutine_handle<> resumeHandle;
|
||||||
@@ -123,7 +128,7 @@ struct RegisteredDeadlineTimerAwaiter
|
|||||||
int delayMilliseconds,
|
int delayMilliseconds,
|
||||||
int registrationLabelMilliseconds,
|
int registrationLabelMilliseconds,
|
||||||
CancelableDeadlineTimerRegistry ®istry)
|
CancelableDeadlineTimerRegistry ®istry)
|
||||||
: timer(std::make_shared<boost::asio::deadline_timer>(ioContext))
|
: timer(std::make_shared<boost::asio::steady_timer>(ioContext))
|
||||||
{
|
{
|
||||||
registry.registerTimer(registrationLabelMilliseconds, timer);
|
registry.registerTimer(registrationLabelMilliseconds, timer);
|
||||||
waiter.emplace(timer, delayMilliseconds);
|
waiter.emplace(timer, delayMilliseconds);
|
||||||
@@ -138,7 +143,7 @@ struct RegisteredDeadlineTimerAwaiter
|
|||||||
boost::system::error_code await_resume() const noexcept
|
boost::system::error_code await_resume() const noexcept
|
||||||
{ return waiter->await_resume(); }
|
{ return waiter->await_resume(); }
|
||||||
|
|
||||||
SharedDeadlineTimer timer;
|
SharedSteadyTimer timer;
|
||||||
std::optional<DeadlineTimerAwaiter> waiter;
|
std::optional<DeadlineTimerAwaiter> waiter;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -147,7 +152,7 @@ inline void throwIfTimerWaitFailed(
|
|||||||
{
|
{
|
||||||
if (waitError) {
|
if (waitError) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"deadline_timer wait failed: " + waitError.message());
|
"steady_timer wait failed: " + waitError.message());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user