blob: 629c9bedf68ac7da3645f37200cfad8e9f0659eb [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 Silvermanf665d692013-02-17 22:11:39 -08005
Brian Silvermanb3616972013-03-05 19:58:10 -08006#include "gtest/gtest.h"
7
brians343bc112013-02-10 01:53:46 +00008#include "aos/common/queue.h"
Brian Silvermanb3616972013-03-05 19:58:10 -08009#include "aos/common/logging/logging_impl.h"
10#include "aos/common/once.h"
11
12using ::aos::logging::LogMessage;
brians343bc112013-02-10 01:53:46 +000013
14namespace aos {
15namespace common {
16namespace testing {
Brian Silvermanb3616972013-03-05 19:58:10 -080017namespace {
18
19class TestLogImplementation : public logging::LogImplementation {
20 public:
21 const ::std::vector<LogMessage> &messages() { return messages_; }
22
23 // This class has to be a singleton so that everybody can get access to the
24 // same instance to read out the messages etc.
25 static TestLogImplementation *GetInstance() {
26 static Once<TestLogImplementation> once(CreateInstance);
27 return once.Get();
28 }
29
30 // Clears out all of the messages already recorded.
31 void ClearMessages() {
32 messages_.clear();
33 }
34
35 // Prints out all of the messages (like when a test fails).
36 void PrintAllMessages() {
37 for (auto it = messages_.begin(); it != messages_.end(); ++it) {
38 logging::internal::PrintMessage(stdout, *it);
39 }
40 }
41
42 private:
43 TestLogImplementation() {}
44
45 static TestLogImplementation *CreateInstance() {
46 return new TestLogImplementation();
47 }
48
49 virtual void DoLog(log_level level, const char *format, va_list ap) {
50 LogMessage message;
51
52 logging::internal::FillInMessage(level, format, ap, &message);
53
54 if (!logging::log_gt_important(WARNING, level)) {
55 logging::internal::PrintMessage(stdout, message);
56 }
57
58 messages_.push_back(message);
59 }
60
61 ::std::vector<LogMessage> messages_;
62};
63
64class MyTestEventListener : public ::testing::EmptyTestEventListener {
65 virtual void OnTestStart(const ::testing::TestInfo &/*test_info*/) {
66 TestLogImplementation::GetInstance()->ClearMessages();
67 }
68 virtual void OnTestEnd(const ::testing::TestInfo &test_info) {
69 if (test_info.result()->Failed()) {
70 printf("Test %s failed. Printing out all log messages.\n",
71 test_info.name());
72 fputs("\tThis will include already printed WARNING and up messages.\n",
73 stdout);
Brian Silvermanb91a37d2013-03-09 17:54:30 -080074 fputs("\tIt will also include duplicates of all gtest failures.\n",
75 stdout);
Brian Silvermanb3616972013-03-05 19:58:10 -080076 TestLogImplementation::GetInstance()->PrintAllMessages();
77 }
78 }
Brian Silvermanb91a37d2013-03-09 17:54:30 -080079
80 virtual void OnTestPartResult( const ::testing::TestPartResult &result) {
81 if (result.failed()) {
82 const char *failure_type = "unknown";
83 switch (result.type()) {
84 case ::testing::TestPartResult::Type::kNonFatalFailure:
85 failure_type = "EXPECT";
86 break;
87 case ::testing::TestPartResult::Type::kFatalFailure:
88 failure_type = "ASSERT";
89 break;
90 case ::testing::TestPartResult::Type::kSuccess:
91 break;
92 }
93 log_do(ERROR, "%s: %d: gtest %s failure\n%s\n",
94 result.file_name(),
95 result.line_number(),
96 failure_type,
97 result.message());
98 }
99 }
Brian Silvermanb3616972013-03-05 19:58:10 -0800100};
101
102void *DoEnableTestLogging() {
103 logging::Init();
104 logging::AddImplementation(TestLogImplementation::GetInstance());
105
106 ::testing::UnitTest::GetInstance()->listeners().Append(
107 new MyTestEventListener());
108
109 return NULL;
110}
111
112Once<void> enable_test_logging_once(DoEnableTestLogging);
113
Brian Silverman797e71e2013-09-06 17:29:39 -0700114const size_t kCoreSize = 0x100000;
115
Brian Silvermanb3616972013-03-05 19:58:10 -0800116} // namespace
brians343bc112013-02-10 01:53:46 +0000117
118GlobalCoreInstance::GlobalCoreInstance() {
brians343bc112013-02-10 01:53:46 +0000119 global_core = &global_core_data_;
120 global_core->owner = 1;
Brian Silverman797e71e2013-09-06 17:29:39 -0700121 // Use mmap(2) manually so that we can pass MAP_SHARED so that forked
122 // processes can still communicate using the "shared" memory.
123 void *memory = mmap(NULL, kCoreSize, PROT_READ | PROT_WRITE,
124 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
125 assert(memory != MAP_FAILED);
brians343bc112013-02-10 01:53:46 +0000126
127 assert(aos_core_use_address_as_shared_mem(memory, kCoreSize) == 0);
Brian Silvermanb3616972013-03-05 19:58:10 -0800128
129 EnableTestLogging();
brians343bc112013-02-10 01:53:46 +0000130}
131
132GlobalCoreInstance::~GlobalCoreInstance() {
Brian Silverman797e71e2013-09-06 17:29:39 -0700133 assert(munmap(global_core->mem_struct, kCoreSize) == 0);
brians343bc112013-02-10 01:53:46 +0000134 global_core = NULL;
135}
136
Brian Silvermanb3616972013-03-05 19:58:10 -0800137void EnableTestLogging() {
138 enable_test_logging_once.Get();
139}
140
brians343bc112013-02-10 01:53:46 +0000141} // namespace testing
142} // namespace common
143} // namespace aos