From c911c83c53ef49084d9b7be6ae3afe7b605f4100 Mon Sep 17 00:00:00 2001 From: Tao He Date: Wed, 13 Mar 2024 21:21:34 +0800 Subject: [PATCH] Add a test to show that two watchers can co-work Signed-off-by: Tao He --- etcd/Value.hpp | 11 +++++++++-- src/Value.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ tst/WatcherTest.cpp | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/etcd/Value.hpp b/etcd/Value.hpp index 46f684b..5cc296c 100644 --- a/etcd/Value.hpp +++ b/etcd/Value.hpp @@ -91,7 +91,9 @@ class Value { int64_t leaseId; }; -typedef std::vector Values; +using Values = std::vector; + +std::ostream& operator<<(std::ostream& os, const Value& value); class Event { public: @@ -122,7 +124,12 @@ class Event { bool _has_kv, _has_prev_kv; }; -typedef std::vector Events; +using Events = std::vector; + +std::ostream& operator<<(std::ostream& os, const Event::EventType& value); + +std::ostream& operator<<(std::ostream& os, const Event& event); + } // namespace etcd #endif diff --git a/src/Value.cpp b/src/Value.cpp index 64bf1cd..cbda697 100644 --- a/src/Value.cpp +++ b/src/Value.cpp @@ -44,6 +44,19 @@ int etcd::Value::ttl() const { return _ttl; } int64_t etcd::Value::lease() const { return leaseId; } +std::ostream& etcd::operator<<(std::ostream& os, const etcd::Value& value) { + os << "Event: {"; + os << "Key: " << value.key() << ", "; + os << "Value: " << value.as_string() << ", "; + os << "Created: " << value.created_index() << ", "; + os << "Modified: " << value.modified_index() << ", "; + os << "Version: " << value.version() << ", "; + os << "TTL: " << value.ttl() << ", "; + os << "Lease: " << value.lease() << ", "; + os << "}"; + return os; +} + etcd::Event::Event(mvccpb::Event const& event) { _has_kv = event.has_kv(); _has_prev_kv = event.has_prev_kv(); @@ -73,3 +86,30 @@ bool etcd::Event::has_prev_kv() const { return _has_prev_kv; } const etcd::Value& etcd::Event::kv() const { return _kv; } const etcd::Value& etcd::Event::prev_kv() const { return _prev_kv; } + +std::ostream& etcd::operator<<(std::ostream& os, + const etcd::Event::EventType& value) { + switch (value) { + case etcd::Event::EventType::PUT: + os << "PUT"; + break; + case etcd::Event::EventType::DELETE_: + os << "DELETE"; + break; + case etcd::Event::EventType::INVALID: + os << "INVALID"; + break; + } + return os; +} + +std::ostream& etcd::operator<<(std::ostream& os, const etcd::Event& event) { + os << "Event type: " << event.event_type(); + if (event.has_kv()) { + os << ", KV: " << event.kv(); + } + if (event.has_prev_kv()) { + os << ", Prev KV: " << event.prev_kv(); + } + return os; +} diff --git a/tst/WatcherTest.cpp b/tst/WatcherTest.cpp index c5fe100..43b05bc 100644 --- a/tst/WatcherTest.cpp +++ b/tst/WatcherTest.cpp @@ -191,6 +191,41 @@ TEST_CASE("create two watcher") { std::this_thread::sleep_for(std::chrono::seconds(5)); } +TEST_CASE("using two watcher") { + etcd::SyncClient etcd(etcd_url); + + int watched1 = 0; + int watched2 = 0; + + etcd::Watcher w1( + etcd, "/test/def", + [&](etcd::Response const& resp) { + std::cout << "w1 called: " << resp.events().at(0).event_type() << " on " + << resp.events().at(0).kv().key() << std::endl; + ++watched1; + }, + true); + etcd::Watcher w2( + etcd, "/test", + [&](etcd::Response const& resp) { + std::cout << "w2 called: " << resp.events().at(0).event_type() << " on " + << resp.events().at(0).kv().key() << std::endl; + ++watched2; + }, + true); + + std::this_thread::sleep_for(std::chrono::seconds(5)); + + etcd.put("/test/def/xxx", "42"); + etcd.put("/test/abc", "42"); + etcd.rm("/test/def/xxx"); + etcd.rm("/test/abc"); + + std::this_thread::sleep_for(std::chrono::seconds(5)); + CHECK(2 == watched1); + CHECK(4 == watched2); +} + // TEST_CASE("request cancellation") // { // etcd::Client etcd(etcd_url);