diff options
author | lukasza <lukasza@chromium.org> | 2015-03-04 11:47:12 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-04 19:48:01 +0000 |
commit | b4d01d31b4553b10aae55b66476e5ab9280b1c9d (patch) | |
tree | 37685ec8eb2ea3150e44e5306a091ed273c35bf1 /net/test/scoped_mock_log.h | |
parent | d773a075d63461f127153766608ec8988d2feb46 (diff) | |
download | chromium_src-b4d01d31b4553b10aae55b66476e5ab9280b1c9d.zip chromium_src-b4d01d31b4553b10aae55b66476e5ab9280b1c9d.tar.gz chromium_src-b4d01d31b4553b10aae55b66476e5ab9280b1c9d.tar.bz2 |
Moving ScopedMockLog from net/test to base/test.
I want to reuse ScopedMockLog from unittests under src/remoting/host.
To do that, I am moving this class from net/test to base/test.
When doing the move, I also wanted to ensure thread-safety in the case when
logging is happening on a thread different from where StartCapturingLogs or
StopCapturingLogs are called. Having proper locks (and memory barriers implied
by the locks) should ensure that 1) LogMessageHandler won't see a half-way
executed StartCapturingLogs or StopCapturingLogs and 2) that a log write
in-progress won't get a rug pulled from underneath by destroying of gMock's
structures embedded in ScopedMockLog's Log mock method.
BUG=
TEST=net_unittests
Review URL: https://codereview.chromium.org/966423003
Cr-Commit-Position: refs/heads/master@{#319105}
Diffstat (limited to 'net/test/scoped_mock_log.h')
-rw-r--r-- | net/test/scoped_mock_log.h | 98 |
1 files changed, 0 insertions, 98 deletions
diff --git a/net/test/scoped_mock_log.h b/net/test/scoped_mock_log.h deleted file mode 100644 index e1edfcc..0000000 --- a/net/test/scoped_mock_log.h +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright 2014 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. - -#ifndef NET_QUIC_TEST_TOOLS_SCOPED_MOCK_LOG_H_ -#define NET_QUIC_TEST_TOOLS_SCOPED_MOCK_LOG_H_ - -#include "base/logging.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace net { -namespace test { - -// A ScopedMockLog object intercepts LOG() messages issued during its -// lifespan. Using this together with gMock, it's very easy to test -// how a piece of code calls LOG(). The typical usage: -// -// TEST(FooTest, LogsCorrectly) { -// ScopedMockLog log; -// -// // We expect the WARNING "Something bad!" exactly twice. -// EXPECT_CALL(log, Log(WARNING, _, "Something bad!")) -// .Times(2); -// -// // We allow foo.cc to call LOG(INFO) any number of times. -// EXPECT_CALL(log, Log(INFO, HasSubstr("/foo.cc"), _)) -// .Times(AnyNumber()); -// -// log.StartCapturingLogs(); // Call this after done setting expectations. -// Foo(); // Exercises the code under test. -// } -// -// CAVEAT: base/logging does not allow a thread to call LOG() again -// when it's already inside a LOG() call. Doing so will cause a -// deadlock. Therefore, it's the user's responsibility to not call -// LOG() in an action triggered by ScopedMockLog::Log(). You may call -// RAW_LOG() instead. -class ScopedMockLog { - public: - // Creates a ScopedMockLog object that is not capturing logs. - // If it were to start to capture logs, it could be a problem if - // some other threads already exist and are logging, as the user - // hasn't had a chance to set up expectation on this object yet - // (calling a mock method before setting the expectation is - // UNDEFINED behavior). - ScopedMockLog(); - - // When the object is destructed, it stops intercepting logs. - ~ScopedMockLog(); - - // Starts log capturing if the object isn't already doing so. - // Otherwise crashes. Usually this method is called in the same - // thread that created this object. It is the user's responsibility - // to not call this method if another thread may be calling it or - // StopCapturingLogs() at the same time. - void StartCapturingLogs(); - - // Stops log capturing if the object is capturing logs. Otherwise - // crashes. Usually this method is called in the same thread that - // created this object. It is the user's responsibility to not call - // this method if another thread may be calling it or - // StartCapturingLogs() at the same time. - void StopCapturingLogs(); - - // Sets the Log Message Handler that gets passed every log message before - // it's sent to other log destinations (if any). - // Returns true to signal that it handled the message and the message - // should not be sent to other log destinations. - MOCK_METHOD5(Log, bool(int severity, - const char* file, - int line, - size_t message_start, - const std::string& str)); - - private: - // The currently active scoped mock log. - static ScopedMockLog* g_instance_; - - // Static function which is set as the logging message handler. - // Called once for each message. - static bool LogMessageHandler(int severity, - const char* file, - int line, - size_t message_start, - const std::string& str); - - // True if this object is currently capturing logs. - bool is_capturing_logs_; - - // The previous handler to restore when the ScopedMockLog is destroyed. - logging::LogMessageHandlerFunction previous_handler_; -}; - -} // namespace test -} // namespace net - -#endif // NET_QUIC_TEST_TOOLS_SCOPED_MOCK_LOG_H_ |