|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * Copyright (c) 2025 Robert Leahy. All rights reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 with LLVM Exceptions (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://llvm.org/LICENSE.txt |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <exec/unless_stop_requested.hpp> |
| 20 | + |
| 21 | +#include <exception> |
| 22 | + |
| 23 | +#include <catch2/catch.hpp> |
| 24 | +#include <stdexec/execution.hpp> |
| 25 | + |
| 26 | +#include "../test_common/receivers.hpp" |
| 27 | +#include "../test_common/type_helpers.hpp" |
| 28 | + |
| 29 | +namespace { |
| 30 | + |
| 31 | + TEST_CASE( |
| 32 | + "When stop has not been requested the child operation runs normally", |
| 33 | + "[unless_stop_requested]") { |
| 34 | + ::stdexec::inplace_stop_source source; |
| 35 | + auto env = ::stdexec::prop(::stdexec::get_stop_token, source.get_token()); |
| 36 | + static_assert(!::exec::__unless_stop_requested::__unstoppable_env<decltype(env)>); |
| 37 | + { |
| 38 | + auto sender = ::stdexec::just() | ::exec::unless_stop_requested; |
| 39 | + static_assert( |
| 40 | + set_equivalent< |
| 41 | + ::stdexec::completion_signatures_of_t<decltype(sender), decltype(env)>, |
| 42 | + ::stdexec::completion_signatures<::stdexec::set_value_t(), ::stdexec::set_stopped_t()>>); |
| 43 | + auto op = ::stdexec::connect(sender, expect_void_receiver(env)); |
| 44 | + ::stdexec::start(op); |
| 45 | + } |
| 46 | + { |
| 47 | + auto sender = ::stdexec::just(5) | ::exec::unless_stop_requested(); |
| 48 | + static_assert(set_equivalent< |
| 49 | + ::stdexec::completion_signatures_of_t<decltype(sender), decltype(env)>, |
| 50 | + ::stdexec::completion_signatures< |
| 51 | + ::stdexec::set_value_t(int), |
| 52 | + ::stdexec::set_stopped_t()>>); |
| 53 | + auto op = ::stdexec::connect(sender, expect_value_receiver(env_tag{}, env, 5)); |
| 54 | + ::stdexec::start(op); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + TEST_CASE( |
| 59 | + "When stop has been requested the child operation is not started", |
| 60 | + "[unless_stop_requested]") { |
| 61 | + ::stdexec::inplace_stop_source source; |
| 62 | + source.request_stop(); |
| 63 | + auto env = ::stdexec::prop(::stdexec::get_stop_token, source.get_token()); |
| 64 | + static_assert(!::exec::__unless_stop_requested::__unstoppable_env<decltype(env)>); |
| 65 | + auto sender = ::stdexec::just() |
| 66 | + | ::stdexec::then([&]() { FAIL_CHECK("Operation should not have been started"); }) |
| 67 | + | ::exec::unless_stop_requested(); |
| 68 | + static_assert(set_equivalent< |
| 69 | + ::stdexec::completion_signatures_of_t<decltype(sender), decltype(env)>, |
| 70 | + ::stdexec::completion_signatures< |
| 71 | + ::stdexec::set_value_t(), |
| 72 | + ::stdexec::set_error_t(std::exception_ptr), |
| 73 | + ::stdexec::set_stopped_t()>>); |
| 74 | + auto op = ::stdexec::connect(sender, expect_stopped_receiver(env)); |
| 75 | + ::stdexec::start(op); |
| 76 | + } |
| 77 | + |
| 78 | + TEST_CASE("No op when the associated stop token is unstoppable", "[unless_stop_requested]") { |
| 79 | + static_assert(::exec::__unless_stop_requested::__unstoppable_env<::stdexec::env<>>); |
| 80 | + auto sender = ::stdexec::just() | ::exec::unless_stop_requested; |
| 81 | + static_assert(set_equivalent< |
| 82 | + ::stdexec::completion_signatures_of_t<decltype(sender), ::stdexec::env<>>, |
| 83 | + ::stdexec::completion_signatures<::stdexec::set_value_t()>>); |
| 84 | + auto op = ::stdexec::connect(sender, expect_void_receiver{}); |
| 85 | + ::stdexec::start(op); |
| 86 | + } |
| 87 | + |
| 88 | +} // unnamed namespace |
0 commit comments