[Libreoffice-commits] online.git: Branch 'distro/collabora/collabora-online-4' - test/helpers.hpp test/test.cpp

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Wed May 1 17:15:00 UTC 2019


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

New commits:
commit 8dc0f525ac8b6f74b020445934ce4ab725bbefc7
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: Wed May 1 19:14:42 2019 +0200

    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
    (cherry picked from commit 5bbf2b2fec2e118f91908325d9e8dd2df9f7a47e)
    Reviewed-on: https://gerrit.libreoffice.org/71626
    Reviewed-by: Michael Meeks <michael.meeks at collabora.com>
    Tested-by: Michael Meeks <michael.meeks at collabora.com>

diff --git a/test/helpers.hpp b/test/helpers.hpp
index 13b351762..07e02a386 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