|
| 1 | +//===- SessionTest.cpp ----------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// Tests for orc-rt's Session.h APIs. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "orc-rt/Session.h" |
| 14 | +#include "gmock/gmock.h" |
| 15 | +#include "gtest/gtest.h" |
| 16 | + |
| 17 | +#include <optional> |
| 18 | + |
| 19 | +using namespace orc_rt; |
| 20 | +using ::testing::Eq; |
| 21 | +using ::testing::Optional; |
| 22 | + |
| 23 | +class MockResourceManager : public ResourceManager { |
| 24 | +public: |
| 25 | + enum class Op { Detach, Shutdown }; |
| 26 | + |
| 27 | + static Error alwaysSucceed(Op) { return Error::success(); } |
| 28 | + |
| 29 | + MockResourceManager(std::optional<size_t> &DetachOpIdx, |
| 30 | + std::optional<size_t> &ShutdownOpIdx, size_t &OpIdx, |
| 31 | + move_only_function<Error(Op)> GenResult = alwaysSucceed) |
| 32 | + : DetachOpIdx(DetachOpIdx), ShutdownOpIdx(ShutdownOpIdx), OpIdx(OpIdx), |
| 33 | + GenResult(std::move(GenResult)) {} |
| 34 | + |
| 35 | + void detach(OnCompleteFn OnComplete) override { |
| 36 | + DetachOpIdx = OpIdx++; |
| 37 | + OnComplete(GenResult(Op::Detach)); |
| 38 | + } |
| 39 | + |
| 40 | + void shutdown(OnCompleteFn OnComplete) override { |
| 41 | + ShutdownOpIdx = OpIdx++; |
| 42 | + OnComplete(GenResult(Op::Shutdown)); |
| 43 | + } |
| 44 | + |
| 45 | +private: |
| 46 | + std::optional<size_t> &DetachOpIdx; |
| 47 | + std::optional<size_t> &ShutdownOpIdx; |
| 48 | + size_t &OpIdx; |
| 49 | + move_only_function<Error(Op)> GenResult; |
| 50 | +}; |
| 51 | + |
| 52 | +// Non-overloaded version of cantFail: allows easy construction of |
| 53 | +// move_only_functions<void(Error)>s. |
| 54 | +static void noErrors(Error Err) { cantFail(std::move(Err)); } |
| 55 | + |
| 56 | +TEST(SessionTest, TrivialConstructionAndDestruction) { Session S(noErrors); } |
| 57 | + |
| 58 | +TEST(SessionTest, ReportError) { |
| 59 | + Error E = Error::success(); |
| 60 | + cantFail(std::move(E)); // Force error into checked state. |
| 61 | + |
| 62 | + Session S([&](Error Err) { E = std::move(Err); }); |
| 63 | + S.reportError(make_error<StringError>("foo")); |
| 64 | + |
| 65 | + if (E) |
| 66 | + EXPECT_EQ(toString(std::move(E)), "foo"); |
| 67 | + else |
| 68 | + ADD_FAILURE() << "Missing error value"; |
| 69 | +} |
| 70 | + |
| 71 | +TEST(SessionTest, SingleResourceManager) { |
| 72 | + size_t OpIdx = 0; |
| 73 | + std::optional<size_t> DetachOpIdx; |
| 74 | + std::optional<size_t> ShutdownOpIdx; |
| 75 | + |
| 76 | + { |
| 77 | + Session S(noErrors); |
| 78 | + S.addResourceManager(std::make_unique<MockResourceManager>( |
| 79 | + DetachOpIdx, ShutdownOpIdx, OpIdx)); |
| 80 | + } |
| 81 | + |
| 82 | + EXPECT_EQ(OpIdx, 1U); |
| 83 | + EXPECT_EQ(DetachOpIdx, std::nullopt); |
| 84 | + EXPECT_THAT(ShutdownOpIdx, Optional(Eq(0))); |
| 85 | +} |
| 86 | + |
| 87 | +TEST(SessionTest, MultipleResourceManagers) { |
| 88 | + size_t OpIdx = 0; |
| 89 | + std::optional<size_t> DetachOpIdx[3]; |
| 90 | + std::optional<size_t> ShutdownOpIdx[3]; |
| 91 | + |
| 92 | + { |
| 93 | + Session S(noErrors); |
| 94 | + for (size_t I = 0; I != 3; ++I) |
| 95 | + S.addResourceManager(std::make_unique<MockResourceManager>( |
| 96 | + DetachOpIdx[I], ShutdownOpIdx[I], OpIdx)); |
| 97 | + } |
| 98 | + |
| 99 | + EXPECT_EQ(OpIdx, 3U); |
| 100 | + // Expect shutdown in reverse order. |
| 101 | + for (size_t I = 0; I != 3; ++I) { |
| 102 | + EXPECT_EQ(DetachOpIdx[I], std::nullopt); |
| 103 | + EXPECT_THAT(ShutdownOpIdx[I], Optional(Eq(2 - I))); |
| 104 | + } |
| 105 | +} |
0 commit comments