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 |
| 23 namespace mojo { |
| 24 namespace { |
| 25 |
| 26 // A Log implementation that remembers the set of all incoming messages. |
| 27 class TestLogServiceImpl : public log::Log { |
| 28 public: |
| 29 explicit TestLogServiceImpl(InterfaceRequest<log::Log> log_req) |
| 30 : binding_(this, std::move(log_req)) {} |
| 31 void AddEntry(mojo::log::EntryPtr entry) override { |
| 32 entry_msgs_.insert(entry->message.To<std::string>()); |
| 33 } |
| 34 const std::set<std::string>& entries() { return entry_msgs_; } |
| 35 |
| 36 private: |
| 37 mojo::StrongBinding<log::Log> binding_; |
| 38 std::set<std::string> entry_msgs_; |
| 39 }; |
| 40 |
| 41 // The number of log entries to issue. |
| 42 const int kNumLogEntries = 1000; |
| 43 |
| 44 // This is our test fallback logger + state. We simply records whether it's |
| 45 // been called. |
| 46 bool g_fallback_logger_invoked = false; |
| 47 MojoLogger g_fallback_logger = { |
| 48 [](MojoLogLevel log_level, |
| 49 const char* source_file, |
| 50 uint32_t source_line, |
| 51 const char* message) { g_fallback_logger_invoked = true; }, |
| 52 nullptr, nullptr}; |
| 53 |
| 54 } // namespace |
| 55 } // namespace mojo |
| 56 |
| 57 // This tests that multiple threads can use the MojoLogger that |
| 58 // mojo::log::LogClient produces, by spawning off |kNumLogEntries| threads, each |
| 59 // issuing one unique log message. |
| 60 TEST_F(LogClientTest, ConcurrentAddEntry) { |
| 61 LogPtr log_ptr; |
| 62 std::unique_ptr<mojo::TestLogServiceImpl> log_impl( |
| 63 new mojo::TestLogServiceImpl(mojo::GetProxy(&log_ptr))); |
| 64 |
| 65 LogClient lc(std::move(log_ptr), &mojo::g_fallback_logger); |
| 66 Environment::SetDefaultLogger(LogClient::GetLogger()); |
| 67 |
| 68 // Spawn off numerous threads, each of them issuing a unique log message. |
| 69 std::vector<std::thread> threads; |
| 70 std::set<std::string> expected_entries; |
| 71 for (int i = 0; i < mojo::kNumLogEntries; i++) { |
| 72 std::stringstream msg; |
| 73 msg << "Test message: " << i; |
| 74 EXPECT_TRUE(expected_entries.insert(msg.str()).second); |
| 75 |
| 76 std::thread t([](std::string msg) { MOJO_LOG(INFO) << msg; }, msg.str()); |
| 77 |
| 78 threads.push_back(std::move(t)); |
| 79 } |
| 80 for (auto& t : threads) { |
| 81 t.join(); |
| 82 } |
| 83 |
| 84 // The log message calls should now be processed by TestLogServiceImpl. |
| 85 mojo::RunLoop::current()->RunUntilIdle(); |
| 86 |
| 87 EXPECT_EQ(expected_entries, log_impl->entries()); |
| 88 |
| 89 // We kill our binding, closing the connection to the log client and |
| 90 // causing the log client to revert to using its fallback logger. |
| 91 log_impl.reset(); |
| 92 |
| 93 EXPECT_FALSE(mojo::g_fallback_logger_invoked); |
| 94 MOJO_LOG(INFO) << "Ignore this log message."; |
| 95 EXPECT_TRUE(mojo::g_fallback_logger_invoked); |
| 96 } |
OLD | NEW |