From e389d998b437e199592908e30779c5eb55490eb3 Mon Sep 17 00:00:00 2001
From: Dan Goodliffe <dan@randomdan.homeip.net>
Date: Fri, 27 Aug 2021 20:34:53 +0100
Subject: Handle errors in the replication thread in tests

---
 test/test-e2e.cpp | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

(limited to 'test')

diff --git a/test/test-e2e.cpp b/test/test-e2e.cpp
index 45a8d49..d0d580c 100644
--- a/test/test-e2e.cpp
+++ b/test/test-e2e.cpp
@@ -53,6 +53,16 @@ class MockSetup {
 public:
 	static constexpr const char * const target_schema {"testout"};
 	MockSetup() : pq {ROOT "/db/schema.sql"}, pqm {pq.mock()}, mym {my.mock()} { }
+	virtual ~MockSetup()
+	{
+		if (src) {
+			src->stopEvents();
+		}
+		if (repl) {
+			repl->join();
+			repl.reset();
+		}
+	}
 
 	TestUpdateDatabase &
 	getUpdateDatabase()
@@ -82,7 +92,19 @@ public:
 	{
 		BOOST_REQUIRE(out);
 		BOOST_REQUIRE(!repl);
-		repl.emplace(&MyGrate::EventSourceBase::readEvents, getSource().get(), std::ref(*out));
+		repl.emplace([&]() {
+			try {
+				getSource()->readEvents(*out);
+			}
+			catch (std::exception & ex) {
+				failed = true;
+				std::cerr << ex.what() << "\n";
+			}
+			catch (...) {
+				failed = true;
+				std::cerr << "bug\n";
+			}
+		});
 	}
 
 	void
@@ -91,16 +113,22 @@ public:
 		BOOST_REQUIRE(out);
 		BOOST_REQUIRE(src);
 		BOOST_REQUIRE(repl);
-		out->waitFor(events);
+		if (!failed) {
+			out->waitFor(events);
+		}
 		src->stopEvents();
 		repl->join();
 		repl.reset();
+		if (failed) {
+			throw std::runtime_error("Replication thread failed");
+		}
 	}
 
 	MySQLDB my;
 	PqConnDB pq;
 	MyGrate::Output::Pq::PqConn pqm;
 	MyGrate::Input::MySQLConn mym;
+	bool failed {false};
 
 	std::optional<TestUpdateDatabase> out;
 	MyGrate::EventSourceBasePtr src;
-- 
cgit v1.2.3