|
| 1 | +// Copyright 2009-present MongoDB, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <bsoncxx/test/exception_guard.hh> |
| 16 | + |
| 17 | +// |
| 18 | + |
| 19 | +#include <atomic> |
| 20 | +#include <chrono> |
| 21 | +#include <stdexcept> |
| 22 | +#include <string> |
| 23 | +#include <thread> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include <bsoncxx/test/catch.hh> |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +TEST_CASE("bsoncxx::test::exception_guard", "[test]") { |
| 31 | + using EGuard = bsoncxx::test::exception_guard_state; |
| 32 | + |
| 33 | + EGuard eguard; |
| 34 | + |
| 35 | + SECTION("init") { |
| 36 | + CHECK(eguard.ptr == nullptr); |
| 37 | + CHECK(eguard.file == bsoncxx::stdx::string_view("")); |
| 38 | + CHECK(eguard.line == 0u); |
| 39 | + CHECK(eguard.func == bsoncxx::stdx::string_view("")); |
| 40 | + } |
| 41 | + |
| 42 | + SECTION("reset") { |
| 43 | + // clang-format off |
| 44 | + BSONCXX_TEST_EXCEPTION_GUARD_RESET(eguard); const auto line = __LINE__; |
| 45 | + // clang-format on |
| 46 | + |
| 47 | + CHECK(eguard.ptr == nullptr); |
| 48 | + CHECK(eguard.file == bsoncxx::stdx::string_view(__FILE__)); |
| 49 | + CHECK(eguard.line == line); |
| 50 | + CHECK(eguard.func == bsoncxx::stdx::string_view(__func__)); |
| 51 | + } |
| 52 | + |
| 53 | + SECTION("simple") { |
| 54 | + SECTION("no throw") { |
| 55 | + BSONCXX_TEST_EXCEPTION_GUARD_BEGIN(eguard); |
| 56 | + BSONCXX_TEST_EXCEPTION_GUARD_END(eguard); |
| 57 | + |
| 58 | + CHECK(eguard.ptr == nullptr); |
| 59 | + CHECK(eguard.file == bsoncxx::stdx::string_view("")); |
| 60 | + CHECK(eguard.line == 0u); |
| 61 | + CHECK(eguard.func == bsoncxx::stdx::string_view("")); |
| 62 | + |
| 63 | + BSONCXX_TEST_EXCEPTION_GUARD_CHECK(eguard); |
| 64 | + SUCCEED("no exception was thrown by the check"); |
| 65 | + } |
| 66 | + |
| 67 | + SECTION("throw") { |
| 68 | + struct EGuardException {}; |
| 69 | + |
| 70 | + EGuard expected; |
| 71 | + |
| 72 | + // clang-format off |
| 73 | + BSONCXX_TEST_EXCEPTION_GUARD_BEGIN(eguard); |
| 74 | + throw EGuardException(); |
| 75 | + BSONCXX_TEST_EXCEPTION_GUARD_END(eguard); BSONCXX_TEST_EXCEPTION_GUARD_RESET(expected); |
| 76 | + // clang-format on |
| 77 | + |
| 78 | + CHECK(eguard.ptr != nullptr); |
| 79 | + CHECK(eguard.file == expected.file); |
| 80 | + CHECK(eguard.line == expected.line); |
| 81 | + CHECK(eguard.func == expected.func); |
| 82 | + CHECK(eguard.ignored.empty()); |
| 83 | + |
| 84 | + const auto check_expr = [&] { BSONCXX_TEST_EXCEPTION_GUARD_CHECK(eguard); }; |
| 85 | + REQUIRE_THROWS_AS(check_expr(), EGuardException); |
| 86 | + REQUIRE_THROWS_AS(check_expr(), EGuardException); // Rethrow is OK. |
| 87 | + |
| 88 | + CHECK(eguard.ptr != nullptr); |
| 89 | + CHECK(eguard.file == expected.file); |
| 90 | + CHECK(eguard.line == expected.line); |
| 91 | + CHECK(eguard.func == expected.func); |
| 92 | + CHECK(eguard.ignored.empty()); |
| 93 | + } |
| 94 | + |
| 95 | + SECTION("ignored") { |
| 96 | + struct EGuardException : std::runtime_error { |
| 97 | + using std::runtime_error::runtime_error; |
| 98 | + }; |
| 99 | + |
| 100 | + // clang-format off |
| 101 | + BSONCXX_TEST_EXCEPTION_GUARD_BEGIN(eguard); |
| 102 | + throw EGuardException("one"); |
| 103 | + BSONCXX_TEST_EXCEPTION_GUARD_END(eguard); |
| 104 | + // clang-format on |
| 105 | + |
| 106 | + REQUIRE(eguard.ptr != nullptr); |
| 107 | + REQUIRE(eguard.ignored.size() == 0u); |
| 108 | + |
| 109 | + EGuard expected; |
| 110 | + |
| 111 | + // clang-format off |
| 112 | + BSONCXX_TEST_EXCEPTION_GUARD_BEGIN(eguard); |
| 113 | + throw EGuardException("two"); |
| 114 | + BSONCXX_TEST_EXCEPTION_GUARD_END(eguard); BSONCXX_TEST_EXCEPTION_GUARD_RESET(expected); |
| 115 | + // clang-format on |
| 116 | + |
| 117 | + const auto npos = std::string::npos; |
| 118 | + |
| 119 | + REQUIRE(eguard.ignored.size() == 1u); |
| 120 | + { |
| 121 | + const auto& log = eguard.ignored[0]; |
| 122 | + const auto log_view = bsoncxx::stdx::string_view(log); |
| 123 | + |
| 124 | + CAPTURE(log); |
| 125 | + CAPTURE(expected.file); |
| 126 | + CAPTURE(expected.line); |
| 127 | + CAPTURE(expected.func); |
| 128 | + |
| 129 | + CHECK_THAT(log, Catch::Contains("two")); |
| 130 | + CHECK(log_view.find(expected.file) != npos); |
| 131 | + CHECK(log_view.find(std::to_string(expected.line)) != npos); |
| 132 | + CHECK(log_view.find(expected.func) == npos); // Func is not logged. |
| 133 | + } |
| 134 | + |
| 135 | + // clang-format off |
| 136 | + BSONCXX_TEST_EXCEPTION_GUARD_BEGIN(eguard); |
| 137 | + throw Catch::TestFailureException(); // As-if by `REQUIRE(false)`. |
| 138 | + BSONCXX_TEST_EXCEPTION_GUARD_END(eguard); BSONCXX_TEST_EXCEPTION_GUARD_RESET(expected); |
| 139 | + // clang-format on |
| 140 | + |
| 141 | + REQUIRE(eguard.ignored.size() == 2u); |
| 142 | + { |
| 143 | + const auto& log = eguard.ignored[1]; |
| 144 | + const auto log_view = bsoncxx::stdx::string_view(log); |
| 145 | + |
| 146 | + CAPTURE(log); |
| 147 | + CAPTURE(expected.file); |
| 148 | + CAPTURE(expected.line); |
| 149 | + CAPTURE(expected.func); |
| 150 | + |
| 151 | + CHECK_THAT(log, Catch::Contains("Catch::TestFailureException")); |
| 152 | + CHECK(log_view.find(expected.file) != npos); |
| 153 | + CHECK(log_view.find(std::to_string(expected.line)) != npos); |
| 154 | + CHECK(log_view.find(expected.func) == npos); // Func is not logged. |
| 155 | + } |
| 156 | + |
| 157 | + // The original exception. |
| 158 | + const auto check_expr = [&] { BSONCXX_TEST_EXCEPTION_GUARD_CHECK(eguard); }; |
| 159 | + REQUIRE_THROWS_WITH(check_expr(), Catch::Contains("one")); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + SECTION("concurrent") { |
| 164 | + EGuard expected; |
| 165 | + |
| 166 | + struct EGuardException : std::runtime_error { |
| 167 | + int id; |
| 168 | + EGuardException(int i) : std::runtime_error(std::to_string(i)), id(i) {} |
| 169 | + }; |
| 170 | + |
| 171 | + { |
| 172 | + std::atomic_int counter; |
| 173 | + std::atomic_bool latch; |
| 174 | + |
| 175 | + std::atomic_init(&counter, 0); |
| 176 | + std::atomic_init(&latch, false); |
| 177 | + |
| 178 | + auto fn = [&] { |
| 179 | + // A simple latch to maximize parallelism. |
| 180 | + while (!latch.load()) { |
| 181 | + } |
| 182 | + |
| 183 | + BSONCXX_TEST_EXCEPTION_GUARD_BEGIN(eguard); |
| 184 | + throw EGuardException(++counter); |
| 185 | + // clang-format off |
| 186 | + BSONCXX_TEST_EXCEPTION_GUARD_END(eguard); BSONCXX_TEST_EXCEPTION_GUARD_RESET(expected); |
| 187 | + // clang-format on |
| 188 | + }; |
| 189 | + |
| 190 | + std::vector<std::thread> threads; |
| 191 | + |
| 192 | + threads.emplace_back(fn); |
| 193 | + threads.emplace_back(fn); |
| 194 | + threads.emplace_back(fn); |
| 195 | + |
| 196 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 197 | + latch.store(true); |
| 198 | + |
| 199 | + for (auto& thread : threads) { |
| 200 | + thread.join(); |
| 201 | + } |
| 202 | + |
| 203 | + REQUIRE(counter.load() == 3); |
| 204 | + } |
| 205 | + |
| 206 | + REQUIRE(eguard.ptr != nullptr); |
| 207 | + CHECK(eguard.file == expected.file); |
| 208 | + CHECK(eguard.line == expected.line); |
| 209 | + CHECK(eguard.func == expected.func); |
| 210 | + |
| 211 | + auto test = [&] { |
| 212 | + try { |
| 213 | + BSONCXX_TEST_EXCEPTION_GUARD_CHECK(eguard); |
| 214 | + FAIL("should have thrown an EGuardException"); |
| 215 | + } catch (const EGuardException& e) { |
| 216 | + CAPTURE(e.id); |
| 217 | + CHECK(e.id > 0); |
| 218 | + } |
| 219 | + }; |
| 220 | + |
| 221 | + REQUIRE_NOTHROW(test()); |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +} // namespace |
0 commit comments