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 | |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame^] | 44 | void SetOutputFile(const char *filename) { |
| 45 | if (strcmp("-", filename) != 0) { |
| 46 | FILE *newfile = fopen(filename, "w"); |
| 47 | |
| 48 | if (newfile) { |
| 49 | output_file_ = newfile; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void PrintMessagesAsTheyComeIn() { print_as_messages_come_in_ = true; } |
| 55 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 56 | private: |
| 57 | TestLogImplementation() {} |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame^] | 58 | ~TestLogImplementation() { |
| 59 | if (output_file_ != stdout) { |
| 60 | fclose(output_file_); |
| 61 | } |
| 62 | } |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 63 | |
| 64 | static TestLogImplementation *CreateInstance() { |
| 65 | return new TestLogImplementation(); |
| 66 | } |
| 67 | |
Brian Silverman | be858a1 | 2014-04-30 17:37:28 -0700 | [diff] [blame] | 68 | virtual void HandleMessage(const LogMessage &message) override { |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame^] | 69 | if (message.level == FATAL || print_as_messages_come_in_) { |
| 70 | logging::internal::PrintMessage(output_file_, message); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | messages_.push_back(message); |
| 74 | } |
| 75 | |
| 76 | ::std::vector<LogMessage> messages_; |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame^] | 77 | bool print_as_messages_come_in_ = false; |
| 78 | FILE *output_file_ = stdout; |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | class MyTestEventListener : public ::testing::EmptyTestEventListener { |
| 82 | virtual void OnTestStart(const ::testing::TestInfo &/*test_info*/) { |
| 83 | TestLogImplementation::GetInstance()->ClearMessages(); |
| 84 | } |
| 85 | virtual void OnTestEnd(const ::testing::TestInfo &test_info) { |
| 86 | if (test_info.result()->Failed()) { |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame^] | 87 | printf("Test %s failed. Use '--print-logs' to see all log messages.\n", |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 88 | test_info.name()); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
Brian Silverman | b91a37d | 2013-03-09 17:54:30 -0800 | [diff] [blame] | 91 | |
| 92 | virtual void OnTestPartResult( const ::testing::TestPartResult &result) { |
| 93 | if (result.failed()) { |
| 94 | const char *failure_type = "unknown"; |
| 95 | switch (result.type()) { |
| 96 | case ::testing::TestPartResult::Type::kNonFatalFailure: |
| 97 | failure_type = "EXPECT"; |
| 98 | break; |
| 99 | case ::testing::TestPartResult::Type::kFatalFailure: |
| 100 | failure_type = "ASSERT"; |
| 101 | break; |
| 102 | case ::testing::TestPartResult::Type::kSuccess: |
| 103 | break; |
| 104 | } |
| 105 | log_do(ERROR, "%s: %d: gtest %s failure\n%s\n", |
| 106 | result.file_name(), |
| 107 | result.line_number(), |
| 108 | failure_type, |
| 109 | result.message()); |
| 110 | } |
| 111 | } |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | void *DoEnableTestLogging() { |
| 115 | logging::Init(); |
| 116 | logging::AddImplementation(TestLogImplementation::GetInstance()); |
| 117 | |
| 118 | ::testing::UnitTest::GetInstance()->listeners().Append( |
| 119 | new MyTestEventListener()); |
| 120 | |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | Once<void> enable_test_logging_once(DoEnableTestLogging); |
| 125 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 126 | const size_t kCoreSize = 0x100000; |
| 127 | |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 128 | void TerminateExitHandler() { |
| 129 | _exit(EXIT_SUCCESS); |
| 130 | } |
| 131 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 132 | } // namespace |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 133 | |
| 134 | GlobalCoreInstance::GlobalCoreInstance() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 135 | global_core = &global_core_data_; |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 136 | global_core->owner = true; |
| 137 | // Use mmap(2) manually instead of through malloc(3) so that we can pass |
| 138 | // MAP_SHARED which allows forked processes to communicate using the |
| 139 | // "shared" memory. |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 140 | void *memory = mmap(NULL, kCoreSize, PROT_READ | PROT_WRITE, |
| 141 | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 142 | CHECK_NE(memory, MAP_FAILED); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 143 | |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 144 | aos_core_use_address_as_shared_mem(memory, kCoreSize); |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 145 | |
| 146 | EnableTestLogging(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | GlobalCoreInstance::~GlobalCoreInstance() { |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 150 | PCHECK(munmap(global_core->mem_struct, kCoreSize)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 151 | global_core = NULL; |
| 152 | } |
| 153 | |
Brian Silverman | b361697 | 2013-03-05 19:58:10 -0800 | [diff] [blame] | 154 | void EnableTestLogging() { |
| 155 | enable_test_logging_once.Get(); |
| 156 | } |
| 157 | |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 158 | void PreventExit() { |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 159 | CHECK_EQ(atexit(TerminateExitHandler), 0); |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Philipp Schrader | e41ed9d | 2015-03-15 22:57:13 +0000 | [diff] [blame^] | 162 | void SetLogFileName(const char* filename) { |
| 163 | TestLogImplementation::GetInstance()->SetOutputFile(filename); |
| 164 | } |
| 165 | |
| 166 | void ForcePrintLogsDuringTests() { |
| 167 | TestLogImplementation::GetInstance()->PrintMessagesAsTheyComeIn(); |
| 168 | } |
| 169 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 170 | } // namespace testing |
| 171 | } // namespace common |
| 172 | } // namespace aos |