mavtables  0.2.1
MAVLink router and firewall.
test_RecursionGuard.cpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #include <thread>
19 
20 #include <catch.hpp>
21 
22 #include "RecursionData.hpp"
23 #include "RecursionError.hpp"
24 #include "RecursionGuard.hpp"
25 
26 
27 TEST_CASE("RecursionGuard's are constructable.", "[RecursionGuard]")
28 {
29  RecursionData rdata;
30  REQUIRE_NOTHROW(RecursionGuard(rdata));
31 }
32 
33 
34 TEST_CASE("RecusionGuard's prevent recursion within a single thread.",
35  "[RecursionGuard]")
36 {
37  SECTION("No recursion.")
38  {
39  RecursionData rdata;
40  {
41  REQUIRE_NOTHROW(RecursionGuard(rdata));
42  }
43  {
44  REQUIRE_NOTHROW(RecursionGuard(rdata));
45  }
46  {
47  REQUIRE_NOTHROW(RecursionGuard(rdata));
48  }
49  }
50  SECTION("Throws an error on recursion.")
51  {
52  RecursionData rdata;
53  RecursionGuard rg(rdata);
54  REQUIRE_THROWS_AS(RecursionGuard(rdata), RecursionError);
55  REQUIRE_THROWS_WITH(RecursionGuard(rdata), "Recursion detected.");
56  }
57  SECTION("Is thread safe (calls across threads is not recursion).")
58  {
59  RecursionData rdata;
60  RecursionGuard rguard(rdata);
61  std::thread thread([&]()
62  {
63  RecursionGuard rguard_(rdata);
64  });
65  thread.join();
66  }
67 }
TEST_CASE("RecursionGuard's are constructable.", "[RecursionGuard]")