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> |
| 7 | #include <assert.h> |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 8 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 9 | #include "gtest/gtest.h" |
| 10 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 11 | #include "aos/common/queue.h" |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 12 | #include "aos/common/logging/logging_impl.h" |
| 13 | #include "aos/common/once.h" |
| 14 | |
| 15 | using ::aos::logging::LogMessage; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 16 | |
| 17 | namespace aos { |
| 18 | namespace common { |
| 19 | namespace testing { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 20 | namespace { |
| 21 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame^] | 22 | class TestLogImplementation : public logging::HandleMessageLogImplementation { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 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 Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame^] | 52 | virtual void HandleMessage(const LogMessage &message) override { |
| 53 | if (!logging::log_gt_important(WARNING, message.level)) { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 54 | logging::internal::PrintMessage(stdout, message); |
| 55 | } |
| 56 | |
| 57 | messages_.push_back(message); |
| 58 | } |
| 59 | |
| 60 | ::std::vector<LogMessage> messages_; |
| 61 | }; |
| 62 | |
| 63 | class MyTestEventListener : public ::testing::EmptyTestEventListener { |
| 64 | virtual void OnTestStart(const ::testing::TestInfo &/*test_info*/) { |
| 65 | TestLogImplementation::GetInstance()->ClearMessages(); |
| 66 | } |
| 67 | virtual void OnTestEnd(const ::testing::TestInfo &test_info) { |
| 68 | if (test_info.result()->Failed()) { |
| 69 | printf("Test %s failed. Printing out all log messages.\n", |
| 70 | test_info.name()); |
| 71 | fputs("\tThis will include already printed WARNING and up messages.\n", |
| 72 | stdout); |
Brian Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 73 | fputs("\tIt will also include duplicates of all gtest failures.\n", |
| 74 | stdout); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 75 | TestLogImplementation::GetInstance()->PrintAllMessages(); |
| 76 | } |
| 77 | } |
Brian Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 78 | |
| 79 | virtual void OnTestPartResult( const ::testing::TestPartResult &result) { |
| 80 | if (result.failed()) { |
| 81 | const char *failure_type = "unknown"; |
| 82 | switch (result.type()) { |
| 83 | case ::testing::TestPartResult::Type::kNonFatalFailure: |
| 84 | failure_type = "EXPECT"; |
| 85 | break; |
| 86 | case ::testing::TestPartResult::Type::kFatalFailure: |
| 87 | failure_type = "ASSERT"; |
| 88 | break; |
| 89 | case ::testing::TestPartResult::Type::kSuccess: |
| 90 | break; |
| 91 | } |
| 92 | log_do(ERROR, "%s: %d: gtest %s failure\n%s\n", |
| 93 | result.file_name(), |
| 94 | result.line_number(), |
| 95 | failure_type, |
| 96 | result.message()); |
| 97 | } |
| 98 | } |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | void *DoEnableTestLogging() { |
| 102 | logging::Init(); |
| 103 | logging::AddImplementation(TestLogImplementation::GetInstance()); |
| 104 | |
| 105 | ::testing::UnitTest::GetInstance()->listeners().Append( |
| 106 | new MyTestEventListener()); |
| 107 | |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | Once<void> enable_test_logging_once(DoEnableTestLogging); |
| 112 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 113 | const size_t kCoreSize = 0x100000; |
| 114 | |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 115 | void TerminateExitHandler() { |
| 116 | _exit(EXIT_SUCCESS); |
| 117 | } |
| 118 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 119 | } // namespace |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 120 | |
| 121 | GlobalCoreInstance::GlobalCoreInstance() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 122 | global_core = &global_core_data_; |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 123 | global_core->owner = true; |
| 124 | // Use mmap(2) manually instead of through malloc(3) so that we can pass |
| 125 | // MAP_SHARED which allows forked processes to communicate using the |
| 126 | // "shared" memory. |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 127 | void *memory = mmap(NULL, kCoreSize, PROT_READ | PROT_WRITE, |
| 128 | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 129 | assert(memory != MAP_FAILED); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 130 | |
| 131 | assert(aos_core_use_address_as_shared_mem(memory, kCoreSize) == 0); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 132 | |
| 133 | EnableTestLogging(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | GlobalCoreInstance::~GlobalCoreInstance() { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 137 | assert(munmap(global_core->mem_struct, kCoreSize) == 0); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 138 | global_core = NULL; |
| 139 | } |
| 140 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 141 | void EnableTestLogging() { |
| 142 | enable_test_logging_once.Get(); |
| 143 | } |
| 144 | |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 145 | void PreventExit() { |
| 146 | assert(atexit(TerminateExitHandler) == 0); |
| 147 | } |
| 148 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 149 | } // namespace testing |
| 150 | } // namespace common |
| 151 | } // namespace aos |