blob: 824cb891a18b6977ec843dd5c15ecfd1875de67d [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>
Brian Silvermanf665d692013-02-17 22:11:39 -08007
Brian Silvermanb3616972013-03-05 19:58:10 -08008#include "gtest/gtest.h"
9
brians343bc112013-02-10 01:53:46 +000010#include "aos/common/queue.h"
Brian Silvermanb3616972013-03-05 19:58:10 -080011#include "aos/common/logging/logging_impl.h"
12#include "aos/common/once.h"
13
14using ::aos::logging::LogMessage;
brians343bc112013-02-10 01:53:46 +000015
16namespace aos {
17namespace common {
18namespace testing {
Brian Silvermanb3616972013-03-05 19:58:10 -080019namespace {
20
Brian Silvermanbe858a12014-04-30 17:37:28 -070021class TestLogImplementation : public logging::HandleMessageLogImplementation {
Brian Silvermanb3616972013-03-05 19:58:10 -080022 public:
23 const ::std::vector<LogMessage> &messages() { return messages_; }
24
25 // This class has to be a singleton so that everybody can get access to the
26 // same instance to read out the messages etc.
27 static TestLogImplementation *GetInstance() {
28 static Once<TestLogImplementation> once(CreateInstance);
29 return once.Get();
30 }
31
32 // Clears out all of the messages already recorded.
33 void ClearMessages() {
34 messages_.clear();
35 }
36
37 // Prints out all of the messages (like when a test fails).
38 void PrintAllMessages() {
39 for (auto it = messages_.begin(); it != messages_.end(); ++it) {
40 logging::internal::PrintMessage(stdout, *it);
41 }
42 }
43
44 private:
45 TestLogImplementation() {}
46
47 static TestLogImplementation *CreateInstance() {
48 return new TestLogImplementation();
49 }
50
Brian Silvermanbe858a12014-04-30 17:37:28 -070051 virtual void HandleMessage(const LogMessage &message) override {
Brian Silverman1094ee02014-05-03 10:34:01 -070052 if (message.level == FATAL) {
Brian Silvermanb3616972013-03-05 19:58:10 -080053 logging::internal::PrintMessage(stdout, message);
54 }
55
56 messages_.push_back(message);
57 }
58
59 ::std::vector<LogMessage> messages_;
60};
61
62class MyTestEventListener : public ::testing::EmptyTestEventListener {
63 virtual void OnTestStart(const ::testing::TestInfo &/*test_info*/) {
64 TestLogImplementation::GetInstance()->ClearMessages();
65 }
66 virtual void OnTestEnd(const ::testing::TestInfo &test_info) {
67 if (test_info.result()->Failed()) {
68 printf("Test %s failed. Printing out all log messages.\n",
69 test_info.name());
70 fputs("\tThis will include already printed WARNING and up messages.\n",
71 stdout);
Brian Silvermanb91a37d2013-03-09 17:54:30 -080072 fputs("\tIt will also include duplicates of all gtest failures.\n",
73 stdout);
Brian Silvermanb3616972013-03-05 19:58:10 -080074 TestLogImplementation::GetInstance()->PrintAllMessages();
75 }
76 }
Brian Silvermanb91a37d2013-03-09 17:54:30 -080077
78 virtual void OnTestPartResult( const ::testing::TestPartResult &result) {
79 if (result.failed()) {
80 const char *failure_type = "unknown";
81 switch (result.type()) {
82 case ::testing::TestPartResult::Type::kNonFatalFailure:
83 failure_type = "EXPECT";
84 break;
85 case ::testing::TestPartResult::Type::kFatalFailure:
86 failure_type = "ASSERT";
87 break;
88 case ::testing::TestPartResult::Type::kSuccess:
89 break;
90 }
91 log_do(ERROR, "%s: %d: gtest %s failure\n%s\n",
92 result.file_name(),
93 result.line_number(),
94 failure_type,
95 result.message());
96 }
97 }
Brian Silvermanb3616972013-03-05 19:58:10 -080098};
99
100void *DoEnableTestLogging() {
101 logging::Init();
102 logging::AddImplementation(TestLogImplementation::GetInstance());
103
104 ::testing::UnitTest::GetInstance()->listeners().Append(
105 new MyTestEventListener());
106
107 return NULL;
108}
109
110Once<void> enable_test_logging_once(DoEnableTestLogging);
111
Brian Silverman797e71e2013-09-06 17:29:39 -0700112const size_t kCoreSize = 0x100000;
113
Brian Silvermaneeb62ca2013-09-11 15:08:03 -0700114void TerminateExitHandler() {
115 _exit(EXIT_SUCCESS);
116}
117
Brian Silvermanb3616972013-03-05 19:58:10 -0800118} // namespace
brians343bc112013-02-10 01:53:46 +0000119
120GlobalCoreInstance::GlobalCoreInstance() {
brians343bc112013-02-10 01:53:46 +0000121 global_core = &global_core_data_;
Brian Silvermaneeb62ca2013-09-11 15:08:03 -0700122 global_core->owner = true;
123 // Use mmap(2) manually instead of through malloc(3) so that we can pass
124 // MAP_SHARED which allows forked processes to communicate using the
125 // "shared" memory.
Brian Silverman797e71e2013-09-06 17:29:39 -0700126 void *memory = mmap(NULL, kCoreSize, PROT_READ | PROT_WRITE,
127 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
Brian Silvermanfe457de2014-05-26 22:04:08 -0700128 CHECK_NE(memory, MAP_FAILED);
brians343bc112013-02-10 01:53:46 +0000129
Brian Silverman01be0002014-05-10 15:44:38 -0700130 aos_core_use_address_as_shared_mem(memory, kCoreSize);
Brian Silvermanb3616972013-03-05 19:58:10 -0800131
132 EnableTestLogging();
brians343bc112013-02-10 01:53:46 +0000133}
134
135GlobalCoreInstance::~GlobalCoreInstance() {
Brian Silvermanfe457de2014-05-26 22:04:08 -0700136 PCHECK(munmap(global_core->mem_struct, kCoreSize));
brians343bc112013-02-10 01:53:46 +0000137 global_core = NULL;
138}
139
Brian Silvermanb3616972013-03-05 19:58:10 -0800140void EnableTestLogging() {
141 enable_test_logging_once.Get();
142}
143
Brian Silvermaneeb62ca2013-09-11 15:08:03 -0700144void PreventExit() {
Brian Silvermanfe457de2014-05-26 22:04:08 -0700145 CHECK_EQ(atexit(TerminateExitHandler), 0);
Brian Silvermaneeb62ca2013-09-11 15:08:03 -0700146}
147
brians343bc112013-02-10 01:53:46 +0000148} // namespace testing
149} // namespace common
150} // namespace aos