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 | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 5 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 6 | #include "gtest/gtest.h" |
| 7 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | #include "aos/common/queue.h" |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 9 | #include "aos/common/logging/logging_impl.h" |
| 10 | #include "aos/common/once.h" |
| 11 | |
| 12 | using ::aos::logging::LogMessage; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 13 | |
| 14 | namespace aos { |
| 15 | namespace common { |
| 16 | namespace testing { |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | class 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 | |
| 64 | class 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 Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 74 | fputs("\tIt will also include duplicates of all gtest failures.\n", |
| 75 | stdout); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 76 | TestLogImplementation::GetInstance()->PrintAllMessages(); |
| 77 | } |
| 78 | } |
Brian Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 79 | |
| 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 Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | void *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 | |
| 112 | Once<void> enable_test_logging_once(DoEnableTestLogging); |
| 113 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame^] | 114 | const size_t kCoreSize = 0x100000; |
| 115 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 116 | } // namespace |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 117 | |
| 118 | GlobalCoreInstance::GlobalCoreInstance() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 119 | global_core = &global_core_data_; |
| 120 | global_core->owner = 1; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame^] | 121 | // 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); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 126 | |
| 127 | assert(aos_core_use_address_as_shared_mem(memory, kCoreSize) == 0); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 128 | |
| 129 | EnableTestLogging(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | GlobalCoreInstance::~GlobalCoreInstance() { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame^] | 133 | assert(munmap(global_core->mem_struct, kCoreSize) == 0); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 134 | global_core = NULL; |
| 135 | } |
| 136 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 137 | void EnableTestLogging() { |
| 138 | enable_test_logging_once.Get(); |
| 139 | } |
| 140 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 141 | } // namespace testing |
| 142 | } // namespace common |
| 143 | } // namespace aos |