Task #1290
closed
Global io_service and Scheduler
Added by Junxiao Shi over 10 years ago.
Updated over 10 years ago.
Description
Create a global instance of io_service
and Scheduler
.
namespace nfd {
boost::asio::io_service& getGlobalIoService();
namespace scheduler {
struct EventId;
typedef function<void()> Event;
EventId schedule(time::Duration after, Event event);
void cancel(const EventId& eventId);
} // namespace scheduler
using scheduler::EventId; // import to nfd namespace
} // namespace nfd
The global instances can be replaced/cleared/reset during unit testing.
This task is divided to two stages:
Stage 1¶
- Create global
io_service
and Scheduler
.
nfd::Scheduler
class still exists. Its global instance is accessed at nfd::scheduler::getGlobalScheduler()
.
- Use the global
io_service
in main()
.
Forwarder
uses the global Scheduler
.
- Unit tests can have their own
io_service
and Scheduler
, if they don't interact with Forwarder
.
Stage 2¶
- Delete references to
io_service
and Scheduler
in all classes and constructors.
- Change all scheduler calls to use
nfd::scheduler::schedule
and nfd::scheduler::cancel
.
- Delete or move away
nfd::Scheduler
class. Delete nfd::getGlobalScheduler()
function.
- Unit tests also use the global instances, and are provided a way to reset / replace the global
io_service
and Scheduler
.
- Description updated (diff)
- Status changed from New to In Progress
- Start date set to 02/25/2014
- Description updated (diff)
- Status changed from In Progress to Code review
- % Done changed from 0 to 40
- Estimated time changed from 1.50 h to 2.50 h
I think this is a step backwards. Singletons and global variables are usually bad. We should aim at reducing their number, not adding more. The dependency injection pattern should be used instead, because it greatly simplifies unit testability.
I'm facing difficulty in cancelling a scheduled event at StrategyInfo
subclass's destructor.
The alternate to having a global Scheduler
is to have a reference to the Scheduler
instance in every StrategyInfo
subclass instance that contains a scheduled event. This has a memory cost.
- Status changed from Code review to In Progress
- Priority changed from High to Low
- % Done changed from 40 to 50
Stage 1 is merged. Stage 2 will start later.
- Status changed from In Progress to Code review
- % Done changed from 50 to 60
All test cases can have a fresh global io_service now.
- Status changed from Code review to In Progress
- Assignee changed from Junxiao Shi to Alex Afanasyev
- Status changed from In Progress to Closed
- % Done changed from 60 to 100
- Status changed from Closed to Code review
If994e3bd303f0788d8e945351c182bbd4ab5e0b2 deletes deprecated getGlobalScheduler()
public API.
- Status changed from Code review to Closed
Also available in: Atom
PDF