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