From feeb976e4ab4ffe1542dcd20d0ae5ea569bfc0bc Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 31 Jul 2021 17:40:43 +0100 Subject: Subclass UpdateDatabase to wait for N operations to be completed Avoids need to sleep --- test/test-e2e.cpp | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/test-e2e.cpp b/test/test-e2e.cpp index 0f5a051..f861603 100644 --- a/test/test-e2e.cpp +++ b/test/test-e2e.cpp @@ -2,6 +2,7 @@ #include #include +#include "semaphore.h" #include "testdb-mysql.h" #include "testdb-postgresql.h" #include @@ -11,7 +12,29 @@ #include #include -BOOST_AUTO_TEST_CASE(e2e) +class TestUpdateDatabase : public MyGrate::Output::Pq::UpdateDatabase { +public: + using MyGrate::Output::Pq::UpdateDatabase::UpdateDatabase; + void + afterEvent(const MyGrate::MariaDB_Event_Ptr & e) override + { + UpdateDatabase::afterEvent(std::move(e)); + ops.release(); + } + + void + waitFor(unsigned int n) + { + while (n--) { + ops.acquire(); + } + } + +private: + semaphore ops {0}; +}; + +BOOST_AUTO_TEST_CASE(e2e, *boost::unit_test::timeout(5)) { const char * const target_schema {"testout"}; using namespace MyGrate::Testing; @@ -24,8 +47,10 @@ BOOST_AUTO_TEST_CASE(e2e) MyGrate::sql::createTestTable::execute(&mym); MyGrate::sql::fillTestTable::execute(&mym); - auto out = MyGrate::Output::Pq::UpdateDatabase::createNew(&pqm, MySQLDB::SERVER, MySQLDB::USER, MySQLDB::PASSWORD, - MySQLDB::PORT, my.mockname.c_str(), 100, target_schema); + TestUpdateDatabase out {pqm.connstr.c_str(), + MyGrate::Output::Pq::UpdateDatabase::createNew(&pqm, MySQLDB::SERVER, MySQLDB::USER, MySQLDB::PASSWORD, + MySQLDB::PORT, my.mockname.c_str(), 100, target_schema) + .source}; BOOST_CHECK_EQUAL(out.source, 1); auto src = out.getSource(); BOOST_REQUIRE(src); @@ -46,7 +71,7 @@ BOOST_AUTO_TEST_CASE(e2e) ins->execute({"hashyhash", "testuser", "groupadm", "10.10.0.1", 2433}); mym.query("flush logs"); - sleep(1); + out.waitFor(4); src->stopEvents(); repl.join(); -- cgit v1.2.3