[Libreoffice-commits] online.git: test/helpers.hpp test/test.cpp

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Mon Mar 4 21:32:52 UTC 2019


 test/helpers.hpp |   17 +++++++++++++----
 test/test.cpp    |   17 +++++++++++------
 2 files changed, 24 insertions(+), 10 deletions(-)

New commits:
commit 0c46b33e7213bf4f91d6f93ad73da9dc79718164
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Mon Mar 4 22:02:59 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Mon Mar 4 22:04:46 2019 +0100

    Don't use the same std::ostringstream from multiple threads.
    
    Avoids tests crashing with obscure memory errors re-allocating the
    string buffer; and hopefully cleans up the output too.
    
    Change-Id: I3e38680c15129e84f0c7dd8cada3b505cf08ad34

diff --git a/test/helpers.hpp b/test/helpers.hpp
index bc4818d98..592d61b0e 100644
--- a/test/helpers.hpp
+++ b/test/helpers.hpp
@@ -37,11 +37,20 @@
 #error TDOC must be defined (see Makefile.am)
 #endif
 
-// Logging in unit-tests go to cerr, for now at least.
-#define TST_LOG_NAME_BEGIN(NAME, X) do { std::cerr << NAME << "(@" << helpers::timeSinceTestStartMs() << "ms) " << X; } while (false)
+// Oh dear std::cerr and/or its re-direction is not
+// necessarily thread safe on Linux
+#ifdef TST_LOG_REDIRECT
+  void tstLog(const std::ostringstream &stream);
+#else
+  inline void tstLog(const std::ostringstream &stream)
+  {
+      fprintf(stderr, "%s", stream.str().c_str());
+  }
+#endif
+#define TST_LOG_NAME_BEGIN(NAME, X) do { std::ostringstream str; str << NAME << "(@" << helpers::timeSinceTestStartMs() << "ms) " << X; tstLog(str); } while (false)
 #define TST_LOG_BEGIN(X) TST_LOG_NAME_BEGIN(testname, X)
-#define TST_LOG_APPEND(X) do { std::cerr << X; } while (false)
-#define TST_LOG_END do { std::cerr << "| " << __FILE__ << ':' << __LINE__ << std::endl; } while (false)
+#define TST_LOG_APPEND(X) do { std::ostringstream str; str << X; tstLog(str); } while (false)
+#define TST_LOG_END do { std::ostringstream str; str << "| " << __FILE__ << ':' << __LINE__ << std::endl; tstLog(str); } while (false)
 #define TST_LOG_NAME(NAME, X) TST_LOG_NAME_BEGIN(NAME, X); TST_LOG_END
 #define TST_LOG(X) TST_LOG_NAME(testname, X)
 
diff --git a/test/test.cpp b/test/test.cpp
index f09e1f855..0b905fb54 100644
--- a/test/test.cpp
+++ b/test/test.cpp
@@ -7,6 +7,7 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  */
 
+#define TST_LOG_REDIRECT
 #include <test.hpp>
 
 #include <config.h>
@@ -79,6 +80,15 @@ bool isStandalone()
     return IsStandalone;
 }
 
+static std::mutex errorMutex;
+static std::stringstream errors;
+
+void tstLog(const std::ostringstream &stream)
+{
+    std::lock_guard<std::mutex> lock(errorMutex);
+    errors << stream.str();
+}
+
 // returns true on success
 bool runClientTests(bool standalone, bool verbose)
 {
@@ -119,16 +129,11 @@ bool runClientTests(bool standalone, bool verbose)
     if (!verbose)
     {
         // redirect std::cerr temporarily
-        std::stringstream errorBuffer;
-        std::streambuf* oldCerr = std::cerr.rdbuf(errorBuffer.rdbuf());
-
         runner.run(controller);
 
-        std::cerr.rdbuf(oldCerr);
-
         // output the errors we got during the testing
         if (!result.wasSuccessful())
-            std::cerr << errorBuffer.str() << std::endl;
+            std::cerr << errors.str() << std::endl;
     }
     else
     {


More information about the Libreoffice-commits mailing list