| 1 | // Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++14 } } |
| 19 | // { dg-add-options libatomic } |
| 20 | // { dg-xfail-if "poll not available" { *-*-rtems* } } |
| 21 | |
| 22 | #include <experimental/timer> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | using std::experimental::net::system_timer; |
| 26 | using std::experimental::net::io_context; |
| 27 | using std::error_code; |
| 28 | |
| 29 | void |
| 30 | test01() |
| 31 | { |
| 32 | bool test __attribute__((unused)) = false; |
| 33 | |
| 34 | io_context ctx; |
| 35 | error_code ec; |
| 36 | bool complete = false; |
| 37 | |
| 38 | auto then = system_timer::clock_type::now() + system_timer::duration(100); |
| 39 | |
| 40 | system_timer timer(ctx, then); |
| 41 | VERIFY( timer.cancel_one() == 0 ); |
| 42 | VERIFY( timer.cancel() == 0 ); |
| 43 | |
| 44 | timer.async_wait([&](error_code e) { ec = e; complete = true; }); |
| 45 | VERIFY( timer.cancel_one() == 1 ); |
| 46 | VERIFY( !complete ); |
| 47 | |
| 48 | VERIFY( timer.cancel_one() == 0 ); |
| 49 | VERIFY( timer.cancel() == 0 ); |
| 50 | |
| 51 | VERIFY( ctx.run() == 1 ); |
| 52 | VERIFY( ctx.stopped() ); |
| 53 | VERIFY( complete ); |
| 54 | VERIFY( ec == std::errc::operation_canceled ); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | test02() |
| 59 | { |
| 60 | bool test __attribute__((unused)) = false; |
| 61 | |
| 62 | io_context ctx; |
| 63 | error_code ec1, ec2; |
| 64 | |
| 65 | const auto now = system_timer::clock_type::now(); |
| 66 | const auto t1 = now + std::chrono::seconds(100); |
| 67 | const auto t2 = t1 + std::chrono::seconds(100); |
| 68 | |
| 69 | system_timer timer(ctx, t1); |
| 70 | VERIFY( timer.expiry() == t1 ); |
| 71 | |
| 72 | VERIFY( timer.expires_at(t2) == 0 ); |
| 73 | VERIFY( timer.expiry() == t2 ); |
| 74 | |
| 75 | timer.async_wait([&ec1](error_code e) { ec1 = e; }); |
| 76 | timer.async_wait([&ec2](error_code e) { ec2 = e; }); |
| 77 | auto n = timer.expires_at(t1); |
| 78 | VERIFY( n == 2 ); |
| 79 | VERIFY( timer.expiry() == t1 ); |
| 80 | |
| 81 | VERIFY( ctx.run_one() == 1 ); |
| 82 | VERIFY( ! ctx.stopped() ); |
| 83 | VERIFY( ctx.run_one() == 1 ); |
| 84 | VERIFY( ctx.stopped() ); |
| 85 | VERIFY( ec1 == std::errc::operation_canceled ); |
| 86 | VERIFY( ec2 == std::errc::operation_canceled ); |
| 87 | |
| 88 | VERIFY( timer.expires_after(std::chrono::seconds(50)) == 0 ); |
| 89 | VERIFY( timer.expiry() < t1 ); |
| 90 | |
| 91 | ec1.clear(); |
| 92 | ec2.clear(); |
| 93 | ctx.restart(); |
| 94 | timer.async_wait([&ec1](error_code e) { ec1 = e; }); |
| 95 | timer.async_wait([&ec2](error_code e) { ec2 = e; }); |
| 96 | VERIFY( timer.expires_after(std::chrono::seconds(10)) == 2 ); |
| 97 | VERIFY( timer.expiry() < t1 ); |
| 98 | ctx.run(); |
| 99 | VERIFY( ec1 == std::errc::operation_canceled ); |
| 100 | VERIFY( ec2 == std::errc::operation_canceled ); |
| 101 | } |
| 102 | |
| 103 | int |
| 104 | main() |
| 105 | { |
| 106 | test01(); |
| 107 | test02(); |
| 108 | } |