|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 HERE Europe B.V. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +#include <olp/core/client/Condition.h> |
| 21 | + |
| 22 | +#include <gtest/gtest.h> |
| 23 | +#include <future> |
| 24 | + |
| 25 | +using olp::client::Condition; |
| 26 | + |
| 27 | +namespace { |
| 28 | +TEST(ConditionTest, NotifyBeforeWaitRespected) { |
| 29 | + Condition condition{}; |
| 30 | + condition.Notify(); |
| 31 | + EXPECT_TRUE(condition.Wait()); |
| 32 | +} |
| 33 | + |
| 34 | +TEST(ConditionTest, WaitClearsTriggered) { |
| 35 | + Condition condition{}; |
| 36 | + condition.Notify(); |
| 37 | + EXPECT_TRUE(condition.Wait()); |
| 38 | + EXPECT_FALSE(condition.Wait(std::chrono::seconds(0))); |
| 39 | +} |
| 40 | + |
| 41 | +TEST(ConditionTest, Timeouted) { |
| 42 | + Condition condition{}; |
| 43 | + EXPECT_FALSE(condition.Wait(std::chrono::seconds(0))); |
| 44 | +} |
| 45 | + |
| 46 | +TEST(ConditionTest, WakeUp) { |
| 47 | + Condition condition{}; |
| 48 | + auto wait_future = |
| 49 | + std::async(std::launch::async, [&]() { return condition.Wait(); }); |
| 50 | + EXPECT_EQ(wait_future.wait_for(std::chrono::milliseconds(10)), |
| 51 | + std::future_status::timeout); |
| 52 | + condition.Notify(); |
| 53 | + EXPECT_EQ(wait_future.wait_for(std::chrono::milliseconds(10)), |
| 54 | + std::future_status::ready); |
| 55 | + EXPECT_TRUE(wait_future.get()); |
| 56 | +} |
| 57 | +} // namespace |
0 commit comments