blob: 8d195c5f3355c97bceec6f5924f86d07b31cd91e [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
52 virtual void DoLog(log_level level, const char *format, va_list ap) {
53 LogMessage message;
54
55 logging::internal::FillInMessage(level, format, ap, &message);
56
57 if (!logging::log_gt_important(WARNING, level)) {
58 logging::internal::PrintMessage(stdout, message);
59 }
60
61 messages_.push_back(message);
62 }
63
64 ::std::vector<LogMessage> messages_;
65};
66
67class MyTestEventListener : public ::testing::EmptyTestEventListener {
68 virtual void OnTestStart(const ::testing::TestInfo &/*test_info*/) {
69 TestLogImplementation::GetInstance()->ClearMessages();
70 }
71 virtual void OnTestEnd(const ::testing::TestInfo &test_info) {
72 if (test_info.result()->Failed()) {
73 printf("Test %s failed. Printing out all log messages.\n",
74 test_info.name());
75 fputs("\tThis will include already printed WARNING and up messages.\n",
76 stdout);
Brian Silvermanb91a37d2013-03-09 17:54:30 -080077 fputs("\tIt will also include duplicates of all gtest failures.\n",
78 stdout);
Brian Silvermanb3616972013-03-05 19:58:10 -080079 TestLogImplementation::GetInstance()->PrintAllMessages();
80 }
81 }
Brian Silvermanb91a37d2013-03-09 17:54:30 -080082
83 virtual void OnTestPartResult( const ::testing::TestPartResult &result) {
84 if (result.failed()) {
85 const char *failure_type = "unknown";
86 switch (result.type()) {
87 case ::testing::TestPartResult::Type::kNonFatalFailure:
88 failure_type = "EXPECT";
89 break;
90 case ::testing::TestPartResult::Type::kFatalFailure:
91 failure_type = "ASSERT";
92 break;
93 case ::testing::TestPartResult::Type::kSuccess:
94 break;
95 }
96 log_do(ERROR, "%s: %d: gtest %s failure\n%s\n",
97 result.file_name(),
98 result.line_number(),
99 failure_type,
100 result.message());
101 }
102 }
Brian Silvermanb3616972013-03-05 19:58:10 -0800103};
104
105void *DoEnableTestLogging() {
106 logging::Init();
107 logging::AddImplementation(TestLogImplementation::GetInstance());
108
109 ::testing::UnitTest::GetInstance()->listeners().Append(
110 new MyTestEventListener());
111
112 return NULL;
113}
114
115Once<void> enable_test_logging_once(DoEnableTestLogging);
116
Brian Silverman797e71e2013-09-06 17:29:39 -0700117const size_t kCoreSize = 0x100000;
118
Brian Silvermaneeb62ca2013-09-11 15:08:03 -0700119void TerminateExitHandler() {
120 _exit(EXIT_SUCCESS);
121}
122
Brian Silvermanb3616972013-03-05 19:58:10 -0800123} // namespace
brians343bc112013-02-10 01:53:46 +0000124
125GlobalCoreInstance::GlobalCoreInstance() {
brians343bc112013-02-10 01:53:46 +0000126 global_core = &global_core_data_;
Brian Silvermaneeb62ca2013-09-11 15:08:03 -0700127 global_core->owner = true;
128 // Use mmap(2) manually instead of through malloc(3) so that we can pass
129 // MAP_SHARED which allows forked processes to communicate using the
130 // "shared" memory.
Brian Silverman797e71e2013-09-06 17:29:39 -0700131 void *memory = mmap(NULL, kCoreSize, PROT_READ | PROT_WRITE,
132 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
133 assert(memory != MAP_FAILED);
brians343bc112013-02-10 01:53:46 +0000134
135 assert(aos_core_use_address_as_shared_mem(memory, kCoreSize) == 0);
Brian Silvermanb3616972013-03-05 19:58:10 -0800136
137 EnableTestLogging();
brians343bc112013-02-10 01:53:46 +0000138}
139
140GlobalCoreInstance::~GlobalCoreInstance() {
Brian Silverman797e71e2013-09-06 17:29:39 -0700141 assert(munmap(global_core->mem_struct, kCoreSize) == 0);
brians343bc112013-02-10 01:53:46 +0000142 global_core = NULL;
143}
144
Brian Silvermanb3616972013-03-05 19:58:10 -0800145void EnableTestLogging() {
146 enable_test_logging_once.Get();
147}
148
Brian Silvermaneeb62ca2013-09-11 15:08:03 -0700149void PreventExit() {
150 assert(atexit(TerminateExitHandler) == 0);
151}
152
brians343bc112013-02-10 01:53:46 +0000153} // namespace testing
154} // namespace common
155} // namespace aos