Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9165505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:55:35+00:00 2026-06-17T14:55:35+00:00

If possible, how do you mock the time for the purpose of triggering boost

  • 0

If possible, how do you mock the time for the purpose of triggering boost timers in a unit test?

For example, is it possible to achieve something like the following:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

void print(const boost::system::error_code& /*e*/)
{
  std::cout << "Hello, world!\n";
}

int main()
{
    boost::asio::io_service io;        // Possibly another class needed here, or a way of setting the clock to be fake

    boost::asio::deadline_timer t(io, boost::posix_time::hours(24));
    t.async_wait(&print);

    io.poll();  // Nothing should happen - no handlers ready

    // PSEUDO-CODE below of what I'd like to happen, jump ahead 24 hours
    io.set_time(io.get_time() + boost::posix_time::hours(24));

    io.poll();  // The timer should go off

    return 0;
}

Update Thank you to all the answers, they have provided excellent insight into the problem. I have provided my own answer (SSCCE), but couldn’t have done that without the help provided.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:55:36+00:00Added an answer on June 17, 2026 at 2:55 pm

    A SSCCE, based on a link posted by @free_coffee:

    #include <boost/asio.hpp>
    #include <boost/optional.hpp>
    
    class mock_time_traits
    {       
        typedef boost::asio::deadline_timer::traits_type  source_traits;
    
    public:
    
        typedef source_traits::time_type time_type;
        typedef source_traits::duration_type duration_type;
    
        // Note this implemenation requires set_now(...) to be called before now()
        static time_type now() { return *now_; }
    
        // After modifying the clock, we need to sleep the thread to give the io_service
        // the opportunity to poll and notice the change in clock time
        static void set_now(time_type t) 
        { 
            now_ = t; 
            boost::this_thread::sleep_for(boost::chrono::milliseconds(2)); 
        }
    
        static time_type add(time_type t, duration_type d) { return source_traits::add(t, d); }
        static duration_type subtract(time_type t1, time_type t2) { return source_traits::subtract(t1, t2); }
        static bool less_than(time_type t1, time_type t2) { return source_traits::less_than(t1, t2); }
    
        // This function is called by asio to determine how often to check 
        // if the timer is ready to fire. By manipulating this function, we
        // can make sure asio detects changes to now_ in a timely fashion.
        static boost::posix_time::time_duration to_posix_duration(duration_type d) 
        { 
            return d < boost::posix_time::milliseconds(1) ? d : boost::posix_time::milliseconds(1);
        }
    
    private:
    
        static boost::optional<time_type> now_;
    };
    
    boost::optional<mock_time_traits::time_type> mock_time_traits::now_;
    
    
    
    typedef boost::asio::basic_deadline_timer<
                boost::posix_time::ptime, mock_time_traits> mock_deadline_timer;
    
    void handler(const boost::system::error_code &ec)
    {
        std::cout << "Handler!" << std::endl;
    }
    
    
    int main()
    {
        mock_time_traits::set_now(boost::posix_time::time_from_string("2013-01-20 1:44:01.000"));
    
        boost::asio::io_service io_service;
        mock_deadline_timer timer(io_service, boost::posix_time::seconds(5));
        timer.async_wait(handler);
    
        std::cout << "Poll 1" << std::endl;
        io_service.poll();
    
        mock_time_traits::set_now(mock_time_traits::now() + boost::posix_time::seconds(6));
    
    
        std::cout << "Poll 2" << std::endl;
        io_service.poll();
    
        std::cout << "Poll 3" << std::endl;
        io_service.poll();
    
        return 0;
    }
    
    // Output
    Poll 1
    Poll 2
    Handler!
    Poll 3
    

    Thankyou to @free_coffee for providing this link to a blog entry from the creator of boost asio. The above is slightly modified (and I believe slightly improved). By not using an offset on the system clock, you gain complete control over the timers: they will not fire until you explicitly set time forward past the timer.

    The solution could be improved by making the this_thread::sleep part configurable. Note that the to_posix_duration hack described in [1] needs to use a smaller duration than the sleep.

    To me this approach still seems a bit magic, since the time_traits are not well documented, and in particular the hack of to_posix_duration has a whiff of voodoo about it. I guess it just comes down to intimate knowledge of the deadline_timer implementation (which I don’t have).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to mock a class object using Mockito and/or PowerMockito? Something like:
Is it possible to test for the following example if the Method1 called 1st,
Is it possible to mock out File calls with rhino mock example: private ServerConnection
Is it possible to determine if an EasyMock mock is on replay mode? Something
Is it possible to do some form of expect NEW in rhino mock. Example:
Possible Duplicate: What is the purpose of mock objects? I'm preparing a presentation and
I am writing unit test for an existing code which is like this class
Is it possible to write code like the following. I'm trying to using Moq
Is it possible to Mock a single method of a Java class? For example:
Is it possible to mock an EF model so that I can test code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.