blob: 9a0c94e0b5ff32108dc61b7c1cf851805eb8501a [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 "glog/logging.h"
Austin Schuh61226052022-06-20 09:40:08 -07004#include "glog/raw_logging.h"
Austin Schuhcc6070c2020-10-10 20:25:56 -07005#include "gtest/gtest.h"
6
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "aos/init.h"
8
Austin Schuh77f3f222022-06-10 16:49:21 -07009DECLARE_bool(die_on_malloc);
10
Stephan Pleinesf63bde82024-01-13 15:59:33 -080011namespace aos::testing {
Austin Schuhcc6070c2020-10-10 20:25:56 -070012
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 Schuh34bd4f92022-06-27 16:39:06 -070080// Malloc hooks don't work with asan/msan.
81#if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer)
82
Austin Schuh77f3f222022-06-10 16:49:21 -070083// Tests that CHECK statements give real error messages rather than die on
84// malloc.
85TEST(RealtimeDeathTest, Check) {
86 EXPECT_DEATH(
87 {
88 ScopedRealtime rt;
89 CHECK_EQ(1, 2) << ": Numbers aren't equal.";
90 },
91 "Numbers aren't equal");
92 EXPECT_DEATH(
93 {
94 ScopedRealtime rt;
95 CHECK_GT(1, 2) << ": Cute error message";
96 },
97 "Cute error message");
98}
99
100// Tests that CHECK statements give real error messages rather than die on
101// malloc.
102TEST(RealtimeDeathTest, Fatal) {
103 EXPECT_DEATH(
104 {
105 ScopedRealtime rt;
106 LOG(FATAL) << "Cute message here";
107 },
108 "Cute message here");
109}
110
111// Tests that the signal handler drops RT permission and prints out a real
112// backtrace instead of crashing on the resulting mallocs.
113TEST(RealtimeDeathTest, SignalHandler) {
114 EXPECT_DEATH(
115 {
116 ScopedRealtime rt;
117 int x = reinterpret_cast<const volatile int *>(0)[0];
118 LOG(INFO) << x;
119 },
James Kuszmaul126dcff2022-08-12 16:30:05 -0700120 "\\*\\*\\* Aborted at .*");
Austin Schuh77f3f222022-06-10 16:49:21 -0700121}
122
Austin Schuh61226052022-06-20 09:40:08 -0700123// Tests that RAW_LOG(FATAL) explodes properly.
124TEST(RealtimeDeathTest, RawFatal) {
125 EXPECT_DEATH(
126 {
127 ScopedRealtime rt;
128 RAW_LOG(FATAL, "Cute message here\n");
129 },
130 "Cute message here");
131}
132
Austin Schuh34bd4f92022-06-27 16:39:06 -0700133#endif
134
Philipp Schrader36d77932024-02-01 18:31:19 -0800135// Tests that we see which CPUs we tried to set when it fails. This can be
136// useful for debugging.
137TEST(RealtimeDeathTest, SetAffinityErrorMessage) {
138 EXPECT_DEATH({ SetCurrentThreadAffinity(MakeCpusetFromCpus({1000})); },
139 "sched_setaffinity\\(0, sizeof\\(cpuset\\), &cpuset\\) == 0 "
140 "\\{CPUs 1000\\}: Invalid argument");
141 EXPECT_DEATH(
142 {
143 SetCurrentThreadAffinity(MakeCpusetFromCpus({1000, 1001}));
144 },
145 "sched_setaffinity\\(0, sizeof\\(cpuset\\), &cpuset\\) == 0 "
146 "\\{CPUs 1000, 1001\\}: Invalid argument");
147}
148
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800149} // namespace aos::testing
Austin Schuh77f3f222022-06-10 16:49:21 -0700150
151// We need a special gtest main to force die_on_malloc support on. Otherwise
152// we can't test CHECK statements before turning die_on_malloc on globally.
153GTEST_API_ int main(int argc, char **argv) {
154 ::testing::InitGoogleTest(&argc, argv);
155 FLAGS_logtostderr = true;
Austin Schuh34bd4f92022-06-27 16:39:06 -0700156
157#if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer)
Austin Schuh77f3f222022-06-10 16:49:21 -0700158 FLAGS_die_on_malloc = true;
Austin Schuh34bd4f92022-06-27 16:39:06 -0700159#endif
Austin Schuh77f3f222022-06-10 16:49:21 -0700160
161 aos::InitGoogle(&argc, &argv);
162
163 return RUN_ALL_TESTS();
164}