OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <set> |
| 6 #include <thread> |
| 7 #include <vector> |
| 8 |
| 9 #include "mojo/public/cpp/application/application_test_base.h" |
| 10 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 11 #include "mojo/public/cpp/system/macros.h" |
| 12 #include "mojo/public/cpp/utility/run_loop.h" |
| 13 #include "mojo/services/log/cpp/log_client.h" |
| 14 #include "mojo/services/log/interfaces/entry.mojom.h" |
| 15 #include "mojo/services/log/interfaces/log.mojom.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 using LogClientTest = mojo::test::ApplicationTestBase; |
| 19 using mojo::Environment; |
| 20 using mojo::log::LogClient; |
| 21 using mojo::log::LogPtr; |
| 22 using mojo::internal::ValidationError; |
| 23 |
| 24 namespace mojo { |
| 25 namespace { |
| 26 |
| 27 // A Log implementation that remembers the set of all incoming messages. |
| 28 class TestLogServiceImpl : public log::Log { |
| 29 public: |
| 30 explicit TestLogServiceImpl(InterfaceRequest<log::Log> log_req) |
| 31 : binding_(this, std::move(log_req)) { |
| 32 EXPECT_TRUE(binding_.is_bound()); |
| 33 binding_.set_connection_error_handler([this]() { |
| 34 FAIL() << "Log service lost connection to the log client."; |
| 35 }); |
| 36 validation_observer_.set_last_error(ValidationError::NONE); |
| 37 } |
| 38 void AddEntry(mojo::log::EntryPtr entry) override { |
| 39 entry_msgs_.insert(entry->message.To<std::string>()); |
| 40 } |
| 41 const std::set<std::string>& entries() { return entry_msgs_; } |
| 42 mojo::internal::ValidationError previous_validation_error() { |
| 43 return validation_observer_.last_error(); |
| 44 } |
| 45 |
| 46 private: |
| 47 mojo::StrongBinding<log::Log> binding_; |
| 48 std::set<std::string> entry_msgs_; |
| 49 mojo::internal::ValidationErrorObserverForTesting validation_observer_; |
| 50 }; |
| 51 |
| 52 // This is our test fallback logger + state. We simply records whether it's |
| 53 // been called. |
| 54 bool g_fallback_logger_invoked = false; |
| 55 MojoLogger g_fallback_logger = { |
| 56 [](MojoLogLevel log_level, |
| 57 const char* source_file, |
| 58 uint32_t source_line, |
| 59 const char* message) { g_fallback_logger_invoked = true; }, |
| 60 nullptr, nullptr}; |
| 61 |
| 62 } // namespace |
| 63 } // namespace mojo |
| 64 |
| 65 // This tests that multiple threads can use the MojoLogger that |
| 66 // mojo::log::LogClient produces, by spawning off |kNumLogEntries| threads, each |
| 67 // issuing one unique log message. |
| 68 TEST_F(LogClientTest, ConcurrentAddEntry) { |
| 69 LogPtr log_ptr; |
| 70 std::unique_ptr<mojo::TestLogServiceImpl> log_impl( |
| 71 new mojo::TestLogServiceImpl(mojo::GetProxy(&log_ptr))); |
| 72 |
| 73 LogClient lc(std::move(log_ptr), &mojo::g_fallback_logger); |
| 74 Environment::SetDefaultLogger(LogClient::GetLogger()); |
| 75 |
| 76 // Spawn off numerous threads, each of them issuing a unique log message. |
| 77 std::vector<std::thread> threads; |
| 78 std::set<std::string> expected_entries; |
| 79 // The number of log entries to issue. |
| 80 const int kNumLogEntries = 1000; |
| 81 for (int i = 0; i < kNumLogEntries; i++) { |
| 82 std::stringstream msg; |
| 83 msg << "Test message: " << i; |
| 84 EXPECT_TRUE(expected_entries.insert(msg.str()).second); |
| 85 |
| 86 std::thread t([](std::string msg) { MOJO_LOG(INFO) << msg; }, msg.str()); |
| 87 |
| 88 threads.push_back(std::move(t)); |
| 89 } |
| 90 for (auto& t : threads) { |
| 91 t.join(); |
| 92 } |
| 93 |
| 94 // The log message calls should now be processed by TestLogServiceImpl. |
| 95 mojo::RunLoop::current()->RunUntilIdle(); |
| 96 |
| 97 EXPECT_EQ(expected_entries, log_impl->entries()); |
| 98 EXPECT_EQ(ValidationError::NONE, log_impl->previous_validation_error()); |
| 99 |
| 100 // We kill our binding, closing the connection to the log client and |
| 101 // causing the log client to revert to using its fallback logger. |
| 102 log_impl.reset(); |
| 103 |
| 104 EXPECT_FALSE(mojo::g_fallback_logger_invoked); |
| 105 MOJO_LOG(INFO) << "Ignore this log message."; |
| 106 EXPECT_TRUE(mojo::g_fallback_logger_invoked); |
| 107 } |
OLD | NEW |