Skip to content

Commit 69b5a0f

Browse files
authored
bring boost timers up to date following deadline_timer deprecation (#67)
1 parent df757b9 commit 69b5a0f

File tree

9 files changed

+83
-72
lines changed

9 files changed

+83
-72
lines changed

src/rmq/rmqio/rmqio_asiotimer.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,14 @@
1414
// limitations under the License.
1515

1616
#include <rmqio_asiotimer.h>
17+
18+
namespace {
19+
void instantiateTemplates()
20+
{
21+
// Instantiate the templates to ensure that they compile.
22+
BloombergLP::rmqio::AsioEventLoop eventLoop;
23+
BloombergLP::rmqio::AsioTimerFactory timerFactory(eventLoop);
24+
BloombergLP::rmqio::AsioTimer timer(eventLoop.context(),
25+
BloombergLP::bsls::TimeInterval(1));
26+
}
27+
} // namespace

src/rmq/rmqio/rmqio_asiotimer.h

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ namespace rmqio {
3838
//
3939
// rmqio::AsioTimer: Provides a class for scheduling a cancellable
4040
// callback to be executed after a given timeout
41+
typedef boost::asio::chrono::system_clock DefaultClockType;
4142

42-
template <typename TIME = boost::asio::deadline_timer::time_type,
43-
typename TIME_TRAITS = boost::asio::time_traits<TIME> >
43+
template <typename ClockType = DefaultClockType>
4444
class basic_AsioTimer
4545
: public Timer,
46-
public bsl::enable_shared_from_this<basic_AsioTimer<TIME, TIME_TRAITS> > {
46+
public bsl::enable_shared_from_this<basic_AsioTimer<ClockType> > {
4747
public:
4848
basic_AsioTimer(boost::asio::io_context& context,
4949
const bsls::TimeInterval& timeout);
@@ -60,23 +60,22 @@ class basic_AsioTimer
6060
basic_AsioTimer& operator=(const basic_AsioTimer&) BSLS_KEYWORD_DELETED;
6161

6262
static void
63-
handler_internal(bsl::weak_ptr<basic_AsioTimer<TIME, TIME_TRAITS> > timer,
63+
handler_internal(bsl::weak_ptr<basic_AsioTimer<ClockType> > timer,
6464
const Timer::Callback callback,
6565
const boost::system::error_code& error);
6666
void handler(const Timer::Callback& callback,
6767
const boost::system::error_code& error);
6868
void startTimer();
6969

70-
boost::asio::basic_deadline_timer<TIME, TIME_TRAITS> d_timer;
70+
boost::asio::basic_waitable_timer<ClockType> d_timer;
7171
Timer::Callback d_callback;
7272
bsls::TimeInterval d_timeout;
7373
BALL_LOG_SET_CLASS_CATEGORY("RMQIO.ASIOTIMER");
7474
};
7575

7676
typedef basic_AsioTimer<> AsioTimer;
7777

78-
template <typename TIME = boost::asio::deadline_timer::time_type,
79-
typename TIME_TRAITS = boost::asio::time_traits<TIME> >
78+
template <typename ClockType = DefaultClockType>
8079
class basic_AsioTimerFactory : public TimerFactory {
8180
public:
8281
basic_AsioTimerFactory(rmqio::AsioEventLoop& eventLoop);
@@ -95,33 +94,33 @@ class basic_AsioTimerFactory : public TimerFactory {
9594

9695
typedef basic_AsioTimerFactory<> AsioTimerFactory;
9796

98-
template <typename T, typename TT>
99-
basic_AsioTimer<T, TT>::basic_AsioTimer(boost::asio::io_context& io_context,
100-
const bsls::TimeInterval& timeout)
97+
template <typename ClockType>
98+
basic_AsioTimer<ClockType>::basic_AsioTimer(boost::asio::io_context& io_context,
99+
const bsls::TimeInterval& timeout)
101100
: Timer()
102101
, d_timer(io_context)
103102
, d_callback()
104103
, d_timeout(timeout)
105104
{
106105
}
107106

108-
template <typename T, typename TT>
109-
basic_AsioTimer<T, TT>::basic_AsioTimer(boost::asio::io_context& io_context,
110-
const Timer::Callback& callback)
107+
template <typename ClockType>
108+
basic_AsioTimer<ClockType>::basic_AsioTimer(boost::asio::io_context& io_context,
109+
const Timer::Callback& callback)
111110
: Timer()
112111
, d_timer(io_context)
113112
, d_callback(callback)
114113
, d_timeout()
115114
{
116115
}
117116

118-
template <typename T, typename TT>
119-
basic_AsioTimer<T, TT>::~basic_AsioTimer()
117+
template <typename ClockType>
118+
basic_AsioTimer<ClockType>::~basic_AsioTimer()
120119
{
121120
}
122121

123-
template <typename T, typename TT>
124-
void basic_AsioTimer<T, TT>::reset(const bsls::TimeInterval& timeout)
122+
template <typename ClockType>
123+
void basic_AsioTimer<ClockType>::reset(const bsls::TimeInterval& timeout)
125124
{
126125
if (!d_callback) {
127126
BALL_LOG_ERROR << "reset() called before start()";
@@ -131,42 +130,42 @@ void basic_AsioTimer<T, TT>::reset(const bsls::TimeInterval& timeout)
131130
startTimer();
132131
}
133132

134-
template <typename T, typename TT>
135-
void basic_AsioTimer<T, TT>::cancel()
133+
template <typename ClockType>
134+
void basic_AsioTimer<ClockType>::cancel()
136135
{
137136
d_timer.cancel();
138137
}
139138

140-
template <typename T, typename TT>
141-
void basic_AsioTimer<T, TT>::resetCallback(const Callback& callback)
139+
template <typename ClockType>
140+
void basic_AsioTimer<ClockType>::resetCallback(const Callback& callback)
142141
{
143142
d_callback = callback;
144143
}
145144

146-
template <typename T, typename TT>
147-
void basic_AsioTimer<T, TT>::start(const Timer::Callback& callback)
145+
template <typename ClockType>
146+
void basic_AsioTimer<ClockType>::start(const Timer::Callback& callback)
148147
{
149148
d_callback = callback;
150149
startTimer();
151150
}
152151

153-
template <typename T, typename TT>
154-
void basic_AsioTimer<T, TT>::handler_internal(
155-
bsl::weak_ptr<basic_AsioTimer<T, TT> > timer,
152+
template <typename ClockType>
153+
void basic_AsioTimer<ClockType>::handler_internal(
154+
bsl::weak_ptr<basic_AsioTimer<ClockType> > timer,
156155
const Timer::Callback callback,
157156
const boost::system::error_code& error)
158157
{
159-
bsl::shared_ptr<basic_AsioTimer<T, TT> > t = timer.lock();
158+
bsl::shared_ptr<basic_AsioTimer<ClockType> > t = timer.lock();
160159
if (t) {
161160
t->handler(callback, error);
162161
}
163162
}
164163

165-
template <typename T, typename TT>
166-
void basic_AsioTimer<T, TT>::handler(const Timer::Callback& callback,
167-
const boost::system::error_code& error)
164+
template <typename ClockType>
165+
void basic_AsioTimer<ClockType>::handler(const Timer::Callback& callback,
166+
const boost::system::error_code& error)
168167
{
169-
if (!error && d_timer.expires_at() <= TT::now()) {
168+
if (!error) {
170169
callback(Timer::EXPIRE);
171170
}
172171
else {
@@ -178,38 +177,40 @@ void basic_AsioTimer<T, TT>::handler(const Timer::Callback& callback,
178177
}
179178
}
180179

181-
template <typename T, typename TT>
182-
void basic_AsioTimer<T, TT>::startTimer()
180+
template <typename ClockType>
181+
void basic_AsioTimer<ClockType>::startTimer()
183182
{
184-
d_timer.expires_from_now(
185-
boost::posix_time::milliseconds(d_timeout.totalMilliseconds()));
183+
d_timer.expires_after(
184+
boost::asio::chrono::milliseconds(d_timeout.totalMilliseconds()));
186185
d_timer.async_wait(
187-
bdlf::BindUtil::bind(&basic_AsioTimer<T, TT>::handler_internal,
186+
bdlf::BindUtil::bind(&basic_AsioTimer<ClockType>::handler_internal,
188187
this->weak_from_this(),
189188
d_callback,
190189
bdlf::PlaceHolders::_1));
191190
}
192191

193-
template <typename T, typename TT>
194-
basic_AsioTimerFactory<T, TT>::basic_AsioTimerFactory(
192+
template <typename ClockType>
193+
basic_AsioTimerFactory<ClockType>::basic_AsioTimerFactory(
195194
rmqio::AsioEventLoop& eventLoop)
196195
: d_eventLoop(eventLoop)
197196
{
198197
}
199198

200-
template <typename T, typename TT>
201-
bsl::shared_ptr<rmqio::Timer> basic_AsioTimerFactory<T, TT>::createWithTimeout(
199+
template <typename ClockType>
200+
bsl::shared_ptr<rmqio::Timer>
201+
basic_AsioTimerFactory<ClockType>::createWithTimeout(
202202
const bsls::TimeInterval& timeout)
203203
{
204-
return bsl::make_shared<rmqio::basic_AsioTimer<T, TT> >(
204+
return bsl::make_shared<rmqio::basic_AsioTimer<ClockType> >(
205205
bsl::ref(d_eventLoop.context()), timeout);
206206
}
207207

208-
template <typename T, typename TT>
209-
bsl::shared_ptr<rmqio::Timer> basic_AsioTimerFactory<T, TT>::createWithCallback(
208+
template <typename ClockType>
209+
bsl::shared_ptr<rmqio::Timer>
210+
basic_AsioTimerFactory<ClockType>::createWithCallback(
210211
const Timer::Callback& callback)
211212
{
212-
return bsl::make_shared<rmqio::basic_AsioTimer<T, TT> >(
213+
return bsl::make_shared<rmqio::basic_AsioTimer<ClockType> >(
213214
bsl::ref(d_eventLoop.context()), callback);
214215
}
215216

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
requests
22
pytest
3+
telnetlib3

src/tests/integration/rmqapitests/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import requests
2020
from requests.auth import HTTPBasicAuth
2121
from typing import Any
22-
from telnetlib import Telnet
22+
from telnetlib3 import Telnet
2323
from contextlib import contextmanager
2424
import logging
2525
import re

src/tests/rmqio/rmqio_asiotimer.t.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include <rmqio_asioeventloop.h>
1919

20-
#include <rmqtestutil_timeoverride.h>
20+
#include <rmqtestutil_clockoverride.h>
2121

2222
#include <bdlf_bind.h>
2323
#include <boost/asio.hpp>
@@ -32,9 +32,7 @@ using namespace rmqio;
3232
using namespace ::testing;
3333

3434
namespace {
35-
typedef rmqio::basic_AsioTimer<boost::asio::deadline_timer::time_type,
36-
rmqtestutil::TimeOverride>
37-
FakeAsioTimer;
35+
typedef rmqio::basic_AsioTimer<rmqtestutil::ClockOverride> FakeAsioTimer;
3836
} // namespace
3937

4038
class MockCallback {
@@ -70,7 +68,7 @@ TEST_F(AsioTimerTests, CallbackWhenExpires)
7068
{
7169
EXPECT_CALL(d_mockCallback, callback(Timer::EXPIRE)).Times(1);
7270
d_timer->start(d_callback);
73-
rmqtestutil::TimeOverride::step_time(boost::posix_time::seconds(10));
71+
rmqtestutil::ClockOverride::step_time(boost::asio::chrono::seconds(10));
7472
EXPECT_THAT(d_io.context().run_one(), Eq(1));
7573
}
7674

@@ -91,7 +89,7 @@ TEST_F(AsioTimerTests, Reset)
9189
}
9290
d_timer->start(d_callback);
9391
d_timer->reset(bsls::TimeInterval(10));
94-
rmqtestutil::TimeOverride::step_time(boost::posix_time::seconds(10));
92+
rmqtestutil::ClockOverride::step_time(boost::asio::chrono::seconds(10));
9593
EXPECT_THAT(d_io.context().run_one(), Eq(1));
9694
EXPECT_THAT(d_io.context().run_one(), Eq(1));
9795
}

src/tests/rmqio/rmqio_backofflevelretrystrategy.t.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include <rmqio_backofflevelretrystrategy.h>
1717

1818
#include <rmqtestutil_callcount.h>
19+
#include <rmqtestutil_clockoverride.h>
1920
#include <rmqtestutil_mocktimerfactory.h>
20-
#include <rmqtestutil_timeoverride.h>
2121

2222
#include <bdlf_bind.h>
2323

src/tests/rmqtestutil/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_library(rmqtestutil
22
rmqtestutil.m.cpp
33
rmqtestutil_callcount.cpp
4+
rmqtestutil_clockoverride.cpp
45
rmqtestutil_mockchannel.t.cpp
56
rmqtestutil_mockeventloop.t.cpp
67
rmqtestutil_mockmetricpublisher.cpp
@@ -10,7 +11,6 @@ add_library(rmqtestutil
1011
rmqtestutil_replayframe.cpp
1112
rmqtestutil_savethreadid.cpp
1213
rmqtestutil_timedmetric.cpp
13-
rmqtestutil_timeoverride.cpp
1414
)
1515

1616
target_link_libraries(rmqtestutil PUBLIC

src/tests/rmqtestutil/rmqtestutil_timeoverride.cpp renamed to src/tests/rmqtestutil/rmqtestutil_clockoverride.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
#include <rmqtestutil_timeoverride.h>
16+
#include <rmqtestutil_clockoverride.h>
1717

1818
namespace BloombergLP {
1919
namespace rmqtestutil {
2020

21-
TimeOverride::duration_type TimeOverride::d_timeOffset =
22-
boost::posix_time::seconds(0);
21+
ClockOverride::duration ClockOverride::d_timeOffset =
22+
boost::asio::chrono::seconds(0);
2323

2424
}
2525
} // namespace BloombergLP

src/tests/rmqtestutil/rmqtestutil_timeoverride.h renamed to src/tests/rmqtestutil/rmqtestutil_clockoverride.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,41 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
#ifndef INCLUDED_RMQTESTUTIL_TIMEOVERRIDE
17-
#define INCLUDED_RMQTESTUTIL_TIMEOVERRIDE
16+
#ifndef INCLUDED_RMQTESTUTIL_CLOCKOVERRIDE
17+
#define INCLUDED_RMQTESTUTIL_CLOCKOVERRIDE
1818

1919
#include <boost/asio.hpp>
20+
#include <rmqio_asiotimer.h>
2021

21-
//@PURPOSE: Provides TimeOverride class extended from
22-
// boost::asio::deadline_timer::traits_type
22+
//@PURPOSE: Provides ClockOverride class extended from
23+
// rmqio::DefaultClockType
2324
//
2425
//@CLASSES:
25-
// rmqtestutil::TimeOverride: Steps time in testing to trigger handler waiting
26-
// on deadline_timer
26+
// rmqtestutil::ClockOverride: Steps time in testing to trigger handler waiting
27+
// on timer
2728

2829
namespace BloombergLP {
2930
namespace rmqtestutil {
3031

31-
class TimeOverride : public boost::asio::deadline_timer::traits_type {
32+
class ClockOverride : public rmqio::DefaultClockType {
3233
public:
33-
static time_type now()
34+
static void step_time(duration t) { d_timeOffset += t; }
35+
36+
static time_point now()
3437
{
35-
return add(boost::asio::deadline_timer::traits_type::now(),
36-
d_timeOffset);
38+
return rmqio::DefaultClockType::now() + d_timeOffset;
3739
}
38-
static void step_time(duration_type t) { d_timeOffset += t; }
39-
static boost::posix_time::time_duration to_posix_duration(duration_type d)
40+
static duration to_wait_duration(duration d)
4041
{
4142
// This is the secret sauce to ensure that boost::asio keeps calling
4243
// `now()` and responds to our adjustments via `step_time`
43-
return d < boost::posix_time::milliseconds(1)
44+
return d < boost::asio::chrono::milliseconds(1)
4445
? d
45-
: boost::posix_time::milliseconds(1);
46+
: boost::asio::chrono::milliseconds(1);
4647
}
4748

48-
static duration_type d_timeOffset;
49+
static duration d_timeOffset;
4950
};
50-
5151
} // namespace rmqtestutil
5252
} // namespace BloombergLP
5353

0 commit comments

Comments
 (0)