mock_log.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: Zhanyong Wan
  31. //
  32. // Defines the ScopedMockLog class (using Google C++ Mocking
  33. // Framework), which is convenient for testing code that uses LOG().
  34. //
  35. // NOTE(keir): This is a fork until Google Log exports the scoped mock log
  36. // class; see: http://code.google.com/p/google-glog/issues/detail?id=88
  37. #ifndef GOOGLE_CERES_INTERNAL_MOCK_LOG_H_
  38. #define GOOGLE_CERES_INTERNAL_MOCK_LOG_H_
  39. #include <string>
  40. #include "gmock/gmock.h"
  41. #include "glog/logging.h"
  42. // Needed to make the scoped mock log tests work without modification.
  43. namespace ceres {
  44. namespace internal {
  45. using google::WARNING;
  46. } // namespace internal
  47. } // namespace ceres
  48. namespace testing {
  49. // A ScopedMockLog object intercepts LOG() messages issued during its
  50. // lifespan. Using this together with Google C++ Mocking Framework,
  51. // it's very easy to test how a piece of code calls LOG(). The
  52. // typical usage:
  53. //
  54. // TEST(FooTest, LogsCorrectly) {
  55. // ScopedMockLog log;
  56. //
  57. // // We expect the WARNING "Something bad!" exactly twice.
  58. // EXPECT_CALL(log, Log(WARNING, _, "Something bad!"))
  59. // .Times(2);
  60. //
  61. // // We allow foo.cc to call LOG(INFO) any number of times.
  62. // EXPECT_CALL(log, Log(INFO, HasSubstr("/foo.cc"), _))
  63. // .Times(AnyNumber());
  64. //
  65. // Foo(); // Exercises the code under test.
  66. // }
  67. class ScopedMockLog : public google::LogSink {
  68. public:
  69. // When a ScopedMockLog object is constructed, it starts to
  70. // intercept logs.
  71. ScopedMockLog()
  72. {
  73. AddLogSink(this);
  74. }
  75. // When the object is destructed, it stops intercepting logs.
  76. virtual ~ScopedMockLog()
  77. {
  78. RemoveLogSink(this);
  79. }
  80. // Implements the mock method:
  81. //
  82. // void Log(LogSeverity severity, const string& file_path,
  83. // const string& message);
  84. //
  85. // The second argument to Send() is the full path of the source file
  86. // in which the LOG() was issued.
  87. //
  88. // Note, that in a multi-threaded environment, all LOG() messages from a
  89. // single thread will be handled in sequence, but that cannot be guaranteed
  90. // for messages from different threads. In fact, if the same or multiple
  91. // expectations are matched on two threads concurrently, their actions will
  92. // be executed concurrently as well and may interleave.
  93. MOCK_METHOD3(Log,
  94. void(google::LogSeverity severity,
  95. const std::string &file_path,
  96. const std::string &message));
  97. private:
  98. // Implements the send() virtual function in class LogSink.
  99. // Whenever a LOG() statement is executed, this function will be
  100. // invoked with information presented in the LOG().
  101. //
  102. // The method argument list is long and carries much information a
  103. // test usually doesn't care about, so we trim the list before
  104. // forwarding the call to Log(), which is much easier to use in
  105. // tests.
  106. //
  107. // We still cannot call Log() directly, as it may invoke other LOG()
  108. // messages, either due to Invoke, or due to an error logged in
  109. // Google C++ Mocking Framework code, which would trigger a deadlock
  110. // since a lock is held during send().
  111. //
  112. // Hence, we save the message for WaitTillSent() which will be called after
  113. // the lock on send() is released, and we'll call Log() inside
  114. // WaitTillSent(). Since while a single send() call may be running at a
  115. // time, multiple WaitTillSent() calls (along with the one send() call) may
  116. // be running simultaneously, we ensure thread-safety of the exchange between
  117. // send() and WaitTillSent(), and that for each message, LOG(), send(),
  118. // WaitTillSent() and Log() are executed in the same thread.
  119. virtual void send(google::LogSeverity severity,
  120. const char *full_filename,
  121. const char * /*base_filename*/,
  122. int /*line*/,
  123. const tm * /*tm_time*/,
  124. const char *message,
  125. size_t message_len)
  126. {
  127. // We are only interested in the log severity, full file name, and
  128. // log message.
  129. message_info_.severity = severity;
  130. message_info_.file_path = full_filename;
  131. message_info_.message = std::string(message, message_len);
  132. }
  133. // Implements the WaitTillSent() virtual function in class LogSink.
  134. // It will be executed after send() and after the global logging lock is
  135. // released, so calls within it (or rather within the Log() method called
  136. // within) may also issue LOG() statements.
  137. //
  138. // LOG(), send(), WaitTillSent() and Log() will occur in the same thread for
  139. // a given log message.
  140. virtual void WaitTillSent()
  141. {
  142. // First, and very importantly, we save a copy of the message being
  143. // processed before calling Log(), since Log() may indirectly call send()
  144. // and WaitTillSent() in the same thread again.
  145. MessageInfo message_info = message_info_;
  146. Log(message_info.severity, message_info.file_path, message_info.message);
  147. }
  148. // All relevant information about a logged message that needs to be passed
  149. // from send() to WaitTillSent().
  150. struct MessageInfo {
  151. google::LogSeverity severity;
  152. std::string file_path;
  153. std::string message;
  154. };
  155. MessageInfo message_info_;
  156. };
  157. } // namespace testing
  158. #endif // GOOGLE_CERES_INTERNAL_MOCK_LOG_H_