Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 1 | #include "aos/realtime.h" |
| 2 | |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame^] | 3 | #include "aos/init.h" |
| 4 | #include "glog/logging.h" |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 5 | #include "gtest/gtest.h" |
| 6 | |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame^] | 7 | DECLARE_bool(die_on_malloc); |
| 8 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 9 | namespace aos { |
| 10 | namespace testing { |
| 11 | |
| 12 | // Tests that ScopedRealtime handles the simple case. |
| 13 | TEST(RealtimeTest, ScopedRealtime) { |
| 14 | CheckNotRealtime(); |
| 15 | { |
| 16 | ScopedRealtime rt; |
| 17 | CheckRealtime(); |
| 18 | } |
| 19 | CheckNotRealtime(); |
| 20 | } |
| 21 | |
| 22 | // Tests that ScopedRealtime handles nesting. |
| 23 | TEST(RealtimeTest, DoubleScopedRealtime) { |
| 24 | CheckNotRealtime(); |
| 25 | { |
| 26 | ScopedRealtime rt; |
| 27 | CheckRealtime(); |
| 28 | { |
| 29 | ScopedRealtime rt2; |
| 30 | CheckRealtime(); |
| 31 | } |
| 32 | CheckRealtime(); |
| 33 | } |
| 34 | CheckNotRealtime(); |
| 35 | } |
| 36 | |
| 37 | // Tests that ScopedRealtime handles nesting with ScopedNotRealtime. |
| 38 | TEST(RealtimeTest, ScopedNotRealtime) { |
| 39 | CheckNotRealtime(); |
| 40 | { |
| 41 | ScopedRealtime rt; |
| 42 | CheckRealtime(); |
| 43 | { |
| 44 | ScopedNotRealtime nrt; |
| 45 | CheckNotRealtime(); |
| 46 | } |
| 47 | CheckRealtime(); |
| 48 | } |
| 49 | CheckNotRealtime(); |
| 50 | } |
| 51 | |
| 52 | // Tests that ScopedRealtimeRestorer works both when starting RT and nonrt. |
| 53 | TEST(RealtimeTest, ScopedRealtimeRestorer) { |
| 54 | CheckNotRealtime(); |
| 55 | { |
| 56 | ScopedRealtime rt; |
| 57 | CheckRealtime(); |
| 58 | { |
| 59 | ScopedRealtimeRestorer restore; |
| 60 | CheckRealtime(); |
| 61 | |
| 62 | MarkRealtime(false); |
| 63 | CheckNotRealtime(); |
| 64 | } |
| 65 | CheckRealtime(); |
| 66 | } |
| 67 | CheckNotRealtime(); |
| 68 | |
| 69 | { |
| 70 | ScopedRealtimeRestorer restore; |
| 71 | CheckNotRealtime(); |
| 72 | |
| 73 | MarkRealtime(true); |
| 74 | CheckRealtime(); |
| 75 | } |
| 76 | CheckNotRealtime(); |
| 77 | } |
| 78 | |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame^] | 79 | // Tests that CHECK statements give real error messages rather than die on |
| 80 | // malloc. |
| 81 | TEST(RealtimeDeathTest, Check) { |
| 82 | EXPECT_DEATH( |
| 83 | { |
| 84 | ScopedRealtime rt; |
| 85 | CHECK_EQ(1, 2) << ": Numbers aren't equal."; |
| 86 | }, |
| 87 | "Numbers aren't equal"); |
| 88 | EXPECT_DEATH( |
| 89 | { |
| 90 | ScopedRealtime rt; |
| 91 | CHECK_GT(1, 2) << ": Cute error message"; |
| 92 | }, |
| 93 | "Cute error message"); |
| 94 | } |
| 95 | |
| 96 | // Tests that CHECK statements give real error messages rather than die on |
| 97 | // malloc. |
| 98 | TEST(RealtimeDeathTest, Fatal) { |
| 99 | EXPECT_DEATH( |
| 100 | { |
| 101 | ScopedRealtime rt; |
| 102 | LOG(FATAL) << "Cute message here"; |
| 103 | }, |
| 104 | "Cute message here"); |
| 105 | } |
| 106 | |
| 107 | // Tests that the signal handler drops RT permission and prints out a real |
| 108 | // backtrace instead of crashing on the resulting mallocs. |
| 109 | TEST(RealtimeDeathTest, SignalHandler) { |
| 110 | EXPECT_DEATH( |
| 111 | { |
| 112 | ScopedRealtime rt; |
| 113 | int x = reinterpret_cast<const volatile int *>(0)[0]; |
| 114 | LOG(INFO) << x; |
| 115 | }, |
| 116 | "SIGSEGV \\(@0x0\\) received by PID.*stack trace:"); |
| 117 | } |
| 118 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 119 | } // namespace testing |
| 120 | } // namespace aos |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame^] | 121 | |
| 122 | // We need a special gtest main to force die_on_malloc support on. Otherwise |
| 123 | // we can't test CHECK statements before turning die_on_malloc on globally. |
| 124 | GTEST_API_ int main(int argc, char **argv) { |
| 125 | ::testing::InitGoogleTest(&argc, argv); |
| 126 | FLAGS_logtostderr = true; |
| 127 | FLAGS_die_on_malloc = true; |
| 128 | |
| 129 | aos::InitGoogle(&argc, &argv); |
| 130 | |
| 131 | return RUN_ALL_TESTS(); |
| 132 | } |