diff options
Diffstat (limited to '3rdparty/asio/src/tests/unit/system_timer.cpp')
-rw-r--r-- | 3rdparty/asio/src/tests/unit/system_timer.cpp | 165 |
1 files changed, 145 insertions, 20 deletions
diff --git a/3rdparty/asio/src/tests/unit/system_timer.cpp b/3rdparty/asio/src/tests/unit/system_timer.cpp index 3f77a2e74dc..864a9f2e7fb 100644 --- a/3rdparty/asio/src/tests/unit/system_timer.cpp +++ b/3rdparty/asio/src/tests/unit/system_timer.cpp @@ -2,7 +2,7 @@ // system_timer.cpp // ~~~~~~~~~~~~~~~~ // -// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// Copyright (c) 2003-2021 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) @@ -25,12 +25,14 @@ #if defined(ASIO_HAS_STD_CHRONO) +#include "asio/bind_cancellation_slot.hpp" +#include "asio/cancellation_signal.hpp" #include "asio/executor_work_guard.hpp" #include "asio/io_context.hpp" #include "asio/thread.hpp" #if defined(ASIO_HAS_BOOST_BIND) -# include <boost/bind.hpp> +# include <boost/bind/bind.hpp> #else // defined(ASIO_HAS_BOOST_BIND) # include <functional> #endif // defined(ASIO_HAS_BOOST_BIND) @@ -90,12 +92,11 @@ void system_timer_test() { using asio::chrono::seconds; using asio::chrono::microseconds; -#if !defined(ASIO_HAS_BOOST_BIND) - using std::placeholders::_1; - using std::placeholders::_2; -#endif // !defined(ASIO_HAS_BOOST_BIND) + using bindns::placeholders::_1; + using bindns::placeholders::_2; asio::io_context ioc; + const asio::io_context::executor_type ioc_ex = ioc.get_executor(); int count = 0; asio::system_timer::time_point start = now(); @@ -110,7 +111,7 @@ void system_timer_test() start = now(); - asio::system_timer t2(ioc, seconds(1) + microseconds(500000)); + asio::system_timer t2(ioc_ex, seconds(1) + microseconds(500000)); t2.wait(); // The timer must block until after its expiry time. @@ -271,21 +272,77 @@ struct custom_allocation_timer_handler custom_allocation_timer_handler(int* count) : count_(count) {} void operator()(const asio::error_code&) {} int* count_; -}; -void* asio_handler_allocate(std::size_t size, - custom_allocation_timer_handler* handler) -{ - ++(*handler->count_); - return ::operator new(size); -} + template <typename T> + struct allocator + { + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef T* pointer; + typedef const T* const_pointer; + typedef T& reference; + typedef const T& const_reference; + typedef T value_type; + + template <typename U> + struct rebind + { + typedef allocator<U> other; + }; -void asio_handler_deallocate(void* pointer, std::size_t, - custom_allocation_timer_handler* handler) -{ - --(*handler->count_); - ::operator delete(pointer); -} + explicit allocator(int* count) ASIO_NOEXCEPT + : count_(count) + { + } + + allocator(const allocator& other) ASIO_NOEXCEPT + : count_(other.count_) + { + } + + template <typename U> + allocator(const allocator<U>& other) ASIO_NOEXCEPT + : count_(other.count_) + { + } + + pointer allocate(size_type n, const void* = 0) + { + ++(*count_); + return static_cast<T*>(::operator new(sizeof(T) * n)); + } + + void deallocate(pointer p, size_type) + { + --(*count_); + ::operator delete(p); + } + + size_type max_size() const + { + return ~size_type(0); + } + + void construct(pointer p, const T& v) + { + new (p) T(v); + } + + void destroy(pointer p) + { + p->~T(); + } + + int* count_; + }; + + typedef allocator<int> allocator_type; + + allocator_type get_allocator() const ASIO_NOEXCEPT + { + return allocator_type(count_); + } +}; void system_timer_custom_allocation_test() { @@ -357,6 +414,19 @@ asio::system_timer make_timer(asio::io_context& ioc, int* count) t.async_wait(bindns::bind(increment, count)); return t; } + +typedef asio::basic_waitable_timer< + asio::system_timer::clock_type, + asio::system_timer::traits_type, + asio::io_context::executor_type> io_context_system_timer; + +io_context_system_timer make_convertible_timer(asio::io_context& ioc, int* count) +{ + io_context_system_timer t(ioc); + t.expires_after(asio::chrono::seconds(1)); + t.async_wait(bindns::bind(increment, count)); + return t; +} #endif void system_timer_move_test() @@ -379,9 +449,63 @@ void system_timer_move_test() io_context1.run(); ASIO_CHECK(count == 2); + + asio::system_timer t4 = make_convertible_timer(io_context1, &count); + asio::system_timer t5 = make_convertible_timer(io_context2, &count); + asio::system_timer t6 = std::move(t4); + + t2 = std::move(t4); + + io_context2.restart(); + io_context2.run(); + + ASIO_CHECK(count == 3); + + io_context1.restart(); + io_context1.run(); + + ASIO_CHECK(count == 4); #endif // defined(ASIO_HAS_MOVE) } +void system_timer_op_cancel_test() +{ + asio::cancellation_signal cancel_signal; + asio::io_context ioc; + int count = 0; + + asio::system_timer timer(ioc, asio::chrono::seconds(10)); + + timer.async_wait(bindns::bind(increment, &count)); + + timer.async_wait( + asio::bind_cancellation_slot( + cancel_signal.slot(), + bindns::bind(increment, &count))); + + timer.async_wait(bindns::bind(increment, &count)); + + ioc.poll(); + + ASIO_CHECK(count == 0); + ASIO_CHECK(!ioc.stopped()); + + cancel_signal.emit(asio::cancellation_type::all); + + ioc.run_one(); + ioc.poll(); + + ASIO_CHECK(count == 1); + ASIO_CHECK(!ioc.stopped()); + + timer.cancel(); + + ioc.run(); + + ASIO_CHECK(count == 3); + ASIO_CHECK(ioc.stopped()); +} + ASIO_TEST_SUITE ( "system_timer", @@ -390,6 +514,7 @@ ASIO_TEST_SUITE ASIO_TEST_CASE(system_timer_custom_allocation_test) ASIO_TEST_CASE(system_timer_thread_test) ASIO_TEST_CASE(system_timer_move_test) + ASIO_TEST_CASE(system_timer_op_cancel_test) ) #else // defined(ASIO_HAS_STD_CHRONO) ASIO_TEST_SUITE |