blob: 5455859bb4e087bb80cea56d04598c2d9eee7d7b [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "aos/common/queue_testutils.h"
Brian Silvermanf665d692013-02-17 22:11:39 -08002
3#include <string.h>
Brian Silverman797e71e2013-09-06 17:29:39 -07004#include <sys/mman.h>
Brian Silvermaneeb62ca2013-09-11 15:08:03 -07005#include <unistd.h>
6#include <stdlib.h>
7#include <assert.h>
Brian Silvermanf665d692013-02-17 22:11:39 -08008
Brian Silvermanb3616972013-03-05 19:58:10 -08009#include "gtest/gtest.h"
10
brians343bc112013-02-10 01:53:46 +000011#include "aos/common/queue.h"
Brian Silvermanb3616972013-03-05 19:58:10 -080012#include "aos/common/logging/logging_impl.h"
13#include "aos/common/once.h"
14
15using ::aos::logging::LogMessage;
brians343bc112013-02-10 01:53:46 +000016
17namespace aos {
18namespace common {
19namespace testing {
Brian Silvermanb3616972013-03-05 19:58:10 -080020namespace {
21
22class TestLogImplementation : public logging::LogImplementation {
23 public:
24 const ::std::vector<LogMessage> &messages() { return messages_; }
25
26 // This class has to be a singleton so that everybody can get access to the
27 // same instance to read out the messages etc.
28 static TestLogImplementation *GetInstance() {
29 static Once<TestLogImplementation> once(CreateInstance);
30 return once.Get();
31 }
32
33 // Clears out all of the messages already recorded.
34 void ClearMessages() {
35 messages_.clear();
36 }
37
38 // Prints out all of the messages (like when a test fails).
39 void PrintAllMessages() {
40 for (auto it = messages_.begin(); it != messages_.end(); ++it) {
41 logging::internal::PrintMessage(stdout, *it);
42 }
43 }
44
45 private:
46 TestLogImplementation() {}
47
48 static TestLogImplementation *CreateInstance() {
49 return new TestLogImplementation();
50 }
51
Brian Silvermanf7986142014-04-21 17:42:35 -070052 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0)))
Brian Silvermanb3616972013-03-05 19:58:10 -080053 virtual void DoLog(log_level level, const char *format, va_list ap) {
54 LogMessage message;
55
56 logging::internal::FillInMessage(level, format, ap, &message);
57
58 if (!logging::log_gt_important(WARNING, level)) {
59 logging::internal::PrintMessage(stdout, message);
60 }
61
62 messages_.push_back(message);
63 }
64
65 ::std::vector<LogMessage> messages_;
66};
67
68class MyTestEventListener : public ::testing::EmptyTestEventListener {
69 virtual void OnTestStart(const ::testing::TestInfo &/*test_info*/) {
70 TestLogImplementation::GetInstance()->ClearMessages();
71 }
72 virtual void OnTestEnd(const ::testing::TestInfo &test_info) {
73 if (test_info.result()->Failed()) {
74 printf("Test %s failed. Printing out all log messages.\n",
75 test_info.name());
76 fputs("\tThis will include already printed WARNING and up messages.\n",
77 stdout);
Brian Silvermanb91a37d2013-03-09 17:54:30 -080078 fputs("\tIt will also include duplicates of all gtest failures.\n",
79 stdout);
Brian Silvermanb3616972013-03-05 19:58:10 -080080 TestLogImplementation::GetInstance()->PrintAllMessages();
81 }
82 }
Brian Silvermanb91a37d2013-03-09 17:54:30 -080083
84 virtual void OnTestPartResult( const ::testing::TestPartResult &result) {
85 if (result.failed()) {
86 const char *failure_type = "unknown";
87 switch (result.type()) {
88 case ::testing::TestPartResult::Type::kNonFatalFailure:
89 failure_type = "EXPECT";
90 break;
91 case ::testing::TestPartResult::Type::kFatalFailure:
92 failure_type = "ASSERT";
93 break;
94 case ::testing::TestPartResult::Type::kSuccess:
95 break;
96 }
97 log_do(ERROR, "%s: %d: gtest %s failure\n%s\n",
98 result.file_name(),
99 result.line_number(),
100 failure_type,
101 result.message());
102 }
103 }
Brian Silvermanb3616972013-03-05 19:58:10 -0800104};
105
106void *DoEnableTestLogging() {
107 logging::Init();
108 logging::AddImplementation(TestLogImplementation::GetInstance());
109
110 ::testing::UnitTest::GetInstance()->listeners().Append(
111 new MyTestEventListener());
112
113 return NULL;
114}
115
116Once<void> enable_test_logging_once(DoEnableTestLogging);
117
Brian Silverman797e71e2013-09-06 17:29:39 -0700118const size_t kCoreSize = 0x100000;
119
Brian Silvermaneeb62ca2013-09-11 15:08:03 -0700120void TerminateExitHandler() {
121 _exit(EXIT_SUCCESS);
122}
123
Brian Silvermanb3616972013-03-05 19:58:10 -0800124} // namespace
brians343bc112013-02-10 01:53:46 +0000125
126GlobalCoreInstance::GlobalCoreInstance() {
brians343bc112013-02-10 01:53:46 +0000127 global_core = &global_core_data_;
Brian Silvermaneeb62ca2013-09-11 15:08:03 -0700128 global_core->owner = true;
129 // Use mmap(2) manually instead of through malloc(3) so that we can pass
130 // MAP_SHARED which allows forked processes to communicate using the
131 // "shared" memory.
Brian Silverman797e71e2013-09-06 17:29:39 -0700132 void *memory = mmap(NULL, kCoreSize, PROT_READ | PROT_WRITE,
133 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
134 assert(memory != MAP_FAILED);
brians343bc112013-02-10 01:53:46 +0000135
136 assert(aos_core_use_address_as_shared_mem(memory, kCoreSize) == 0);
Brian Silvermanb3616972013-03-05 19:58:10 -0800137
138 EnableTestLogging();
brians343bc112013-02-10 01:53:46 +0000139}
140
141GlobalCoreInstance::~GlobalCoreInstance() {
Brian Silverman797e71e2013-09-06 17:29:39 -0700142 assert(munmap(global_core->mem_struct, kCoreSize) == 0);
brians343bc112013-02-10 01:53:46 +0000143 global_core = NULL;
144}
145
Brian Silvermanb3616972013-03-05 19:58:10 -0800146void EnableTestLogging() {
147 enable_test_logging_once.Get();
148}
149
Brian Silvermaneeb62ca2013-09-11 15:08:03 -0700150void PreventExit() {
151 assert(atexit(TerminateExitHandler) == 0);
152}
153
brians343bc112013-02-10 01:53:46 +0000154} // namespace testing
155} // namespace common
156} // namespace aos