|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License Version 2.0 with LLVM Exceptions |
| 5 | + * (the "License"); you may not use this file except in compliance with |
| 6 | + * the License. You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://llvm.org/LICENSE.txt |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include <unifex/just.hpp> |
| 17 | +#include <unifex/just_from.hpp> |
| 18 | +#include <unifex/on.hpp> |
| 19 | +#include <unifex/single_thread_context.hpp> |
| 20 | +#include <unifex/sync_wait.hpp> |
| 21 | + |
| 22 | +#include <gtest/gtest.h> |
| 23 | +#include <thread> |
| 24 | + |
| 25 | +using namespace unifex; |
| 26 | + |
| 27 | +namespace { |
| 28 | +struct Customized { |
| 29 | + std::thread::id id2 = std::this_thread::get_id(); |
| 30 | + template < |
| 31 | + template <typename...> |
| 32 | + class Variant, |
| 33 | + template <typename...> |
| 34 | + class Tuple> |
| 35 | + using value_types = Variant<Tuple<>>; |
| 36 | + |
| 37 | + template <template <typename...> class Variant> |
| 38 | + using error_types = Variant<>; |
| 39 | + |
| 40 | + static constexpr bool sends_done = true; |
| 41 | + |
| 42 | + struct op {}; |
| 43 | + |
| 44 | + template <typename Receiver> |
| 45 | + op connect(Receiver&&) & { |
| 46 | + return op{}; |
| 47 | + } |
| 48 | + |
| 49 | + template <typename Scheduler> |
| 50 | + friend auto tag_invoke(tag_t<on>, Scheduler&& scheduler, Customized& self) { |
| 51 | + return on(static_cast<Scheduler&&>(scheduler), just_from([&self] { |
| 52 | + self.id2 = std::this_thread::get_id(); |
| 53 | + })); |
| 54 | + } |
| 55 | +}; |
| 56 | +} // namespace |
| 57 | + |
| 58 | +TEST(On, Smoke) { |
| 59 | + auto id1 = std::this_thread::get_id(); |
| 60 | + auto id2 = std::this_thread::get_id(); |
| 61 | + |
| 62 | + single_thread_context thread; |
| 63 | + |
| 64 | + auto result = sync_wait(on(thread.get_scheduler(), just_from([&id2] { |
| 65 | + id2 = std::this_thread::get_id(); |
| 66 | + }))); |
| 67 | + EXPECT_NE(id1, id2); |
| 68 | + EXPECT_EQ(id2, thread.get_thread_id()); |
| 69 | + EXPECT_TRUE(result.has_value()); |
| 70 | +} |
| 71 | + |
| 72 | +TEST(On, Tag) { |
| 73 | + auto id1 = std::this_thread::get_id(); |
| 74 | + |
| 75 | + single_thread_context thread; |
| 76 | + Customized c; |
| 77 | + auto result = sync_wait(on(thread.get_scheduler(), c)); |
| 78 | + EXPECT_NE(id1, c.id2); |
| 79 | + EXPECT_EQ(c.id2, thread.get_thread_id()); |
| 80 | + EXPECT_TRUE(result.has_value()); |
| 81 | +} |
0 commit comments