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