From de50cd15f12216760bb746ec8936fc1501113425 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 22 Aug 2021 13:09:25 +0100 Subject: Add EventCounter class --- lib/eventCounter.cpp | 30 ++++++++++++++++++ lib/eventCounter.h | 22 +++++++++++++ test/Jamfile.jam | 1 + test/helpers.cpp | 7 +++++ test/helpers.h | 6 ++++ test/test-counters.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 150 insertions(+) create mode 100644 lib/eventCounter.cpp create mode 100644 lib/eventCounter.h create mode 100644 test/test-counters.cpp diff --git a/lib/eventCounter.cpp b/lib/eventCounter.cpp new file mode 100644 index 0000000..92383c5 --- /dev/null +++ b/lib/eventCounter.cpp @@ -0,0 +1,30 @@ +#include "eventCounter.h" +#include + +namespace MyGrate { + void + EventCounter::tick(mariadb_rpl_event e) + { + counters[e]++; + } + const EventCounter::Counters & + EventCounter::getAll() const + { + return counters; + } + unsigned long + EventCounter::get(mariadb_rpl_event e) const + { + return counters[e]; + } + bool + EventCounter::operator>=(const EventCounter & target) const + { + for (size_t e {}; e < ENUM_END_EVENT; e++) { + if (counters[e] < target.counters[e]) { + return false; + } + } + return true; + } +} diff --git a/lib/eventCounter.h b/lib/eventCounter.h new file mode 100644 index 0000000..522310a --- /dev/null +++ b/lib/eventCounter.h @@ -0,0 +1,22 @@ +#ifndef MYGRATE_EVENTCOUNTER_H +#define MYGRATE_EVENTCOUNTER_H + +#include "mariadb_repl.h" +#include + +namespace MyGrate { + class EventCounter { + public: + using Counters = std::array; + + void tick(mariadb_rpl_event); + const Counters & getAll() const; + unsigned long get(mariadb_rpl_event) const; + bool operator>=(const EventCounter &) const; + + protected: + Counters counters {}; + }; +} + +#endif diff --git a/test/Jamfile.jam b/test/Jamfile.jam index 1f0dac3..16b7244 100644 --- a/test/Jamfile.jam +++ b/test/Jamfile.jam @@ -17,6 +17,7 @@ run test-rawDataReader.cpp ; run test-bitset.cpp ; run test-streams.cpp ; run test-misc.cpp ; +run test-counters.cpp : : : testdb ; run test-mysql.cpp : : : testdb ; run test-postgresql.cpp : -- : ../db/schema.sql : testdb ; run test-e2e.cpp : -- : ../db/schema.sql : testdb testdb ; diff --git a/test/helpers.cpp b/test/helpers.cpp index f4fc030..6cb6fdc 100644 --- a/test/helpers.cpp +++ b/test/helpers.cpp @@ -13,3 +13,10 @@ MemStream::flush() { fflush(s); } + +EventCounterTarget & +EventCounterTarget::add(mariadb_rpl_event e, unsigned long n) +{ + counters[e] += n; + return *this; +} diff --git a/test/helpers.h b/test/helpers.h index 87ef31d..9596e12 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -68,4 +69,9 @@ struct MemStream { FILE * s; }; +class EventCounterTarget : public MyGrate::EventCounter { +public: + EventCounterTarget & add(mariadb_rpl_event, unsigned long); +}; + #endif diff --git a/test/test-counters.cpp b/test/test-counters.cpp new file mode 100644 index 0000000..1bc30dc --- /dev/null +++ b/test/test-counters.cpp @@ -0,0 +1,84 @@ +#define BOOST_TEST_MODULE Counters +#include + +#include "helpers.h" + +BOOST_FIXTURE_TEST_SUITE(ec, EventCounterTarget); + +BOOST_AUTO_TEST_CASE(zero) +{ + std::array z {}; + BOOST_CHECK_EQUAL_COLLECTIONS(counters.begin(), counters.end(), z.begin(), z.end()); +} + +BOOST_AUTO_TEST_CASE(tick_getAll) +{ + BOOST_CHECK_EQUAL(&counters, &getAll()); +} + +BOOST_AUTO_TEST_CASE(tick_one) +{ + tick(ROTATE_EVENT); + BOOST_CHECK_EQUAL(get(ROTATE_EVENT), 1); +} + +BOOST_AUTO_TEST_CASE(tick_three) +{ + add(ROTATE_EVENT, 3); + BOOST_CHECK_EQUAL(get(ROTATE_EVENT), 3); +} + +BOOST_AUTO_TEST_CASE(tick_different) +{ + add(ROTATE_EVENT, 2).add(UPDATE_ROWS_EVENT_V1, 1); + BOOST_CHECK_EQUAL(get(ROTATE_EVENT), 2); + BOOST_CHECK_EQUAL(get(UPDATE_ROWS_EVENT_V1), 1); +} + +BOOST_AUTO_TEST_CASE(compare_zero) +{ + EventCounter other; + BOOST_CHECK(other >= *this); +} + +BOOST_AUTO_TEST_CASE(compare_one) +{ + EventCounter other; + tick(ROTATE_EVENT); + + BOOST_CHECK(!(other >= *this)); + + other.tick(ROTATE_EVENT); + BOOST_CHECK(other >= *this); + + other.tick(ROTATE_EVENT); + BOOST_CHECK(other >= *this); +} + +BOOST_AUTO_TEST_CASE(compare_many) +{ + EventCounter other; + add(ROTATE_EVENT, 2).add(UPDATE_ROWS_EVENT_V1, 1); + + BOOST_CHECK(!(other >= *this)); + + other.tick(UPDATE_ROWS_EVENT_V1); + BOOST_CHECK(!(other >= *this)); + + other.tick(UPDATE_ROWS_EVENT_V1); + BOOST_CHECK(!(other >= *this)); + + other.tick(ROTATE_EVENT); + BOOST_CHECK(!(other >= *this)); + + other.tick(ROTATE_EVENT); + BOOST_CHECK(other >= *this); + + other.tick(ROTATE_EVENT); + BOOST_CHECK(other >= *this); + + other.tick(UPDATE_ROWS_EVENT_V1); + BOOST_CHECK(other >= *this); +} + +BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3