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