brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include "aos/common/queue_testutils.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 2 | |
| 3 | #include <string.h> |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 4 | #include <sys/mman.h> |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 5 | #include <unistd.h> |
| 6 | #include <stdlib.h> |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 7 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 8 | #include "gtest/gtest.h" |
| 9 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | #include "aos/common/queue.h" |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 11 | #include "aos/common/logging/logging_impl.h" |
| 12 | #include "aos/common/once.h" |
| 13 | |
| 14 | using ::aos::logging::LogMessage; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | |
| 16 | namespace aos { |
| 17 | namespace common { |
| 18 | namespace testing { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 19 | namespace { |
| 20 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 21 | class TestLogImplementation : public logging::HandleMessageLogImplementation { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 22 | 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 Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 51 | virtual void HandleMessage(const LogMessage &message) override { |
Brian Silverman | 1094ee0 | 2014-05-03 10:34:01 -0700 | [diff] [blame] | 52 | if (message.level == FATAL) { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 53 | logging::internal::PrintMessage(stdout, message); |
| 54 | } |
| 55 | |
| 56 | messages_.push_back(message); |
| 57 | } |
| 58 | |
| 59 | ::std::vector<LogMessage> messages_; |
| 60 | }; |
| 61 | |
| 62 | class 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 Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 72 | fputs("\tIt will also include duplicates of all gtest failures.\n", |
| 73 | stdout); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 74 | TestLogImplementation::GetInstance()->PrintAllMessages(); |
| 75 | } |
| 76 | } |
Brian Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 77 | |
| 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 Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | void *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 | |
| 110 | Once<void> enable_test_logging_once(DoEnableTestLogging); |
| 111 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 112 | const size_t kCoreSize = 0x100000; |
| 113 | |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 114 | void TerminateExitHandler() { |
| 115 | _exit(EXIT_SUCCESS); |
| 116 | } |
| 117 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 118 | } // namespace |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 119 | |
| 120 | GlobalCoreInstance::GlobalCoreInstance() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 121 | global_core = &global_core_data_; |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 122 | 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 126 | void *memory = mmap(NULL, kCoreSize, PROT_READ | PROT_WRITE, |
| 127 | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 128 | CHECK_NE(memory, MAP_FAILED); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 129 | |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 130 | aos_core_use_address_as_shared_mem(memory, kCoreSize); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 131 | |
| 132 | EnableTestLogging(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | GlobalCoreInstance::~GlobalCoreInstance() { |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 136 | PCHECK(munmap(global_core->mem_struct, kCoreSize)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 137 | global_core = NULL; |
| 138 | } |
| 139 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 140 | void EnableTestLogging() { |
| 141 | enable_test_logging_once.Get(); |
| 142 | } |
| 143 | |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 144 | void PreventExit() { |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 145 | CHECK_EQ(atexit(TerminateExitHandler), 0); |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 146 | } |
| 147 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 148 | } // namespace testing |
| 149 | } // namespace common |
| 150 | } // namespace aos |