/* * Copyright (c) Facebook, Inc. and its affiliates. * * Licensed under the Apache License Version 2.0 with LLVM Exceptions * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * https://llvm.org/LICENSE.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include #include TEST(StopWhen, SourceCompletesFirst) { using namespace std::chrono_literals; unifex::timed_single_thread_context ctx; bool sourceExecuted = false; bool triggerExecuted = false; std::optional result = unifex::sync_wait( unifex::on( ctx.get_scheduler(), unifex::stop_when( unifex::then( unifex::schedule_after(10ms), [&] { sourceExecuted = true; return 42; }), unifex::then( unifex::schedule_after(1s), [&] { triggerExecuted = true; })))); EXPECT_TRUE(result.has_value()); EXPECT_EQ(42, result.value()); EXPECT_TRUE(sourceExecuted); EXPECT_FALSE(triggerExecuted); } TEST(StopWhen, TriggerCompletesFirst) { using namespace std::chrono_literals; unifex::timed_single_thread_context ctx; bool sourceExecuted = false; bool triggerExecuted = false; std::optional result = unifex::sync_wait( unifex::on( ctx.get_scheduler(), unifex::stop_when( unifex::then( unifex::schedule_after(1s), [&] { sourceExecuted = true; return 42; }), unifex::then( unifex::schedule_after(10ms), [&] { triggerExecuted = true; })))); EXPECT_FALSE(result.has_value()); EXPECT_FALSE(sourceExecuted); EXPECT_TRUE(triggerExecuted); } TEST(StopWhen, CancelledFromParent) { using namespace std::chrono_literals; unifex::timed_single_thread_context ctx; bool sourceExecuted = false; bool triggerExecuted = false; std::optional result = unifex::sync_wait( unifex::on( ctx.get_scheduler(), unifex::stop_when( unifex::stop_when( unifex::then( unifex::schedule_after(1s), [&] { sourceExecuted = true; return 42; }), unifex::then( unifex::schedule_after(2s), [&] { triggerExecuted = true; })), unifex::schedule_after(10ms)))); EXPECT_FALSE(result.has_value()); EXPECT_FALSE(sourceExecuted); EXPECT_FALSE(triggerExecuted); } TEST(StopWhen, Pipeable) { using namespace std::chrono_literals; unifex::timed_single_thread_context ctx; bool sourceExecuted = false; bool triggerExecuted = false; auto op = unifex::schedule_after(1s) | unifex::then( [&] { sourceExecuted = true; return 42; }) | unifex::stop_when( unifex::schedule_after(10ms) | unifex::then( [&] { triggerExecuted = true; })); std::optional result = unifex::sync_wait(unifex::on(ctx.get_scheduler(), std::move(op))); EXPECT_FALSE(result.has_value()); EXPECT_FALSE(sourceExecuted); EXPECT_TRUE(triggerExecuted); }