| Index: mojo/services/log/cpp/tests/log_client_unittest.cc
|
| diff --git a/mojo/services/log/cpp/tests/log_client_unittest.cc b/mojo/services/log/cpp/tests/log_client_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..3538bce593e58da50eac1584600bbb2451e114e7
|
| --- /dev/null
|
| +++ b/mojo/services/log/cpp/tests/log_client_unittest.cc
|
| @@ -0,0 +1,96 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include <set>
|
| +#include <thread>
|
| +#include <vector>
|
| +
|
| +#include "mojo/public/cpp/application/application_test_base.h"
|
| +#include "mojo/public/cpp/bindings/strong_binding.h"
|
| +#include "mojo/public/cpp/system/macros.h"
|
| +#include "mojo/public/cpp/utility/run_loop.h"
|
| +#include "mojo/services/log/cpp/log_client.h"
|
| +#include "mojo/services/log/interfaces/entry.mojom.h"
|
| +#include "mojo/services/log/interfaces/log.mojom.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +using LogClientTest = mojo::test::ApplicationTestBase;
|
| +using mojo::Environment;
|
| +using mojo::log::LogClient;
|
| +using mojo::log::LogPtr;
|
| +
|
| +namespace mojo {
|
| +namespace {
|
| +
|
| +// A Log implementation that remembers the set of all incoming messages.
|
| +class TestLogServiceImpl : public log::Log {
|
| + public:
|
| + explicit TestLogServiceImpl(InterfaceRequest<log::Log> log_req)
|
| + : binding_(this, std::move(log_req)) {}
|
| + void AddEntry(mojo::log::EntryPtr entry) override {
|
| + entry_msgs_.insert(entry->message.To<std::string>());
|
| + }
|
| + const std::set<std::string>& entries() { return entry_msgs_; }
|
| +
|
| + private:
|
| + mojo::StrongBinding<log::Log> binding_;
|
| + std::set<std::string> entry_msgs_;
|
| +};
|
| +
|
| +// The number of log entries to issue.
|
| +const int kNumLogEntries = 1000;
|
| +
|
| +// This is our test fallback logger + state. We simply records whether it's
|
| +// been called.
|
| +bool g_fallback_logger_invoked = false;
|
| +MojoLogger g_fallback_logger = {
|
| + [](MojoLogLevel log_level,
|
| + const char* source_file,
|
| + uint32_t source_line,
|
| + const char* message) { g_fallback_logger_invoked = true; },
|
| + nullptr, nullptr};
|
| +
|
| +} // namespace
|
| +} // namespace mojo
|
| +
|
| +// This tests that multiple threads can use the MojoLogger that
|
| +// mojo::log::LogClient produces, by spawning off |kNumLogEntries| threads, each
|
| +// issuing one unique log message.
|
| +TEST_F(LogClientTest, ConcurrentAddEntry) {
|
| + LogPtr log_ptr;
|
| + std::unique_ptr<mojo::TestLogServiceImpl> log_impl(
|
| + new mojo::TestLogServiceImpl(mojo::GetProxy(&log_ptr)));
|
| +
|
| + LogClient lc(std::move(log_ptr), &mojo::g_fallback_logger);
|
| + Environment::SetDefaultLogger(LogClient::GetLogger());
|
| +
|
| + // Spawn off numerous threads, each of them issuing a unique log message.
|
| + std::vector<std::thread> threads;
|
| + std::set<std::string> expected_entries;
|
| + for (int i = 0; i < mojo::kNumLogEntries; i++) {
|
| + std::stringstream msg;
|
| + msg << "Test message: " << i;
|
| + EXPECT_TRUE(expected_entries.insert(msg.str()).second);
|
| +
|
| + std::thread t([](std::string msg) { MOJO_LOG(INFO) << msg; }, msg.str());
|
| +
|
| + threads.push_back(std::move(t));
|
| + }
|
| + for (auto& t : threads) {
|
| + t.join();
|
| + }
|
| +
|
| + // The log message calls should now be processed by TestLogServiceImpl.
|
| + mojo::RunLoop::current()->RunUntilIdle();
|
| +
|
| + EXPECT_EQ(expected_entries, log_impl->entries());
|
| +
|
| + // We kill our binding, closing the connection to the log client and
|
| + // causing the log client to revert to using its fallback logger.
|
| + log_impl.reset();
|
| +
|
| + EXPECT_FALSE(mojo::g_fallback_logger_invoked);
|
| + MOJO_LOG(INFO) << "Ignore this log message.";
|
| + EXPECT_TRUE(mojo::g_fallback_logger_invoked);
|
| +}
|
|
|