2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-27 18:42:07 +00:00
Files
asio/test/deadline_timer.cpp
Christopher Kohlhoff 68a7cd26a6 Merge asio from trunk.
------------------------------------------------------------------------
r85735 | chris_kohlhoff | 2013-09-18 07:31:55 +1000 (Wed, 18 Sep 2013) | 1 line

Remove dependency on Boost.Preprocessor library.
------------------------------------------------------------------------
r85736 | chris_kohlhoff | 2013-09-18 07:32:48 +1000 (Wed, 18 Sep 2013) | 1 line

Fix error in async_receive_from example.
------------------------------------------------------------------------
r85737 | chris_kohlhoff | 2013-09-18 07:33:39 +1000 (Wed, 18 Sep 2013) | 1 line

Remove unused variable assignment.
------------------------------------------------------------------------
r85738 | chris_kohlhoff | 2013-09-18 07:35:43 +1000 (Wed, 18 Sep 2013) | 3 lines

Fix a regression where, on some platforms, errors from async_connect are not
correctly propagated through to the completion handler.

------------------------------------------------------------------------
r85739 | chris_kohlhoff | 2013-09-18 07:36:54 +1000 (Wed, 18 Sep 2013) | 1 line

Remove unused parameters and member variables.
------------------------------------------------------------------------
r85740 | chris_kohlhoff | 2013-09-18 07:38:41 +1000 (Wed, 18 Sep 2013) | 1 line

Fix nfds argument to select.
------------------------------------------------------------------------
r85741 | chris_kohlhoff | 2013-09-18 07:39:38 +1000 (Wed, 18 Sep 2013) | 1 line

Fix a regression on Windows where multiple threads are running an io_service.
------------------------------------------------------------------------
r85742 | chris_kohlhoff | 2013-09-18 07:40:19 +1000 (Wed, 18 Sep 2013) | 1 line

Fix typo in serial ports overview.
------------------------------------------------------------------------
r85743 | chris_kohlhoff | 2013-09-18 07:41:04 +1000 (Wed, 18 Sep 2013) | 1 line

Ensure ssl::io_op::want_ member is initialised.
------------------------------------------------------------------------
r85744 | chris_kohlhoff | 2013-09-18 07:42:08 +1000 (Wed, 18 Sep 2013) | 1 line

Fix a bug in handler tracking, where it was not correctly printing out some handler IDs.
------------------------------------------------------------------------
r85745 | chris_kohlhoff | 2013-09-18 07:42:48 +1000 (Wed, 18 Sep 2013) | 1 line

Fix comparison used to test for a successful synchronous accept.
------------------------------------------------------------------------
r85746 | chris_kohlhoff | 2013-09-18 07:43:29 +1000 (Wed, 18 Sep 2013) | 3 lines

Ensure signal number is correctly passed to the completion handler when
starting an async_wait on a signal that is already raised.

------------------------------------------------------------------------
r85747 | chris_kohlhoff | 2013-09-18 07:45:43 +1000 (Wed, 18 Sep 2013) | 1 line

Suppress g++ 4.8+ warning about unused typedefs.
------------------------------------------------------------------------
r85748 | chris_kohlhoff | 2013-09-18 07:48:54 +1000 (Wed, 18 Sep 2013) | 1 line

Fix link to refer to native_handle() rather than the deprecated native() function.
------------------------------------------------------------------------
r85749 | chris_kohlhoff | 2013-09-18 07:51:28 +1000 (Wed, 18 Sep 2013) | 3 lines

Enable the move optimisation (and otherwise eliminate a copy) for handlers
using the default invocation hook.

------------------------------------------------------------------------
r85750 | chris_kohlhoff | 2013-09-18 07:52:27 +1000 (Wed, 18 Sep 2013) | 2 lines

Clarify that programs must not issue overlapping async_write_at operations.

------------------------------------------------------------------------
r85751 | chris_kohlhoff | 2013-09-18 07:53:03 +1000 (Wed, 18 Sep 2013) | 1 line

Fix error in comment.
------------------------------------------------------------------------
r85752 | chris_kohlhoff | 2013-09-18 07:53:45 +1000 (Wed, 18 Sep 2013) | 1 line

Remove spurious whitespace.
------------------------------------------------------------------------
r85753 | chris_kohlhoff | 2013-09-18 08:00:53 +1000 (Wed, 18 Sep 2013) | 2 lines

Clean up some internal forward declarations.

------------------------------------------------------------------------
r85754 | chris_kohlhoff | 2013-09-18 08:01:26 +1000 (Wed, 18 Sep 2013) | 1 line

Add missing move cast.
------------------------------------------------------------------------
r85755 | chris_kohlhoff | 2013-09-18 08:03:29 +1000 (Wed, 18 Sep 2013) | 3 lines

Fix another socket descriptor comparison that doesn't work correctly if
the descriptor type is unsigned.

------------------------------------------------------------------------
r85756 | chris_kohlhoff | 2013-09-18 08:04:10 +1000 (Wed, 18 Sep 2013) | 3 lines

Fix documentation error where an asynchronous function was described as
having synchronous behaviour.

------------------------------------------------------------------------
r85757 | chris_kohlhoff | 2013-09-18 08:04:55 +1000 (Wed, 18 Sep 2013) | 1 line

Add missing forward declarations needed for Windows.
------------------------------------------------------------------------
r85758 | chris_kohlhoff | 2013-09-18 08:05:38 +1000 (Wed, 18 Sep 2013) | 1 line

Remove use of std::min.
------------------------------------------------------------------------
r85759 | chris_kohlhoff | 2013-09-18 08:06:25 +1000 (Wed, 18 Sep 2013) | 3 lines

Inore ERROR_MORE_DATA as a non-fatal error when returned by
GetOverlappedResult for a synchronous read.

------------------------------------------------------------------------
r85760 | chris_kohlhoff | 2013-09-18 08:08:07 +1000 (Wed, 18 Sep 2013) | 2 lines

Enable certain C++11 standard library facilities for recent versions of Microsoft Visual Studio.

------------------------------------------------------------------------
r85761 | chris_kohlhoff | 2013-09-18 08:08:43 +1000 (Wed, 18 Sep 2013) | 4 lines

Visual C++ language extensions use generic as a keyword. Add a
workaround that renames the namespace to cpp_generic when those
language extensions are in effect.

------------------------------------------------------------------------
r85762 | chris_kohlhoff | 2013-09-18 08:12:07 +1000 (Wed, 18 Sep 2013) | 2 lines

Some async operations that missed getting the async_result treatment.

------------------------------------------------------------------------
r85763 | chris_kohlhoff | 2013-09-18 08:14:14 +1000 (Wed, 18 Sep 2013) | 2 lines

Eliminate some unnecessary handler copies.

------------------------------------------------------------------------
r85764 | chris_kohlhoff | 2013-09-18 08:24:19 +1000 (Wed, 18 Sep 2013) | 26 lines

Initial port to Windows Runtime.

This change adds limited support for using Asio with the Windows
Runtime. It requires that the language extensions be enabled. Due to the
restricted facilities exposed by the Windows Runtime API, the port comes
with the following caveats:

* The core facilities such as the io_service, strand, buffers, composed
  operations, timers, etc., should all work as normal.

* For sockets, only client-side TCP is supported.

* Explicit binding of a client-side TCP socket is not supported.

* The cancel() function is not supported for sockets. Asynchronous
  operations may only be cancelled by closing the socket.

* Operations that use null_buffers are not supported.

* Only tcp::no_delay and socket_base::keep_alive options are supported.

* Resolvers do not support service names, only numbers. I.e. you must
  use 80 rather than http.

* Most resolver query flags have no effect.

------------------------------------------------------------------------
r85765 | chris_kohlhoff | 2013-09-18 08:32:13 +1000 (Wed, 18 Sep 2013) | 1 line

Regenerate documentation.
------------------------------------------------------------------------
r85766 | chris_kohlhoff | 2013-09-18 08:38:04 +1000 (Wed, 18 Sep 2013) | 2 lines

Enable move support for Microsoft Visual C++ 2012.

------------------------------------------------------------------------
r85767 | chris_kohlhoff | 2013-09-18 08:42:50 +1000 (Wed, 18 Sep 2013) | 2 lines

Add use_future support for Microsoft Visual C++.

------------------------------------------------------------------------
r85768 | chris_kohlhoff | 2013-09-18 08:53:17 +1000 (Wed, 18 Sep 2013) | 2 lines

Fix prefix on extern "C" function name.

------------------------------------------------------------------------
r85781 | chris_kohlhoff | 2013-09-19 08:43:01 +1000 (Thu, 19 Sep 2013) | 1 line

Implement end-of-file condition for WinRT stream sockets.
------------------------------------------------------------------------
r85798 | chris_kohlhoff | 2013-09-20 22:00:44 +1000 (Fri, 20 Sep 2013) | 2 lines

Update buffered stream operations to adhere to current handler patterns.

------------------------------------------------------------------------
r85799 | chris_kohlhoff | 2013-09-20 22:02:33 +1000 (Fri, 20 Sep 2013) | 1 line

MinGW fix.
------------------------------------------------------------------------
r85800 | chris_kohlhoff | 2013-09-20 22:04:00 +1000 (Fri, 20 Sep 2013) | 1 line

Change unit tests so that they don't depend on Boost.Thread.
------------------------------------------------------------------------
r85801 | chris_kohlhoff | 2013-09-20 22:10:45 +1000 (Fri, 20 Sep 2013) | 1 line

Regenerate documentation.
------------------------------------------------------------------------
r85823 | danieljames | 2013-09-22 20:32:36 +1000 (Sun, 22 Sep 2013) | 1 line

Fix copying C++11 allocation examples
------------------------------------------------------------------------


[SVN r85838]
2013-09-22 22:13:33 +00:00

357 lines
9.4 KiB
C++

//
// deadline_timer.cpp
// ~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)
// Test that header file is self-contained.
#include <boost/asio/deadline_timer.hpp>
#include "unit_test.hpp"
#if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
#include <boost/bind.hpp>
#include "archetypes/async_result.hpp"
#include <boost/asio/io_service.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/asio/detail/thread.hpp>
using namespace boost::posix_time;
void increment(int* count)
{
++(*count);
}
void decrement_to_zero(boost::asio::deadline_timer* t, int* count)
{
if (*count > 0)
{
--(*count);
int before_value = *count;
t->expires_at(t->expires_at() + seconds(1));
t->async_wait(boost::bind(decrement_to_zero, t, count));
// Completion cannot nest, so count value should remain unchanged.
BOOST_ASIO_CHECK(*count == before_value);
}
}
void increment_if_not_cancelled(int* count,
const boost::system::error_code& ec)
{
if (!ec)
++(*count);
}
void cancel_timer(boost::asio::deadline_timer* t)
{
std::size_t num_cancelled = t->cancel();
BOOST_ASIO_CHECK(num_cancelled == 1);
}
void cancel_one_timer(boost::asio::deadline_timer* t)
{
std::size_t num_cancelled = t->cancel_one();
BOOST_ASIO_CHECK(num_cancelled == 1);
}
ptime now()
{
#if defined(BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK)
return microsec_clock::universal_time();
#else // defined(BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK)
return second_clock::universal_time();
#endif // defined(BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK)
}
void deadline_timer_test()
{
boost::asio::io_service ios;
int count = 0;
ptime start = now();
boost::asio::deadline_timer t1(ios, seconds(1));
t1.wait();
// The timer must block until after its expiry time.
ptime end = now();
ptime expected_end = start + seconds(1);
BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
start = now();
boost::asio::deadline_timer t2(ios, seconds(1) + microseconds(500000));
t2.wait();
// The timer must block until after its expiry time.
end = now();
expected_end = start + seconds(1) + microseconds(500000);
BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
t2.expires_at(t2.expires_at() + seconds(1));
t2.wait();
// The timer must block until after its expiry time.
end = now();
expected_end += seconds(1);
BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
start = now();
t2.expires_from_now(seconds(1) + microseconds(200000));
t2.wait();
// The timer must block until after its expiry time.
end = now();
expected_end = start + seconds(1) + microseconds(200000);
BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
start = now();
boost::asio::deadline_timer t3(ios, seconds(5));
t3.async_wait(boost::bind(increment, &count));
// No completions can be delivered until run() is called.
BOOST_ASIO_CHECK(count == 0);
ios.run();
// The run() call will not return until all operations have finished, and
// this should not be until after the timer's expiry time.
BOOST_ASIO_CHECK(count == 1);
end = now();
expected_end = start + seconds(1);
BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
count = 3;
start = now();
boost::asio::deadline_timer t4(ios, seconds(1));
t4.async_wait(boost::bind(decrement_to_zero, &t4, &count));
// No completions can be delivered until run() is called.
BOOST_ASIO_CHECK(count == 3);
ios.reset();
ios.run();
// The run() call will not return until all operations have finished, and
// this should not be until after the timer's final expiry time.
BOOST_ASIO_CHECK(count == 0);
end = now();
expected_end = start + seconds(3);
BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
count = 0;
start = now();
boost::asio::deadline_timer t5(ios, seconds(10));
t5.async_wait(boost::bind(increment_if_not_cancelled, &count,
boost::asio::placeholders::error));
boost::asio::deadline_timer t6(ios, seconds(1));
t6.async_wait(boost::bind(cancel_timer, &t5));
// No completions can be delivered until run() is called.
BOOST_ASIO_CHECK(count == 0);
ios.reset();
ios.run();
// The timer should have been cancelled, so count should not have changed.
// The total run time should not have been much more than 1 second (and
// certainly far less than 10 seconds).
BOOST_ASIO_CHECK(count == 0);
end = now();
expected_end = start + seconds(2);
BOOST_ASIO_CHECK(end < expected_end);
// Wait on the timer again without cancelling it. This time the asynchronous
// wait should run to completion and increment the counter.
t5.async_wait(boost::bind(increment_if_not_cancelled, &count,
boost::asio::placeholders::error));
ios.reset();
ios.run();
// The timer should not have been cancelled, so count should have changed.
// The total time since the timer was created should be more than 10 seconds.
BOOST_ASIO_CHECK(count == 1);
end = now();
expected_end = start + seconds(10);
BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
count = 0;
start = now();
// Start two waits on a timer, one of which will be cancelled. The one
// which is not cancelled should still run to completion and increment the
// counter.
boost::asio::deadline_timer t7(ios, seconds(3));
t7.async_wait(boost::bind(increment_if_not_cancelled, &count,
boost::asio::placeholders::error));
t7.async_wait(boost::bind(increment_if_not_cancelled, &count,
boost::asio::placeholders::error));
boost::asio::deadline_timer t8(ios, seconds(1));
t8.async_wait(boost::bind(cancel_one_timer, &t7));
ios.reset();
ios.run();
// One of the waits should not have been cancelled, so count should have
// changed. The total time since the timer was created should be more than 3
// seconds.
BOOST_ASIO_CHECK(count == 1);
end = now();
expected_end = start + seconds(3);
BOOST_ASIO_CHECK(expected_end < end || expected_end == end);
}
void timer_handler(const boost::system::error_code&)
{
}
void deadline_timer_cancel_test()
{
static boost::asio::io_service io_service;
struct timer
{
boost::asio::deadline_timer t;
timer() : t(io_service) { t.expires_at(boost::posix_time::pos_infin); }
} timers[50];
timers[2].t.async_wait(&timer_handler);
timers[41].t.async_wait(&timer_handler);
for (int i = 10; i < 20; ++i)
timers[i].t.async_wait(&timer_handler);
BOOST_ASIO_CHECK(timers[2].t.cancel() == 1);
BOOST_ASIO_CHECK(timers[41].t.cancel() == 1);
for (int i = 10; i < 20; ++i)
BOOST_ASIO_CHECK(timers[i].t.cancel() == 1);
}
struct custom_allocation_timer_handler
{
custom_allocation_timer_handler(int* count) : count_(count) {}
void operator()(const boost::system::error_code&) {}
int* count_;
};
void* asio_handler_allocate(std::size_t size,
custom_allocation_timer_handler* handler)
{
++(*handler->count_);
return ::operator new(size);
}
void asio_handler_deallocate(void* pointer, std::size_t,
custom_allocation_timer_handler* handler)
{
--(*handler->count_);
::operator delete(pointer);
}
void deadline_timer_custom_allocation_test()
{
static boost::asio::io_service io_service;
struct timer
{
boost::asio::deadline_timer t;
timer() : t(io_service) {}
} timers[100];
int allocation_count = 0;
for (int i = 0; i < 50; ++i)
{
timers[i].t.expires_at(boost::posix_time::pos_infin);
timers[i].t.async_wait(custom_allocation_timer_handler(&allocation_count));
}
for (int i = 50; i < 100; ++i)
{
timers[i].t.expires_at(boost::posix_time::neg_infin);
timers[i].t.async_wait(custom_allocation_timer_handler(&allocation_count));
}
for (int i = 0; i < 50; ++i)
timers[i].t.cancel();
io_service.run();
BOOST_ASIO_CHECK(allocation_count == 0);
}
void io_service_run(boost::asio::io_service* ios)
{
ios->run();
}
void deadline_timer_thread_test()
{
boost::asio::io_service ios;
boost::asio::io_service::work w(ios);
boost::asio::deadline_timer t1(ios);
boost::asio::deadline_timer t2(ios);
int count = 0;
boost::asio::detail::thread th(boost::bind(io_service_run, &ios));
t2.expires_from_now(boost::posix_time::seconds(2));
t2.wait();
t1.expires_from_now(boost::posix_time::seconds(2));
t1.async_wait(boost::bind(increment, &count));
t2.expires_from_now(boost::posix_time::seconds(4));
t2.wait();
ios.stop();
th.join();
BOOST_ASIO_CHECK(count == 1);
}
void deadline_timer_async_result_test()
{
boost::asio::io_service ios;
boost::asio::deadline_timer t1(ios);
t1.expires_from_now(boost::posix_time::seconds(1));
int i = t1.async_wait(archetypes::lazy_handler());
BOOST_ASIO_CHECK(i == 42);
ios.run();
}
BOOST_ASIO_TEST_SUITE
(
"deadline_timer",
BOOST_ASIO_TEST_CASE(deadline_timer_test)
BOOST_ASIO_TEST_CASE(deadline_timer_cancel_test)
BOOST_ASIO_TEST_CASE(deadline_timer_custom_allocation_test)
BOOST_ASIO_TEST_CASE(deadline_timer_thread_test)
BOOST_ASIO_TEST_CASE(deadline_timer_async_result_test)
)
#else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
BOOST_ASIO_TEST_SUITE
(
"deadline_timer",
BOOST_ASIO_TEST_CASE(null_test)
)
#endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)