Skip to content

Commit

Permalink
Add mutex to catch's code to support concurrency in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nadav-fireblocks committed Dec 7, 2023
1 parent 33a2148 commit a1a72df
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion include/tests/catch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,7 @@ namespace Catch {
#include <iosfwd>
#include <cstddef>
#include <ostream>
#include <mutex>

namespace Catch {

Expand Down Expand Up @@ -5876,6 +5877,7 @@ namespace Catch {
void testCaseStarting( TestCaseInfo const& ) override {}

void sectionStarting( SectionInfo const& sectionInfo ) override {
std::lock_guard<std::mutex> lg(lock);
SectionStats incompleteStats( sectionInfo, Counts(), 0, false );
std::shared_ptr<SectionNode> node;
if( m_sectionStack.empty() ) {
Expand Down Expand Up @@ -5903,6 +5905,7 @@ namespace Catch {
void assertionStarting(AssertionInfo const&) override {}

bool assertionEnded(AssertionStats const& assertionStats) override {
std::lock_guard<std::mutex> lg(lock);
assert(!m_sectionStack.empty());
// AssertionResult holds a pointer to a temporary DecomposedExpression,
// which getExpandedExpression() calls to build the expression string.
Expand All @@ -5915,12 +5918,14 @@ namespace Catch {
return true;
}
void sectionEnded(SectionStats const& sectionStats) override {
std::lock_guard<std::mutex> lg(lock);
assert(!m_sectionStack.empty());
SectionNode& node = *m_sectionStack.back();
node.stats = sectionStats;
m_sectionStack.pop_back();
}
void testCaseEnded(TestCaseStats const& testCaseStats) override {
std::lock_guard<std::mutex> lg(lock);
auto node = std::make_shared<TestCaseNode>(testCaseStats);
assert(m_sectionStack.size() == 0);
node->children.push_back(m_rootSection);
Expand All @@ -5946,6 +5951,7 @@ namespace Catch {

void skipTest(TestCaseInfo const&) override {}

std::mutex lock;
IConfigPtr m_config;
std::ostream& stream;
std::vector<AssertionStats> m_assertions;
Expand Down Expand Up @@ -17963,4 +17969,3 @@ using Catch::Detail::Approx;
// end catch_reenable_warnings.h
// end catch.hpp
#endif // TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED

0 comments on commit a1a72df

Please sign in to comment.